AuditedRebutController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\backend\modules\income\models\Income;
  11. use app\common\events\withdraw\WithdrawRebutAuditEvent;
  12. use app\common\exceptions\ShopException;
  13. use app\common\services\income\WithdrawIncomeApplyService;
  14. use Illuminate\Support\Facades\DB;
  15. use Yunshop\WithdrawalLimit\Common\services\WithdrawHandleService;
  16. class AuditedRebutController extends PreController
  17. {
  18. /**
  19. * 提现记录 审核后驳回接口
  20. */
  21. public function index()
  22. {
  23. $result = $this->auditedRebut();
  24. return $result == true ? $this->successJson('驳回成功') : $this->errorJson('驳回失败,请刷新重试');
  25. }
  26. public function validatorWithdrawModel($withdrawModel)
  27. {
  28. if ($withdrawModel->status != Withdraw::STATUS_AUDIT) {
  29. throw new ShopException('状态错误,不符合审核后驳回规则!');
  30. }
  31. }
  32. /**
  33. * @return bool
  34. */
  35. private function auditedRebut()
  36. {
  37. DB::transaction(function () {
  38. $this->_auditedRebut();
  39. });
  40. return true;
  41. }
  42. /**
  43. * @throws ShopException
  44. */
  45. private function _auditedRebut()
  46. {
  47. $result = $this->updateWithdrawStatus();
  48. if (!$result) {
  49. throw new ShopException('驳回失败:更新状态失败');
  50. }
  51. $result = $this->updateIncomePayStatus();
  52. if (!$result) {
  53. throw new ShopException('驳回失败:更新收入失败');
  54. }
  55. WithdrawIncomeApplyService::rebut($this->withdrawModel);
  56. }
  57. /**
  58. * @return bool
  59. */
  60. private function updateWithdrawStatus()
  61. {
  62. $this->withdrawModel->status = Withdraw::STATUS_REBUT;
  63. $this->withdrawModel->arrival_at = time();
  64. $this->withdrawModel->reject_reason = request()->reject_reason ? : '';
  65. return $this->withdrawModel->save();
  66. }
  67. /**
  68. * @return bool
  69. */
  70. private function updateIncomePayStatus()
  71. {
  72. $income_ids = explode(',', $this->withdrawModel->type_id);
  73. if (count($income_ids) > 0) {
  74. //提现额度
  75. if(app('plugins')->isEnabled('withdrawal-limit'))
  76. {
  77. WithdrawHandleService::handle('reject',$income_ids,$this->withdrawModel);
  78. }
  79. //后台审核执行驳回事件
  80. event(new WithdrawRebutAuditEvent($this->withdrawModel,$income_ids));
  81. return Income::whereIn('id', $income_ids)->where('pay_status', Income::PAY_STATUS_WAIT)->update(['status' => Income::STATUS_INITIAL, 'pay_status' => Income::PAY_STATUS_REJECT]);
  82. }
  83. return false;
  84. }
  85. }