UserExtractService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 = 0;
  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. // 'extract_type' => $data['extract_type'],
  57. 'price' => $data['price'], //提现金额
  58. 'extract_price' => $data['price'] - $charge, //到账金额
  59. 'service_price'=>$charge, //手续费
  60. 'balance' => $user['brokerage_price'] - $data['price'],
  61. 'status' => 0
  62. ];
  63. $mark = '提现' . $data['price'] . '元'."(手续费:$charge 元)";
  64. return $this->transaction(function () use ($insertData, $data, $user, $userService, $mark) {
  65. if (!$res = $this->dao->save($insertData)) {
  66. throw new ValidateException('提现失败');
  67. }
  68. if (!$userService->decField(['uid'=>$user['uid']], 'brokerage_price', $data['price'])) {
  69. throw new ValidateException('修改用户信息失败');
  70. }
  71. $balance = bcsub((string)$user['brokerage_price'], (string)$data['price'], 2) ?? 0;
  72. //保存佣金记录
  73. $userBrokerageServices = new UserBrokerageService();
  74. $userBrokerageServices->income('extract', $user['uid'], ['mark' => $mark, 'number' => $data['price']], $balance, $res['id']);
  75. return true;
  76. });
  77. }
  78. /**
  79. * 拒绝
  80. * @param $id
  81. * @return mixed
  82. */
  83. public function refuse(int $id, string $message)
  84. {
  85. $extract = $this->dao->getOne(['id'=>$id]);
  86. if ($extract->isEmpty()) {
  87. throw new ValidateException('操作记录不存在');
  88. }
  89. if ($extract->status == 1) {
  90. throw new ValidateException('已经提现,错误操作');
  91. }
  92. if ($extract->status == -1) {
  93. throw new ValidateException('您的提现申请已被拒绝,请勿重复操作!');
  94. }
  95. $fail_time = getDateFull();
  96. $extract_number = $extract['extract_price'] + $extract['service_price'];
  97. $mark = '提现失败,退回佣金' . $extract_number . '元';
  98. $uid = $extract['uid'];
  99. $status = -1;
  100. $userServices = new UserService();
  101. $user = $userServices->getOne(['uid'=>$uid]);
  102. return $this->transaction(function () use ($user, $uid, $id, $extract_number, $message, $userServices, $status, $fail_time) {
  103. //增加佣金记录
  104. $userBrokerageServices = new UserBrokerageService();
  105. $now_brokerage = bcadd((string)$user['brokerage_price'], (string)$extract_number, 2);
  106. $userBrokerageServices->income('extract_fail', $uid, $extract_number, $now_brokerage, $id);
  107. if (!$userServices->incField(['uid'=>$uid], 'brokerage_price', $extract_number))
  108. throw new ValidateException('增加用户佣金失败');
  109. if (!$this->dao->update($id, ['fail_time' => $fail_time, 'fail_msg' => $message, 'status' => $status])) {
  110. throw new ValidateException('增加用户佣金失败');
  111. }
  112. return true;
  113. });
  114. }
  115. /**
  116. * 通过
  117. * @param $id
  118. * @return mixed
  119. */
  120. public function adopt(int $id)
  121. {
  122. $extract = $this->dao->getOne(['id'=>$id]);
  123. if ($extract->isEmpty()) {
  124. throw new ValidateException('操作记录不存在');
  125. }
  126. if ($extract->status == 1) {
  127. throw new AdminException('您已提现,请勿重复提现!');
  128. }
  129. if ($extract->status == -1) {
  130. throw new AdminException('您的提现申请已被拒绝!');
  131. }
  132. $data['status'] = 1;
  133. $data['succeed_time'] = getDateFull();
  134. return $this->dao->update($id, $data);
  135. }
  136. }