UserBrokerageServices.php 24 KB

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