UserBrokerageServices.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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. ];
  117. /**
  118. * UserBrokerageServices constructor.
  119. * @param UserBrokerageDao $dao
  120. */
  121. public function __construct(UserBrokerageDao $dao)
  122. {
  123. $this->dao = $dao;
  124. }
  125. /**
  126. * 计算佣金
  127. * @param array $where
  128. * @param int $time
  129. * @return mixed
  130. */
  131. public function getUsersBokerageSum(array $where, $time = 0)
  132. {
  133. $where_data = [
  134. 'status' => 1,
  135. 'pm' => $where['pm'] ?? '',
  136. 'uid' => $where['uid'] ?? '',
  137. 'time' => $where['time'] ?? 0,
  138. 'type' => $where['type'] ?? '',
  139. 'not_type' => $where['not_type'] ?? ''
  140. ];
  141. if ($time) $where_data['time'] = $time;
  142. return $this->dao->getBrokerageSumColumn($where_data);
  143. }
  144. /**
  145. * 某个用户佣金总和
  146. * @param int $uid
  147. * @param array|string[] $type
  148. * @param string $time
  149. * @return float
  150. */
  151. public function getUserBillBrokerageSum(int $uid, array $type = ['self_brokerage', 'one_brokerage', 'two_brokerage', 'brokerage_user'], $time = '')
  152. {
  153. $where = ['uid' => $uid];
  154. if ($type) $where['type'] = $type;
  155. if ($time) $where['time'] = $time;
  156. return $this->dao->getBrokerageSum($where);
  157. }
  158. /**
  159. * 写入用户记录
  160. * @param string $type 写入类型
  161. * @param int $uid
  162. * @param int|string|array $number
  163. * @param int|string $balance
  164. * @param int $link_id
  165. * @return bool|mixed
  166. */
  167. public function income(string $type, int $uid, $number, $balance, $link_id)
  168. {
  169. $data = $this->incomeData[$type] ?? null;
  170. if (!$data) {
  171. return true;
  172. }
  173. $data['uid'] = $uid;
  174. $data['balance'] = $balance ?? 0;
  175. $data['link_id'] = $link_id;
  176. if (is_array($number)) {
  177. $key = array_keys($number);
  178. $key = array_map(function ($item) {
  179. return '{%' . $item . '%}';
  180. }, $key);
  181. $value = array_values($number);
  182. $data['number'] = $number['number'] ?? 0;
  183. $data['frozen_time'] = $number['frozen_time'] ?? 0;
  184. $data['mark'] = str_replace($key, $value, $data['mark']);
  185. } else {
  186. $data['number'] = $number;
  187. $data['mark'] = str_replace(['{%num%}'], $number, $data['mark']);
  188. }
  189. $data['add_time'] = time();
  190. if ((float)$data['number']) {
  191. return $this->dao->save($data);
  192. }
  193. return true;
  194. }
  195. /**
  196. * 资金类型
  197. */
  198. public function bill_type()
  199. {
  200. return CacheService::get('user_brokerage_type_list', function () {
  201. return ['list' => $this->dao->getBrokerageType([])];
  202. }, 600);
  203. }
  204. /**
  205. * 获取资金列表
  206. * @param array $where
  207. * @param string $field
  208. * @param int $limit
  209. * @return array
  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 ($limit) {
  227. [$page] = $this->getPageValue();
  228. } else {
  229. [$page, $limit] = $this->getPageValue();
  230. }
  231. $data = $this->dao->getBrokerageList($where_data, $field, $page, $limit);
  232. foreach ($data as &$item) {
  233. $item['nickname'] = $item['user']['nickname'] ?? '';
  234. $item['_add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
  235. unset($item['user']);
  236. }
  237. $count = $this->dao->count($where_data);
  238. return compact('data', 'count');
  239. }
  240. /**
  241. * 获取佣金列表
  242. * @param array $where
  243. * @param int $limit
  244. * @return array
  245. */
  246. public function getCommissionList(array $where, int $limit = 0)
  247. {
  248. $where_data = [];
  249. $where_data['time'] = $where['time'];
  250. if (isset($where['nickname']) && $where['nickname']) {
  251. $where_data[] = ['u.account|u.nickname|u.uid|u.phone', 'LIKE', "%$where[nickname]%"];
  252. }
  253. if (isset($where['price_max']) && isset($where['price_min'])) {
  254. if ($where['price_max'] != '' && $where['price_min'] != '') {
  255. $where_data[] = ['u.brokerage_price', 'between', [$where['price_min'], $where['price_max']]];
  256. } elseif ($where['price_min'] != '' && $where['price_max'] == '') {
  257. $where_data[] = ['u.brokerage_price', '>=', $where['price_min']];
  258. } elseif ($where['price_min'] == '' && $where['price_max'] != '') {
  259. $where_data[] = ['u.brokerage_price', '<=', $where['price_max']];
  260. }
  261. }
  262. $order_string = '';
  263. $order_arr = ['asc', 'desc'];
  264. if (isset($where['sum_number']) && in_array($where['sum_number'], $order_arr)) {
  265. $order_string .= ',income ' . $where['sum_number'];
  266. }
  267. if (isset($where['brokerage_price']) && in_array($where['brokerage_price'], $order_arr)) {
  268. $order_string .= ',u.brokerage_price ' . $where['brokerage_price'];
  269. }
  270. if ($order_string) {
  271. $order_string = trim($order_string, ',');
  272. }
  273. /** @var UserUserBrokerageServices $userUserBrokerage */
  274. $userUserBrokerage = app()->make(UserUserBrokerageServices::class);
  275. [$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);
  276. $uids = array_unique(array_column($list, 'uid'));
  277. /** @var UserExtractServices $userExtract */
  278. $userExtract = app()->make(UserExtractServices::class);
  279. $extractSumList = $userExtract->getUsersSumList($uids);
  280. foreach ($list as &$item) {
  281. // $item['sum_number'] = $item['income'] > $item['pay'] ? bcsub($item['income'], $item['pay'], 2) : 0;
  282. $item['nickname'] = $item['nickname'] . "|" . ($item['phone'] ? $item['phone'] . "|" : '') . $item['uid'];
  283. $item['extract_price'] = $extractSumList[$item['uid']] ?? 0;
  284. $item['sum_number'] = bcadd((string)$item['extract_price'], (string)$item['brokerage_price'], 2);
  285. $item['time'] = $item['time'] ? date('Y-m-d H:i:s', $item['time']) : '';
  286. }
  287. return compact('count', 'list');
  288. }
  289. /**
  290. * 用户佣金详情
  291. * @param int $uid
  292. * @return array
  293. */
  294. public function user_info(int $uid)
  295. {
  296. /** @var UserServices $user */
  297. $user = app()->make(UserServices::class);
  298. $user_info = $user->getUserWithTrashedInfo($uid, 'nickname,spread_uid,now_money,brokerage_price,add_time');
  299. if (!$user_info) {
  300. throw new ValidateException('您查看的用户信息不存在!');
  301. }
  302. $user_info = $user_info->toArray();
  303. $income = $this->getUserBillBrokerageSum($uid);
  304. $expend = $this->getUserBillBrokerageSum($uid, ['refund']);
  305. $number = (float)bcsub((string)$income, (string)$expend, 2);
  306. $user_info['number'] = max($number, 0);
  307. $user_info['add_time'] = date('Y-m-d H:i:s', $user_info['add_time']);
  308. $user_info['spread_name'] = $user_info['spread_uid'] ? $user->getUserInfo((int)$user_info['spread_uid'], 'nickname', true)['nickname'] ?? '' : '';
  309. return compact('user_info');
  310. }
  311. /**
  312. * 退佣金
  313. * @param $order
  314. * @return bool
  315. * @throws \think\db\exception\DataNotFoundException
  316. * @throws \think\db\exception\DbException
  317. * @throws \think\db\exception\ModelNotFoundException
  318. */
  319. public function orderRefundBrokerageBack($order)
  320. {
  321. $id = (int)$order['id'];
  322. $where = [
  323. 'uid' => [$order['spread_uid'], $order['spread_two_uid']],
  324. 'type' => ['self_brokerage', 'one_brokerage', 'two_brokerage'],
  325. 'link_id' => $id,
  326. 'pm' => 1
  327. ];
  328. $brokerageList = $this->dao->getUserBrokerageList($where);
  329. //子订单
  330. if (!$brokerageList && $order['pid']) {
  331. $where['link_id'] = $order['pid'];
  332. $p_brokerageList = $this->dao->getUserBrokerageList($where);
  333. //主订单已分佣 子订单按订单拆分后计算结果回退
  334. if ($p_brokerageList) {
  335. $brokerageList = [
  336. ['uid' => $order['spread_uid'], 'number' => $order['one_brokerage']],
  337. ['uid' => $order['spread_two_uid'], 'number' => $order['two_brokerage']],
  338. ];
  339. }
  340. }
  341. $res = true;
  342. if ($brokerageList) {
  343. /** @var UserServices $userServices */
  344. $userServices = app()->make(UserServices::class);
  345. $brokerages = $userServices->getColumn([['uid', 'in', array_column($brokerageList, 'uid')]], 'brokerage_price', 'uid');
  346. $brokerageData = [];
  347. foreach ($brokerageList as $item) {
  348. if (!$item['uid'] || $item['uid'] <= 0) continue;
  349. $usermoney = $brokerages[$item['uid']] ?? 0;
  350. if ($item['number'] > $usermoney) {
  351. $item['number'] = $usermoney;
  352. }
  353. if ($item['number'] <= 0) continue;
  354. $res = $res && $userServices->bcDec($item['uid'], 'brokerage_price', (string)$item['number'], 'uid');
  355. $brokerageData[] = [
  356. 'title' => '退款退佣金',
  357. 'uid' => $item['uid'],
  358. 'pm' => 0,
  359. 'add_time' => time(),
  360. 'type' => 'refund',
  361. 'number' => $item['number'],
  362. 'link_id' => $id,
  363. 'balance' => bcsub((string)$usermoney, (string)$item['number'], 2),
  364. 'mark' => '订单退款扣除佣金' . floatval($item['number']) . '元'
  365. ];
  366. }
  367. if ($brokerageData) {
  368. $res = $res && $this->dao->saveAll($brokerageData);
  369. }
  370. //修改佣金冻结时间
  371. $this->dao->update($where, ['frozen_time' => 0]);
  372. }
  373. return $res;
  374. }
  375. /**
  376. * 佣金排行
  377. * @param string $time
  378. * @return array
  379. * @throws \think\db\exception\DataNotFoundException
  380. * @throws \think\db\exception\DbException
  381. * @throws \think\db\exception\ModelNotFoundException
  382. */
  383. public function brokerageRankList(string $time = 'week')
  384. {
  385. $where = [];
  386. if ($time) {
  387. $where['time'] = $time;
  388. }
  389. [$page, $limit] = $this->getPageValue();
  390. $list = $this->dao->brokerageRankList($where, $page, $limit);
  391. foreach ($list as $key => &$item) {
  392. if (!isset($item['user']) || !$item['user'] || $item['brokerage_price'] <= 0) {
  393. unset($list[$key]);
  394. continue;
  395. }
  396. $item['nickname'] = $item['user']['nickname'] ?? '';
  397. $item['avatar'] = $item['user']['avatar'] ?? '';
  398. if ($item['brokerage_price'] == '0.00' || $item['brokerage_price'] == 0 || !$item['brokerage_price']) {
  399. unset($list[$key]);
  400. }
  401. unset($item['user']);
  402. }
  403. return array_merge($list);
  404. }
  405. /**
  406. * 获取用户排名
  407. * @param int $uid
  408. * @param string $time
  409. */
  410. public function getUserBrokerageRank(int $uid, string $time = 'week')
  411. {
  412. $where = [];
  413. if ($time) {
  414. $where['time'] = $time;
  415. }
  416. $list = $this->dao->brokerageRankList($where);
  417. foreach ($list as $key => &$item) {
  418. if (!isset($item['user']) || !$item['user'] || $item['brokerage_price'] <= 0) {
  419. unset($list[$key]);
  420. }
  421. }
  422. $position_tmp_one = array_column($list, 'uid');
  423. $position_tmp_two = array_column($list, 'brokerage_price', 'uid');
  424. if (!in_array($uid, $position_tmp_one)) {
  425. $position = 0;
  426. } else {
  427. if ($position_tmp_two[$uid] == 0.00) {
  428. $position = 0;
  429. } else {
  430. $position = array_search($uid, $position_tmp_one) + 1;
  431. }
  432. }
  433. return $position;
  434. }
  435. /**
  436. * 推广数据 昨天的佣金 累计提现金额 当前佣金
  437. * @param int $uid
  438. * @return mixed
  439. */
  440. public function commission(int $uid)
  441. {
  442. /** @var UserServices $userServices */
  443. $userServices = app()->make(UserServices::class);
  444. if (!$userServices->userExist($uid)) {
  445. throw new ValidateException('数据不存在');
  446. }
  447. /** @var UserExtractServices $userExtract */
  448. $userExtract = app()->make(UserExtractServices::class);
  449. $data = [];
  450. $data['uid'] = $uid;
  451. $data['pm'] = 1;
  452. $data['commissionSum'] = $this->getUsersBokerageSum($data);
  453. $data['pm'] = 0;
  454. $data['commissionRefund'] = $this->getUsersBokerageSum($data);
  455. $data['commissionCount'] = $data['commissionSum'] > $data['commissionRefund'] ? bcsub((string)$data['commissionSum'], (string)$data['commissionRefund'], 2) : 0.00;
  456. $data['lastDayCount'] = $this->getUsersBokerageSum($data, 'yesterday');//昨天的佣金
  457. $data['extractCount'] = $userExtract->getUserExtract($uid);//累计提现金额
  458. return $data;
  459. }
  460. /**
  461. * 前端佣金排行页面数据
  462. * @param int $uid
  463. * @param $type
  464. * @return array
  465. * @throws \think\db\exception\DataNotFoundException
  466. * @throws \think\db\exception\DbException
  467. * @throws \think\db\exception\ModelNotFoundException
  468. */
  469. public function brokerage_rank(int $uid, $type)
  470. {
  471. /** @var UserServices $userService */
  472. $userService = app()->make(UserServices::class);
  473. if (!$userService->userExist($uid)) {
  474. throw new ValidateException('数据不存在');
  475. }
  476. return [
  477. 'rank' => $this->brokerageRankList($type),
  478. 'position' => $this->getUserBrokerageRank($uid, $type)
  479. ];
  480. }
  481. /**
  482. * 推广 佣金/提现 总和
  483. * @param int $uid
  484. * @param $type 3 佣金 4 提现
  485. * @return mixed
  486. */
  487. public function spread_count(int $uid, $type)
  488. {
  489. /** @var UserServices $userService */
  490. $userService = app()->make(UserServices::class);
  491. if (!$userService->userExist($uid)) {
  492. throw new ValidateException('数据不存在');
  493. }
  494. $count = 0;
  495. if ($type == 3) {
  496. $where = [
  497. 'uid' => $uid,
  498. 'status' => 1,
  499. 'pm' => 1
  500. ];
  501. $count1 = $this->dao->getBrokerageSumColumn($where);
  502. $where['pm'] = 0;
  503. $count2 = $this->dao->getBrokerageSumColumn($where);
  504. $count = $count1 - $count2;
  505. } else if ($type == 4) {
  506. /** @var UserExtractServices $userExtract */
  507. $userExtract = app()->make(UserExtractServices::class);
  508. $count = $userExtract->getUserExtract($uid);//累计提现
  509. }
  510. return $count ? $count : 0;
  511. }
  512. /**
  513. * 推广订单
  514. * @param Request $request
  515. * @return mixed
  516. */
  517. public function spread_order(int $uid, array $data)
  518. {
  519. /** @var UserServices $userService */
  520. $userService = app()->make(UserServices::class);
  521. if (!$userService->userExist($uid)) {
  522. throw new ValidateException('数据不存在');
  523. }
  524. $result = ['list' => [], 'time' => [], 'count' => 0];
  525. /** @var StoreOrderServices $storeOrderServices */
  526. $storeOrderServices = app()->make(StoreOrderServices::class);
  527. [$page, $limit] = $this->getPageValue();
  528. $time_data = [];
  529. $where = ['type' => 0, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0, 'spread_or_uid' => $uid];
  530. if ($data['start'] || $data['stop']) {
  531. $where['time'] = [$data['start'], $data['stop']];
  532. }
  533. $where['real_name'] = $data['keyword'];
  534. $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) {
  535. $query->where('uid', $uid);
  536. }]);
  537. $result['count'] = $storeOrderServices->count($where);
  538. if ($list) {
  539. /** @var StoreOrderCartInfoServices $cartInfoServices */
  540. $cartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  541. $uids = array_unique(array_column($list, 'uid'));
  542. $userInfos = $userService->getColumn([['uid', 'in', $uids]], 'uid,avatar,nickname', 'uid');
  543. foreach ($list as &$item) {
  544. $item['store_name'] = $cartInfoServices->getCarIdByProductTitle((int)$item['id']);
  545. $item['avatar'] = $userInfos[$item['uid']]['avatar'] ?? '';
  546. $item['nickname'] = $userInfos[$item['uid']]['nickname'] ?? '';
  547. $item['number'] = $item['spread_uid'] == $uid ? $item['one_brokerage'] : $item['two_brokerage'];
  548. $item['time_key'] = $item['add_time'] ? date('Y-m', $item['add_time']) : '';
  549. $item['type'] = in_array($item['status'], [2, 3]) ? 'brokerage' : 'number';
  550. $brokerage = $item['brokerage'];
  551. $time = $brokerage['add_time'] ?? $item['add_time'];
  552. $item['time'] = $time ? date('Y-m-d H:i', $time) : '';
  553. $item['is_frozen'] = $brokerage && $brokerage['frozen_time'] > time() ? 1 : 0;
  554. unset($item['brokerage']);
  555. }
  556. $times = array_unique(array_column($list, 'time_key'));
  557. $time_data = [];
  558. $i = 0;
  559. foreach ($times as $time) {
  560. $time_data[$i]['time'] = $time;
  561. $time_data[$i]['count'] = $storeOrderServices->getMonthCount($where, $time);
  562. $time_data[$i]['sumPrice'] = $storeOrderServices->getMonthMoneyCount($where, $time, 'pay_price');
  563. $i++;
  564. }
  565. }
  566. $result['list'] = $list;
  567. $result['time'] = $time_data;
  568. $priceWhere = ['pid' => 0, 'type' => 0, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0];
  569. if ($data['start'] || $data['stop']) {
  570. $priceWhere['time'] = [$data['start'], $data['stop']];
  571. }
  572. //条件获取一级佣金总和
  573. $sum_price_one = $storeOrderServices->sum($priceWhere + ['spread_uid' => $uid], 'one_brokerage', true);
  574. $sum_price_two = $storeOrderServices->sum($priceWhere + ['spread_two_uid' => $uid], 'two_brokerage', true);
  575. $result['sum_brokerage'] = bcadd((string)$sum_price_one, (string)$sum_price_two, 2);
  576. return $result;
  577. }
  578. /**
  579. * 用户佣金记录v2
  580. * @param int $uid
  581. * @return array
  582. */
  583. public function userBrokerageList(int $uid, $data = [])
  584. {
  585. $where = [];
  586. $where['uid'] = $uid;
  587. if ($data['start'] || $data['stop']) {
  588. $where['time'] = [$data['start'], $data['stop']];
  589. }
  590. $where['like'] = $data['keyword'];
  591. [$page, $limit] = $this->getPageValue();
  592. $list = $this->dao->getList($where, '*', $page, $limit);
  593. $times = [];
  594. if ($list) {
  595. foreach ($list as &$item) {
  596. $item['time_key'] = $item['add_time'] ? date('Y-m', (int)$item['add_time']) : '';
  597. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i', (int)$item['add_time']) : '';
  598. }
  599. $times = array_merge(array_unique(array_column($list, 'time_key')));
  600. }
  601. $income = $this->dao->sum($where + ['pm' => 1, 'not_type' => ['extract_fail']], 'number', true);
  602. $expend = $this->dao->sum($where + ['pm' => 0], 'number', true);
  603. return ['list' => $list, 'time' => $times, 'income' => $income, 'expend' => $expend];
  604. }
  605. /**
  606. * 用户提现记录v2
  607. * @param int $uid
  608. * @param array $data
  609. * @return array
  610. */
  611. public function userExtractList(int $uid, array $data)
  612. {
  613. $where = [];
  614. $where['uid'] = $uid;
  615. $where['type'] = ['extract', 'extract_money', 'extract_fail'];
  616. if ((isset($data['start']) && $data['start']) || (isset($data['stop']) && $data['stop'])) {
  617. $where['time'] = [$data['start'], $data['stop']];
  618. }
  619. [$page, $limit] = $this->getPageValue();
  620. $list = $this->dao->getList($where, '*', $page, $limit);
  621. $times = [];
  622. if ($list) {
  623. foreach ($list as &$item) {
  624. $item['time_key'] = $item['add_time'] ? date('Y-m', (int)$item['add_time']) : '';
  625. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i', (int)$item['add_time']) : '';
  626. }
  627. $times = array_merge(array_unique(array_column($list, 'time_key')));
  628. }
  629. return ['list' => $list, 'time' => $times];
  630. }
  631. }