AccountWithdrawService.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\account\Withdraws;
  4. use app\common\traits\ServiceTrait;
  5. use think\Db;
  6. use think\Log;
  7. class AccountWithdrawService
  8. {
  9. use ServiceTrait;
  10. /** @var Withdraws */
  11. public static $Model = Withdraws::class;
  12. //转账类型:0=默认,1=微信,2=支付宝,3=线下转款
  13. const TYPE_WECHAT = 1;
  14. const TYPE_ALIPAY = 2;
  15. const TYPE_OFFLINE = 3;
  16. const TYPE_MAP = [
  17. self::TYPE_WECHAT => '微信',
  18. self::TYPE_ALIPAY => '支付宝',
  19. self::TYPE_OFFLINE => '线下转款',
  20. ];
  21. const STATUS_MAP = [
  22. CommonService::ONGOING => '审核中',
  23. CommonService::SUCCESS => '审核通过',
  24. CommonService::FAILED => '审核驳回',
  25. ];
  26. /**
  27. * 用户申请提现
  28. * @param int|string $userId 用户id
  29. * @param int|float $amount 余额
  30. * @param string $name 名字
  31. * @return void
  32. * @throws \Exception
  33. */
  34. public static function applyWithdraw($userId, $amount, string $name = '')
  35. {
  36. //开启事务 抛出异常时自动回滚
  37. Db::transaction(function () use ($userId, $amount, $name) {
  38. if($name){
  39. $user = UserService::getOne(['id' => $userId]);
  40. if ($user->username != $name) {
  41. $user->username = $name;
  42. $user->save();
  43. }
  44. }
  45. //添加提现记录
  46. $data = [
  47. 'apply_sn' => generate_order_sn('TX'),
  48. 'user_id' => $userId,
  49. 'amount' => $amount,
  50. 'status' => AccountWithdrawService::$Common::ONGOING,
  51. ];
  52. $recordRes = self::create($data);
  53. if (!($recordRes)) {
  54. throw new \Exception('提现申请失败');
  55. }
  56. //记录提现日志
  57. $title = '用户发起提现';
  58. $log_res = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_AMOUNT, $userId, $amount, $title . '-余额扣除', AccountLogService::TYPE_WITHDRAW, 0, AccountLogService::INC_EXP_EXPEND, $recordRes->id);
  59. $log_res2 = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_FREEZE, $userId, $amount, $title . '-冻结金额增加', AccountLogService::TYPE_WITHDRAW, 0, AccountLogService::INC_EXP_INCREASE, $recordRes->id);
  60. //减去余额,加到冻结里面
  61. AccountService::updateData($userId, -$amount, $amount, 0,0, $log_res->id);
  62. });
  63. }
  64. /**
  65. * 审核提现
  66. * @param $withdraw_record_id int 提现申请id
  67. * @param $approval bool 是否同意
  68. * @param $remark string 驳回意见
  69. * @param $type int 转款方式
  70. * @throws \Exception
  71. */
  72. public static function approvalWithdraw($withdraw_record_id, $approval = false, $remark = '', $type = 0)
  73. {
  74. $withdraw_record = AccountWithdrawService::get($withdraw_record_id);
  75. if (!$withdraw_record) {
  76. throw new \Exception('提现申请不存在');
  77. }
  78. if ($withdraw_record->status == AccountWithdrawService::$Common::SUCCESS) {
  79. throw new \Exception('已经提现成功,请勿重复操作');
  80. }
  81. $userId = $withdraw_record->user_id;
  82. $amount = $withdraw_record->amount;
  83. $user = UserService::get($userId);
  84. //检测收支是否相同
  85. // if(!AccountService::checkBreakEven($userId, $user->is_merchant)){
  86. // throw new \Exception('该用户余额收支异常');
  87. // }
  88. //提现通过
  89. if ($approval) {
  90. //记录提现日志
  91. $title = '用户提现成功(' . self::TYPE_MAP[$type] .')';
  92. $log_res = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_FREEZE, $userId, $amount, $title . '-冻结金额扣除', AccountLogService::TYPE_WITHDRAW, 0, AccountLogService::INC_EXP_EXPEND, $withdraw_record->id);
  93. //减去冻结
  94. AccountService::updateData($userId, 0, -$amount, 0,0, $log_res->id);
  95. if($type == AccountWithdrawService::TYPE_WECHAT){
  96. //发起微信付款到用户微信余额
  97. $payRes = (new EasyWeChatService())->mchPay($withdraw_record->apply_sn, $user->openid, $user->username, $amount);
  98. if (!(isset($payRes['result_code']) && in_array($payRes['result_code'], ['SUCCESS']))) {
  99. throw new \Exception(($payRes['return_msg'] ?? '企业付款给用户失败') . ':' . ($payRes['err_code_des'] ?? ''));
  100. }
  101. $withdraw_record->result_status = $payRes['result_code'];
  102. $withdraw_record->save();
  103. }
  104. } else {
  105. if (!$remark) {
  106. throw new \Exception('驳回原因必填');
  107. }
  108. if ($withdraw_record->status == AccountWithdrawService::$Common::FAILED) {
  109. throw new \Exception('已经审核驳回,请勿重复操作');
  110. }
  111. //记录提现日志
  112. $title = '提现失败(' . $remark .')';
  113. $log_res = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_FREEZE, $userId, $amount, $title . '-冻结金额扣除', AccountLogService::TYPE_WITHDRAW, 0, AccountLogService::INC_EXP_EXPEND, $withdraw_record->id);
  114. $log_res2 = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_AMOUNT, $userId, $amount, $title . '-余额增加', AccountLogService::TYPE_WITHDRAW, 0, AccountLogService::INC_EXP_INCREASE, $withdraw_record->id);
  115. //减去冻结,返回到余额
  116. AccountService::updateData($userId, $amount, -$amount, 0,0, $log_res->id);
  117. }
  118. }
  119. }