UserBillServices.php 39 KB

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