UserBrokerageServices.php 29 KB

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