UserExtractRepository.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\common\repositories\user;
  3. use app\common\repositories\BaseRepository;
  4. use app\common\dao\user\UserExtractDao as dao;
  5. use app\common\repositories\wechat\WechatUserRepository;
  6. use ln\jobs\SendTemplateMessageJob;
  7. use ln\services\SwooleTaskService;
  8. use ln\services\WechatService;
  9. use think\exception\ValidateException;
  10. use think\facade\Db;
  11. use think\facade\Queue;
  12. class UserExtractRepository extends BaseRepository
  13. {
  14. /**
  15. * @var dao
  16. */
  17. protected $dao;
  18. /**
  19. * UserExtractRepository constructor.
  20. * @param dao $dao
  21. */
  22. public function __construct(dao $dao)
  23. {
  24. $this->dao = $dao;
  25. }
  26. /**
  27. * TODO
  28. * @param $id
  29. * @return bool
  30. * @author Qinii
  31. * @day 2020-06-16
  32. */
  33. public function getWhereCount($id)
  34. {
  35. $where['extract_id'] = $id;
  36. $where['status'] = 0;
  37. return $this->dao->getWhereCount($where) > 0;
  38. }
  39. /**
  40. * TODO
  41. * @param array $where
  42. * @param $page
  43. * @param $limit
  44. * @return array
  45. * @author Qinii
  46. * @day 2020-06-16
  47. */
  48. public function search(array $where, $page, $limit)
  49. {
  50. $query = $this->dao->search($where)->with(['user' => function ($query) {
  51. $query->field('uid,avatar,nickname');
  52. }]);
  53. $count = $query->count();
  54. $list = $query->page($page, $limit)->select();
  55. return compact('count', 'list');
  56. }
  57. public function getTotalExtractPrice()
  58. {
  59. return $this->dao->search(['status' => 1])->sum('extract_price');
  60. }
  61. /**
  62. * @param $uid
  63. * @return mixed
  64. * @author zfy
  65. * @day 2020/6/22
  66. */
  67. public function userTotalExtract($uid)
  68. {
  69. return $this->dao->search(['status' => 1, 'uid' => $uid])->sum('extract_price');
  70. }
  71. /**
  72. * TODO
  73. * @param $user
  74. * @param $data
  75. * @author Qinii
  76. * @day 2020-06-16
  77. */
  78. public function create($user,$data)
  79. {
  80. $data = Db::transaction(function()use($user,$data){
  81. if($user['brokerage_price'] < (systemConfig('user_extract_min')))
  82. throw new ValidateException('可提现金额不足');
  83. if($data['extract_price'] < (systemConfig('user_extract_min')))
  84. throw new ValidateException('提现金额不得小于最低额度');
  85. if($user['brokerage_price'] < $data['extract_price'])
  86. throw new ValidateException('提现金额不足');
  87. if($data['extract_type'] == 3) {
  88. $make = app()->make(WechatUserRepository::class);
  89. $openid = $make->idByOpenId((int)$user['wechat_user_id']);
  90. if (!$openid){
  91. $openid = $make->idByRoutineId((int)$user['wechat_user_id']);
  92. if(!$openid) throw new ValidateException('openID获取失败,请确认是微信用户');
  93. }
  94. }
  95. $brokerage_price = bcsub($user['brokerage_price'],$data['extract_price'],2);
  96. $user->brokerage_price = $brokerage_price;
  97. $user->save();
  98. $data['extract_sn'] = $this->createSn();
  99. $data['uid'] = $user['uid'];
  100. $data['balance'] = $brokerage_price;
  101. return $this->dao->create($data);
  102. });
  103. SwooleTaskService::admin('notice', [
  104. 'type' => 'extract',
  105. 'title' => '您有一条新的提醒申请',
  106. 'id' => $data->extract_id
  107. ]);
  108. }
  109. public function switchStatus($id,$data)
  110. {
  111. Db::transaction(function()use($id,$data){
  112. $extract = $this->dao->getWhere(['extract_id' => $id]);
  113. $user = app()->make(UserRepository::class)->get($extract['uid']);
  114. if(!$user) throw new ValidateException('用户不存在');
  115. if($data['status'] == '-1'){
  116. $user = app()->make(UserRepository::class)->get($extract['uid']);
  117. $brokerage_price = bcadd($user['brokerage_price'] ,$extract['extract_price'],2);
  118. $user->brokerage_price = $brokerage_price;
  119. $user->save();
  120. }
  121. if($data['status'] == 1 && $extract['extract_type'] == 3){
  122. $openid = app()->make(WechatUserRepository::class)->idByOpenId((int)$user['wechat_user_id']);
  123. if(!$openid) throw new ValidateException('openID获取失败');
  124. $ret = [
  125. 'openid' => $openid,
  126. 'sn' => $extract['extract_sn'],
  127. 'price' => $extract['extract_price']
  128. ];
  129. WechatService::create()->merchantPay($ret);
  130. }
  131. $this->dao->update($id,$data);
  132. });
  133. Queue::push(SendTemplateMessageJob::class,[
  134. 'tempCode' => 'ORDER_DELIVER_SUCCESS',
  135. 'id' =>$id
  136. ]);
  137. }
  138. public function createSn()
  139. {
  140. list($msec, $sec) = explode(' ', microtime());
  141. $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
  142. $sn = 'ue' . $msectime . mt_rand(10000, max(intval($msec * 10000) + 10000, 98369));
  143. return $sn;
  144. }
  145. }