UserBrokerageServices.php 25 KB

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