Cash.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace app\shop\model\plus\agent;
  3. use app\common\library\easywechat\AppWx;
  4. use app\common\library\easywechat\AppMp;
  5. use app\common\service\message\MessageService;
  6. use app\common\service\order\OrderService;
  7. use app\common\library\easywechat\WxPay;
  8. use app\common\model\plus\agent\Cash as CashModel;
  9. use app\shop\model\user\User as UserModel;
  10. use app\shop\service\order\ExportService;
  11. /**
  12. * 分销商提现明细模型
  13. */
  14. class Cash extends CashModel
  15. {
  16. /**
  17. * 获取器:申请时间
  18. */
  19. public function getAuditTimeAttr($value)
  20. {
  21. return $value > 0 ? date('Y-m-d H:i:s', $value) : 0;
  22. }
  23. /**
  24. * 获取器:打款方式
  25. */
  26. public function getPayTypeAttr($value)
  27. {
  28. return ['text' => $this->payType[$value], 'value' => $value];
  29. }
  30. /**
  31. * 获取分销商提现列表
  32. */
  33. public function getList($user_id = null, $apply_status = -1, $pay_type = -1, $search = '')
  34. {
  35. $model = $this;
  36. // 构建查询规则
  37. $model = $model->alias('cash')
  38. ->with(['user'])
  39. ->field('cash.*, agent.real_name, agent.mobile, user.nickName, user.avatarUrl')
  40. ->join('user', 'user.user_id = cash.user_id')
  41. ->join('agent_user agent', 'agent.user_id = cash.user_id')
  42. ->order(['cash.create_time' => 'desc']);
  43. // 查询条件
  44. if ($user_id > 0) {
  45. $model = $model->where('cash.user_id', '=', $user_id);
  46. }
  47. if (!empty($search)) {
  48. $model = $model->where('agent.real_name|agent.mobile', 'like', '%' . $search . '%');
  49. }
  50. if ($apply_status > 0) {
  51. $model = $model->where('cash.apply_status', '=', $apply_status);
  52. }
  53. if ($pay_type > 0) {
  54. $model = $model->where('cash.pay_type', '=', $pay_type);
  55. }
  56. // 获取列表数据
  57. return $model->paginate(15);
  58. }
  59. /**
  60. * 分销商提现审核
  61. */
  62. public function submit($param)
  63. {
  64. $data = ['apply_status' => $param['apply_status']];
  65. if ($param['apply_status'] == 30) {
  66. $data['reject_reason'] = $param['reject_reason'];
  67. }
  68. // 更新申请记录
  69. $data['audit_time'] = time();
  70. self::update($data, ['id' => $param['id']]);
  71. // 提现驳回:解冻分销商资金
  72. if ($param['apply_status'] == 30) {
  73. User::backFreezeMoney($param['user_id'], $param['money']);
  74. }
  75. // 发送模板消息
  76. (new MessageService)->cash($this);
  77. return true;
  78. }
  79. /**
  80. * 确认已打款
  81. */
  82. public function money()
  83. {
  84. $this->startTrans();
  85. try {
  86. // 更新申请状态
  87. $data = ['apply_status' => 40, 'audit_time' => time()];
  88. self::update($data, ['id' => $this['id']]);
  89. // 更新分销商累积提现佣金
  90. User::totalMoney($this['user_id'], $this['money']);
  91. // 记录分销商资金明细
  92. Capital::add([
  93. 'user_id' => $this['user_id'],
  94. 'flow_type' => 20,
  95. 'money' => -$this['money'],
  96. 'describe' => '申请提现',
  97. ]);
  98. // 发送模板消息
  99. //(new Message)->withdraw($this);
  100. // 事务提交
  101. $this->commit();
  102. return true;
  103. } catch (\Exception $e) {
  104. $this->error = $e->getMessage();
  105. $this->rollback();
  106. return false;
  107. }
  108. }
  109. /**
  110. * 分销商提现:微信支付企业付款
  111. */
  112. public function wechatPay()
  113. {
  114. // 微信用户信息
  115. $user = UserModel::detail($this['user_id']);
  116. // 生成付款订单号
  117. $orderNO = OrderService::createOrderNo();
  118. // 付款描述
  119. $desc = '分销商提现付款';
  120. // 微信支付api:企业付款到零钱
  121. $open_id = '';
  122. $app = null;
  123. if ($user['reg_source'] == 'mp') {
  124. $app = AppMp::getWxPayApp($user['app_id']);
  125. $open_id = $user['mpopen_id'];
  126. } else if ($user['reg_source'] == 'wx') {
  127. $app = AppWx::getWxPayApp($user['app_id']);
  128. $open_id = $user['open_id'];
  129. }
  130. if ($open_id == '') {
  131. $this->error = '未找到用户open_id';
  132. return false;
  133. }
  134. $WxPay = new WxPay($app);
  135. // 请求付款api
  136. if ($WxPay->transfers($orderNO, $open_id, $this['money'], $desc)) {
  137. // 确认已打款
  138. $this->money();
  139. return true;
  140. }
  141. return false;
  142. }
  143. /*
  144. *统计提现总数量
  145. */
  146. public function getAgentCashTotal()
  147. {
  148. return $this->where('apply_status', '=', 10)->count();
  149. }
  150. /*
  151. *统计提现总数量
  152. */
  153. public function getAgentApplyTotal()
  154. {
  155. return $this->where('apply_status', '=', '10')->count();
  156. }
  157. /**
  158. * 导出分销商提现
  159. */
  160. public function exportList($user_id = null, $apply_status = -1, $pay_type = -1, $search = '')
  161. {
  162. $model = $this;
  163. // 构建查询规则
  164. $model = $model->alias('cash')
  165. ->with(['user'])
  166. ->field('cash.*, agent.real_name, agent.mobile, user.nickName, user.avatarUrl')
  167. ->join('user', 'user.user_id = cash.user_id')
  168. ->join('agent_user agent', 'agent.user_id = cash.user_id')
  169. ->order(['cash.create_time' => 'desc']);
  170. // 查询条件
  171. if ($user_id > 0) {
  172. $model = $model->where('cash.user_id', '=', $user_id);
  173. }
  174. if (!empty($search)) {
  175. $model = $model->where('agent.real_name|agent.mobile', 'like', '%' . $search . '%');
  176. }
  177. if ($apply_status > 0) {
  178. $model = $model->where('cash.apply_status', '=', $apply_status);
  179. }
  180. if ($pay_type > 0) {
  181. $model = $model->where('cash.pay_type', '=', $pay_type);
  182. }
  183. // 获取列表数据
  184. $list = $model->select();
  185. // 导出excel文件
  186. (new Exportservice)->cashList($list);
  187. }
  188. }