UserBrokerageServices.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\user;
  13. use app\model\user\UserBrokerage;
  14. use qiniu\basic\BaseServices;
  15. use think\exception\ValidateException;
  16. use qiniu\services\CacheService;
  17. /**
  18. * 用户佣金
  19. * Class UserBrokerageServices
  20. * @package app\services\user
  21. * @mixin UserBrokerage
  22. */
  23. class UserBrokerageServices extends BaseServices
  24. {
  25. /**
  26. * 用户记录模板
  27. * @var array[]
  28. */
  29. protected $incomeData = [
  30. 'system_add_brokerage' => [
  31. 'title' => '系统添加佣金',
  32. 'type' => 'system_add',
  33. 'mark' => '系统添加{%number%}佣金{%mark%}',
  34. 'status' => 1,
  35. 'pm' => 1
  36. ],
  37. 'system_sub_brokerage' => [
  38. 'title' => '系统减少佣金',
  39. 'type' => 'system_sub',
  40. 'mark' => '系统扣除{%number%}佣金{%mark%}',
  41. 'status' => 1,
  42. 'pm' => 0
  43. ],
  44. 'trade_in' => [
  45. 'title' => '佣金转入',
  46. 'type' => 'trade_in',
  47. 'mark' => '{%mark%}{%number%}元',
  48. 'status' => 1,
  49. 'pm' => 1
  50. ],
  51. 'trade_out' => [
  52. 'title' => '佣金转出',
  53. 'type' => 'trade_out',
  54. 'mark' => '{%mark%}{%number%}元',
  55. 'status' => 1,
  56. 'pm' => 0
  57. ],
  58. 'extract' => [
  59. 'title' => '佣金提现',
  60. 'type' => 'extract',
  61. 'mark' => '{%mark%},佣金提现{%number%}元',
  62. 'status' => 1,
  63. 'pm' => 0
  64. ],
  65. 'extract_fail' => [
  66. 'title' => '提现失败',
  67. 'type' => 'extract_fail',
  68. 'mark' => '提现失败,退回佣金{%num%}元',
  69. 'status' => 1,
  70. 'pm' => 1
  71. ],
  72. ];
  73. /**
  74. * UserBrokerageServices constructor.
  75. * @param UserBrokerage $model
  76. */
  77. public function __construct(UserBrokerage $model)
  78. {
  79. $this->model = $model;
  80. }
  81. /**
  82. * 计算佣金
  83. * @param array $where
  84. * @param int $time
  85. * @return mixed
  86. */
  87. public function getUsersBrokerageSum(array $where, $time = 0)
  88. {
  89. $where_data = [
  90. 'status' => 1,
  91. 'pm' => $where['pm'] ?? '',
  92. 'uid' => $where['uid'] ?? '',
  93. 'time' => $where['time'] ?? 0,
  94. 'type' => $where['type'] ?? '',
  95. 'not_type' => $where['not_type'] ?? ''
  96. ];
  97. if ($time) $where_data['time'] = $time;
  98. return $this->getBrokerageSumColumn($where_data);
  99. }
  100. /**
  101. * 获取某些条件的bill总数
  102. * @param array $where
  103. * @return mixed
  104. */
  105. public function getBrokerageSumColumn(array $where)
  106. {
  107. if (isset($where['uid']) && is_array($where['uid'])) {
  108. return $this->search($where)->group('uid')->column('sum(number) as num', 'uid');
  109. } else
  110. return $this->search($where)->sum('number');
  111. }
  112. /**
  113. * 某个用户佣金总和
  114. * @param int $uid
  115. * @param string $time
  116. * @param int $pm
  117. * @return float
  118. */
  119. public function getUserBillBrokerageSum(int $uid, string $time = '', int $pm = 1)
  120. {
  121. $where = ['uid' => $uid];
  122. $where['pm'] = $pm;
  123. $where['status'] = 1;
  124. if ($time) $where['time'] = $time;
  125. return $this->getBrokerageSum($where);
  126. }
  127. /**
  128. * 获取某个条件总数
  129. * @param array $where
  130. * @return float
  131. */
  132. public function getBrokerageSum(array $where)
  133. {
  134. return $this->search($where)->sum('number');
  135. }
  136. /**
  137. * 写入用户记录
  138. * @param string $type 写入类型
  139. * @param int $uid
  140. * @param int|string|array $number
  141. * @param int|string $link_id
  142. * @return bool|mixed
  143. * @throws \think\db\exception\DataNotFoundException
  144. * @throws \think\db\exception\DbException
  145. * @throws \think\db\exception\ModelNotFoundException
  146. */
  147. public function income(string $type, int $uid, $number, $link_id = 0, $mark = '')
  148. {
  149. $data = $this->incomeData[$type] ?? null;
  150. if (!$data) {
  151. throw new ValidateException('方法未完成');
  152. }
  153. $data['uid'] = $uid;
  154. $data['link_id'] = $link_id;
  155. if (is_array($number)) {
  156. $key = array_keys($number);
  157. $key = array_map(function ($item) {
  158. return '{%' . $item . '%}';
  159. }, $key);
  160. $value = array_values($number);
  161. $data['number'] = (string)($number['number'] ?? 0);
  162. $data['frozen_time'] = $number['frozen_time'] ?? 0;
  163. $data['mark'] = $mark ?: str_replace($key, $value, $data['mark']);
  164. } else {
  165. $data['number'] = (string)$number;
  166. $data['mark'] = $mark ?: str_replace(['{%number%}'], $number, $data['mark']);
  167. }
  168. /** @var UserServices $userService */
  169. $userService = app()->make(UserServices::class);
  170. if ((float)$data['number'] > 0) {
  171. if ($data['status'] == 1) {
  172. $user = $userService->getUserInfo($uid);
  173. if ($data['pm'] == 1) {
  174. $data['balance'] = bcadd((string)$user['brokerage_price'], $data['number'], 2);
  175. $userService->bcInc($uid, 'brokerage_price', $data['number'], 'uid');
  176. } else {
  177. if ($data['number'] > $user['brokerage_price']) {
  178. throw new ValidateException('用户佣金不足');
  179. }
  180. $data['balance'] = bcsub((string)$user['brokerage_price'], $data['number'], 2);
  181. $userService->bcDec($uid, 'brokerage_price', $data['number'], 'uid');
  182. }
  183. }
  184. return $this->create($data);
  185. }
  186. return true;
  187. }
  188. /**
  189. * 资金类型
  190. */
  191. public function bill_type()
  192. {
  193. $getBrokerageType = function () {
  194. $array = $this->incomeData;
  195. $list = [];
  196. foreach ($array as $v) {
  197. $list[] = ['type' => $v['type'], 'title' => $v['title'], 'pm' => $v['pm']];
  198. }
  199. return compact('list');
  200. };
  201. return CacheService::get('user_brokerage_type_list', $getBrokerageType, 600);
  202. }
  203. /**
  204. * 获取资金列表
  205. * @param array $where
  206. * @param string $field
  207. * @param int $limit
  208. * @return array
  209. * @throws \think\db\exception\DbException
  210. */
  211. public function getBrokerageList(array $where, string $field = '*', int $limit = 0)
  212. {
  213. $where_data = [];
  214. if (isset($where['uid']) && $where['uid'] != '') {
  215. $where_data['uid'] = $where['uid'];
  216. }
  217. if (($where['start_time'] ?? '') != '' && ($where['end_time'] ?? '') != '') {
  218. $where_data['time'] = str_replace('-', '/', $where['start_time']) . ' - ' . str_replace('-', '/', $where['end_time']);
  219. }
  220. if (isset($where['type']) && $where['type'] != '') {
  221. $where_data['type'] = $where['type'];
  222. }
  223. if (isset($where['nickname']) && $where['nickname'] != '') {
  224. $where_data['like'] = $where['nickname'];
  225. }
  226. if (isset($where['pm']) && $where['pm'] != '') {
  227. $where_data['pm'] = $where['pm'];
  228. }
  229. if ($limit) {
  230. [$page] = $this->getPageValue();
  231. } else {
  232. [$page, $limit] = $this->getPageValue();
  233. }
  234. $data = $this->getList($where_data, $field, $page, $limit, [
  235. 'user' => function ($query) {
  236. $query->field('uid,nickname');
  237. }
  238. ]);
  239. foreach ($data as &$item) {
  240. $item['nickname'] = $item['user']['nickname'] ?? '';
  241. unset($item['user']);
  242. }
  243. $count = $this->getCount($where_data);
  244. return compact('data', 'count');
  245. }
  246. /**
  247. * 用户佣金详情
  248. * @param int $uid
  249. * @return array
  250. */
  251. public function user_info(int $uid)
  252. {
  253. /** @var UserServices $user */
  254. $user = app()->make(UserServices::class);
  255. $user_info = $user->getUserWithTrashedInfo($uid, 'nickname,spread_uid,now_money,brokerage_price,add_time');
  256. if (!$user_info) {
  257. throw new ValidateException('您查看的用户信息不存在!');
  258. }
  259. $user_info = $user_info->toArray();
  260. $income = $this->getUserBillBrokerageSum($uid);
  261. $expend = $this->getUserBillBrokerageSum($uid, '', 0);
  262. $number = (float)bcsub((string)$income, (string)$expend, 2);
  263. $user_info['number'] = max($number, 0);
  264. $user_info['add_time'] = date('Y-m-d H:i:s', $user_info['add_time']);
  265. $user_info['spread_name'] = $user_info['spread_uid'] ? $user->getUserInfo((int)$user_info['spread_uid'], 'nickname')['nickname'] ?? '' : '';
  266. return compact('user_info');
  267. }
  268. /**
  269. * 获取某个账户下的冻结佣金
  270. * @param int $uid
  271. * @return float
  272. */
  273. public function getUserFrozenPrice(int $uid)
  274. {
  275. return $this->search(['uid' => $uid, 'status' => 1, 'pm' => 1])->where('frozen_time', '>', time())->sum('number');
  276. }
  277. public function trade(int $uid, int $to_uid, $num)
  278. {
  279. /** @var UserServices $userService */
  280. $userService = app()->make(UserServices::class);
  281. $user = $userService->getUserInfo($uid);
  282. $to_user = $userService->getUserInfo($to_uid);
  283. if (!$user || !$to_user) {
  284. throw new ValidateException('数据不存在');
  285. }
  286. if ($to_uid == $uid) throw new ValidateException('不能自己转给自己');
  287. $broken_commission = $this->getUserFrozenPrice($uid);
  288. if ($broken_commission < 0)
  289. $broken_commission = 0;
  290. $brokerage_price = $user['brokerage_price'];
  291. //可提现佣金
  292. $commissionCount = (float)bcsub((string)$brokerage_price, (string)$broken_commission, 2);
  293. if ($num > $commissionCount) {
  294. throw new ValidateException('可用佣金不足');
  295. }
  296. $extractPrice = $user['brokerage_price'];
  297. if ($extractPrice < 0) {
  298. throw new ValidateException('转账佣金不足' . $num);
  299. }
  300. if ($num > $extractPrice) {
  301. throw new ValidateException('转账佣金不足' . $num);
  302. }
  303. if ($num <= 0) {
  304. throw new ValidateException('转账佣金大于0');
  305. }
  306. return $this->transaction(function () use ($num, $user, $to_user, $userService) {
  307. //保存佣金记录
  308. $this->income('trade_out', $user['uid'], ['mark' => '转账给' . $to_user['nickname'] . '(' . $to_user['uid'] . ')', 'number' => $num], $to_user['uid']);
  309. $this->income('trade_in', $to_user['uid'], ['mark' => '转账自' . $user['nickname'] . '(' . $user['uid'] . ')', 'number' => $num], $user['uid']);
  310. return true;
  311. });
  312. }
  313. }