UserBillServices.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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 app\services\user\level\UserLevelServices;
  16. use think\Exception;
  17. use think\exception\ValidateException;
  18. use crmeb\services\CacheService;
  19. use think\facade\Log;
  20. /**
  21. *
  22. * Class UserBillServices
  23. * @package app\services\user
  24. * @mixin UserBillDao
  25. */
  26. class UserBillServices extends BaseServices
  27. {
  28. /**
  29. * 用户记录模板
  30. * @var array[]
  31. */
  32. protected $incomeData = [
  33. 'pay_give_integral' => [
  34. 'title' => '购买商品赠送积分',
  35. 'category' => 'integral',
  36. 'type' => 'gain',
  37. 'mark' => '购买商品赠送{%num%}积分',
  38. 'status' => 1,
  39. 'pm' => 1
  40. ],
  41. 'order_give_integral' => [
  42. 'title' => '下单赠送积分',
  43. 'category' => 'integral',
  44. 'type' => 'gain',
  45. 'mark' => '下单赠送{%num%}积分',
  46. 'status' => 1,
  47. 'pm' => 1
  48. ],
  49. 'order_promotions_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. 'integral_refund' => [
  66. 'title' => '积分回退',
  67. 'category' => 'integral',
  68. 'type' => 'deduction',
  69. 'mark' => '购买商品失败,回退{%num%}积分',
  70. 'status' => 1,
  71. 'pm' => 0
  72. ],
  73. 'order_integral_refund' => [
  74. 'title' => '返还下单使用积分',
  75. 'category' => 'integral',
  76. 'type' => 'integral_refund',
  77. 'mark' => '购买商品失败,回退{%num%}积分',
  78. 'status' => 1,
  79. 'pm' => 1
  80. ],
  81. 'pay_product_integral_back' => [
  82. 'title' => '商品退积分',
  83. 'category' => 'integral',
  84. 'type' => 'pay_product_integral_back',
  85. 'mark' => '订单返还{%num%}积分',
  86. 'status' => 1,
  87. 'pm' => 1
  88. ],
  89. 'deduction' => [
  90. 'title' => '积分抵扣',
  91. 'category' => 'integral',
  92. 'type' => 'deduction',
  93. 'mark' => '购买商品使用{%number%}积分抵扣{%deductionPrice%}元',
  94. 'status' => 1,
  95. 'pm' => 0
  96. ],
  97. 'lottery_use_integral' => [
  98. 'title' => '参与抽奖使用积分',
  99. 'category' => 'integral',
  100. 'type' => 'lottery_use',
  101. 'mark' => '参与抽奖使用{%num%}积分',
  102. 'status' => 1,
  103. 'pm' => 0
  104. ],
  105. 'lottery_give_integral' => [
  106. 'title' => '抽奖中奖赠送积分',
  107. 'category' => 'integral',
  108. 'type' => 'lottery_add',
  109. 'mark' => '抽奖中奖赠送{%num%}积分',
  110. 'status' => 1,
  111. 'pm' => 1
  112. ],
  113. 'storeIntegral_use_integral' => [
  114. 'title' => '积分兑换商品',
  115. 'category' => 'integral',
  116. 'type' => 'storeIntegral_use',
  117. 'mark' => '积分商城兑换商品使用{%num%}积分',
  118. 'status' => 1,
  119. 'pm' => 0
  120. ],
  121. 'system_clear_integral' => [
  122. 'title' => '到期自动清除积分',
  123. 'category' => 'integral',
  124. 'type' => 'system_clear',
  125. 'mark' => '到期自动清除{%num%}积分',
  126. 'status' => 1,
  127. 'pm' => 0
  128. ],
  129. 'newcomer_give_integral' => [
  130. 'title' => '新人礼赠送积分',
  131. 'category' => 'integral',
  132. 'type' => 'newcomer_add',
  133. 'mark' => '新人礼赠送{%num%}积分',
  134. 'status' => 1,
  135. 'pm' => 1
  136. ],
  137. 'level_give_integral' => [
  138. 'title' => '会员卡激活赠送积分',
  139. 'category' => 'integral',
  140. 'type' => 'level_add',
  141. 'mark' => '会员卡激活赠送{%num%}积分',
  142. 'status' => 1,
  143. 'pm' => 1
  144. ],
  145. 'system_add_integral' => [
  146. 'title' => '系统增加积分',
  147. 'category' => 'integral',
  148. 'type' => 'system_add',
  149. 'mark' => '系统增加了{%num%}积分',
  150. 'status' => 1,
  151. 'pm' => 1
  152. ],
  153. 'system_sub_integral' => [
  154. 'title' => '系统减少积分',
  155. 'category' => 'integral',
  156. 'type' => 'system_sub',
  157. 'mark' => '系统扣除了{%num%}积分',
  158. 'status' => 1,
  159. 'pm' => 0
  160. ],
  161. 'extract_integral' => [
  162. 'title' => '积分出局',
  163. 'category' => 'energy',
  164. 'type' => 'extract_integral',
  165. 'mark' => '用户{%type%}出局,出局时积分价格{%price%},共出局{%integral_num%}积分,获得{%number%}能量',
  166. 'status' => 1,
  167. 'pm' => 1
  168. ],
  169. 'exchange' => [
  170. 'title' => '能量转换',
  171. 'category' => 'energy',
  172. 'type' => 'exchange',
  173. 'mark' => '{%mark%},{%number%}能量转换份额',
  174. 'status' => 1,
  175. 'pm' => 0
  176. ],
  177. 'exchange_fail' => [
  178. 'title' => '转换失败',
  179. 'category' => 'energy',
  180. 'type' => 'exchange_fail',
  181. 'mark' => '转换失败,退回能量{%num%}',
  182. 'status' => 1,
  183. 'pm' => 1
  184. ],
  185. 'brokerage_to_energy' => [
  186. 'title' => '佣金转换到能量',
  187. 'category' => 'energy',
  188. 'type' => 'exchange_to_energy',
  189. 'mark' => '佣金转换到能量{%num%}',
  190. 'status' => 1,
  191. 'pm' => 1
  192. ],
  193. ];
  194. /**
  195. * UserBillServices constructor.
  196. * @param UserBillDao $dao
  197. */
  198. public function __construct(UserBillDao $dao)
  199. {
  200. $this->dao = $dao;
  201. }
  202. /**
  203. * 获取用户记录总和
  204. * @param $uid
  205. * @param string $category
  206. * @param array $type
  207. * @return mixed
  208. */
  209. public function getRecordCount(int $uid, $category = 'now_money', $type = [], $time = '', $pm = false)
  210. {
  211. $where = [];
  212. $where['uid'] = $uid;
  213. $where['category'] = $category;
  214. $where['status'] = 1;
  215. if (is_string($type) && strlen(trim($type))) {
  216. $where['type'] = explode(',', $type);
  217. }
  218. if ($time) {
  219. $where['time'] = $time;
  220. }
  221. $where['pm'] = $pm ? 1 : 0;
  222. return $this->dao->getBillSumColumn($where);
  223. }
  224. /**
  225. * 获取积分列表
  226. * @param int $uid
  227. * @param array $where_time
  228. * @param string $field
  229. * @return array
  230. */
  231. public function getIntegralList(int $uid = 0, $where_time = [], string $field = '*')
  232. {
  233. [$page, $limit] = $this->getPageValue();
  234. $where = ['category' => 'integral'];
  235. if ($uid) $where['uid'] = $uid;
  236. if ($where_time) $where['add_time'] = $where_time;
  237. $list = $this->dao->getList($where, $field, $page, $limit);
  238. foreach ($list as &$item) {
  239. $item['number'] = intval($item['number']);
  240. $item['balance'] = intval($item['balance']);
  241. }
  242. $count = $this->dao->count($where);
  243. return compact('list', 'count');
  244. }
  245. /**
  246. * 获取积分列表
  247. * @param int $uid
  248. * @param array $where_time
  249. * @param string $field
  250. * @return array
  251. */
  252. public function getEnergyList(int $uid = 0, $where_time = [], string $field = '*')
  253. {
  254. [$page, $limit] = $this->getPageValue();
  255. $where = ['category' => 'energy'];
  256. if ($uid) $where['uid'] = $uid;
  257. if ($where_time) $where['add_time'] = $where_time;
  258. $list = $this->dao->getList($where, $field, $page, $limit);
  259. foreach ($list as &$item) {
  260. $item['number'] = intval($item['number']);
  261. $item['balance'] = intval($item['balance']);
  262. }
  263. $count = $this->dao->count($where);
  264. return compact('list', 'count');
  265. }
  266. /**
  267. * 获取签到列表
  268. * @param int $uid
  269. * @param array $where_time
  270. * @param string $field
  271. * @return array
  272. */
  273. public function getSignList(int $uid = 0, $where_time = [], string $field = '*')
  274. {
  275. [$page, $limit] = $this->getPageValue();
  276. $where = ['category' => 'integral', 'type' => 'sign'];
  277. if ($uid) $where['uid'] = $uid;
  278. if ($where_time) $where['add_time'] = $where_time;
  279. $list = $this->dao->getList($where, $field, $page, $limit);
  280. $count = $this->dao->count($where);
  281. return compact('list', 'count');
  282. }
  283. /**
  284. * 经验总数
  285. * @param int $uid
  286. * @param array $where_time
  287. * @return float
  288. */
  289. public function getExpSum(int $uid = 0, $where_time = [])
  290. {
  291. $where = ['category' => ['exp'], 'pm' => 1, 'status' => 1];
  292. if ($uid) $where['uid'] = $uid;
  293. if ($where_time) $where['time'] = $where_time;
  294. return $this->dao->getBillSum($where);
  295. }
  296. /**
  297. * 获取所有经验列表
  298. * @param int $uid
  299. * @param array $where_time
  300. * @param string $field
  301. * @return array
  302. */
  303. public function getExpList(int $uid = 0, $where_time = [], string $field = '*')
  304. {
  305. [$page, $limit] = $this->getPageValue();
  306. $where = ['category' => ['exp']];
  307. $where['status'] = 1;
  308. if ($uid) $where['uid'] = $uid;
  309. if ($where_time) $where['time'] = $where_time;
  310. $list = $this->dao->getList($where, $field, $page, $limit);
  311. $count = $this->dao->count($where);
  312. return compact('list', 'count');
  313. }
  314. /**
  315. * 记录积分变化
  316. * @param int $uid
  317. * @param string $type
  318. * @param array $data
  319. * @param int $pm
  320. * @return bool
  321. * @throws Exception
  322. */
  323. public function incomeIntegral(int $uid, string $type, array $data, int $pm = 1)
  324. {
  325. $data['uid'] = $uid;
  326. $data['category'] = 'integral';
  327. $data['type'] = $type;
  328. $data['pm'] = $pm;
  329. $data['status'] = 1;
  330. $data['add_time'] = time();
  331. if (!$this->dao->save($data))
  332. throw new Exception('增加记录失败');
  333. return true;
  334. }
  335. /**
  336. * 写入用户记录
  337. * @param string $type 写入类型
  338. * @param int $uid
  339. * @param int|string|array $number
  340. * @param int|string $balance
  341. * @param int $link_id
  342. * @return bool|mixed
  343. */
  344. public function income(string $type, int $uid, $number, $balance, $link_id = 0)
  345. {
  346. $data = $this->incomeData[$type] ?? null;
  347. if (!$data) {
  348. return true;
  349. }
  350. $data['uid'] = $uid;
  351. $data['balance'] = $balance ?? 0;
  352. $data['link_id'] = $link_id;
  353. if (is_array($number)) {
  354. $key = array_keys($number);
  355. $key = array_map(function ($item) {
  356. return '{%' . $item . '%}';
  357. }, $key);
  358. $value = array_values($number);
  359. $data['number'] = $number['number'] ?? 0;
  360. $data['mark'] = str_replace($key, $value, $data['mark']);
  361. } else {
  362. $data['number'] = $number;
  363. $data['mark'] = str_replace(['{%num%}'], $number, $data['mark']);
  364. }
  365. $data['add_time'] = time();
  366. if ((float)$data['number']) {
  367. return $this->dao->save($data);
  368. }
  369. return true;
  370. }
  371. /**
  372. * 邀请新用户增加经验
  373. * @param int $spreadUid
  374. */
  375. public function inviteUserIncExp(int $spreadUid)
  376. {
  377. if (!$spreadUid) {
  378. return false;
  379. }
  380. //用户等级是否开启
  381. if (!sys_config('member_func_status', 1)) {
  382. return false;
  383. }
  384. /** @var UserServices $userService */
  385. $userService = app()->make(UserServices::class);
  386. $spread_user = $userService->getUserInfo($spreadUid);
  387. if (!$spread_user) {
  388. return false;
  389. }
  390. $exp_num = sys_config('invite_user_exp', 0);
  391. if ($exp_num) {
  392. $userService->incField($spreadUid, 'exp', (int)$exp_num);
  393. $data = [];
  394. $data['uid'] = $spreadUid;
  395. $data['number'] = $exp_num;
  396. $data['category'] = 'exp';
  397. $data['type'] = 'invite_user';
  398. $data['title'] = $data['mark'] = '邀新奖励';
  399. $data['balance'] = (int)$spread_user['exp'];
  400. $data['pm'] = 1;
  401. $data['status'] = 1;
  402. $this->dao->save($data);
  403. }
  404. //检测会员等级
  405. try {
  406. /** @var UserLevelServices $levelServices */
  407. $levelServices = app()->make(UserLevelServices::class);
  408. //检测会员升级
  409. $levelServices->detection($spreadUid);
  410. } catch (\Throwable $e) {
  411. Log::error('会员等级升级失败,失败原因:' . $e->getMessage());
  412. }
  413. return true;
  414. }
  415. /**
  416. * 获取type
  417. * @param array $where
  418. * @param string $filed
  419. */
  420. public function getBillType(array $where)
  421. {
  422. return $this->dao->getType($where);
  423. }
  424. /**
  425. * 资金类型
  426. */
  427. public function bill_type()
  428. {
  429. $where = [];
  430. $where['not_type'] = ['gain', 'system_sub', 'deduction', 'sign'];
  431. $where['not_category'] = ['exp', 'integral'];
  432. return CacheService::get('user_type_list', function () use ($where) {
  433. return ['list' => $this->dao->getType($where)];
  434. }, 600);
  435. }
  436. /**
  437. * 记录分享次数
  438. * @param int $uid 用户uid
  439. * @param int $cd 冷却时间
  440. * @return Boolean
  441. * */
  442. public function setUserShare(int $uid, $cd = 300)
  443. {
  444. /** @var UserServices $userServices */
  445. $userServices = app()->make(UserServices::class);
  446. $user = $userServices->getUserInfo($uid);
  447. if (!$user) {
  448. throw new ValidateException('用户不存在!');
  449. }
  450. $cachename = 'Share_' . $uid;
  451. if (CacheService::get($cachename)) {
  452. return false;
  453. }
  454. $data = ['title' => '用户分享记录', 'uid' => $uid, 'category' => 'share', 'type' => 'share', 'number' => 0, 'link_id' => 0, 'balance' => 0, 'mark' => date('Y-m-d H:i:s', time()) . ':用户分享'];
  455. if (!$this->dao->save($data)) {
  456. throw new ValidateException('记录分享记录失败');
  457. }
  458. CacheService::set($cachename, 1, $cd);
  459. return true;
  460. }
  461. /**
  462. * 获取积分列表
  463. * @param array $where
  464. * @param string $field
  465. * @param int $limit
  466. * @return array
  467. */
  468. public function getPointList(array $where, string $field = '*', int $limit = 0)
  469. {
  470. $where_data = [];
  471. $where_data['category'] = 'integral';
  472. if (isset($where['uid']) && $where['uid'] != '') {
  473. $where_data['uid'] = $where['uid'];
  474. }
  475. if ($where['start_time'] != '' && $where['end_time'] != '') {
  476. $where_data['time'] = $where['start_time'] . ' - ' . $where['end_time'];
  477. }
  478. if (isset($where['type']) && $where['type'] != '') {
  479. $where_data['type'] = $where['type'];
  480. }
  481. if (isset($where['nickname']) && $where['nickname'] != '') {
  482. $where_data['like'] = $where['nickname'];
  483. }
  484. if (isset($where['excel']) && $where['excel'] != '') {
  485. $where_data['excel'] = $where['excel'];
  486. } else {
  487. $where_data['excel'] = 0;
  488. }
  489. if ($limit) {
  490. [$page] = $this->getPageValue();
  491. } else {
  492. [$page, $limit] = $this->getPageValue();
  493. }
  494. $list = $this->dao->getBillList($where_data, $field, $page, $limit);
  495. foreach ($list as &$item) {
  496. $item['nickname'] = $item['user']['nickname'] ?? '';
  497. $item['number'] = intval($item['number']);
  498. $item['balance'] = intval($item['balance']);
  499. unset($item['user']);
  500. }
  501. $count = $this->dao->count($where_data);
  502. return compact('list', 'count');
  503. }
  504. /**
  505. * 积分头部信息
  506. * @param array $where
  507. * @return array[]
  508. */
  509. public function getUserPointBadgelist(array $where)
  510. {
  511. $data = [];
  512. $where_data = [];
  513. $where_data['category'] = 'integral';
  514. if ($where['start_time'] != '' && $where['end_time'] != '') {
  515. $where_data['time'] = $where['start_time'] . ' - ' . $where['end_time'];
  516. }
  517. if (isset($where['nickname']) && $where['nickname'] != '') {
  518. $where_data['like'] = $where['nickname'];
  519. }
  520. $data['SumIntegral'] = intval($this->dao->getBillSumColumn($where_data + ['pm' => 1]));
  521. $where_data['type'] = 'sign';
  522. $data['CountSign'] = $this->dao->getUserSignPoint($where_data);
  523. $data['SumSign'] = intval($this->dao->getBillSumColumn($where_data));
  524. unset($where_data['type']);
  525. $data['SumDeductionIntegral'] = intval($this->dao->getBillSumColumn($where_data + ['pm' => 0]));
  526. return [
  527. [
  528. 'col' => 6,
  529. 'count' => $data['SumIntegral'],
  530. 'name' => '总积分(个)',
  531. ],
  532. [
  533. 'col' => 6,
  534. 'count' => $data['CountSign'],
  535. 'name' => '客户签到次数(次)',
  536. ],
  537. [
  538. 'col' => 6,
  539. 'count' => $data['SumSign'],
  540. 'name' => '签到送出积分(个)',
  541. ],
  542. [
  543. 'col' => 6,
  544. 'count' => $data['SumDeductionIntegral'],
  545. 'name' => '使用积分(个)',
  546. ],
  547. ];
  548. }
  549. /**
  550. * @param $uid
  551. * @param $type
  552. * @return array
  553. */
  554. public function getUserBillList(int $uid, int $type)
  555. {
  556. $where = [];
  557. $where['uid'] = $uid;
  558. $where['category'] = 'now_money';
  559. switch ((int)$type) {
  560. case 0:
  561. $where['type'] = ['recharge', 'pay_money', 'system_add', 'pay_product_refund', 'system_sub', 'pay_member', 'offline_scan', 'lottery_use', 'lottery_add'];
  562. break;
  563. case 1:
  564. $where['type'] = ['pay_money', 'pay_member', 'offline_scan', 'user_recharge_refund', 'lottery_use'];
  565. break;
  566. case 2:
  567. $where['type'] = ['recharge', 'system_add', 'lottery_add'];
  568. break;
  569. case 3:
  570. $where['type'] = ['brokerage', 'brokerage_user'];
  571. break;
  572. case 4:
  573. $where['type'] = ['extract'];
  574. break;
  575. }
  576. $field = 'FROM_UNIXTIME(add_time,"%Y-%m") as time,group_concat(id SEPARATOR ",") ids';
  577. [$page, $limit] = $this->getPageValue();
  578. $list = $this->dao->getUserBillListByGroup($where, $field, 'time', $page, $limit);
  579. $data = [];
  580. if ($list) {
  581. $listIds = array_column($list, 'ids');
  582. $ids = [];
  583. foreach ($listIds as $id) {
  584. $ids = array_merge($ids, explode(',', $id));
  585. }
  586. $info = $this->dao->getColumn([['id', 'in', $ids]], 'FROM_UNIXTIME(add_time,"%Y-%m-%d %H:%i") as add_time,title,number,pm', 'id');
  587. foreach ($list as $item) {
  588. $value['time'] = $item['time'];
  589. $id = explode(',', $item['ids']);
  590. array_multisort($id, SORT_DESC);
  591. $value['list'] = [];
  592. foreach ($id as $v) {
  593. if (isset($info[$v])) {
  594. $value['list'][] = $info[$v];
  595. }
  596. }
  597. array_push($data, $value);
  598. }
  599. }
  600. return $data;
  601. }
  602. }