UserBillServices.php 47 KB

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