UserBrokerageServices.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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\dao\user\UserBrokerageDao;
  14. use app\services\BaseServices;
  15. use app\services\order\StoreOrderCartInfoServices;
  16. use app\services\order\StoreOrderServices;
  17. use think\exception\ValidateException;
  18. use crmeb\services\CacheService;
  19. /**
  20. * 用户佣金
  21. * Class UserBrokerageServices
  22. * @package app\services\user
  23. * @mixin UserBrokerageDao
  24. */
  25. class UserBrokerageServices extends BaseServices
  26. {
  27. /**
  28. * 用户记录模板
  29. * @var array[]
  30. */
  31. protected $incomeData = [
  32. 'get_self_brokerage' => [
  33. 'title' => '获得自购订单佣金',
  34. 'type' => 'self_brokerage',
  35. 'mark' => '您成功消费{%pay_price%}元,奖励自购佣金{%number%}',
  36. 'status' => 1,
  37. 'pm' => 1
  38. ],
  39. 'get_brokerage' => [
  40. 'title' => '获得下级推广订单佣金',
  41. 'type' => 'one_brokerage',
  42. 'mark' => '{%nickname%}成功消费{%pay_price%}元,奖励推广佣金{%number%}',
  43. 'status' => 1,
  44. 'pm' => 1
  45. ],
  46. 'get_two_brokerage' => [
  47. 'title' => '获得推广订单佣金',
  48. 'type' => 'two_brokerage',
  49. 'mark' => '二级推广人{%nickname%}成功消费{%pay_price%}元,奖励推广佣金{%number%}',
  50. 'status' => 1,
  51. 'pm' => 1
  52. ],
  53. 'get_user_brokerage' => [
  54. 'title' => '获得推广用户佣金',
  55. 'type' => 'brokerage_user',
  56. 'mark' => '成功推广用户:{%nickname%},奖励推广佣金{%number%}',
  57. 'status' => 1,
  58. 'pm' => 1
  59. ],
  60. 'extract' => [
  61. 'title' => '佣金提现',
  62. 'type' => 'extract',
  63. 'mark' => '{%mark%},佣金提现{%number%}元',
  64. 'status' => 1,
  65. 'pm' => 0
  66. ],
  67. 'extract_fail' => [
  68. 'title' => '提现失败',
  69. 'type' => 'extract_fail',
  70. 'mark' => '提现失败,退回佣金{%num%}元',
  71. 'status' => 1,
  72. 'pm' => 1
  73. ],
  74. 'brokerage_to_nowMoney' => [
  75. 'title' => '佣金提现到余额',
  76. 'type' => 'extract_money',
  77. 'mark' => '佣金提现到余额{%num%}元',
  78. 'status' => 1,
  79. 'pm' => 0
  80. ],
  81. 'brokerage_to_energy' => [
  82. 'title' => '佣金转换到能量',
  83. 'type' => 'extract_energy',
  84. 'mark' => '转换到能量{%num%}',
  85. 'status' => 1,
  86. 'pm' => 0
  87. ],
  88. 'brokerage_refund' => [
  89. 'title' => '退款退佣金',
  90. 'type' => 'refund',
  91. 'mark' => '订单退款扣除佣金{%num%}元',
  92. 'status' => 1,
  93. 'pm' => 0
  94. ],
  95. 'extract_integral' => [
  96. 'title' => '积分出局',
  97. 'type' => 'extract_integral',
  98. 'mark' => '用户{%type%}出局,出局时积分价格{%price%},共出局{%integral_num%}积分,获得佣金{%number%}元',
  99. 'status' => 1,
  100. 'pm' => 1
  101. ],
  102. 'get_area_brokerage' => [
  103. 'title' => '获得运营中心订单佣金',
  104. 'type' => 'area_brokerage',
  105. 'mark' => '{%nickname%}成功消费{%pay_price%}元,奖励{%area%}运营中心分红{%number%}',
  106. 'status' => 1,
  107. 'pm' => 1
  108. ],
  109. 'get_area_award' => [
  110. 'title' => '获得运营中心订单佣金',
  111. 'type' => 'area_brokerage',
  112. 'mark' => '结算周期内地区订单达到{%pay_price%}元,奖励{%area%}运营中心分红{%number%}',
  113. 'status' => 1,
  114. 'pm' => 1
  115. ],
  116. 'trade_in' => [
  117. 'title' => '佣金转入',
  118. 'type' => 'trade_in',
  119. 'mark' => '{%mark%}{$number}元',
  120. 'status' => 1,
  121. 'pm' => 1
  122. ],
  123. 'trade_out' => [
  124. 'title' => '佣金转出',
  125. 'type' => 'trade_out',
  126. 'mark' => '{%mark%}{$number}元',
  127. 'status' => 1,
  128. 'pm' => 0
  129. ],
  130. ];
  131. /**
  132. * UserBrokerageServices constructor.
  133. * @param UserBrokerageDao $dao
  134. */
  135. public function __construct(UserBrokerageDao $dao)
  136. {
  137. $this->dao = $dao;
  138. }
  139. /**
  140. * 计算佣金
  141. * @param array $where
  142. * @param int $time
  143. * @return mixed
  144. */
  145. public function getUsersBokerageSum(array $where, $time = 0)
  146. {
  147. $where_data = [
  148. 'status' => 1,
  149. 'pm' => $where['pm'] ?? '',
  150. 'uid' => $where['uid'] ?? '',
  151. 'time' => $where['time'] ?? 0,
  152. 'type' => $where['type'] ?? '',
  153. 'not_type' => $where['not_type'] ?? ''
  154. ];
  155. if ($time) $where_data['time'] = $time;
  156. return $this->dao->getBrokerageSumColumn($where_data);
  157. }
  158. /**
  159. * 某个用户佣金总和
  160. * @param int $uid
  161. * @param array|string[] $type
  162. * @param string $time
  163. * @return float
  164. */
  165. public function getUserBillBrokerageSum(int $uid, array $type = ['self_brokerage', 'one_brokerage', 'two_brokerage', 'brokerage_user'], $time = '')
  166. {
  167. $where = ['uid' => $uid];
  168. if ($type) $where['type'] = $type;
  169. if ($time) $where['time'] = $time;
  170. return $this->dao->getBrokerageSum($where);
  171. }
  172. /**
  173. * 写入用户记录
  174. * @param string $type 写入类型
  175. * @param int $uid
  176. * @param int|string|array $number
  177. * @param int|string $balance
  178. * @param int $link_id
  179. * @return bool|mixed
  180. */
  181. public function income(string $type, int $uid, $number, $balance, $link_id)
  182. {
  183. $data = $this->incomeData[$type] ?? null;
  184. if (!$data) {
  185. return true;
  186. }
  187. $data['uid'] = $uid;
  188. $data['balance'] = $balance ?? 0;
  189. $data['link_id'] = $link_id;
  190. if (is_array($number)) {
  191. $key = array_keys($number);
  192. $key = array_map(function ($item) {
  193. return '{%' . $item . '%}';
  194. }, $key);
  195. $value = array_values($number);
  196. $data['number'] = $number['number'] ?? 0;
  197. $data['frozen_time'] = $number['frozen_time'] ?? 0;
  198. $data['mark'] = str_replace($key, $value, $data['mark']);
  199. } else {
  200. $data['number'] = $number;
  201. $data['mark'] = str_replace(['{%num%}'], $number, $data['mark']);
  202. }
  203. $data['add_time'] = time();
  204. if ((float)$data['number']) {
  205. return $this->dao->save($data);
  206. }
  207. return true;
  208. }
  209. /**
  210. * 资金类型
  211. */
  212. public function bill_type()
  213. {
  214. return CacheService::get('user_brokerage_type_list', function () {
  215. return ['list' => $this->dao->getBrokerageType([])];
  216. }, 600);
  217. }
  218. /**
  219. * 获取资金列表
  220. * @param array $where
  221. * @param string $field
  222. * @param int $limit
  223. * @return array
  224. */
  225. public function getBrokerageList(array $where, string $field = '*', int $limit = 0)
  226. {
  227. $where_data = [];
  228. if (isset($where['uid']) && $where['uid'] != '') {
  229. $where_data['uid'] = $where['uid'];
  230. }
  231. if ($where['start_time'] != '' && $where['end_time'] != '') {
  232. $where_data['time'] = str_replace('-', '/', $where['start_time']) . ' - ' . str_replace('-', '/', $where['end_time']);
  233. }
  234. if (isset($where['type']) && $where['type'] != '') {
  235. $where_data['type'] = $where['type'];
  236. }
  237. if (isset($where['nickname']) && $where['nickname'] != '') {
  238. $where_data['like'] = $where['nickname'];
  239. }
  240. if ($limit) {
  241. [$page] = $this->getPageValue();
  242. } else {
  243. [$page, $limit] = $this->getPageValue();
  244. }
  245. $data = $this->dao->getBrokerageList($where_data, $field, $page, $limit);
  246. foreach ($data as &$item) {
  247. $item['nickname'] = $item['user']['nickname'] ?? '';
  248. $item['_add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
  249. unset($item['user']);
  250. }
  251. $count = $this->dao->count($where_data);
  252. return compact('data', 'count');
  253. }
  254. /**
  255. * 获取佣金列表
  256. * @param array $where
  257. * @param int $limit
  258. * @return array
  259. */
  260. public function getCommissionList(array $where, int $limit = 0)
  261. {
  262. $where_data = [];
  263. $where_data['time'] = $where['time'];
  264. if (isset($where['nickname']) && $where['nickname']) {
  265. $where_data[] = ['u.account|u.nickname|u.uid|u.phone', 'LIKE', "%$where[nickname]%"];
  266. }
  267. if (isset($where['price_max']) && isset($where['price_min'])) {
  268. if ($where['price_max'] != '' && $where['price_min'] != '') {
  269. $where_data[] = ['u.brokerage_price', 'between', [$where['price_min'], $where['price_max']]];
  270. } elseif ($where['price_min'] != '' && $where['price_max'] == '') {
  271. $where_data[] = ['u.brokerage_price', '>=', $where['price_min']];
  272. } elseif ($where['price_min'] == '' && $where['price_max'] != '') {
  273. $where_data[] = ['u.brokerage_price', '<=', $where['price_max']];
  274. }
  275. }
  276. $order_string = '';
  277. $order_arr = ['asc', 'desc'];
  278. if (isset($where['sum_number']) && in_array($where['sum_number'], $order_arr)) {
  279. $order_string .= ',income ' . $where['sum_number'];
  280. }
  281. if (isset($where['brokerage_price']) && in_array($where['brokerage_price'], $order_arr)) {
  282. $order_string .= ',u.brokerage_price ' . $where['brokerage_price'];
  283. }
  284. if ($order_string) {
  285. $order_string = trim($order_string, ',');
  286. }
  287. /** @var UserUserBrokerageServices $userUserBrokerage */
  288. $userUserBrokerage = app()->make(UserUserBrokerageServices::class);
  289. [$count, $list] = $userUserBrokerage->getBrokerageList($where_data, 'b.type,b.pm,sum(IF(b.pm = 1, b.number, 0)) as income,sum(IF(b.pm = 0, b.number, 0)) as pay,u.nickname,u.phone,u.uid,u.now_money,u.brokerage_price,u.delete_time,b.add_time as time', $order_string, $limit);
  290. $uids = array_unique(array_column($list, 'uid'));
  291. /** @var UserExtractServices $userExtract */
  292. $userExtract = app()->make(UserExtractServices::class);
  293. $extractSumList = $userExtract->getUsersSumList($uids);
  294. foreach ($list as &$item) {
  295. // $item['sum_number'] = $item['income'] > $item['pay'] ? bcsub($item['income'], $item['pay'], 2) : 0;
  296. $item['nickname'] = $item['nickname'] . "|" . ($item['phone'] ? $item['phone'] . "|" : '') . $item['uid'];
  297. $item['extract_price'] = $extractSumList[$item['uid']] ?? 0;
  298. $item['sum_number'] = bcadd((string)$item['extract_price'], (string)$item['brokerage_price'], 2);
  299. $item['time'] = $item['time'] ? date('Y-m-d H:i:s', $item['time']) : '';
  300. }
  301. return compact('count', 'list');
  302. }
  303. /**
  304. * 用户佣金详情
  305. * @param int $uid
  306. * @return array
  307. */
  308. public function user_info(int $uid)
  309. {
  310. /** @var UserServices $user */
  311. $user = app()->make(UserServices::class);
  312. $user_info = $user->getUserWithTrashedInfo($uid, 'nickname,spread_uid,now_money,brokerage_price,add_time');
  313. if (!$user_info) {
  314. throw new ValidateException('您查看的用户信息不存在!');
  315. }
  316. $user_info = $user_info->toArray();
  317. $income = $this->getUserBillBrokerageSum($uid);
  318. $expend = $this->getUserBillBrokerageSum($uid, ['refund']);
  319. $number = (float)bcsub((string)$income, (string)$expend, 2);
  320. $user_info['number'] = max($number, 0);
  321. $user_info['add_time'] = date('Y-m-d H:i:s', $user_info['add_time']);
  322. $user_info['spread_name'] = $user_info['spread_uid'] ? $user->getUserInfo((int)$user_info['spread_uid'], 'nickname', true)['nickname'] ?? '' : '';
  323. return compact('user_info');
  324. }
  325. /**
  326. * 退佣金
  327. * @param $order
  328. * @return bool
  329. * @throws \think\db\exception\DataNotFoundException
  330. * @throws \think\db\exception\DbException
  331. * @throws \think\db\exception\ModelNotFoundException
  332. */
  333. public function orderRefundBrokerageBack($order)
  334. {
  335. $id = (int)$order['id'];
  336. $where = [
  337. 'uid' => [$order['spread_uid'], $order['spread_two_uid']],
  338. 'type' => ['self_brokerage', 'one_brokerage', 'two_brokerage'],
  339. 'link_id' => $id,
  340. 'pm' => 1
  341. ];
  342. $brokerageList = $this->dao->getUserBrokerageList($where);
  343. //子订单
  344. if (!$brokerageList && $order['pid']) {
  345. $where['link_id'] = $order['pid'];
  346. $p_brokerageList = $this->dao->getUserBrokerageList($where);
  347. //主订单已分佣 子订单按订单拆分后计算结果回退
  348. if ($p_brokerageList) {
  349. $brokerageList = [
  350. ['uid' => $order['spread_uid'], 'number' => $order['one_brokerage']],
  351. ['uid' => $order['spread_two_uid'], 'number' => $order['two_brokerage']],
  352. ];
  353. }
  354. }
  355. $res = true;
  356. if ($brokerageList) {
  357. /** @var UserServices $userServices */
  358. $userServices = app()->make(UserServices::class);
  359. $brokerages = $userServices->getColumn([['uid', 'in', array_column($brokerageList, 'uid')]], 'brokerage_price', 'uid');
  360. $brokerageData = [];
  361. foreach ($brokerageList as $item) {
  362. if (!$item['uid'] || $item['uid'] <= 0) continue;
  363. $usermoney = $brokerages[$item['uid']] ?? 0;
  364. if ($item['number'] > $usermoney) {
  365. $item['number'] = $usermoney;
  366. }
  367. if ($item['number'] <= 0) continue;
  368. $res = $res && $userServices->bcDec($item['uid'], 'brokerage_price', (string)$item['number'], 'uid');
  369. $brokerageData[] = [
  370. 'title' => '退款退佣金',
  371. 'uid' => $item['uid'],
  372. 'pm' => 0,
  373. 'add_time' => time(),
  374. 'type' => 'refund',
  375. 'number' => $item['number'],
  376. 'link_id' => $id,
  377. 'balance' => bcsub((string)$usermoney, (string)$item['number'], 2),
  378. 'mark' => '订单退款扣除佣金' . floatval($item['number']) . '元'
  379. ];
  380. }
  381. if ($brokerageData) {
  382. $res = $res && $this->dao->saveAll($brokerageData);
  383. }
  384. //修改佣金冻结时间
  385. $this->dao->update($where, ['frozen_time' => 0]);
  386. }
  387. return $res;
  388. }
  389. /**
  390. * 佣金排行
  391. * @param string $time
  392. * @return array
  393. * @throws \think\db\exception\DataNotFoundException
  394. * @throws \think\db\exception\DbException
  395. * @throws \think\db\exception\ModelNotFoundException
  396. */
  397. public function brokerageRankList(string $time = 'week')
  398. {
  399. $where = [];
  400. if ($time) {
  401. $where['time'] = $time;
  402. }
  403. [$page, $limit] = $this->getPageValue();
  404. $list = $this->dao->brokerageRankList($where, $page, $limit);
  405. foreach ($list as $key => &$item) {
  406. if (!isset($item['user']) || !$item['user'] || $item['brokerage_price'] <= 0) {
  407. unset($list[$key]);
  408. continue;
  409. }
  410. $item['nickname'] = $item['user']['nickname'] ?? '';
  411. $item['avatar'] = $item['user']['avatar'] ?? '';
  412. if ($item['brokerage_price'] == '0.00' || $item['brokerage_price'] == 0 || !$item['brokerage_price']) {
  413. unset($list[$key]);
  414. }
  415. unset($item['user']);
  416. }
  417. return array_merge($list);
  418. }
  419. /**
  420. * 获取用户排名
  421. * @param int $uid
  422. * @param string $time
  423. */
  424. public function getUserBrokerageRank(int $uid, string $time = 'week')
  425. {
  426. $where = [];
  427. if ($time) {
  428. $where['time'] = $time;
  429. }
  430. $list = $this->dao->brokerageRankList($where);
  431. foreach ($list as $key => &$item) {
  432. if (!isset($item['user']) || !$item['user'] || $item['brokerage_price'] <= 0) {
  433. unset($list[$key]);
  434. }
  435. }
  436. $position_tmp_one = array_column($list, 'uid');
  437. $position_tmp_two = array_column($list, 'brokerage_price', 'uid');
  438. if (!in_array($uid, $position_tmp_one)) {
  439. $position = 0;
  440. } else {
  441. if ($position_tmp_two[$uid] == 0.00) {
  442. $position = 0;
  443. } else {
  444. $position = array_search($uid, $position_tmp_one) + 1;
  445. }
  446. }
  447. return $position;
  448. }
  449. /**
  450. * 推广数据 昨天的佣金 累计提现金额 当前佣金
  451. * @param int $uid
  452. * @return mixed
  453. */
  454. public function commission(int $uid)
  455. {
  456. /** @var UserServices $userServices */
  457. $userServices = app()->make(UserServices::class);
  458. if (!$userServices->userExist($uid)) {
  459. throw new ValidateException('数据不存在');
  460. }
  461. /** @var UserExtractServices $userExtract */
  462. $userExtract = app()->make(UserExtractServices::class);
  463. $data = [];
  464. $data['uid'] = $uid;
  465. $data['pm'] = 1;
  466. $data['commissionSum'] = $this->getUsersBokerageSum($data);
  467. $data['pm'] = 0;
  468. $data['commissionRefund'] = $this->getUsersBokerageSum($data);
  469. $data['commissionCount'] = $data['commissionSum'] > $data['commissionRefund'] ? bcsub((string)$data['commissionSum'], (string)$data['commissionRefund'], 2) : 0.00;
  470. $data['lastDayCount'] = $this->getUsersBokerageSum($data, 'yesterday');//昨天的佣金
  471. $data['extractCount'] = $userExtract->getUserExtract($uid);//累计提现金额
  472. return $data;
  473. }
  474. /**
  475. * 前端佣金排行页面数据
  476. * @param int $uid
  477. * @param $type
  478. * @return array
  479. * @throws \think\db\exception\DataNotFoundException
  480. * @throws \think\db\exception\DbException
  481. * @throws \think\db\exception\ModelNotFoundException
  482. */
  483. public function brokerage_rank(int $uid, $type)
  484. {
  485. /** @var UserServices $userService */
  486. $userService = app()->make(UserServices::class);
  487. if (!$userService->userExist($uid)) {
  488. throw new ValidateException('数据不存在');
  489. }
  490. return [
  491. 'rank' => $this->brokerageRankList($type),
  492. 'position' => $this->getUserBrokerageRank($uid, $type)
  493. ];
  494. }
  495. /**
  496. * 推广 佣金/提现 总和
  497. * @param int $uid
  498. * @param $type 3 佣金 4 提现
  499. * @return mixed
  500. */
  501. public function spread_count(int $uid, $type)
  502. {
  503. /** @var UserServices $userService */
  504. $userService = app()->make(UserServices::class);
  505. if (!$userService->userExist($uid)) {
  506. throw new ValidateException('数据不存在');
  507. }
  508. $count = 0;
  509. if ($type == 3) {
  510. $where = [
  511. 'uid' => $uid,
  512. 'status' => 1,
  513. 'pm' => 1
  514. ];
  515. $count1 = $this->dao->getBrokerageSumColumn($where);
  516. $where['pm'] = 0;
  517. $count2 = $this->dao->getBrokerageSumColumn($where);
  518. $count = $count1 - $count2;
  519. } else if ($type == 4) {
  520. /** @var UserExtractServices $userExtract */
  521. $userExtract = app()->make(UserExtractServices::class);
  522. $count = $userExtract->getUserExtract($uid);//累计提现
  523. }
  524. return $count ? $count : 0;
  525. }
  526. /**
  527. * 推广订单
  528. * @param Request $request
  529. * @return mixed
  530. */
  531. public function spread_order(int $uid, array $data)
  532. {
  533. /** @var UserServices $userService */
  534. $userService = app()->make(UserServices::class);
  535. if (!$userService->userExist($uid)) {
  536. throw new ValidateException('数据不存在');
  537. }
  538. $result = ['list' => [], 'time' => [], 'count' => 0];
  539. /** @var StoreOrderServices $storeOrderServices */
  540. $storeOrderServices = app()->make(StoreOrderServices::class);
  541. [$page, $limit] = $this->getPageValue();
  542. $time_data = [];
  543. $where = ['type' => 0, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0, 'spread_or_uid' => $uid];
  544. if ($data['start'] || $data['stop']) {
  545. $where['time'] = [$data['start'], $data['stop']];
  546. }
  547. $where['real_name'] = $data['keyword'];
  548. $list = $storeOrderServices->getList($where, ['id,order_id,uid,add_time,spread_uid,status,spread_two_uid,one_brokerage,two_brokerage,pay_price,cart_id'], $page, $limit, ['brokerage' => function ($query) use ($uid) {
  549. $query->where('uid', $uid);
  550. }]);
  551. $result['count'] = $storeOrderServices->count($where);
  552. if ($list) {
  553. /** @var StoreOrderCartInfoServices $cartInfoServices */
  554. $cartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  555. $uids = array_unique(array_column($list, 'uid'));
  556. $userInfos = $userService->getColumn([['uid', 'in', $uids]], 'uid,avatar,nickname', 'uid');
  557. foreach ($list as &$item) {
  558. $item['store_name'] = $cartInfoServices->getCarIdByProductTitle((int)$item['id']);
  559. $item['avatar'] = $userInfos[$item['uid']]['avatar'] ?? '';
  560. $item['nickname'] = $userInfos[$item['uid']]['nickname'] ?? '';
  561. $item['number'] = $item['spread_uid'] == $uid ? $item['one_brokerage'] : $item['two_brokerage'];
  562. $item['time_key'] = $item['add_time'] ? date('Y-m', $item['add_time']) : '';
  563. $item['type'] = in_array($item['status'], [2, 3]) ? 'brokerage' : 'number';
  564. $brokerage = $item['brokerage'];
  565. $time = $brokerage['add_time'] ?? $item['add_time'];
  566. $item['time'] = $time ? date('Y-m-d H:i', $time) : '';
  567. $item['is_frozen'] = $brokerage && $brokerage['frozen_time'] > time() ? 1 : 0;
  568. unset($item['brokerage']);
  569. }
  570. $times = array_unique(array_column($list, 'time_key'));
  571. $time_data = [];
  572. $i = 0;
  573. foreach ($times as $time) {
  574. $time_data[$i]['time'] = $time;
  575. $time_data[$i]['count'] = $storeOrderServices->getMonthCount($where, $time);
  576. $time_data[$i]['sumPrice'] = $storeOrderServices->getMonthMoneyCount($where, $time, 'pay_price');
  577. $i++;
  578. }
  579. }
  580. $result['list'] = $list;
  581. $result['time'] = $time_data;
  582. $priceWhere = ['pid' => 0, 'type' => 0, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0];
  583. if ($data['start'] || $data['stop']) {
  584. $priceWhere['time'] = [$data['start'], $data['stop']];
  585. }
  586. //条件获取一级佣金总和
  587. $sum_price_one = $storeOrderServices->sum($priceWhere + ['spread_uid' => $uid], 'one_brokerage', true);
  588. $sum_price_two = $storeOrderServices->sum($priceWhere + ['spread_two_uid' => $uid], 'two_brokerage', true);
  589. $result['sum_brokerage'] = bcadd((string)$sum_price_one, (string)$sum_price_two, 2);
  590. return $result;
  591. }
  592. /**
  593. * 用户佣金记录v2
  594. * @param int $uid
  595. * @return array
  596. */
  597. public function userBrokerageList(int $uid, $data = [])
  598. {
  599. $where = [];
  600. $where['uid'] = $uid;
  601. if ($data['start'] || $data['stop']) {
  602. $where['time'] = [$data['start'], $data['stop']];
  603. }
  604. $where['like'] = $data['keyword'];
  605. [$page, $limit] = $this->getPageValue();
  606. $list = $this->dao->getList($where, '*', $page, $limit);
  607. $times = [];
  608. if ($list) {
  609. foreach ($list as &$item) {
  610. $item['time_key'] = $item['add_time'] ? date('Y-m', (int)$item['add_time']) : '';
  611. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i', (int)$item['add_time']) : '';
  612. }
  613. $times = array_merge(array_unique(array_column($list, 'time_key')));
  614. }
  615. $income = $this->dao->sum($where + ['pm' => 1, 'not_type' => ['extract_fail']], 'number', true);
  616. $expend = $this->dao->sum($where + ['pm' => 0], 'number', true);
  617. return ['list' => $list, 'time' => $times, 'income' => $income, 'expend' => $expend];
  618. }
  619. /**
  620. * 用户提现记录v2
  621. * @param int $uid
  622. * @param array $data
  623. * @return array
  624. */
  625. public function userExtractList(int $uid, array $data)
  626. {
  627. $where = [];
  628. $where['uid'] = $uid;
  629. $where['type'] = ['extract', 'extract_money', 'extract_fail'];
  630. if ((isset($data['start']) && $data['start']) || (isset($data['stop']) && $data['stop'])) {
  631. $where['time'] = [$data['start'], $data['stop']];
  632. }
  633. [$page, $limit] = $this->getPageValue();
  634. $list = $this->dao->getList($where, '*', $page, $limit);
  635. $times = [];
  636. if ($list) {
  637. foreach ($list as &$item) {
  638. $item['time_key'] = $item['add_time'] ? date('Y-m', (int)$item['add_time']) : '';
  639. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i', (int)$item['add_time']) : '';
  640. }
  641. $times = array_merge(array_unique(array_column($list, 'time_key')));
  642. }
  643. return ['list' => $list, 'time' => $times];
  644. }
  645. public function trade(int $uid, int $to_uid, $num)
  646. {
  647. $switch = sys_config('brokerage_trade_switch', 1);
  648. if (!$switch) throw new ValidateException('暂不支持');
  649. /** @var UserServices $userService */
  650. $userService = app()->make(UserServices::class);
  651. $user = $userService->getUserInfo($uid);
  652. $to_user = $userService->getUserInfo($to_uid);
  653. if (!$user || !$to_user) {
  654. throw new ValidateException('数据不存在');
  655. }
  656. if ($to_uid == $uid) throw new ValidateException('不能自己转给自己');
  657. if ($user['is_auth'] != 2) throw new ValidateException('请先完成实名认证');
  658. $broken_commission = $this->getUserFrozenPrice($uid);
  659. if ($broken_commission < 0)
  660. $broken_commission = 0;
  661. $brokerage_price = $user['brokerage_price'];
  662. //可提现佣金
  663. $commissionCount = (float)bcsub((string)$brokerage_price, (string)$broken_commission, 2);
  664. if ($num > $commissionCount) {
  665. throw new ValidateException('可用佣金不足');
  666. }
  667. $extractPrice = $user['brokerage_price'];
  668. if ($extractPrice < 0) {
  669. throw new ValidateException('转账佣金不足' . $num);
  670. }
  671. if ($num > $extractPrice) {
  672. throw new ValidateException('转账佣金不足' . $num);
  673. }
  674. if ($num <= 0) {
  675. throw new ValidateException('转账佣金大于0');
  676. }
  677. return $this->transaction(function () use ($num, $user, $to_user, $userService) {
  678. //修改用户佣金
  679. $balance = bcsub((string)$user['brokerage_price'], (string)$num, 2) ?? 0;
  680. $balance2 = bcadd((string)$to_user['brokerage_price'], (string)$num, 2) ?? 0;
  681. if (!$userService->update($user['uid'], ['brokerage_price' => $balance], 'uid')) {
  682. throw new ValidateException('修改用户信息失败');
  683. }
  684. if (!$userService->update($to_user['uid'], ['brokerage_price' => $balance2], 'uid')) {
  685. throw new ValidateException('修改用户信息失败');
  686. }
  687. //保存佣金记录
  688. $this->income('trade_out', $user['uid'], ['mark' => '转账给' . $to_user['nickname'] . '(' . $to_user['uid'] . ')', 'number' => $num], $balance, 0);
  689. $this->income('trade_in', $to_user['uid'], ['mark' => '转账自' . $user['nickname'] . '(' . $user['uid'] . ')', 'number' => $num], $balance2, 0);
  690. return true;
  691. });
  692. }
  693. }