UserBillServices.php 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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. */
  393. public function getIntegralList(int $uid = 0, $where_time = [], string $field = '*')
  394. {
  395. [$page, $limit] = $this->getPageValue();
  396. $where = ['category' => 'integral'];
  397. if ($uid) $where['uid'] = $uid;
  398. if ($where_time) $where['add_time'] = $where_time;
  399. $list = $this->dao->getList($where, $field, $page, $limit);
  400. foreach ($list as &$item) {
  401. $item['number'] = intval($item['number']);
  402. $item['is_frozen'] = $item['frozen_time'] > time() ? 1 : 0;
  403. }
  404. $count = $this->dao->count($where);
  405. return compact('list', 'count');
  406. }
  407. /**
  408. * 获取用户签到总数
  409. * @param int $uid
  410. * @return float
  411. */
  412. public function getSignlSum(int $uid = 0, $where_time = [])
  413. {
  414. $where = ['category' => 'integral', 'type' => 'sign', 'pm' => 1, 'status' => 1];
  415. if ($uid) $where['uid'] = $uid;
  416. if ($where_time) $where['add_time'] = $where_time;
  417. return $this->dao->getBillSum($where);
  418. }
  419. /**
  420. * 获取用户的签到总次数
  421. * @param int $uid
  422. * @return float
  423. */
  424. public function getSignCount(int $uid = 0, $where_time = [])
  425. {
  426. $where = ['category' => 'integral', 'type' => 'sign', 'pm' => 1, 'status' => 1];
  427. if ($uid) $where['uid'] = $uid;
  428. if ($where_time) $where['add_time'] = $where_time;
  429. return $this->dao->getBillCount($where);
  430. }
  431. /**
  432. * 获取签到列表
  433. * @param int $uid
  434. * @param array $where_time
  435. * @param string $field
  436. * @return array
  437. */
  438. public function getSignList(int $uid = 0, $where_time = [], string $field = '*')
  439. {
  440. [$page, $limit] = $this->getPageValue();
  441. $where = ['category' => 'integral', 'type' => 'sign'];
  442. if ($uid) $where['uid'] = $uid;
  443. if ($where_time) $where['add_time'] = $where_time;
  444. $list = $this->dao->getList($where, $field, $page, $limit);
  445. $count = $this->dao->count($where);
  446. return compact('list', 'count');
  447. }
  448. /**
  449. * 经验总数
  450. * @param int $uid
  451. * @param array $where_time
  452. * @return float
  453. */
  454. public function getExpSum(int $uid = 0, $where_time = [])
  455. {
  456. $where = ['category' => ['exp'], 'pm' => 1, 'status' => 1];
  457. if ($uid) $where['uid'] = $uid;
  458. if ($where_time) $where['time'] = $where_time;
  459. return $this->dao->getBillSum($where);
  460. }
  461. /**
  462. * 获取所有经验列表
  463. * @param int $uid
  464. * @param array $where_time
  465. * @param string $field
  466. * @return array
  467. */
  468. public function getExpList(int $uid = 0, $where_time = [], string $field = '*')
  469. {
  470. [$page, $limit] = $this->getPageValue();
  471. $where = ['category' => ['exp']];
  472. $where['status'] = 1;
  473. if ($uid) $where['uid'] = $uid;
  474. if ($where_time) $where['time'] = $where_time;
  475. $list = $this->dao->getList($where, $field, $page, $limit);
  476. $count = $this->dao->count($where);
  477. return compact('list', 'count');
  478. }
  479. /**
  480. * 增加佣金
  481. * @param int $uid
  482. * @param string $type
  483. * @param array $data
  484. * @return bool
  485. * @throws Exception
  486. */
  487. public function incomeNowMoney(int $uid, string $type, array $data)
  488. {
  489. $data['uid'] = $uid;
  490. $data['category'] = 'now_money';
  491. $data['type'] = $type;
  492. $data['pm'] = 1;
  493. $data['status'] = 1;
  494. $data['add_time'] = time();
  495. if (!$this->dao->save($data))
  496. throw new AdminException(400655);
  497. return true;
  498. }
  499. /**
  500. * 扣除佣金
  501. * @param int $uid
  502. * @param string $type
  503. * @param array $data
  504. * @return bool
  505. * @throws Exception
  506. */
  507. public function expendNowMoney(int $uid, string $type, array $data)
  508. {
  509. $data['uid'] = $uid;
  510. $data['category'] = 'now_money';
  511. $data['type'] = $type;
  512. $data['pm'] = 0;
  513. $data['status'] = 1;
  514. $data['add_time'] = time();
  515. if (!$this->dao->save($data))
  516. throw new AdminException(400655);
  517. return true;
  518. }
  519. /**
  520. * 增加积分
  521. * @param int $uid
  522. * @param string $type
  523. * @param array $data
  524. * @return bool
  525. * @throws Exception
  526. */
  527. public function incomeIntegral(int $uid, string $type, array $data)
  528. {
  529. $data['uid'] = $uid;
  530. $data['category'] = 'integral';
  531. $data['type'] = $type;
  532. $data['pm'] = 1;
  533. $data['status'] = 1;
  534. $data['add_time'] = time();
  535. if (!$this->dao->save($data))
  536. throw new AdminException(400655);
  537. return true;
  538. }
  539. /**
  540. * 扣除积分
  541. * @param int $uid
  542. * @param string $type
  543. * @param array $data
  544. * @return bool
  545. * @throws Exception
  546. */
  547. public function expendIntegral(int $uid, string $type, array $data)
  548. {
  549. $data['uid'] = $uid;
  550. $data['category'] = 'integral';
  551. $data['type'] = $type;
  552. $data['pm'] = 0;
  553. $data['status'] = 1;
  554. $data['add_time'] = time();
  555. if (!$this->dao->save($data))
  556. throw new AdminException(400655);
  557. return true;
  558. }
  559. /**
  560. * 写入用户记录
  561. * @param string $type 写入类型
  562. * @param int $uid
  563. * @param int|string|array $number
  564. * @param int|string $balance
  565. * @param int $link_id
  566. * @return bool|mixed
  567. */
  568. public function income(string $type, int $uid, $number, $balance, $link_id)
  569. {
  570. $data = $this->incomeData[$type] ?? null;
  571. if (!$data) {
  572. return true;
  573. }
  574. $data['uid'] = $uid;
  575. $data['balance'] = $balance ?? 0;
  576. $data['link_id'] = $link_id;
  577. if (is_array($number)) {
  578. $key = array_keys($number);
  579. $key = array_map(function ($item) {
  580. return '{%' . $item . '%}';
  581. }, $key);
  582. $value = array_values($number);
  583. $data['number'] = $number['number'] ?? 0;
  584. $data['mark'] = str_replace($key, $value, $data['mark']);
  585. } else {
  586. $data['number'] = $number;
  587. $data['mark'] = str_replace(['{%num%}'], $number, $data['mark']);
  588. }
  589. $data['add_time'] = time();
  590. if ($type == 'pay_give_integral' || $type == 'order_give_integral') {
  591. $integral_frozen = sys_config('integral_frozen');
  592. if ($integral_frozen) {
  593. $data['frozen_time'] = $data['add_time'] + ($integral_frozen * 86400);
  594. }
  595. }
  596. return $this->dao->save($data);
  597. }
  598. /**
  599. * 邀请新用户增加经验
  600. * @param int $spreadUid
  601. */
  602. public function inviteUserIncExp(int $spreadUid)
  603. {
  604. if (!$spreadUid) {
  605. return false;
  606. }
  607. //用户等级是否开启
  608. if (!sys_config('member_func_status', 1)) {
  609. return false;
  610. }
  611. /** @var UserServices $userService */
  612. $userService = app()->make(UserServices::class);
  613. $spread_user = $userService->getUserInfo($spreadUid);
  614. if (!$spread_user) {
  615. return false;
  616. }
  617. $exp_num = sys_config('invite_user_exp', 0);
  618. if ($exp_num) {
  619. $userService->incField($spreadUid, 'exp', (int)$exp_num);
  620. $data = [];
  621. $data['uid'] = $spreadUid;
  622. $data['number'] = $exp_num;
  623. $data['category'] = 'exp';
  624. $data['type'] = 'invite_user';
  625. $data['title'] = $data['mark'] = '邀新奖励';
  626. $data['balance'] = (int)$spread_user['exp'] + (int)$exp_num;
  627. $data['pm'] = 1;
  628. $data['status'] = 1;
  629. $this->dao->save($data);
  630. }
  631. //检测会员等级
  632. try {
  633. //用户升级事件
  634. event('user.userLevel', [$spreadUid]);
  635. } catch (\Throwable $e) {
  636. Log::error('会员等级升级失败,失败原因:' . $e->getMessage());
  637. }
  638. return true;
  639. }
  640. /**
  641. * 获取type
  642. * @param array $where
  643. * @param string $filed
  644. */
  645. public function getBillType(array $where)
  646. {
  647. return $this->dao->getType($where);
  648. }
  649. /**
  650. * 资金类型
  651. */
  652. public function bill_type()
  653. {
  654. $where = [];
  655. $where['not_type'] = ['gain', 'system_sub', 'deduction', 'sign'];
  656. $where['not_category'] = ['exp', 'integral'];
  657. return Cache::remember('user_type_list', function () use ($where) {
  658. return ['list' => $this->getBillType($where)];
  659. }, 600);
  660. }
  661. /**
  662. * 获取资金列表
  663. * @param array $where
  664. * @param string $field
  665. * @return array
  666. */
  667. public function getBillList(array $where, string $field = '*', $is_page = true)
  668. {
  669. $where_data = [];
  670. if (isset($where['uid']) && $where['uid'] != '') {
  671. $where_data['uid'] = $where['uid'];
  672. }
  673. if ($where['start_time'] != '' && $where['end_time'] != '') {
  674. $where_data['time'] = str_replace('-', '/', $where['start_time']) . ' - ' . str_replace('-', '/', $where['end_time']);
  675. }
  676. if (isset($where['category']) && $where['category'] != '') {
  677. $where_data['category'] = $where['category'];
  678. }
  679. if (isset($where['type']) && $where['type'] != '') {
  680. $where_data['type'] = $where['type'];
  681. if ($where['type'] == 'brokerage') $where_data['pm'] = 1;
  682. }
  683. $where_data['not_category'] = ['integral', 'exp', 'share'];
  684. $where_data['not_type'] = $where['type'] == 'pay_product' ? ['gain', 'system_sub', 'deduction', 'sign'] : ['gain', 'system_sub', 'deduction', 'sign', 'pay_product'];
  685. if (isset($where['nickname']) && $where['nickname'] != '') {
  686. $where_data['like'] = $where['nickname'];
  687. }
  688. if (isset($where['excel']) && $where['excel'] != '') {
  689. $where_data['excel'] = $where['excel'];
  690. } else {
  691. $where_data['excel'] = 0;
  692. }
  693. [$page, $limit] = $this->getPageValue($is_page);
  694. $data = $this->dao->getBillList($where_data, $field, $page, $limit);
  695. foreach ($data as &$item) {
  696. $item['nickname'] = $item['user']['nickname'] ?? '';
  697. unset($item['user']);
  698. }
  699. $count = $this->dao->count($where_data);
  700. return compact('data', 'count');
  701. }
  702. /**
  703. * 获取佣金列表
  704. * @param array $where
  705. * @param int $limit
  706. * @return array
  707. */
  708. public function getCommissionList(array $where, int $limit = 0)
  709. {
  710. $where_data = [];
  711. $where_data['time'] = $where['time'];
  712. if (isset($where['nickname']) && $where['nickname']) {
  713. $where_data[] = ['u.account|u.nickname|u.uid|u.phone', 'LIKE', "%$where[nickname]%"];
  714. }
  715. if (isset($where['price_max']) && isset($where['price_min'])) {
  716. if ($where['price_max'] != '' && $where['price_min'] != '') {
  717. $where_data[] = ['u.brokerage_price', 'between', [$where['price_min'], $where['price_max']]];
  718. } elseif ($where['price_min'] != '' && $where['price_max'] == '') {
  719. $where_data[] = ['u.brokerage_price', '>=', $where['price_min']];
  720. } elseif ($where['price_min'] == '' && $where['price_max'] != '') {
  721. $where_data[] = ['u.brokerage_price', '<=', $where['price_max']];
  722. }
  723. }
  724. $order_string = '';
  725. $order_arr = ['asc', 'desc'];
  726. if (isset($where['sum_number']) && in_array($where['sum_number'], $order_arr)) {
  727. $order_string .= ',income ' . $where['sum_number'];
  728. }
  729. if (isset($where['brokerage_price']) && in_array($where['brokerage_price'], $order_arr)) {
  730. $order_string .= ',u.brokerage_price ' . $where['brokerage_price'];
  731. }
  732. if ($order_string) {
  733. $order_string = trim($order_string, ',');
  734. }
  735. /** @var UserUserBrokerageServices $userUserBrokerage */
  736. $userUserBrokerage = app()->make(UserUserBrokerageServices::class);
  737. [$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);
  738. $uids = array_unique(array_column($list, 'uid'));
  739. /** @var UserExtractServices $userExtract */
  740. $userExtract = app()->make(UserExtractServices::class);
  741. $extractSumList = $userExtract->getUsersSumList($uids);
  742. foreach ($list as &$item) {
  743. $item['sum_number'] = $item['income'] > $item['pay'] ? bcsub($item['income'], $item['pay'], 2) : 0;
  744. $item['nickname'] = $item['nickname'] . "|" . ($item['phone'] ? $item['phone'] . "|" : '') . $item['uid'];
  745. $item['extract_price'] = $extractSumList[$item['uid']] ?? 0;
  746. $item['time'] = $item['time'] ? date('Y-m-d H:i:s', $item['time']) : '';
  747. }
  748. return compact('count', 'list');
  749. }
  750. public function user_info(int $uid)
  751. {
  752. /** @var UserServices $user */
  753. $user = app()->make(UserServices::class);
  754. $user_info = $user->getUserInfo($uid, 'nickname,spread_uid,now_money,add_time,brokerage_price');
  755. if (!$user_info) {
  756. throw new AdminException(400119);
  757. }
  758. $user_info = $user_info->toArray();
  759. $user_info['number'] = $user_info['brokerage_price'];
  760. $user_info['add_time'] = date('Y-m-d H:i:s', $user_info['add_time']);
  761. $user_info['spread_name'] = $user_info['spread_uid'] ? $user->getUserInfo((int)$user_info['spread_uid'], 'nickname', true)['nickname'] ?? '' : '';
  762. return compact('user_info');
  763. }
  764. /**
  765. * 记录分享次数
  766. * @param int $uid 用户uid
  767. * @param int $cd 冷却时间
  768. * @return Boolean
  769. * */
  770. public function setUserShare(int $uid, $cd = 300)
  771. {
  772. /** @var UserServices $userServices */
  773. $userServices = app()->make(UserServices::class);
  774. $user = $userServices->getUserInfo($uid);
  775. if (!$user) {
  776. throw new AdminException(400119);
  777. }
  778. $cachename = 'Share_' . $uid;
  779. if (CacheService::get($cachename)) {
  780. return false;
  781. }
  782. $data = ['title' => '用户分享记录', 'uid' => $uid, 'category' => 'share', 'type' => 'share', 'number' => 0, 'link_id' => 0, 'balance' => 0, 'mark' => date('Y-m-d H:i:s', time()) . ':用户分享'];
  783. if (!$this->dao->save($data)) {
  784. throw new AdminException(400656);
  785. }
  786. CacheService::set($cachename, 1, $cd);
  787. return true;
  788. }
  789. /**
  790. * 获取佣金提现列表
  791. * @param int $uid
  792. * @param array $where
  793. * @return array
  794. * @throws \think\db\exception\DataNotFoundException
  795. * @throws \think\db\exception\DbException
  796. * @throws \think\db\exception\ModelNotFoundException
  797. */
  798. public function getBillOneList(int $uid, array $where)
  799. {
  800. $where['uid'] = $uid;
  801. $data = $this->getBillList($where);
  802. foreach ($data['data'] as &$item) {
  803. $item['_add_time'] = $item['add_time'] ?? '';
  804. }
  805. return $data;
  806. }
  807. /**
  808. * 获取积分列表
  809. * @param array $where
  810. * @param string $field
  811. * @return array
  812. */
  813. public function getPointList(array $where, string $field = '*', $is_page = true)
  814. {
  815. $where_data = [];
  816. $where_data['category'] = 'integral';
  817. if (isset($where['uid']) && $where['uid'] != '') {
  818. $where_data['uid'] = $where['uid'];
  819. }
  820. if ($where['start_time'] != '' && $where['end_time'] != '') {
  821. $where_data['time'] = $where['start_time'] . ' - ' . $where['end_time'];
  822. }
  823. if (isset($where['type']) && $where['type'] != '') {
  824. $where_data['type'] = $where['type'];
  825. }
  826. if (isset($where['nickname']) && $where['nickname'] != '') {
  827. $where_data['like'] = $where['nickname'];
  828. }
  829. if (isset($where['excel']) && $where['excel'] != '') {
  830. $where_data['excel'] = $where['excel'];
  831. } else {
  832. $where_data['excel'] = 0;
  833. }
  834. [$page, $limit] = $this->getPageValue($is_page);
  835. $list = $this->dao->getBillList($where_data, $field, $page, $limit);
  836. foreach ($list as &$item) {
  837. $item['nickname'] = $item['user']['nickname'] ?? '';
  838. $item['number'] = intval($item['number']);
  839. $item['balance'] = intval($item['balance']);
  840. unset($item['user']);
  841. }
  842. $count = $this->dao->count($where_data);
  843. return compact('list', 'count');
  844. }
  845. /**
  846. * 积分头部信息
  847. * @param array $where
  848. * @return array[]
  849. */
  850. public function getUserPointBadgelist(array $where)
  851. {
  852. $data = [];
  853. $where_data = [];
  854. $where_data['category'] = 'integral';
  855. if ($where['start_time'] != '' && $where['end_time'] != '') {
  856. $where_data['time'] = $where['start_time'] . ' - ' . $where['end_time'];
  857. }
  858. if (isset($where['nickname']) && $where['nickname'] != '') {
  859. $where_data['like'] = $where['nickname'];
  860. }
  861. $data['SumIntegral'] = intval($this->dao->getBillSumColumn($where_data + ['pm' => 1, 'integral_type' => 'get']));
  862. $where_data['type'] = 'sign';
  863. $data['CountSign'] = $this->dao->getUserSignPoint($where_data);
  864. $data['SumSign'] = intval($this->dao->getBillSumColumn($where_data));
  865. $where_data['type'] = ['deduction', 'system_sub'];
  866. $data['SumDeductionIntegral'] = intval($this->dao->getBillSumColumn($where_data));
  867. return [
  868. [
  869. 'col' => 6,
  870. 'count' => $data['SumIntegral'],
  871. 'name' => '总积分(个)',
  872. ],
  873. [
  874. 'col' => 6,
  875. 'count' => $data['CountSign'],
  876. 'name' => '客户签到次数(次)',
  877. ],
  878. [
  879. 'col' => 6,
  880. 'count' => $data['SumSign'],
  881. 'name' => '签到送出积分(个)',
  882. ],
  883. [
  884. 'col' => 6,
  885. 'count' => $data['SumDeductionIntegral'],
  886. 'name' => '使用积分(个)',
  887. ],
  888. ];
  889. }
  890. /**
  891. * 退佣金
  892. * @param int $id
  893. * @throws \think\db\exception\DataNotFoundException
  894. * @throws \think\db\exception\DbException
  895. * @throws \think\db\exception\ModelNotFoundException
  896. */
  897. public function orderRefundBrokerageBack(int $id, string $orderId)
  898. {
  899. $brokerageList = $this->dao->getUserBillList([
  900. 'category' => 'now_money',
  901. 'type' => 'brokerage',
  902. 'link_id' => $id,
  903. 'pm' => 1
  904. ]);
  905. /** @var UserServices $userServices */
  906. $userServices = app()->make(UserServices::class);
  907. $brokerages = $userServices->getColumn([['uid', 'in', array_column($brokerageList, 'uid')]], 'brokerage_price', 'uid');
  908. $userBillData = [];
  909. $res = true;
  910. foreach ($brokerageList as $item) {
  911. $usermoney = $brokerages[$item['uid']] ?? 0;
  912. if ($item['number'] > $usermoney) {
  913. $item['number'] = $usermoney;
  914. }
  915. $res = $res && $userServices->bcDec($item['uid'], 'brokerage_price', (string)$item['number'], 'uid');
  916. $userBillData[] = [
  917. 'title' => '退款退佣金',
  918. 'uid' => $item['uid'],
  919. 'pm' => 0,
  920. 'add_time' => time(),
  921. 'category' => 'now_money',
  922. 'type' => 'brokerage',
  923. 'number' => $item['number'],
  924. 'link_id' => $id,
  925. 'balance' => bcsub((string)$usermoney, (string)$item['number'], 2),
  926. 'mark' => '订单退款扣除佣金' . floatval($item['number']) . '元'
  927. ];
  928. }
  929. if ($userBillData) {
  930. $res = $res && $this->dao->saveAll($userBillData);
  931. }
  932. /** @var UserBrokerageFrozenServices $services */
  933. $services = app()->make(UserBrokerageFrozenServices::class);
  934. $services->updateFrozen($orderId);
  935. return $res;
  936. }
  937. /**
  938. * 佣金排行
  939. * @param string $time
  940. * @return array
  941. * @throws \think\db\exception\DataNotFoundException
  942. * @throws \think\db\exception\DbException
  943. * @throws \think\db\exception\ModelNotFoundException
  944. */
  945. public function brokerageRankList(string $time = 'week')
  946. {
  947. $where = [];
  948. $where['category'] = 'now_money';
  949. $where['type'] = ['brokerage', 'brokerage_user'];
  950. if ($time) {
  951. $where['time'] = $time;
  952. }
  953. [$page, $limit] = $this->getPageValue();
  954. $list = $this->dao->brokerageRankList($where, $page, $limit);
  955. foreach ($list as $key => &$item) {
  956. if (!isset($item['user']) || !$item['user']) {
  957. unset($list['$key']);
  958. continue;
  959. }
  960. $item['nickname'] = $item['user']['nickname'] ?? '';
  961. $item['avatar'] = $item['user']['avatar'] ?? '';
  962. if ($item['brokerage_price'] == '0.00' || $item['brokerage_price'] == 0 || !$item['brokerage_price']) {
  963. unset($list[$key]);
  964. }
  965. unset($item['user']);
  966. }
  967. return $list;
  968. }
  969. /**
  970. * 获取用户排名
  971. * @param int $uid
  972. * @param string $time
  973. */
  974. public function getUserBrokerageRank(int $uid, string $time = 'week')
  975. {
  976. $where = [];
  977. $where['category'] = 'now_money';
  978. $where['type'] = ['brokerage', 'brokerage_user'];
  979. if ($time) {
  980. $where['time'] = $time;
  981. }
  982. $list = $this->dao->brokerageRankList($where, 0, 0);
  983. $position_tmp_one = array_column($list, 'uid');
  984. $position_tmp_two = array_column($list, 'brokerage_price', 'uid');
  985. if (!in_array($uid, $position_tmp_one)) {
  986. $position = 0;
  987. } else {
  988. if ($position_tmp_two[$uid] == 0.00) {
  989. $position = 0;
  990. } else {
  991. $position = array_search($uid, $position_tmp_one) + 1;
  992. }
  993. }
  994. return $position;
  995. }
  996. /**
  997. * 推广数据 昨天的佣金 累计提现金额 当前佣金
  998. * @param int $uid
  999. * @return mixed
  1000. */
  1001. public function commission(int $uid)
  1002. {
  1003. /** @var UserServices $userServices */
  1004. $userServices = app()->make(UserServices::class);
  1005. if (!$userServices->getUserInfo($uid)) {
  1006. throw new ApiException(100026);
  1007. }
  1008. /** @var UserExtractServices $userExtract */
  1009. $userExtract = app()->make(UserExtractServices::class);
  1010. $data = [];
  1011. $data['uid'] = $uid;
  1012. $data['pm'] = 1;
  1013. $data['commissionSum'] = $this->getUsersBokerageSum($data);
  1014. $data['pm'] = 0;
  1015. $data['commissionRefund'] = $this->getUsersBokerageSum($data);
  1016. $data['commissionCount'] = $data['commissionSum'] > $data['commissionRefund'] ? bcsub((string)$data['commissionSum'], (string)$data['commissionRefund'], 2) : 0.00;
  1017. $data['lastDayCount'] = $this->getUsersBokerageSum($data, 'yesterday');//昨天的佣金
  1018. $data['extractCount'] = $userExtract->getUserExtract($uid);//累计提现金额
  1019. return $data;
  1020. }
  1021. /**
  1022. * 前端佣金排行页面数据
  1023. * @param int $uid
  1024. * @param $type
  1025. * @return array
  1026. * @throws \think\db\exception\DataNotFoundException
  1027. * @throws \think\db\exception\DbException
  1028. * @throws \think\db\exception\ModelNotFoundException
  1029. */
  1030. public function brokerage_rank(int $uid, $type)
  1031. {
  1032. /** @var UserServices $userService */
  1033. $userService = app()->make(UserServices::class);
  1034. if (!$userService->getUserInfo($uid)) {
  1035. throw new ApiException(100026);
  1036. }
  1037. return [
  1038. 'rank' => $this->brokerageRankList($type),
  1039. 'position' => $this->getUserBrokerageRank($uid, $type)
  1040. ];
  1041. }
  1042. /**
  1043. * @param $uid
  1044. * @param $type
  1045. * @return array
  1046. */
  1047. public function getUserBillList(int $uid, int $type)
  1048. {
  1049. $where = [];
  1050. $where['uid'] = $uid;
  1051. $where['category'] = 'now_money';
  1052. switch ((int)$type) {
  1053. case 0:
  1054. $where['type'] = ['recharge', 'pay_money', 'system_add', 'pay_product_refund', 'system_sub', 'pay_member', 'offline_scan', 'lottery_add'];
  1055. break;
  1056. case 1:
  1057. $where['type'] = ['pay_money', 'pay_member', 'offline_scan', 'user_recharge_refund'];
  1058. break;
  1059. case 2:
  1060. $where['type'] = ['recharge', 'system_add', 'lottery_add'];
  1061. break;
  1062. case 3:
  1063. $where['type'] = ['brokerage', 'brokerage_user'];
  1064. break;
  1065. case 4:
  1066. $where['type'] = ['extract'];
  1067. /** @var UserExtractServices $userExtractService */
  1068. $userExtractService = app()->make(UserExtractServices::class);
  1069. $userExtract = $userExtractService->getColumn(['uid' => $uid], 'fail_msg', 'id');
  1070. break;
  1071. }
  1072. $field = 'FROM_UNIXTIME(add_time,"%Y-%m") as time,group_concat(id SEPARATOR ",") ids';
  1073. [$page, $limit] = $this->getPageValue();
  1074. $list = $this->dao->getUserBillListByGroup($where, $field, 'time', $page, $limit);
  1075. $data = [];
  1076. if ($list) {
  1077. $listIds = array_column($list, 'ids');
  1078. $ids = [];
  1079. foreach ($listIds as $id) {
  1080. $ids = array_merge($ids, explode(',', $id));
  1081. }
  1082. $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');
  1083. foreach ($list as $item) {
  1084. $value['time'] = $item['time'];
  1085. $id = explode(',', $item['ids']);
  1086. array_multisort($id, SORT_DESC);
  1087. $value['list'] = [];
  1088. foreach ($id as $v) {
  1089. if (isset($info[$v])) {
  1090. if ($info[$v]['pm'] == 1 && $type == 4) $info[$v]['fail_msg'] = $userExtract[$info[$v]['link_id']];
  1091. $value['list'][] = $info[$v];
  1092. }
  1093. }
  1094. array_push($data, $value);
  1095. }
  1096. }
  1097. return $data;
  1098. }
  1099. /**
  1100. * 推广 佣金/提现 总和
  1101. * @param int $uid
  1102. * @param $type 3 佣金 4 提现
  1103. * @return mixed
  1104. */
  1105. public function spread_count(int $uid, $type)
  1106. {
  1107. /** @var UserServices $userService */
  1108. $userService = app()->make(UserServices::class);
  1109. if (!$userService->getUserInfo($uid)) {
  1110. throw new ApiException(100026);
  1111. }
  1112. $count = 0;
  1113. if ($type == 3) {
  1114. $count1 = $this->getRecordCount($uid, 'now_money', ['brokerage', 'brokerage_user']);
  1115. $count2 = $this->getRecordCount($uid, 'now_money', ['brokerage', 'brokerage_user'], '', true);
  1116. $count = $count1 - $count2;
  1117. } else if ($type == 4) {
  1118. /** @var UserExtractServices $userExtract */
  1119. $userExtract = app()->make(UserExtractServices::class);
  1120. $count = $userExtract->getUserExtract($uid);//累计提现
  1121. }
  1122. return $count ?: 0;
  1123. }
  1124. /**
  1125. * 推广订单
  1126. * @param Request $request
  1127. * @return mixed
  1128. */
  1129. public function spread_order(int $uid, array $data)
  1130. {
  1131. /** @var UserServices $userService */
  1132. $userService = app()->make(UserServices::class);
  1133. if (!$userService->getUserInfo($uid)) {
  1134. throw new ApiException(100026);
  1135. }
  1136. $result = ['list' => [], 'time' => [], 'count' => 0];
  1137. /** @var StoreOrderServices $storeOrderServices */
  1138. $storeOrderServices = app()->make(StoreOrderServices::class);
  1139. [$page, $limit] = $this->getPageValue();
  1140. $time = [];
  1141. $where = ['paid' => 1, 'type' => 6, 'spread_or_uid' => $uid, 'pid' => 0];
  1142. $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']);
  1143. $result['count'] = $storeOrderServices->count($where);
  1144. $time_data = [];
  1145. if ($list) {
  1146. $uids = array_unique(array_column($list, 'uid'));
  1147. $userInfos = $userService->getColumn([['uid', 'in', $uids]], 'uid,avatar,nickname', 'uid');
  1148. foreach ($list as &$item) {
  1149. $item['avatar'] = $userInfos[$item['uid']]['avatar'] ?? '';
  1150. $item['nickname'] = $userInfos[$item['uid']]['nickname'] ?? '';
  1151. $item['number'] = $item['spread_uid'] == $uid ? $item['one_brokerage'] : $item['two_brokerage'];
  1152. $item['time'] = $item['add_time'] ? date('Y-m-d H:i', $item['add_time']) : '';
  1153. $item['time_key'] = $item['add_time'] ? date('Y-m', $item['add_time']) : '';
  1154. $item['type'] = in_array($item['status'], [2, 3]) ? 'brokerage' : 'number';
  1155. foreach ($item['split'] as $key => $items) {
  1156. $item['children'][] = [
  1157. 'order_id' => $items['order_id'],
  1158. 'number' => $item['spread_uid'] == $uid ? $items['one_brokerage'] : $items['two_brokerage'],
  1159. 'type' => in_array($item['status'], [2, 3]) ? 'brokerage' : 'number',
  1160. ];
  1161. unset($item['split'][$key]);
  1162. }
  1163. }
  1164. $times = array_unique(array_column($list, 'time_key'));
  1165. $time_data = [];
  1166. $i = 0;
  1167. foreach ($times as $time) {
  1168. $time_data[$i]['time'] = $time;
  1169. $time_data[$i]['count'] = $storeOrderServices->getMonthCount($where + ['pid' => 0], $time);
  1170. $i++;
  1171. }
  1172. }
  1173. $result['list'] = $list;
  1174. $result['time'] = $time_data;
  1175. return $result;
  1176. }
  1177. /**根据查询用户充值金额
  1178. * @param array $where
  1179. * @return float|int
  1180. */
  1181. public function getRechargeMoneyByWhere(array $where, string $rechargeSumField, string $selectType, string $group = "")
  1182. {
  1183. switch ($selectType) {
  1184. case "sum" :
  1185. return $this->dao->getWhereSumField($where, $rechargeSumField);
  1186. case "group" :
  1187. return $this->dao->getGroupField($where, $rechargeSumField, $group);
  1188. }
  1189. }
  1190. /**
  1191. * 事业部/代理商订单
  1192. * @param $uid
  1193. * @return array
  1194. */
  1195. public function divisionOrder($uid)
  1196. {
  1197. /** @var UserServices $userService */
  1198. $userService = app()->make(UserServices::class);
  1199. /** @var StoreOrderServices $storeOrderServices */
  1200. $storeOrderServices = app()->make(StoreOrderServices::class);
  1201. $userInfo = $userService->getUserInfo($uid);
  1202. if (!$userInfo) {
  1203. throw new ApiException(100026);
  1204. }
  1205. $division_type = $userInfo['division_type'];
  1206. [$page, $limit] = $this->getPageValue();
  1207. $where = ['paid' => 1, 'type' => 1, 'pid' => 0];
  1208. if ($division_type == 1) {
  1209. $where = $where + ['division_id' => $uid];
  1210. } elseif ($division_type == 2) {
  1211. $where = $where + ['agent_id' => $uid];
  1212. }
  1213. $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']);
  1214. $result['count'] = $storeOrderServices->count($where);
  1215. $time_data = [];
  1216. if ($list) {
  1217. $uids = array_unique(array_column($list, 'uid'));
  1218. $userInfos = $userService->getColumn([['uid', 'in', $uids]], 'uid,avatar,nickname', 'uid');
  1219. foreach ($list as &$item) {
  1220. $item['avatar'] = $userInfos[$item['uid']]['avatar'] ?? '';
  1221. $item['nickname'] = $userInfos[$item['uid']]['nickname'] ?? '';
  1222. $item['time'] = $item['add_time'] ? date('Y-m-d H:i', $item['add_time']) : '';
  1223. $item['time_key'] = $item['add_time'] ? date('Y-m', $item['add_time']) : '';
  1224. $item['type'] = in_array($item['status'], [2, 3]) ? 'brokerage' : 'number';
  1225. if ($division_type == 1) {
  1226. $item['number'] = $item['division_brokerage'];
  1227. } elseif ($division_type == 2) {
  1228. $item['number'] = $item['agent_brokerage'];
  1229. }
  1230. }
  1231. $times = array_unique(array_column($list, 'time_key'));
  1232. $time_data = [];
  1233. $i = 0;
  1234. foreach ($times as $time) {
  1235. $time_data[$i]['time'] = $time;
  1236. $time_data[$i]['count'] = $storeOrderServices->getMonthCount($where + ['pid' => 0], $time);
  1237. $i++;
  1238. }
  1239. }
  1240. $result['list'] = $list;
  1241. $result['time'] = $time_data;
  1242. return $result;
  1243. }
  1244. }