ShopCashOutService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace app\service\admin\shop\site;
  3. use app\model\shop\ShopCashOut;
  4. use core\exception\CommonException;
  5. use app\dict\member\MemberAccountTypeDict;
  6. use core\base\BaseAdminService;
  7. use think\facade\Cache;
  8. use think\facade\Db;
  9. class ShopCashOutService extends BaseAdminService
  10. {
  11. public function __construct()
  12. {
  13. parent::__construct();
  14. $this->model = new ShopCashOut();
  15. }
  16. public function getPage(array $where = [])
  17. {
  18. $field = 'apply_money, money, service_money, shop_cash_out.status, shop_cash_out.create_time, audit_time, transfer_time';
  19. $order = 'shop_cash_out.create_time desc';
  20. $search_model = $this->model
  21. ->withJoin(['siteInfo'])
  22. ->withSearch(['cash_out_no', 'create_time', 'audit_time', 'transfer_time', 'transfer_type', 'status'], $where)
  23. ->where([['shop_cash_out.site_id', '=', $this->site_id]])
  24. ->append([ 'status_name', 'transfer_type_name' ])
  25. ->field($field)->order($order);
  26. return $this->pageQuery($search_model);
  27. }
  28. /**
  29. * 店铺提现详情
  30. * @param int $id
  31. * @return array
  32. */
  33. public function getInfo(int $id)
  34. {
  35. $field = 'apply_money, money, service_money, shop_cash_out.status, shop_cash_out.create_time, audit_time, transfer_type, transfer_time';
  36. return $this->model
  37. ->withJoin(['siteInfo'])
  38. ->append([ 'status_name', 'transfer_type_name' ])
  39. ->field($field)->where([ [ 'id|cash_out_no', '=', $id ] ])->findOrEmpty()->toArray();
  40. }
  41. /**
  42. * 申请提现
  43. * @param array $data
  44. * @return true
  45. */
  46. public function apply(array $data)
  47. {
  48. $cash_out_config = ( new ShopConfigService() )->getCashOutConfig();
  49. if ($cash_out_config['is_open'] == 0) throw new CommonException('SHOP_CASH_OUT_NOT_START');//店铺提现未开启
  50. if ($cash_out_config['min'] > $data['apply_money']) throw new CommonException(get_lang('MIN_CASH_OUT_MONEY_CANNOT_UNDER').$cash_out_config['min']);//最低提现金额不能小于
  51. $shopAccount = ( new ShopAccountService() )->find();
  52. if (!in_array($shopAccount['bank_type'], $cash_out_config['transfer_type'])) throw new CommonException('TRANSFER_TYPE_NOT_SUPPORT');//系统暂不支持该转账方式
  53. if (empty($shopAccount)) throw new CommonException('CASH_OUT_ACCOUNT_NOT_EXIST');
  54. if ($data['apply_money'] <= 0) throw new CommonException('CASH_OUT_MONEY_MUST_BE_GREATER_THAN_ZERO');
  55. if ($shopAccount['money'] < $data['apply_money']) throw new CommonException('EXCEEDING_MAXIMUM_WITHDRAWABLE_BALANCE');
  56. $service_money = format_round_money($data['apply_money'] * $cash_out_config['rate']/100);
  57. $money = $data['apply_money'] - $service_money;
  58. Db::startTrans();
  59. try {
  60. //生成提现交易号
  61. $cash_out_no = $this->createCashOutNo($this->site_id);
  62. //创建提现订单
  63. $data[ 'site_id' ] = $this->site_id;
  64. $data[ 'cash_out_no' ] = $cash_out_no;
  65. $data[ 'account_type' ] = MemberAccountTypeDict::MONEY;
  66. $data[ 'service_money' ] = $service_money;
  67. $data[ 'money' ] = $money;
  68. $data[ 'rate' ] = $cash_out_config['rate'];
  69. $data[ 'transfer_realname' ] = $shopAccount['bank_type'] == 1 ? $shopAccount['bank_account_name'] : $shopAccount['alipay_name'] ?? '';
  70. $data[ 'transfer_mobile' ] = '';
  71. $data[ 'transfer_bank' ] = $shopAccount['bank_type'] == 1 ? $shopAccount['bank_name'] ?? '' : '';
  72. $data[ 'transfer_account' ] = $shopAccount['bank_type'] == 1 ? $shopAccount['bank_account_no'] : $shopAccount['alipay_account_no'] ?? '';
  73. $data[ 'transfer_type' ] = $shopAccount['bank_type'] ?? '';
  74. $data[ 'status' ] = $cash_out_config['is_auto_verify'] == 1 ? 2 : 1;
  75. $data[ 'create_time' ] = time();
  76. $res = $this->model->create($data);
  77. //更新店铺信息
  78. $shopAccount->save([
  79. 'money_cash_outing' => $shopAccount['money_cash_outing'] + $data['apply_money'], //提现中金额计算
  80. 'money' => $shopAccount['money'] - $data['apply_money'], //当前余额计算
  81. ]);
  82. //添加账户收支记录
  83. $log_data['site_id'] = $this->site_id;
  84. $log_data['money'] = -1 * $data['apply_money'];
  85. $log_data['money_sum'] = $shopAccount['money'] - $data['apply_money'];
  86. $log_data['from_type'] = 'cash_out';
  87. $log_data['related_id'] = $cash_out_no;
  88. $log_data['memo'] = get_lang('SHOP_CASH_OUT');
  89. ( new ShopAccountLogService() )->add($log_data);
  90. Db::commit();
  91. } catch ( Exception $e) {
  92. Db::rollback();
  93. throw new CommonException($e->getMessage());
  94. }
  95. return true;
  96. }
  97. /**
  98. * 取消申请
  99. * @return true
  100. */
  101. public function cancel(int $id, array $data)
  102. {
  103. $info = $this->model->where([ [ 'id', '=', $id ] ])->findOrEmpty()->toArray();
  104. if(empty($info)) return true;
  105. $data[ 'update_time' ] = time();
  106. $data[ 'cancel_time'] = time();
  107. $data[ 'status' ] = -2;
  108. Db::startTrans();
  109. try {
  110. $this->model->where([['id', '=', $id]])->update($data);
  111. //审核取消,退回余额及减去提现中金额
  112. $shopAccount = ( new ShopAccountService() )->find();
  113. $shopAccount->where([ [ 'site_id', '=', $info[ 'site_id' ] ] ])->update([
  114. 'money_cash_outing' => $shopAccount['money_cash_outing'] - $info['apply_money'],
  115. 'money' => $shopAccount['money'] + $info['apply_money']
  116. ]);
  117. $shopAccountLogService = new ShopAccountLogService();
  118. //添加账户收支记录
  119. $log_data['site_id'] = $info[ 'site_id' ];
  120. $log_data['money'] = $info['apply_money'];
  121. $log_data['money_sum'] = $shopAccount['money'] + $info['apply_money'];
  122. $log_data['from_type'] = 'cash_out';
  123. $log_data['related_id'] = $info['cash_out_no'];
  124. $log_data['memo'] = get_lang('USER_CANCEL_CASH_OUT_BACK_MONEY');
  125. $shopAccountLogService->add($log_data);
  126. Db::commit();
  127. } catch (\Exception $e) {
  128. Db::rollback();
  129. throw new CommonException($e->getMessage());
  130. }
  131. return true;
  132. }
  133. /**
  134. * 创建订单编号
  135. * @param int $site_id
  136. * @return string
  137. */
  138. public function createCashOutNo(int $site_id)
  139. {
  140. $time_str = date('YmdHi');
  141. $max_no = Cache::get('cash_out_no_' . $site_id . '_' . $time_str);
  142. if (!isset($max_no) || empty($max_no)) {
  143. $max_no = 1;
  144. } else {
  145. ++$max_no;
  146. }
  147. $cash_out_no = $time_str . $site_id . sprintf('%03d', $max_no);
  148. Cache::set('cash_out_no_' . $site_id . '_' . $time_str, $max_no);
  149. return $cash_out_no;
  150. }
  151. }