UserBrokerageServices.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. ];
  59. /**
  60. * UserBrokerageServices constructor.
  61. * @param UserBrokerage $model
  62. */
  63. public function __construct(UserBrokerage $model)
  64. {
  65. $this->model = $model;
  66. }
  67. /**
  68. * 计算佣金
  69. * @param array $where
  70. * @param int $time
  71. * @return mixed
  72. */
  73. public function getUsersBrokerageSum(array $where, $time = 0)
  74. {
  75. $where_data = [
  76. 'status' => 1,
  77. 'pm' => $where['pm'] ?? '',
  78. 'uid' => $where['uid'] ?? '',
  79. 'time' => $where['time'] ?? 0,
  80. 'type' => $where['type'] ?? '',
  81. 'not_type' => $where['not_type'] ?? ''
  82. ];
  83. if ($time) $where_data['time'] = $time;
  84. return $this->getBrokerageSumColumn($where_data);
  85. }
  86. /**
  87. * 获取某些条件的bill总数
  88. * @param array $where
  89. * @return mixed
  90. */
  91. public function getBrokerageSumColumn(array $where)
  92. {
  93. if (isset($where['uid']) && is_array($where['uid'])) {
  94. return $this->search($where)->group('uid')->column('sum(number) as num', 'uid');
  95. } else
  96. return $this->search($where)->sum('number');
  97. }
  98. /**
  99. * 某个用户佣金总和
  100. * @param int $uid
  101. * @param string $time
  102. * @param int $pm
  103. * @return float
  104. */
  105. public function getUserBillBrokerageSum(int $uid, string $time = '', int $pm = 1)
  106. {
  107. $where = ['uid' => $uid];
  108. $where['pm'] = $pm;
  109. $where['status'] = 1;
  110. if ($time) $where['time'] = $time;
  111. return $this->getBrokerageSum($where);
  112. }
  113. /**
  114. * 获取某个条件总数
  115. * @param array $where
  116. * @return float
  117. */
  118. public function getBrokerageSum(array $where)
  119. {
  120. return $this->search($where)->sum('number');
  121. }
  122. /**
  123. * 写入用户记录
  124. * @param string $type 写入类型
  125. * @param int $uid
  126. * @param int|string|array $number
  127. * @param int|string $link_id
  128. * @return bool|mixed
  129. * @throws \think\db\exception\DataNotFoundException
  130. * @throws \think\db\exception\DbException
  131. * @throws \think\db\exception\ModelNotFoundException
  132. */
  133. public function income(string $type, int $uid, $number, $link_id = 0)
  134. {
  135. $data = $this->incomeData[$type] ?? null;
  136. if (!$data) {
  137. return true;
  138. }
  139. $data['uid'] = $uid;
  140. $data['link_id'] = $link_id;
  141. if (is_array($number)) {
  142. $key = array_keys($number);
  143. $key = array_map(function ($item) {
  144. return '{%' . $item . '%}';
  145. }, $key);
  146. $value = array_values($number);
  147. $data['number'] = $number['number'] ?? 0;
  148. $data['frozen_time'] = $number['frozen_time'] ?? 0;
  149. $data['mark'] = str_replace($key, $value, $data['mark']);
  150. } else {
  151. $data['number'] = $number;
  152. $data['mark'] = str_replace(['{%number%}'], $number, $data['mark']);
  153. }
  154. /** @var UserServices $userService */
  155. $userService = app()->make(UserServices::class);
  156. if ((float)$data['number'] > 0) {
  157. if ($data['status'] == 1) {
  158. $user = $userService->getUserInfo($uid);
  159. if ($data['pm'] == 1) {
  160. $data['balance'] = bcadd($user['brokerage_price'], $data['number'], 2);
  161. $userService->bcInc($uid, 'brokerage_price', $data['number'], 'uid');
  162. } else {
  163. if ($data['number'] > $user['brokerage_price']) {
  164. throw new ValidateException('用户佣金不足');
  165. }
  166. $data['balance'] = bcsub($user['brokerage_price'], $data['number'], 2);
  167. $userService->bcDec($uid, 'brokerage_price', $data['number'], 'uid');
  168. }
  169. }
  170. return $this->save($data);
  171. }
  172. return true;
  173. }
  174. /**
  175. * 资金类型
  176. */
  177. public function bill_type()
  178. {
  179. $getBrokerageType = function () {
  180. $array = $this->incomeData;
  181. $list = [];
  182. foreach ($array as $v) {
  183. $list[] = ['type' => $v['type'], 'title' => $v['title'], 'pm' => $v['pm']];
  184. }
  185. return compact('list');
  186. };
  187. return CacheService::get('user_brokerage_type_list', $getBrokerageType, 600);
  188. }
  189. /**
  190. * 获取资金列表
  191. * @param array $where
  192. * @param string $field
  193. * @param int $limit
  194. * @return array
  195. * @throws \think\db\exception\DbException
  196. */
  197. public function getBrokerageList(array $where, string $field = '*', int $limit = 0)
  198. {
  199. $where_data = [];
  200. if (isset($where['uid']) && $where['uid'] != '') {
  201. $where_data['uid'] = $where['uid'];
  202. }
  203. if ($where['start_time'] != '' && $where['end_time'] != '') {
  204. $where_data['time'] = str_replace('-', '/', $where['start_time']) . ' - ' . str_replace('-', '/', $where['end_time']);
  205. }
  206. if (isset($where['type']) && $where['type'] != '') {
  207. $where_data['type'] = $where['type'];
  208. }
  209. if (isset($where['nickname']) && $where['nickname'] != '') {
  210. $where_data['like'] = $where['nickname'];
  211. }
  212. if (isset($where['pm']) && $where['pm'] != '') {
  213. $where_data['pm'] = $where['pm'];
  214. }
  215. if ($limit) {
  216. [$page] = $this->getPageValue();
  217. } else {
  218. [$page, $limit] = $this->getPageValue();
  219. }
  220. $data = $this->getList($where_data, $field, $page, $limit, [
  221. 'user' => function ($query) {
  222. $query->field('uid,nickname');
  223. }
  224. ]);
  225. foreach ($data as &$item) {
  226. $item['nickname'] = $item['user']['nickname'] ?? '';
  227. $item['_add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
  228. unset($item['user']);
  229. }
  230. $count = $this->getCount($where_data);
  231. return compact('data', 'count');
  232. }
  233. /**
  234. * 用户佣金详情
  235. * @param int $uid
  236. * @return array
  237. */
  238. public function user_info(int $uid)
  239. {
  240. /** @var UserServices $user */
  241. $user = app()->make(UserServices::class);
  242. $user_info = $user->getUserWithTrashedInfo($uid, 'nickname,spread_uid,now_money,brokerage_price,add_time');
  243. if (!$user_info) {
  244. throw new ValidateException('您查看的用户信息不存在!');
  245. }
  246. $user_info = $user_info->toArray();
  247. $income = $this->getUserBillBrokerageSum($uid);
  248. $expend = $this->getUserBillBrokerageSum($uid, '', 0);
  249. $number = (float)bcsub((string)$income, (string)$expend, 2);
  250. $user_info['number'] = max($number, 0);
  251. $user_info['add_time'] = date('Y-m-d H:i:s', $user_info['add_time']);
  252. $user_info['spread_name'] = $user_info['spread_uid'] ? $user->getUserInfo((int)$user_info['spread_uid'], 'nickname')['nickname'] ?? '' : '';
  253. return compact('user_info');
  254. }
  255. /**
  256. * 获取某个账户下的冻结佣金
  257. * @param int $uid
  258. * @return float
  259. */
  260. public function getUserFrozenPrice(int $uid)
  261. {
  262. return $this->search(['uid' => $uid, 'status' => 1, 'pm' => 1])->where('frozen_time', '>', time())->sum('number');
  263. }
  264. public function trade(int $uid, int $to_uid, $num)
  265. {
  266. /** @var UserServices $userService */
  267. $userService = app()->make(UserServices::class);
  268. $user = $userService->getUserInfo($uid);
  269. $to_user = $userService->getUserInfo($to_uid);
  270. if (!$user || !$to_user) {
  271. throw new ValidateException('数据不存在');
  272. }
  273. if ($to_uid == $uid) throw new ValidateException('不能自己转给自己');
  274. $broken_commission = $this->getUserFrozenPrice($uid);
  275. if ($broken_commission < 0)
  276. $broken_commission = 0;
  277. $brokerage_price = $user['brokerage_price'];
  278. //可提现佣金
  279. $commissionCount = (float)bcsub((string)$brokerage_price, (string)$broken_commission, 2);
  280. if ($num > $commissionCount) {
  281. throw new ValidateException('可用佣金不足');
  282. }
  283. $extractPrice = $user['brokerage_price'];
  284. if ($extractPrice < 0) {
  285. throw new ValidateException('转账佣金不足' . $num);
  286. }
  287. if ($num > $extractPrice) {
  288. throw new ValidateException('转账佣金不足' . $num);
  289. }
  290. if ($num <= 0) {
  291. throw new ValidateException('转账佣金大于0');
  292. }
  293. return $this->transaction(function () use ($num, $user, $to_user, $userService) {
  294. //保存佣金记录
  295. $this->income('trade_out', $user['uid'], ['mark' => '转账给' . $to_user['nickname'] . '(' . $to_user['uid'] . ')', 'number' => $num], $to_user['uid']);
  296. $this->income('trade_in', $to_user['uid'], ['mark' => '转账自' . $user['nickname'] . '(' . $user['uid'] . ')', 'number' => $num], $user['uid']);
  297. return true;
  298. });
  299. }
  300. }