AuditRejectedController.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: king
  5. * Date: 2018/10/27
  6. * Time: 4:33 PM
  7. */
  8. namespace app\backend\modules\withdraw\controllers;
  9. use app\backend\models\Withdraw;
  10. use app\common\events\withdraw\BalanceWithdrawRejectEvent;
  11. use app\common\exceptions\ShopException;
  12. use app\common\services\credit\ConstService;
  13. use app\common\services\finance\BalanceChange;
  14. use Illuminate\Support\Facades\DB;
  15. class AuditRejectedController extends PreController
  16. {
  17. /**
  18. * 提现记录 审核后驳回接口
  19. */
  20. public function index()
  21. {
  22. $result = $this->auditedRebut();
  23. if ($result == true) {
  24. event(new BalanceWithdrawRejectEvent($this->withdrawModel));
  25. // BalanceNoticeService::withdrawRejectNotice($this->withdrawModel);
  26. return $this->message('驳回成功', yzWebUrl("withdraw.detail.index", ['id' => $this->withdrawModel->id]));
  27. }
  28. return $this->message('驳回失败,请刷新重试', yzWebUrl("withdraw.detail.index", ['id' => $this->withdrawModel->id]), 'error');
  29. }
  30. public function validatorWithdrawModel($withdrawModel)
  31. {
  32. if (!in_array($withdrawModel->status, [Withdraw::STATUS_INITIAL, Withdraw::STATUS_AUDIT, Withdraw::STATUS_INVALID])) {
  33. throw new ShopException('状态错误,不符合驳回规则!');
  34. }
  35. }
  36. /**
  37. * @return bool
  38. */
  39. private function auditedRebut()
  40. {
  41. DB::transaction(function () {
  42. $this->_auditedRebut();
  43. });
  44. return true;
  45. }
  46. /**
  47. * @throws ShopException
  48. */
  49. private function _auditedRebut()
  50. {
  51. $result = $this->updateWithdrawStatus();
  52. if (!$result) {
  53. throw new ShopException('驳回失败:更新状态失败');
  54. }
  55. $result = $this->updateBalance();
  56. if (!$result) {
  57. throw new ShopException('驳回失败:更新余额明细失败');
  58. }
  59. }
  60. /**
  61. * @return bool
  62. */
  63. private function updateWithdrawStatus()
  64. {
  65. $this->withdrawModel->status = Withdraw::STATUS_REBUT;
  66. $this->withdrawModel->reject_reason = request()->reject_reason ? : '';
  67. $this->withdrawModel->arrival_at = time();
  68. return $this->withdrawModel->save();
  69. }
  70. private function updateBalance()
  71. {
  72. $data = array(
  73. 'member_id' => $this->withdrawModel->member_id,
  74. 'change_value' => $this->withdrawModel->amounts,
  75. 'operator' => ConstService::OPERATOR_SHOP,
  76. 'operator_id' => \YunShop::app()->uid,
  77. 'remark' => '余额提现驳回' . $amounts = $this->withdrawModel->amounts . "元",
  78. 'relation' => $this->withdrawModel->withdraw_sn,
  79. );
  80. $result = (new BalanceChange())->rejected($data);
  81. if ($result === true) {
  82. return true;
  83. }
  84. return false;
  85. }
  86. }