UserExtractRepository.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\repositories\user;
  12. use app\common\repositories\BaseRepository;
  13. use app\common\dao\user\UserExtractDao as dao;
  14. use app\common\repositories\wechat\WechatUserRepository;
  15. use crmeb\jobs\SendSmsJob;
  16. use crmeb\services\SwooleTaskService;
  17. use crmeb\services\WechatService;
  18. use think\exception\ValidateException;
  19. use think\facade\Db;
  20. use think\facade\Queue;
  21. class UserExtractRepository extends BaseRepository
  22. {
  23. /**
  24. * @var dao
  25. */
  26. protected $dao;
  27. /**
  28. * UserExtractRepository constructor.
  29. * @param dao $dao
  30. */
  31. public function __construct(dao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. /**
  36. * TODO
  37. * @param $id
  38. * @return bool
  39. * @author Qinii
  40. * @day 2020-06-16
  41. */
  42. public function getWhereCount($id)
  43. {
  44. $where['extract_id'] = $id;
  45. $where['status'] = 0;
  46. return $this->dao->getWhereCount($where) > 0;
  47. }
  48. /**
  49. * TODO
  50. * @param array $where
  51. * @param $page
  52. * @param $limit
  53. * @return array
  54. * @author Qinii
  55. * @day 2020-06-16
  56. */
  57. public function getList(array $where, $page, $limit)
  58. {
  59. $query = $this->dao->search($where)->with(['user' => function ($query) {
  60. $query->field('uid,avatar,nickname');
  61. }]);
  62. $count = $query->count();
  63. $list = $query->page($page, $limit)->select();
  64. return compact('count', 'list');
  65. }
  66. public function getTotalExtractPrice()
  67. {
  68. return $this->dao->search(['status' => 1])->sum('extract_price');
  69. }
  70. /**
  71. * @param $uid
  72. * @return mixed
  73. * @author xaboy
  74. * @day 2020/6/22
  75. */
  76. public function userTotalExtract($uid)
  77. {
  78. return $this->dao->search(['status' => 1, 'uid' => $uid])->sum('extract_price');
  79. }
  80. /**
  81. * TODO
  82. * @param $user
  83. * @param $data
  84. * @author Qinii
  85. * @day 2020-06-16
  86. */
  87. public function create($user,$data)
  88. {
  89. event('user.extract.before',compact('user','data'));
  90. $userExtract = Db::transaction(function()use($user,$data){
  91. if($user['brokerage_price'] < (systemConfig('user_extract_min')))
  92. throw new ValidateException('可提现金额不足');
  93. if($data['extract_price'] < (systemConfig('user_extract_min')))
  94. throw new ValidateException('提现金额不得小于最低额度');
  95. if($user['brokerage_price'] < $data['extract_price'])
  96. throw new ValidateException('提现金额不足');
  97. if($data['extract_type'] == 3) {
  98. $make = app()->make(WechatUserRepository::class);
  99. $openid = $make->idByOpenId((int)$user['wechat_user_id']);
  100. if (!$openid){
  101. $openid = $make->idByRoutineId((int)$user['wechat_user_id']);
  102. if(!$openid) throw new ValidateException('openID获取失败,请确认是微信用户');
  103. }
  104. }
  105. $brokerage_price = bcsub($user['brokerage_price'],$data['extract_price'],2);
  106. $user->brokerage_price = $brokerage_price;
  107. $user->save();
  108. $data['extract_sn'] = $this->createSn();
  109. $data['uid'] = $user['uid'];
  110. $data['balance'] = $brokerage_price;
  111. return $this->dao->create($data);
  112. });
  113. event('user.extract',compact('userExtract'));
  114. SwooleTaskService::admin('notice', [
  115. 'type' => 'extract',
  116. 'title' => '您有一条新的提醒申请',
  117. 'id' => $userExtract->extract_id
  118. ]);
  119. }
  120. public function switchStatus($id,$data)
  121. {
  122. $extract = $this->dao->getWhere(['extract_id' => $id]);
  123. $user = app()->make(UserRepository::class)->get($extract['uid']);
  124. if(!$user) throw new ValidateException('用户不存在');
  125. $brokerage_price = 0;
  126. if($data['status'] == -1)
  127. $brokerage_price = bcadd($user['brokerage_price'] ,$extract['extract_price'],2);
  128. $ret = [];
  129. if ($data['status'] == 1 && $extract['extract_type'] == 3) {
  130. $openid = app()->make(WechatUserRepository::class)->idByOpenId((int)$user['wechat_user_id']);
  131. if (!$openid) {
  132. $openid = app()->make(WechatUserRepository::class)->idByRoutineId((int)$user['wechat_user_id']);
  133. }
  134. if (!$openid) throw new ValidateException('非微信用户不支持付款到零钱');
  135. $ret = [
  136. 'openid' => $openid,
  137. 'sn' => $extract['extract_sn'],
  138. 'price' => $extract['extract_price']
  139. ];
  140. }
  141. Db::transaction(function()use($id,$data,$ret,$user,$brokerage_price){
  142. event('user.extractStatus.before',compact('id','data'));
  143. if ($ret) WechatService::create()->merchantPay($ret);
  144. if($brokerage_price){
  145. $user->brokerage_price = $brokerage_price;
  146. $user->save();
  147. }
  148. $userExtract = $this->dao->update($id,$data);
  149. event('user.extractStatus',compact('id','userExtract'));
  150. });
  151. Queue::push(SendSmsJob::class,['tempId' => 'EXTRACT_NOTICE', 'id' =>$id]);
  152. }
  153. public function createSn()
  154. {
  155. list($msec, $sec) = explode(' ', microtime());
  156. $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
  157. $sn = 'ue' . $msectime . mt_rand(10000, max(intval($msec * 10000) + 10000, 98369));
  158. return $sn;
  159. }
  160. public function getHistoryBank($uid)
  161. {
  162. return $this->dao->getSearch(['uid' => $uid,'extract_type' => 0])->order('create_time DESC')->field('real_name,bank_code,bank_address,bank_name')->find();
  163. }
  164. }