UserBillServices.php 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\services\BaseServices;
  14. use app\dao\user\UserBillDao;
  15. use app\services\order\StoreOrderServices;
  16. use crmeb\exceptions\AdminException;
  17. use crmeb\exceptions\ApiException;
  18. use think\Exception;
  19. use think\facade\Cache;
  20. use crmeb\services\CacheService;
  21. use think\facade\Log;
  22. /**
  23. *
  24. * Class UserBillServices
  25. * @package app\services\user
  26. * @method takeUpdate(int $uid, int $id) 修改收货状态
  27. * @method sum(array $where, string $field) 求和
  28. * @method count(array $where) 求条数
  29. * @method getTotalSum(array $where) 计算某个条件下订单内商品总数
  30. * @method getBillSum(array $where) 获取某个条件总数
  31. * @method getList(array $where, string $field, int $page, int $limit, $typeWhere = [], $order = 'id desc') 获取某个条件总数
  32. * @method getUserRefundPriceList(array $time, string $timeType, string $str, string $field = 'add_time', array $with = []) 获取退款金额按照时间分组
  33. */
  34. class UserBillServices extends BaseServices
  35. {
  36. /**
  37. * 用户记录模板
  38. * @var array[]
  39. */
  40. protected $incomeData = [
  41. 'pay_give_integral' => [
  42. 'title' => '购买商品赠送积分',
  43. 'category' => 'integral',
  44. 'type' => 'product_gain',
  45. 'mark' => '购买商品赠送{%num%}积分',
  46. 'status' => 1,
  47. 'pm' => 1
  48. ],
  49. 'order_give_integral' => [
  50. 'title' => '下单赠送积分',
  51. 'category' => 'integral',
  52. 'type' => 'gain',
  53. 'mark' => '下单赠送{%num%}积分',
  54. 'status' => 1,
  55. 'pm' => 1
  56. ],
  57. 'order_give_exp' => [
  58. 'title' => '下单赠送经验',
  59. 'category' => 'exp',
  60. 'type' => 'gain',
  61. 'mark' => '下单赠送{%num%}经验',
  62. 'status' => 1,
  63. 'pm' => 1
  64. ],
  65. 'get_brokerage' => [
  66. 'title' => '获得推广佣金',
  67. 'category' => 'now_money',
  68. 'type' => 'brokerage',
  69. 'mark' => '{%nickname%}成功消费{%pay_price%}元,奖励推广佣金{%number%}',
  70. 'status' => 1,
  71. 'pm' => 1
  72. ],
  73. 'get_two_brokerage' => [
  74. 'title' => '获得推广佣金',
  75. 'category' => 'now_money',
  76. 'type' => 'brokerage',
  77. 'mark' => '二级推广人{%nickname%}成功消费{%pay_price%}元,奖励推广佣金{%number%}',
  78. 'status' => 1,
  79. 'pm' => 1
  80. ],
  81. 'get_user_brokerage' => [
  82. 'title' => '获得推广用户佣金',
  83. 'category' => 'now_money',
  84. 'type' => 'brokerage_user',
  85. 'mark' => '成功推广用户:{%nickname%},奖励推广佣金{%number%}',
  86. 'status' => 1,
  87. 'pm' => 1
  88. ],
  89. 'pay_product_refund' => [
  90. 'title' => '商品退款',
  91. 'category' => 'now_money',
  92. 'type' => 'pay_product_refund',
  93. 'mark' => '订单退款{%payType%}{%number%}元',
  94. 'status' => 1,
  95. 'pm' => 1
  96. ],
  97. 'integral_refund' => [
  98. 'title' => '扣除订单下单赠送积分',
  99. 'category' => 'integral',
  100. 'type' => 'order_deduction',
  101. 'mark' => '购买商品失败,回退赠送积分{%num%}',
  102. 'status' => 1,
  103. 'pm' => 0
  104. ],
  105. 'order_integral_refund' => [
  106. 'title' => '返还下单使用积分',
  107. 'category' => 'integral',
  108. 'type' => 'integral_refund',
  109. 'mark' => '购买商品失败,回退积分{%num%}',
  110. 'status' => 1,
  111. 'pm' => 1
  112. ],
  113. 'pay_product_integral_back' => [
  114. 'title' => '商品退积分',
  115. 'category' => 'integral',
  116. 'type' => 'pay_product_integral_back',
  117. 'mark' => '订单退积分{%num%}积分到用户积分',
  118. 'status' => 1,
  119. 'pm' => 1
  120. ],
  121. 'deduction' => [
  122. 'title' => '积分抵扣',
  123. 'category' => 'integral',
  124. 'type' => 'deduction',
  125. 'mark' => '购买商品使用{%number%}积分抵扣{%deductionPrice%}元',
  126. 'status' => 1,
  127. 'pm' => 0
  128. ],
  129. 'pay_product' => [
  130. 'title' => '余额支付购买商品',
  131. 'category' => 'now_money',
  132. 'type' => 'pay_product',
  133. 'mark' => '余额支付{%num%}元购买商品',
  134. 'status' => 1,
  135. 'pm' => 0
  136. ],
  137. 'pay_money' => [
  138. 'title' => '购买商品',
  139. 'category' => 'now_money',
  140. 'type' => 'pay_money',
  141. 'mark' => '支付{%num%}元购买商品',
  142. 'status' => 1,
  143. 'pm' => 0
  144. ],
  145. 'system_add' => [
  146. 'title' => '系统增加余额',
  147. 'category' => 'now_money',
  148. 'type' => 'system_add',
  149. 'mark' => '系统增加{%num%}元',
  150. 'status' => 1,
  151. 'pm' => 1
  152. ],
  153. 'brokerage_to_nowMoney' => [
  154. 'title' => '佣金提现到余额',
  155. 'category' => 'now_money',
  156. 'type' => 'extract',
  157. 'mark' => '佣金提现到余额{%num%}元',
  158. 'status' => 1,
  159. 'pm' => 0
  160. ],
  161. 'pay_member' => [
  162. 'title' => '购买会员',
  163. 'category' => 'now_money',
  164. 'type' => 'pay_member',
  165. 'mark' => '支付{%num%}元购买会员',
  166. 'status' => 1,
  167. 'pm' => 0
  168. ],
  169. 'offline_scan' => [
  170. 'title' => '线下收银',
  171. 'category' => 'now_money',
  172. 'type' => 'offline_scan',
  173. 'mark' => '线下收银支付{%num%}元',
  174. 'status' => 1,
  175. 'pm' => 0
  176. ],
  177. 'lottery_use_integral' => [
  178. 'title' => '参与抽奖使用积分',
  179. 'category' => 'integral',
  180. 'type' => 'lottery_use',
  181. 'mark' => '参与抽奖使用{%num%}积分',
  182. 'status' => 1,
  183. 'pm' => 0
  184. ],
  185. 'lottery_give_integral' => [
  186. 'title' => '抽奖中奖赠送积分',
  187. 'category' => 'integral',
  188. 'type' => 'lottery_add',
  189. 'mark' => '抽奖中奖赠送{%num%}积分',
  190. 'status' => 1,
  191. 'pm' => 1
  192. ],
  193. 'lottery_use_money' => [
  194. 'title' => '参与抽奖使用余额',
  195. 'category' => 'now_money',
  196. 'type' => 'lottery_use',
  197. 'mark' => '参与抽奖使用{%num%}余额',
  198. 'status' => 1,
  199. 'pm' => 0
  200. ],
  201. 'lottery_give_money' => [
  202. 'title' => '抽奖中奖赠送余额',
  203. 'category' => 'now_money',
  204. 'type' => 'lottery_add',
  205. 'mark' => '抽奖中奖赠送{%num%}余额',
  206. 'status' => 1,
  207. 'pm' => 1
  208. ],
  209. 'storeIntegral_use_integral' => [
  210. 'title' => '积分兑换商品',
  211. 'category' => 'integral',
  212. 'type' => 'storeIntegral_use',
  213. 'mark' => '积分商城兑换商品使用{%num%}积分',
  214. 'status' => 1,
  215. 'pm' => 0
  216. ],
  217. ];
  218. /**
  219. * UserBillServices constructor.
  220. * @param UserBillDao $dao
  221. */
  222. public function __construct(UserBillDao $dao)
  223. {
  224. $this->dao = $dao;
  225. }
  226. /**
  227. * TODO 获取用户记录总和
  228. * @param $uid
  229. * @param string $category
  230. * @param array $type
  231. * @return mixed
  232. */
  233. public function getRecordCount(int $uid, $category = 'now_money', $type = [], $time = '', $pm = false)
  234. {
  235. $where = [];
  236. $where['uid'] = $uid;
  237. $where['category'] = $category;
  238. $where['status'] = 1;
  239. if (is_string($type) && strlen(trim($type))) {
  240. $where['type'] = explode(',', $type);
  241. }
  242. if ($time) {
  243. $where['time'] = $time;
  244. }
  245. if ($pm) {
  246. $where['pm'] = 0;
  247. }
  248. return $this->dao->getBillSumColumn($where);
  249. }
  250. public function getUsersBokerageSum(array $where, $time = 0)
  251. {
  252. $where_data = [
  253. 'type' => ['brokerage', 'brokerage_user'],
  254. 'category' => 'now_money',
  255. 'status' => 1,
  256. 'pm' => $where['pm'] ?? '',
  257. 'uid' => $where['uid'] ?? '',
  258. 'time' => $where['time'] ?? 0
  259. ];
  260. if ($time) $where_data['time'] = $time;
  261. return $this->dao->getBillSumColumn($where_data);
  262. }
  263. /**
  264. * 某个用户佣金总和
  265. * @param int $uid
  266. * @return float
  267. */
  268. public function getUserBillBrokerageSum(int $uid, array $type = ['brokerage', 'brokerage_user'], $time = '')
  269. {
  270. $where = ['uid' => $uid, 'category' => 'now_money'];
  271. if ($type) $where['type'] = $type;
  272. if ($time) $where['time'] = $time;
  273. return $this->dao->getBillSum($where);
  274. }
  275. /**
  276. * 获取用户|所有佣金总数
  277. * @param int $uid
  278. * @param array $where_time
  279. * @return float
  280. */
  281. public function getBrokerageSum(int $uid = 0, $where_time = [])
  282. {
  283. $where = ['category' => 'now_money', 'type' => ['system_add', 'pay_product', 'extract', 'pay_product_refund', 'system_sub'], 'pm' => 1, 'status' => 1];
  284. if ($uid) $where['uid'] = $uid;
  285. if ($where_time) $where['add_time'] = $where_time;
  286. return $this->dao->getBillSum($where);
  287. }
  288. public function getBrokerageNumSum($link_ids = [])
  289. {
  290. $where = ['category' => 'now_money', 'type' => ['brokerage', 'brokerage_user']];
  291. if ($link_ids) $where['link_id'] = $link_ids;
  292. return $this->dao->getBillSum($where);
  293. }
  294. /**
  295. * 获取用户|所有佣金总数
  296. * @param int $uid
  297. * @param array $where_time
  298. * @return float
  299. */
  300. public function getBrokerageCount(int $uid = 0, $where_time = [])
  301. {
  302. $where = ['category' => 'now_money', 'type' => ['system_add', 'pay_product', 'extract', 'pay_product_refund', 'system_sub'], 'pm' => 1, 'status' => 1];
  303. if ($uid) $where['uid'] = $uid;
  304. if ($where_time) $where['add_time'] = $where_time;
  305. return $this->dao->getBillCount($where);
  306. }
  307. /**
  308. * 用户|所有资金变动列表
  309. * @param int $uid
  310. * @param string $field
  311. * @return array
  312. */
  313. public function getBrokerageList(int $uid = 0, $where_time = [], string $field = '*')
  314. {
  315. [$page, $limit] = $this->getPageValue();
  316. $where = ['category' => 'now_money', 'type' => ['pay_money', 'system_add', 'pay_product_refund', 'pay_member', 'offline_scan', 'lottery_add', 'system_sub']];
  317. if ($uid) $where['uid'] = $uid;
  318. if ($where_time) $where['add_time'] = $where_time;
  319. $list = $this->dao->getList($where, $field, $page, $limit);
  320. $count = $this->dao->count($where);
  321. foreach ($list as &$item) {
  322. $value = array_filter($this->incomeData, function ($value) use ($item) {
  323. if ($item['type'] == $value['type']) {
  324. return $item['title'];
  325. }
  326. });
  327. $item['type_title'] = $value[$item['type']]['title'] ?? '未知类型';
  328. }
  329. return compact('list', 'count');
  330. }
  331. /**
  332. * 获取用户的充值总数
  333. * @param int $uid
  334. * @return float
  335. */
  336. public function getRechargeSum(int $uid = 0, $where_time = [])
  337. {
  338. $where = ['category' => 'now_money', 'type' => 'recharge', 'pm' => 1, 'status' => 1];//用户充值余额
  339. $where_system = ['category' => 'now_money', 'type' => 'system_add', 'pm' => 1, 'status' => 1];//系统赠送余额
  340. if ($uid) $where['uid'] = $where_system['uid'] = $uid;
  341. if ($where_time) $where['add_time'] = $where_system['add_time'] = $where_time;
  342. $sum1 = $this->dao->getBillSum($where);
  343. $sum2 = $this->dao->getBillSum($where_system);
  344. return bcadd((string)$sum1, (string)$sum2, 2);
  345. }
  346. /**
  347. * 用户|所有充值列表
  348. * @param int $uid
  349. * @param string $field
  350. * @return array
  351. */
  352. public function getRechargeList(int $uid = 0, $where_time = [], string $field = '*')
  353. {
  354. [$page, $limit] = $this->getPageValue();
  355. $where = ['category' => 'now_money', 'type' => 'recharge'];
  356. if ($uid) $where['uid'] = $uid;
  357. if ($where_time) $where['add_time'] = $where_time;
  358. $list = $this->dao->getList($where, $field, $page, $limit);
  359. $count = $this->dao->count($where);
  360. return compact('list', 'count');
  361. }
  362. /**
  363. * 获取用户的积分总数
  364. * @param int $uid
  365. * @return float
  366. */
  367. public function getIntegralSum(int $uid = 0, $where_time = [])
  368. {
  369. $where = ['category' => 'integral', 'type' => ['sign', 'system_add'], 'pm' => 1, 'status' => 1];
  370. if ($uid) $where['uid'] = $uid;
  371. if ($where_time) $where['add_time'] = $where_time;
  372. return $this->dao->getBillSum($where);
  373. }
  374. /**
  375. * 获取用户的获取积分总次数
  376. * @param int $uid
  377. * @return float
  378. */
  379. public function getIntegralCount(int $uid = 0, $where_time = [])
  380. {
  381. $where = ['category' => 'integral', 'type' => ['sign', 'system_add'], 'pm' => 1, 'status' => 1];
  382. if ($uid) $where['uid'] = $uid;
  383. if ($where_time) $where['add_time'] = $where_time;
  384. return $this->dao->getBillCount($where);
  385. }
  386. /**
  387. * 获取积分列表
  388. * @param int $uid
  389. * @param array $where_time
  390. * @param string $field
  391. * @return array
  392. * @throws \think\db\exception\DataNotFoundException
  393. * @throws \think\db\exception\DbException
  394. * @throws \think\db\exception\ModelNotFoundException
  395. */
  396. public function getIntegralList(int $uid = 0, array $where_time = [], string $field = '*')
  397. {
  398. [$page, $limit] = $this->getPageValue();
  399. $where = ['category' => 'integral'];
  400. if ($uid) $where['uid'] = $uid;
  401. if ($where_time) $where['add_time'] = $where_time;
  402. $list = $this->dao->getList($where, $field, $page, $limit);
  403. foreach ($list as &$item) {
  404. $item['number'] = intval($item['number']);
  405. $item['is_frozen'] = $item['frozen_time'] > time() ? 1 : 0;
  406. }
  407. $count = $this->dao->count($where);
  408. return compact('list', 'count');
  409. }
  410. /**
  411. * 获取用户签到总数
  412. * @param int $uid
  413. * @return float
  414. */
  415. public function getSignlSum(int $uid = 0, $where_time = [])
  416. {
  417. $where = ['category' => 'integral', 'type' => 'sign', 'pm' => 1, 'status' => 1];
  418. if ($uid) $where['uid'] = $uid;
  419. if ($where_time) $where['add_time'] = $where_time;
  420. return $this->dao->getBillSum($where);
  421. }
  422. /**
  423. * 获取用户的签到总次数
  424. * @param int $uid
  425. * @return float
  426. */
  427. public function getSignCount(int $uid = 0, $where_time = [])
  428. {
  429. $where = ['category' => 'integral', 'type' => 'sign', 'pm' => 1, 'status' => 1];
  430. if ($uid) $where['uid'] = $uid;
  431. if ($where_time) $where['add_time'] = $where_time;
  432. return $this->dao->getBillCount($where);
  433. }
  434. /**
  435. * 获取签到列表
  436. * @param int $uid
  437. * @param array $where_time
  438. * @param string $field
  439. * @return array
  440. */
  441. public function getSignList(int $uid = 0, $where_time = [], string $field = '*')
  442. {
  443. [$page, $limit] = $this->getPageValue();
  444. $where = ['category' => 'integral', 'type' => 'sign'];
  445. if ($uid) $where['uid'] = $uid;
  446. if ($where_time) $where['add_time'] = $where_time;
  447. $list = $this->dao->getList($where, $field, $page, $limit);
  448. $count = $this->dao->count($where);
  449. return compact('list', 'count');
  450. }
  451. /**
  452. * 经验总数
  453. * @param int $uid
  454. * @param array $where_time
  455. * @return float
  456. */
  457. public function getExpSum(int $uid = 0, $where_time = [])
  458. {
  459. $where = ['category' => ['exp'], 'pm' => 1, 'status' => 1];
  460. if ($uid) $where['uid'] = $uid;
  461. if ($where_time) $where['time'] = $where_time;
  462. return $this->dao->getBillSum($where);
  463. }
  464. /**
  465. * 获取所有经验列表
  466. * @param int $uid
  467. * @param array $where_time
  468. * @param string $field
  469. * @return array
  470. */
  471. public function getExpList(int $uid = 0, $where_time = [], string $field = '*')
  472. {
  473. [$page, $limit] = $this->getPageValue();
  474. $where = ['category' => ['exp']];
  475. $where['status'] = 1;
  476. if ($uid) $where['uid'] = $uid;
  477. if ($where_time) $where['time'] = $where_time;
  478. $list = $this->dao->getList($where, $field, $page, $limit);
  479. $count = $this->dao->count($where);
  480. return compact('list', 'count');
  481. }
  482. /**
  483. * 增加佣金
  484. * @param int $uid
  485. * @param string $type
  486. * @param array $data
  487. * @return bool
  488. * @throws Exception
  489. */
  490. public function incomeNowMoney(int $uid, string $type, array $data)
  491. {
  492. $data['uid'] = $uid;
  493. $data['category'] = 'now_money';
  494. $data['type'] = $type;
  495. $data['pm'] = 1;
  496. $data['status'] = 1;
  497. $data['add_time'] = time();
  498. if (!$this->dao->save($data))
  499. throw new AdminException(400655);
  500. return true;
  501. }
  502. /**
  503. * 扣除佣金
  504. * @param int $uid
  505. * @param string $type
  506. * @param array $data
  507. * @return bool
  508. * @throws Exception
  509. */
  510. public function expendNowMoney(int $uid, string $type, array $data)
  511. {
  512. $data['uid'] = $uid;
  513. $data['category'] = 'now_money';
  514. $data['type'] = $type;
  515. $data['pm'] = 0;
  516. $data['status'] = 1;
  517. $data['add_time'] = time();
  518. if (!$this->dao->save($data))
  519. throw new AdminException(400655);
  520. return true;
  521. }
  522. /**
  523. * 增加积分
  524. * @param int $uid
  525. * @param string $type
  526. * @param array $data
  527. * @return bool
  528. * @throws Exception
  529. */
  530. public function incomeIntegral(int $uid, string $type, array $data)
  531. {
  532. $data['uid'] = $uid;
  533. $data['category'] = 'integral';
  534. $data['type'] = $type;
  535. $data['pm'] = 1;
  536. $data['status'] = 1;
  537. $data['add_time'] = time();
  538. if (!$this->dao->save($data))
  539. throw new AdminException(400655);
  540. return true;
  541. }
  542. /**
  543. * 扣除积分
  544. * @param int $uid
  545. * @param string $type
  546. * @param array $data
  547. * @return bool
  548. * @throws Exception
  549. */
  550. public function expendIntegral(int $uid, string $type, array $data)
  551. {
  552. $data['uid'] = $uid;
  553. $data['category'] = 'integral';
  554. $data['type'] = $type;
  555. $data['pm'] = 0;
  556. $data['status'] = 1;
  557. $data['add_time'] = time();
  558. if (!$this->dao->save($data))
  559. throw new AdminException(400655);
  560. return true;
  561. }
  562. /**
  563. * 写入用户记录
  564. * @param string $type 写入类型
  565. * @param int $uid
  566. * @param int|string|array $number
  567. * @param int|string $balance
  568. * @param int $link_id
  569. * @return bool|mixed
  570. */
  571. public function income(string $type, int $uid, $number, $balance, $link_id)
  572. {
  573. $data = $this->incomeData[$type] ?? null;
  574. if (!$data) {
  575. return true;
  576. }
  577. $data['uid'] = $uid;
  578. $data['balance'] = $balance ?? 0;
  579. $data['link_id'] = $link_id;
  580. if (is_array($number)) {
  581. $key = array_keys($number);
  582. $key = array_map(function ($item) {
  583. return '{%' . $item . '%}';
  584. }, $key);
  585. $value = array_values($number);
  586. $data['number'] = $number['number'] ?? 0;
  587. $data['mark'] = str_replace($key, $value, $data['mark']);
  588. } else {
  589. $data['number'] = $number;
  590. $data['mark'] = str_replace(['{%num%}'], $number, $data['mark']);
  591. }
  592. $data['add_time'] = time();
  593. if ($type == 'pay_give_integral' || $type == 'order_give_integral') {
  594. $integral_frozen = sys_config('integral_frozen');
  595. if ($integral_frozen) {
  596. $data['frozen_time'] = $data['add_time'] + ($integral_frozen * 86400);
  597. }
  598. }
  599. return $this->dao->save($data);
  600. }
  601. /**
  602. * 邀请新用户增加经验
  603. * @param int $spreadUid
  604. */
  605. public function inviteUserIncExp(int $spreadUid)
  606. {
  607. if (!$spreadUid) {
  608. return false;
  609. }
  610. //用户等级是否开启
  611. if (!sys_config('member_func_status', 1)) {
  612. return false;
  613. }
  614. /** @var UserServices $userService */
  615. $userService = app()->make(UserServices::class);
  616. $spread_user = $userService->getUserInfo($spreadUid);
  617. if (!$spread_user) {
  618. return false;
  619. }
  620. $exp_num = sys_config('invite_user_exp', 0);
  621. if ($exp_num) {
  622. $userService->incField($spreadUid, 'exp', (int)$exp_num);
  623. $data = [];
  624. $data['uid'] = $spreadUid;
  625. $data['number'] = $exp_num;
  626. $data['category'] = 'exp';
  627. $data['type'] = 'invite_user';
  628. $data['title'] = $data['mark'] = '邀新奖励';
  629. $data['balance'] = (int)$spread_user['exp'] + (int)$exp_num;
  630. $data['pm'] = 1;
  631. $data['status'] = 1;
  632. $this->dao->save($data);
  633. }
  634. //检测会员等级
  635. try {
  636. //用户升级事件
  637. event('UserLevelListener', [$spreadUid]);
  638. } catch (\Throwable $e) {
  639. Log::error('会员等级升级失败,失败原因:' . $e->getMessage());
  640. }
  641. return true;
  642. }
  643. /**
  644. * 获取type
  645. * @param array $where
  646. * @param string $filed
  647. */
  648. public function getBillType(array $where)
  649. {
  650. return $this->dao->getType($where);
  651. }
  652. /**
  653. * 资金类型
  654. */
  655. public function bill_type()
  656. {
  657. $where = [];
  658. $where['not_type'] = ['gain', 'system_sub', 'deduction', 'sign'];
  659. $where['not_category'] = ['exp', 'integral'];
  660. return $this->cacheDriver()->remember('user_type_list', function () use ($where) {
  661. return ['list' => $this->getBillType($where)];
  662. }, 600);
  663. }
  664. /**
  665. * 获取资金列表
  666. * @param array $where
  667. * @param string $field
  668. * @return array
  669. */
  670. public function getBillList(array $where, string $field = '*', $is_page = true)
  671. {
  672. $where_data = [];
  673. if (isset($where['uid']) && $where['uid'] != '') {
  674. $where_data['uid'] = $where['uid'];
  675. }
  676. if ($where['start_time'] != '' && $where['end_time'] != '') {
  677. $where_data['time'] = str_replace('-', '/', $where['start_time']) . ' - ' . str_replace('-', '/', $where['end_time']);
  678. }
  679. if (isset($where['category']) && $where['category'] != '') {
  680. $where_data['category'] = $where['category'];
  681. }
  682. if (isset($where['type']) && $where['type'] != '') {
  683. $where_data['type'] = $where['type'];
  684. if ($where['type'] == 'brokerage') $where_data['pm'] = 1;
  685. }
  686. $where_data['not_category'] = ['integral', 'exp', 'share'];
  687. $where_data['not_type'] = $where['type'] == 'pay_product' ? ['gain', 'system_sub', 'deduction', 'sign'] : ['gain', 'system_sub', 'deduction', 'sign', 'pay_product'];
  688. if (isset($where['nickname']) && $where['nickname'] != '') {
  689. $where_data['like'] = $where['nickname'];
  690. }
  691. if (isset($where['excel']) && $where['excel'] != '') {
  692. $where_data['excel'] = $where['excel'];
  693. } else {
  694. $where_data['excel'] = 0;
  695. }
  696. [$page, $limit] = $this->getPageValue($is_page);
  697. $data = $this->dao->getBillList($where_data, $field, $page, $limit);
  698. foreach ($data as &$item) {
  699. $item['nickname'] = $item['user']['nickname'] ?? '';
  700. unset($item['user']);
  701. }
  702. $count = $this->dao->count($where_data);
  703. return compact('data', 'count');
  704. }
  705. /**
  706. * 获取佣金列表
  707. * @param array $where
  708. * @param int $limit
  709. * @return array
  710. */
  711. public function getCommissionList(array $where, int $limit = 0)
  712. {
  713. $where_data = [];
  714. $where_data['time'] = $where['time'];
  715. if (isset($where['nickname']) && $where['nickname']) {
  716. $where_data[] = ['u.nickname|u.uid', 'LIKE', "%$where[nickname]%"];
  717. }
  718. if (isset($where['price_max']) && isset($where['price_min'])) {
  719. if ($where['price_max'] != '' && $where['price_min'] != '') {
  720. $where_data[] = ['u.brokerage_price', 'between', [$where['price_min'], $where['price_max']]];
  721. } elseif ($where['price_min'] != '' && $where['price_max'] == '') {
  722. $where_data[] = ['u.brokerage_price', '>=', $where['price_min']];
  723. } elseif ($where['price_min'] == '' && $where['price_max'] != '') {
  724. $where_data[] = ['u.brokerage_price', '<=', $where['price_max']];
  725. }
  726. }
  727. $order_string = '';
  728. $order_arr = ['asc', 'desc'];
  729. if (isset($where['sum_number']) && in_array($where['sum_number'], $order_arr)) {
  730. $order_string .= ',income ' . $where['sum_number'];
  731. }
  732. if (isset($where['brokerage_price']) && in_array($where['brokerage_price'], $order_arr)) {
  733. $order_string .= ',u.brokerage_price ' . $where['brokerage_price'];
  734. }
  735. if ($order_string) {
  736. $order_string = trim($order_string, ',');
  737. }
  738. /** @var UserUserBrokerageServices $userUserBrokerage */
  739. $userUserBrokerage = app()->make(UserUserBrokerageServices::class);
  740. [$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,b.add_time as time', $order_string, $limit);
  741. $uids = array_unique(array_column($list, 'uid'));
  742. /** @var UserExtractServices $userExtract */
  743. $userExtract = app()->make(UserExtractServices::class);
  744. $extractSumList = $userExtract->getUsersSumList($uids);
  745. foreach ($list as &$item) {
  746. $item['sum_number'] = $item['income'];
  747. $item['nickname'] = $item['nickname'] . "|" . ($item['phone'] ? $item['phone'] . "|" : '') . $item['uid'];
  748. $item['extract_price'] = $extractSumList[$item['uid']] ?? 0;
  749. $item['time'] = $item['time'] ? date('Y-m-d H:i:s', $item['time']) : '';
  750. }
  751. return compact('count', 'list');
  752. }
  753. public function user_info(int $uid)
  754. {
  755. /** @var UserServices $user */
  756. $user = app()->make(UserServices::class);
  757. $user_info = $user->getUserInfo($uid, 'nickname,spread_uid,now_money,add_time,brokerage_price');
  758. if (!$user_info) {
  759. throw new AdminException(400119);
  760. }
  761. $user_info = $user_info->toArray();
  762. $user_info['number'] = $user_info['brokerage_price'];
  763. $user_info['add_time'] = date('Y-m-d H:i:s', $user_info['add_time']);
  764. $user_info['spread_name'] = $user_info['spread_uid'] ? $user->getUserInfo((int)$user_info['spread_uid'], 'nickname', true)['nickname'] ?? '' : '';
  765. return compact('user_info');
  766. }
  767. /**
  768. * 记录分享次数
  769. * @param int $uid 用户uid
  770. * @param int $cd 冷却时间
  771. * @return Boolean
  772. * */
  773. public function setUserShare(int $uid, $cd = 300)
  774. {
  775. /** @var UserServices $userServices */
  776. $userServices = app()->make(UserServices::class);
  777. $user = $userServices->getUserInfo($uid);
  778. if (!$user) {
  779. throw new AdminException(400119);
  780. }
  781. $cachename = 'Share_' . $uid;
  782. if (CacheService::get($cachename)) {
  783. return false;
  784. }
  785. $data = ['title' => '用户分享记录', 'uid' => $uid, 'category' => 'share', 'type' => 'share', 'number' => 0, 'link_id' => 0, 'balance' => 0, 'mark' => date('Y-m-d H:i:s', time()) . ':用户分享'];
  786. if (!$this->dao->save($data)) {
  787. throw new AdminException(400656);
  788. }
  789. CacheService::set($cachename, 1, $cd);
  790. return true;
  791. }
  792. /**
  793. * 获取佣金提现列表
  794. * @param int $uid
  795. * @param array $where
  796. * @return array
  797. * @throws \think\db\exception\DataNotFoundException
  798. * @throws \think\db\exception\DbException
  799. * @throws \think\db\exception\ModelNotFoundException
  800. */
  801. public function getBillOneList(int $uid, array $where)
  802. {
  803. $where['uid'] = $uid;
  804. $data = $this->getBillList($where);
  805. foreach ($data['data'] as &$item) {
  806. $item['_add_time'] = $item['add_time'] ?? '';
  807. }
  808. return $data;
  809. }
  810. /**
  811. * 获取积分列表
  812. * @param array $where
  813. * @param string $field
  814. * @return array
  815. */
  816. public function getPointList(array $where, string $field = '*', $is_page = true)
  817. {
  818. $where_data = [];
  819. $where_data['category'] = 'integral';
  820. if (isset($where['uid']) && $where['uid'] != '') {
  821. $where_data['uid'] = $where['uid'];
  822. }
  823. if ($where['start_time'] != '' && $where['end_time'] != '') {
  824. $where_data['time'] = $where['start_time'] . ' - ' . $where['end_time'];
  825. }
  826. if (isset($where['type']) && $where['type'] != '') {
  827. $where_data['type'] = $where['type'];
  828. }
  829. if (isset($where['nickname']) && $where['nickname'] != '') {
  830. $where_data['like'] = $where['nickname'];
  831. }
  832. if (isset($where['excel']) && $where['excel'] != '') {
  833. $where_data['excel'] = $where['excel'];
  834. } else {
  835. $where_data['excel'] = 0;
  836. }
  837. [$page, $limit] = $this->getPageValue($is_page);
  838. $list = $this->dao->getBillList($where_data, $field, $page, $limit);
  839. foreach ($list as &$item) {
  840. $item['nickname'] = $item['user']['nickname'] ?? '';
  841. $item['number'] = intval($item['number']);
  842. $item['balance'] = intval($item['balance']);
  843. unset($item['user']);
  844. }
  845. $count = $this->dao->count($where_data);
  846. return compact('list', 'count');
  847. }
  848. /**
  849. * 积分头部信息
  850. * @param array $where
  851. * @return array[]
  852. */
  853. public function getUserPointBadgelist(array $where)
  854. {
  855. $data = [];
  856. $where_data = [];
  857. $where_data['category'] = 'integral';
  858. if ($where['start_time'] != '' && $where['end_time'] != '') {
  859. $where_data['time'] = $where['start_time'] . ' - ' . $where['end_time'];
  860. }
  861. if (isset($where['nickname']) && $where['nickname'] != '') {
  862. $where_data['like'] = $where['nickname'];
  863. }
  864. $data['SumIntegral'] = intval($this->dao->getBillSumColumn($where_data + ['pm' => 1, 'integral_type' => 'get']));
  865. $where_data['type'] = 'sign';
  866. $data['CountSign'] = $this->dao->getUserSignPoint($where_data);
  867. $data['SumSign'] = intval($this->dao->getBillSumColumn($where_data));
  868. $where_data['type'] = ['deduction', 'system_sub'];
  869. $data['SumDeductionIntegral'] = intval($this->dao->getBillSumColumn($where_data));
  870. return [
  871. [
  872. 'col' => 6,
  873. 'count' => $data['SumIntegral'],
  874. 'name' => '总积分(个)',
  875. ],
  876. [
  877. 'col' => 6,
  878. 'count' => $data['CountSign'],
  879. 'name' => '客户签到次数(次)',
  880. ],
  881. [
  882. 'col' => 6,
  883. 'count' => $data['SumSign'],
  884. 'name' => '签到送出积分(个)',
  885. ],
  886. [
  887. 'col' => 6,
  888. 'count' => $data['SumDeductionIntegral'],
  889. 'name' => '使用积分(个)',
  890. ],
  891. ];
  892. }
  893. /**
  894. * 退佣金
  895. * @param int $id
  896. * @throws \think\db\exception\DataNotFoundException
  897. * @throws \think\db\exception\DbException
  898. * @throws \think\db\exception\ModelNotFoundException
  899. */
  900. public function orderRefundBrokerageBack(int $id, string $orderId)
  901. {
  902. $brokerageList = $this->dao->getUserBillList([
  903. 'category' => 'now_money',
  904. 'type' => 'brokerage',
  905. 'link_id' => $id,
  906. 'pm' => 1
  907. ]);
  908. /** @var UserServices $userServices */
  909. $userServices = app()->make(UserServices::class);
  910. $brokerages = $userServices->getColumn([['uid', 'in', array_column($brokerageList, 'uid')]], 'brokerage_price', 'uid');
  911. $userBillData = [];
  912. $res = true;
  913. foreach ($brokerageList as $item) {
  914. $usermoney = $brokerages[$item['uid']] ?? 0;
  915. if ($item['number'] > $usermoney) {
  916. $item['number'] = $usermoney;
  917. }
  918. $res = $res && $userServices->bcDec($item['uid'], 'brokerage_price', (string)$item['number'], 'uid');
  919. $userBillData[] = [
  920. 'title' => '退款退佣金',
  921. 'uid' => $item['uid'],
  922. 'pm' => 0,
  923. 'add_time' => time(),
  924. 'category' => 'now_money',
  925. 'type' => 'brokerage',
  926. 'number' => $item['number'],
  927. 'link_id' => $id,
  928. 'balance' => bcsub((string)$usermoney, (string)$item['number'], 2),
  929. 'mark' => '订单退款扣除佣金' . floatval($item['number']) . '元'
  930. ];
  931. }
  932. if ($userBillData) {
  933. $res = $res && $this->dao->saveAll($userBillData);
  934. }
  935. /** @var UserBrokerageFrozenServices $services */
  936. $services = app()->make(UserBrokerageFrozenServices::class);
  937. $services->updateFrozen($orderId);
  938. return $res;
  939. }
  940. /**
  941. * 佣金排行
  942. * @param string $time
  943. * @return array
  944. * @throws \think\db\exception\DataNotFoundException
  945. * @throws \think\db\exception\DbException
  946. * @throws \think\db\exception\ModelNotFoundException
  947. */
  948. public function brokerageRankList(string $time = 'week')
  949. {
  950. $where = [];
  951. $where['category'] = 'now_money';
  952. $where['type'] = ['brokerage', 'brokerage_user'];
  953. if ($time) {
  954. $where['time'] = $time;
  955. }
  956. [$page, $limit] = $this->getPageValue();
  957. $list = $this->dao->brokerageRankList($where, $page, $limit);
  958. foreach ($list as $key => &$item) {
  959. if (!isset($item['user']) || !$item['user']) {
  960. unset($list['$key']);
  961. continue;
  962. }
  963. $item['nickname'] = $item['user']['nickname'] ?? '';
  964. $item['avatar'] = $item['user']['avatar'] ?? '';
  965. if ($item['brokerage_price'] == '0.00' || $item['brokerage_price'] == 0 || !$item['brokerage_price']) {
  966. unset($list[$key]);
  967. }
  968. unset($item['user']);
  969. }
  970. return $list;
  971. }
  972. /**
  973. * 获取用户排名
  974. * @param int $uid
  975. * @param string $time
  976. */
  977. public function getUserBrokerageRank(int $uid, string $time = 'week')
  978. {
  979. $where = [];
  980. $where['category'] = 'now_money';
  981. $where['type'] = ['brokerage', 'brokerage_user'];
  982. if ($time) {
  983. $where['time'] = $time;
  984. }
  985. $list = $this->dao->brokerageRankList($where, 0, 0);
  986. $position_tmp_one = array_column($list, 'uid');
  987. $position_tmp_two = array_column($list, 'brokerage_price', 'uid');
  988. if (!in_array($uid, $position_tmp_one)) {
  989. $position = 0;
  990. } else {
  991. if ($position_tmp_two[$uid] == 0.00) {
  992. $position = 0;
  993. } else {
  994. $position = array_search($uid, $position_tmp_one) + 1;
  995. }
  996. }
  997. return $position;
  998. }
  999. /**
  1000. * 推广数据 昨天的佣金 累计提现金额 当前佣金
  1001. * @param int $uid
  1002. * @return mixed
  1003. */
  1004. public function commission(int $uid)
  1005. {
  1006. /** @var UserServices $userServices */
  1007. $userServices = app()->make(UserServices::class);
  1008. if (!$userServices->getUserInfo($uid)) {
  1009. throw new ApiException(100026);
  1010. }
  1011. /** @var UserExtractServices $userExtract */
  1012. $userExtract = app()->make(UserExtractServices::class);
  1013. $data = [];
  1014. $data['uid'] = $uid;
  1015. $data['pm'] = 1;
  1016. $data['commissionSum'] = $this->getUsersBokerageSum($data);
  1017. $data['pm'] = 0;
  1018. $data['commissionRefund'] = $this->getUsersBokerageSum($data);
  1019. $data['commissionCount'] = $data['commissionSum'] > $data['commissionRefund'] ? bcsub((string)$data['commissionSum'], (string)$data['commissionRefund'], 2) : 0.00;
  1020. $data['lastDayCount'] = $this->getUsersBokerageSum($data, 'yesterday');//昨天的佣金
  1021. $data['extractCount'] = $userExtract->getUserExtract($uid);//累计提现金额
  1022. return $data;
  1023. }
  1024. /**
  1025. * 前端佣金排行页面数据
  1026. * @param int $uid
  1027. * @param $type
  1028. * @return array
  1029. * @throws \think\db\exception\DataNotFoundException
  1030. * @throws \think\db\exception\DbException
  1031. * @throws \think\db\exception\ModelNotFoundException
  1032. */
  1033. public function brokerage_rank(int $uid, $type)
  1034. {
  1035. /** @var UserServices $userService */
  1036. $userService = app()->make(UserServices::class);
  1037. if (!$userService->getUserInfo($uid)) {
  1038. throw new ApiException(100026);
  1039. }
  1040. return [
  1041. 'rank' => $this->brokerageRankList($type),
  1042. 'position' => $this->getUserBrokerageRank($uid, $type)
  1043. ];
  1044. }
  1045. /**
  1046. * @param $uid
  1047. * @param $type
  1048. * @return array
  1049. */
  1050. public function getUserBillList(int $uid, int $type)
  1051. {
  1052. $where = [];
  1053. $where['uid'] = $uid;
  1054. $where['category'] = 'now_money';
  1055. switch ((int)$type) {
  1056. case 0:
  1057. $where['type'] = ['recharge', 'pay_money', 'system_add', 'pay_product_refund', 'system_sub', 'pay_member', 'offline_scan', 'lottery_add'];
  1058. break;
  1059. case 1:
  1060. $where['type'] = ['pay_money', 'pay_member', 'offline_scan', 'user_recharge_refund'];
  1061. break;
  1062. case 2:
  1063. $where['type'] = ['recharge', 'system_add', 'lottery_add'];
  1064. break;
  1065. case 3:
  1066. $where['type'] = ['brokerage', 'brokerage_user'];
  1067. break;
  1068. case 4:
  1069. $where['type'] = ['extract'];
  1070. /** @var UserExtractServices $userExtractService */
  1071. $userExtractService = app()->make(UserExtractServices::class);
  1072. $userExtract = $userExtractService->getColumn(['uid' => $uid], 'fail_msg', 'id');
  1073. break;
  1074. }
  1075. $field = 'FROM_UNIXTIME(add_time,"%Y-%m") as time,group_concat(id SEPARATOR ",") ids';
  1076. [$page, $limit] = $this->getPageValue();
  1077. $list = $this->dao->getUserBillListByGroup($where, $field, 'time', $page, $limit);
  1078. $data = [];
  1079. if ($list) {
  1080. $listIds = array_column($list, 'ids');
  1081. $ids = [];
  1082. foreach ($listIds as $id) {
  1083. $ids = array_merge($ids, explode(',', $id));
  1084. }
  1085. $info = $this->dao->getColumn([['id', 'in', $ids]], 'FROM_UNIXTIME(add_time,"%Y-%m-%d %H:%i") as add_time,title,number,pm,link_id', 'id');
  1086. foreach ($list as $item) {
  1087. $value['time'] = $item['time'];
  1088. $id = explode(',', $item['ids']);
  1089. array_multisort($id, SORT_DESC);
  1090. $value['list'] = [];
  1091. foreach ($id as $v) {
  1092. if (isset($info[$v])) {
  1093. if ($info[$v]['pm'] == 1 && $type == 4) $info[$v]['fail_msg'] = $userExtract[$info[$v]['link_id']];
  1094. $value['list'][] = $info[$v];
  1095. }
  1096. }
  1097. array_push($data, $value);
  1098. }
  1099. }
  1100. return $data;
  1101. }
  1102. /**
  1103. * 推广 佣金/提现 总和
  1104. * @param int $uid
  1105. * @param $type 3 佣金 4 提现
  1106. * @return mixed
  1107. */
  1108. public function spread_count(int $uid, $type)
  1109. {
  1110. /** @var UserServices $userService */
  1111. $userService = app()->make(UserServices::class);
  1112. if (!$userService->getUserInfo($uid)) {
  1113. throw new ApiException(100026);
  1114. }
  1115. $count = 0;
  1116. if ($type == 3) {
  1117. $count1 = $this->getRecordCount($uid, 'now_money', ['brokerage', 'brokerage_user']);
  1118. $count2 = $this->getRecordCount($uid, 'now_money', ['brokerage', 'brokerage_user'], '', true);
  1119. $count = $count1 - $count2;
  1120. } else if ($type == 4) {
  1121. /** @var UserExtractServices $userExtract */
  1122. $userExtract = app()->make(UserExtractServices::class);
  1123. $count = $userExtract->getUserExtract($uid);//累计提现
  1124. }
  1125. return $count ?: 0;
  1126. }
  1127. /**
  1128. * 推广订单
  1129. * @param Request $request
  1130. * @return mixed
  1131. */
  1132. public function spread_order(int $uid, array $data)
  1133. {
  1134. /** @var UserServices $userService */
  1135. $userService = app()->make(UserServices::class);
  1136. if (!$userService->getUserInfo($uid, 'uid')) {
  1137. throw new ApiException(100026);
  1138. }
  1139. $result = ['list' => [], 'time' => [], 'count' => 0];
  1140. /** @var StoreOrderServices $storeOrderServices */
  1141. $storeOrderServices = app()->make(StoreOrderServices::class);
  1142. [$page, $limit] = $this->getPageValue();
  1143. $time = [];
  1144. $where = ['paid' => 1, 'type' => 6, 'spread_or_uid' => $uid, 'pid' => 0, 'refund_status' => 0];
  1145. $list = $storeOrderServices->getlist($where, ['id,order_id,uid,add_time,spread_uid,status,spread_two_uid,one_brokerage,two_brokerage,pay_price,pid'], $page, $limit, ['split']);
  1146. $result['count'] = $storeOrderServices->count($where);
  1147. $time_data = [];
  1148. if ($list) {
  1149. $uids = array_unique(array_column($list, 'uid'));
  1150. $userInfos = $userService->getColumn([['uid', 'in', $uids]], 'uid,avatar,nickname', 'uid');
  1151. foreach ($list as &$item) {
  1152. $item['avatar'] = $userInfos[$item['uid']]['avatar'] ?? '';
  1153. $item['nickname'] = $userInfos[$item['uid']]['nickname'] ?? '';
  1154. $item['number'] = $item['spread_uid'] == $uid ? $item['one_brokerage'] : $item['two_brokerage'];
  1155. $item['time'] = $item['add_time'] ? date('Y-m-d H:i', $item['add_time']) : '';
  1156. $item['time_key'] = $item['add_time'] ? date('Y-m', $item['add_time']) : '';
  1157. $item['type'] = in_array($item['status'], [2, 3]) ? 'brokerage' : 'number';
  1158. foreach ($item['split'] as $key => $items) {
  1159. $item['children'][] = [
  1160. 'order_id' => $items['order_id'],
  1161. 'number' => $item['spread_uid'] == $uid ? $items['one_brokerage'] : $items['two_brokerage'],
  1162. 'type' => in_array($item['status'], [2, 3]) ? 'brokerage' : 'number',
  1163. ];
  1164. unset($item['split'][$key]);
  1165. }
  1166. }
  1167. $times = array_unique(array_column($list, 'time_key'));
  1168. $time_data = [];
  1169. $i = 0;
  1170. foreach ($times as $time) {
  1171. $time_data[$i]['time'] = $time;
  1172. $time_data[$i]['count'] = $storeOrderServices->getMonthCount($where + ['pid' => 0], $time);
  1173. $i++;
  1174. }
  1175. }
  1176. $result['list'] = $list;
  1177. $result['time'] = $time_data;
  1178. return $result;
  1179. }
  1180. /**根据查询用户充值金额
  1181. * @param array $where
  1182. * @return float|int
  1183. */
  1184. public function getRechargeMoneyByWhere(array $where, string $rechargeSumField, string $selectType, string $group = "")
  1185. {
  1186. switch ($selectType) {
  1187. case "sum" :
  1188. return $this->dao->getWhereSumField($where, $rechargeSumField);
  1189. case "group" :
  1190. return $this->dao->getGroupField($where, $rechargeSumField, $group);
  1191. }
  1192. }
  1193. /**
  1194. * 事业部/代理商订单
  1195. * @param $uid
  1196. * @return array
  1197. */
  1198. public function divisionOrder($uid)
  1199. {
  1200. /** @var UserServices $userService */
  1201. $userService = app()->make(UserServices::class);
  1202. /** @var StoreOrderServices $storeOrderServices */
  1203. $storeOrderServices = app()->make(StoreOrderServices::class);
  1204. $userInfo = $userService->getUserInfo($uid);
  1205. if (!$userInfo) {
  1206. throw new ApiException(100026);
  1207. }
  1208. $division_type = $userInfo['division_type'];
  1209. [$page, $limit] = $this->getPageValue();
  1210. $where = ['paid' => 1, 'type' => 1, 'pid' => 0];
  1211. if ($division_type == 1) {
  1212. $where = $where + ['division_id' => $uid];
  1213. } elseif ($division_type == 2) {
  1214. $where = $where + ['agent_id' => $uid];
  1215. }
  1216. $list = $storeOrderServices->getlist($where, ['id,order_id,uid,add_time,spread_uid,division_id,agent_id,status,spread_two_uid,one_brokerage,two_brokerage,agent_brokerage,division_brokerage,pay_price,pid'], $page, $limit, ['split']);
  1217. $result['count'] = $storeOrderServices->count($where);
  1218. $time_data = [];
  1219. if ($list) {
  1220. $uids = array_unique(array_column($list, 'uid'));
  1221. $userInfos = $userService->getColumn([['uid', 'in', $uids]], 'uid,avatar,nickname', 'uid');
  1222. foreach ($list as &$item) {
  1223. $item['avatar'] = $userInfos[$item['uid']]['avatar'] ?? '';
  1224. $item['nickname'] = $userInfos[$item['uid']]['nickname'] ?? '';
  1225. $item['time'] = $item['add_time'] ? date('Y-m-d H:i', $item['add_time']) : '';
  1226. $item['time_key'] = $item['add_time'] ? date('Y-m', $item['add_time']) : '';
  1227. $item['type'] = in_array($item['status'], [2, 3]) ? 'brokerage' : 'number';
  1228. if ($division_type == 1) {
  1229. $item['number'] = $item['division_brokerage'];
  1230. } elseif ($division_type == 2) {
  1231. $item['number'] = $item['agent_brokerage'];
  1232. }
  1233. }
  1234. $times = array_unique(array_column($list, 'time_key'));
  1235. $time_data = [];
  1236. $i = 0;
  1237. foreach ($times as $time) {
  1238. $time_data[$i]['time'] = $time;
  1239. $time_data[$i]['count'] = $storeOrderServices->getMonthCount($where + ['pid' => 0], $time);
  1240. $i++;
  1241. }
  1242. }
  1243. $result['list'] = $list;
  1244. $result['time'] = $time_data;
  1245. return $result;
  1246. }
  1247. }