UserExtractService.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace app\extra\service\user;
  3. use app\extra\basic\Service;
  4. use app\extra\dao\user\UserExtractDao;
  5. use app\extra\service\cutter\CutterOrderService;
  6. use support\exception\BusinessException;
  7. use think\exception\ValidateException;
  8. class UserExtractService extends Service
  9. {
  10. /**
  11. * 设置模型
  12. * @return string
  13. */
  14. public function __construct()
  15. {
  16. $this->dao = new UserExtractDao();
  17. }
  18. /**
  19. * 列表
  20. * @param array $param
  21. */
  22. public function getList(array $param = [],string $field = '*', int $page = 0,int $limit = 0,string $order = 'id desc')
  23. {
  24. $list = $this->dao->getList($this->searchFilter($param),$field,$page,$limit,$order);
  25. $count = $this->dao->count($this->searchFilter($param));
  26. return compact('list','count');
  27. }
  28. protected function searchFilter(array $param = []): array
  29. {
  30. $filter = [];
  31. !empty($param['status']) && $filter[] = ["status", '=', $param['status']];
  32. return $filter;
  33. }
  34. public function cash(array $user,array $data):bool
  35. {
  36. $userService = new UserService();
  37. $extractPrice = $user['brokerage_price'];
  38. $userExtractMinPrice = sConf('extract.extract_min_price');
  39. if ($data['price'] < $userExtractMinPrice) {
  40. throw new ValidateException('提现金额不能小于' . $userExtractMinPrice . '元');
  41. }
  42. if ($extractPrice < 0) {
  43. throw new ValidateException('提现佣金不足' . $data['price']);
  44. }
  45. if ($data['price'] > $extractPrice) {
  46. throw new ValidateException('提现佣金不足' . $data['price']);
  47. }
  48. if ($data['price'] <= 0) {
  49. throw new ValidateException('提现佣金大于0');
  50. }
  51. //手续费
  52. $charge = $data['price'] * (0 / 100);
  53. $insertData = [
  54. 'real_name'=>$data['real_name'],
  55. 'uid' => $user['uid'],
  56. 'bank_code' => $data['bank_code'],
  57. 'bank_address' => $data['bank_address'],
  58. 'card' => $data['card'],
  59. // 'extract_type' => $data['extract_type'],
  60. 'price' => $data['price'], //提现金额
  61. 'extract_price' => $data['price'] - $charge, //到账金额
  62. 'service_price'=>$charge, //手续费
  63. 'balance' => $user['brokerage_price'] - $data['price'],
  64. 'status' => 0
  65. ];
  66. $mark = '提现' . $data['price'] . '元'."(手续费:$charge 元)";
  67. return $this->transaction(function () use ($insertData, $data, $user, $userService, $mark) {
  68. if (!$res = $this->dao->save($insertData)) {
  69. throw new ValidateException('提现失败');
  70. }
  71. if (!$userService->decField(['uid'=>$user['uid']], 'brokerage_price', $data['price'])) {
  72. throw new ValidateException('修改用户信息失败');
  73. }
  74. $balance = bcsub((string)$user['brokerage_price'], (string)$data['price'], 2) ?? 0;
  75. //保存佣金记录
  76. $userBrokerageServices = new UserBrokerageService();
  77. $userBrokerageServices->income('extract', $user['uid'], ['mark' => $mark, 'number' => $data['price']], $balance, $res['id']);
  78. return true;
  79. });
  80. }
  81. /**
  82. * 拒绝
  83. * @param $id
  84. * @return mixed
  85. */
  86. public function refuse(int $id, string $message)
  87. {
  88. $extract = $this->dao->getOne(['id'=>$id]);
  89. if ($extract->isEmpty()) {
  90. throw new ValidateException('操作记录不存在');
  91. }
  92. if ($extract->status == 1) {
  93. throw new ValidateException('已经提现,错误操作');
  94. }
  95. if ($extract->status == -1) {
  96. throw new ValidateException('您的提现申请已被拒绝,请勿重复操作!');
  97. }
  98. $fail_time = getDateFull();
  99. $extract_number = $extract['extract_price'] + $extract['service_price'];
  100. $mark = '提现失败,退回佣金' . $extract_number . '元';
  101. $uid = $extract['uid'];
  102. $status = -1;
  103. $userServices = new UserService();
  104. $user = $userServices->getOne(['uid'=>$uid]);
  105. return $this->transaction(function () use ($user, $uid, $id, $extract_number, $message, $userServices, $status, $fail_time) {
  106. //增加佣金记录
  107. $userBrokerageServices = new UserBrokerageService();
  108. $now_brokerage = bcadd((string)$user['brokerage_price'], (string)$extract_number, 2);
  109. $userBrokerageServices->income('extract_fail', $uid, $extract_number, $now_brokerage, $id);
  110. if (!$userServices->incField(['uid'=>$uid], 'brokerage_price', $extract_number))
  111. throw new ValidateException('增加用户佣金失败');
  112. if (!$this->dao->update($id, ['fail_time' => $fail_time, 'fail_msg' => $message, 'status' => $status])) {
  113. throw new ValidateException('增加用户佣金失败');
  114. }
  115. return true;
  116. });
  117. }
  118. /**
  119. * 通过
  120. * @param $id
  121. * @return mixed
  122. */
  123. public function adopt(int $id)
  124. {
  125. $extract = $this->dao->getOne(['id'=>$id]);
  126. if ($extract->isEmpty()) {
  127. throw new ValidateException('操作记录不存在');
  128. }
  129. if ($extract->status == 1) {
  130. throw new AdminException('您已提现,请勿重复提现!');
  131. }
  132. if ($extract->status == -1) {
  133. throw new AdminException('您的提现申请已被拒绝!');
  134. }
  135. $data['status'] = 1;
  136. $data['succeed_time'] = getDateFull();
  137. return $this->dao->update($id, $data);
  138. }
  139. }