UserBillServices.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  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, 'all_spread' => $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,staff_id,agent_id,division_id,staff_brokerage,agent_brokerage,division_brokerage'], $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. if ($item['division_id'] == $uid) {
  1154. $item['number'] = $item['division_brokerage'];
  1155. } elseif ($item['agent_id'] == $uid) {
  1156. $item['number'] = $item['agent_brokerage'];
  1157. } elseif ($item['staff_id'] == $uid) {
  1158. $item['number'] = $item['staff_brokerage'];
  1159. } else {
  1160. $item['number'] = $item['spread_uid'] == $uid ? $item['one_brokerage'] : $item['two_brokerage'];
  1161. }
  1162. $item['time'] = $item['add_time'] ? date('Y-m-d H:i', $item['add_time']) : '';
  1163. $item['time_key'] = $item['add_time'] ? date('Y-m', $item['add_time']) : '';
  1164. $item['type'] = in_array($item['status'], [2, 3]) ? 'brokerage' : 'number';
  1165. foreach ($item['split'] as $key => $items) {
  1166. $item['children'][] = [
  1167. 'order_id' => $items['order_id'],
  1168. 'number' => $item['spread_uid'] == $uid ? $items['one_brokerage'] : $items['two_brokerage'],
  1169. 'type' => in_array($item['status'], [2, 3]) ? 'brokerage' : 'number',
  1170. ];
  1171. unset($item['split'][$key]);
  1172. }
  1173. }
  1174. $times = array_unique(array_column($list, 'time_key'));
  1175. $time_data = [];
  1176. $i = 0;
  1177. foreach ($times as $time) {
  1178. $time_data[$i]['time'] = $time;
  1179. $time_data[$i]['count'] = $storeOrderServices->getMonthCount($where + ['pid' => 0], $time);
  1180. $i++;
  1181. }
  1182. }
  1183. $result['list'] = $list;
  1184. $result['time'] = $time_data;
  1185. return $result;
  1186. }
  1187. /**根据查询用户充值金额
  1188. * @param array $where
  1189. * @return float|int
  1190. */
  1191. public function getRechargeMoneyByWhere(array $where, string $rechargeSumField, string $selectType, string $group = "")
  1192. {
  1193. switch ($selectType) {
  1194. case "sum" :
  1195. return $this->dao->getWhereSumField($where, $rechargeSumField);
  1196. case "group" :
  1197. return $this->dao->getGroupField($where, $rechargeSumField, $group);
  1198. }
  1199. }
  1200. /**
  1201. * 事业部/代理商订单
  1202. * @param $uid
  1203. * @return array
  1204. */
  1205. public function divisionOrder($uid)
  1206. {
  1207. /** @var UserServices $userService */
  1208. $userService = app()->make(UserServices::class);
  1209. /** @var StoreOrderServices $storeOrderServices */
  1210. $storeOrderServices = app()->make(StoreOrderServices::class);
  1211. $userInfo = $userService->getUserInfo($uid);
  1212. if (!$userInfo) {
  1213. throw new ApiException(100026);
  1214. }
  1215. $division_type = $userInfo['division_type'];
  1216. [$page, $limit] = $this->getPageValue();
  1217. $where = ['paid' => 1, 'type' => 1, 'pid' => 0];
  1218. if ($division_type == 1) {
  1219. $where = $where + ['division_id' => $uid, 'division_brokerage_greater' => 0];
  1220. } elseif ($division_type == 2) {
  1221. $where = $where + ['agent_id' => $uid, 'agent_brokerage_greater' => 0];
  1222. }
  1223. $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']);
  1224. $result['count'] = $storeOrderServices->count($where);
  1225. $time_data = [];
  1226. if ($list) {
  1227. $uids = array_unique(array_column($list, 'uid'));
  1228. $userInfos = $userService->getColumn([['uid', 'in', $uids]], 'uid,avatar,nickname', 'uid');
  1229. foreach ($list as &$item) {
  1230. $item['avatar'] = $userInfos[$item['uid']]['avatar'] ?? '';
  1231. $item['nickname'] = $userInfos[$item['uid']]['nickname'] ?? '';
  1232. $item['time'] = $item['add_time'] ? date('Y-m-d H:i', $item['add_time']) : '';
  1233. $item['time_key'] = $item['add_time'] ? date('Y-m', $item['add_time']) : '';
  1234. $item['type'] = in_array($item['status'], [2, 3]) ? 'brokerage' : 'number';
  1235. if ($division_type == 1) {
  1236. $item['number'] = $item['division_brokerage'];
  1237. } elseif ($division_type == 2) {
  1238. $item['number'] = $item['agent_brokerage'];
  1239. }
  1240. }
  1241. $times = array_unique(array_column($list, 'time_key'));
  1242. $time_data = [];
  1243. $i = 0;
  1244. foreach ($times as $time) {
  1245. $time_data[$i]['time'] = $time;
  1246. $time_data[$i]['count'] = $storeOrderServices->getMonthCount($where + ['pid' => 0], $time);
  1247. $i++;
  1248. }
  1249. }
  1250. $result['list'] = $list;
  1251. $result['time'] = $time_data;
  1252. return $result;
  1253. }
  1254. }