UserBillServices.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. ];
  170. /**
  171. * UserBillServices constructor.
  172. * @param UserBillDao $dao
  173. */
  174. public function __construct(UserBillDao $dao)
  175. {
  176. $this->dao = $dao;
  177. }
  178. /**
  179. * 获取用户记录总和
  180. * @param $uid
  181. * @param string $category
  182. * @param array $type
  183. * @return mixed
  184. */
  185. public function getRecordCount(int $uid, $category = 'now_money', $type = [], $time = '', $pm = false)
  186. {
  187. $where = [];
  188. $where['uid'] = $uid;
  189. $where['category'] = $category;
  190. $where['status'] = 1;
  191. if (is_string($type) && strlen(trim($type))) {
  192. $where['type'] = explode(',', $type);
  193. }
  194. if ($time) {
  195. $where['time'] = $time;
  196. }
  197. $where['pm'] = $pm ? 1 : 0;
  198. return $this->dao->getBillSumColumn($where);
  199. }
  200. /**
  201. * 获取积分列表
  202. * @param int $uid
  203. * @param array $where_time
  204. * @param string $field
  205. * @return array
  206. */
  207. public function getIntegralList(int $uid = 0, $where_time = [], string $field = '*')
  208. {
  209. [$page, $limit] = $this->getPageValue();
  210. $where = ['category' => 'integral'];
  211. if ($uid) $where['uid'] = $uid;
  212. if ($where_time) $where['add_time'] = $where_time;
  213. $list = $this->dao->getList($where, $field, $page, $limit);
  214. foreach ($list as &$item) {
  215. $item['number'] = intval($item['number']);
  216. $item['balance'] = intval($item['balance']);
  217. }
  218. $count = $this->dao->count($where);
  219. return compact('list', 'count');
  220. }
  221. /**
  222. * 获取积分列表
  223. * @param int $uid
  224. * @param array $where_time
  225. * @param string $field
  226. * @return array
  227. */
  228. public function getEnergyList(int $uid = 0, $where_time = [], string $field = '*')
  229. {
  230. [$page, $limit] = $this->getPageValue();
  231. $where = ['category' => 'energy'];
  232. if ($uid) $where['uid'] = $uid;
  233. if ($where_time) $where['add_time'] = $where_time;
  234. $list = $this->dao->getList($where, $field, $page, $limit);
  235. foreach ($list as &$item) {
  236. $item['number'] = intval($item['number']);
  237. $item['balance'] = intval($item['balance']);
  238. }
  239. $count = $this->dao->count($where);
  240. return compact('list', 'count');
  241. }
  242. /**
  243. * 获取签到列表
  244. * @param int $uid
  245. * @param array $where_time
  246. * @param string $field
  247. * @return array
  248. */
  249. public function getSignList(int $uid = 0, $where_time = [], string $field = '*')
  250. {
  251. [$page, $limit] = $this->getPageValue();
  252. $where = ['category' => 'integral', 'type' => 'sign'];
  253. if ($uid) $where['uid'] = $uid;
  254. if ($where_time) $where['add_time'] = $where_time;
  255. $list = $this->dao->getList($where, $field, $page, $limit);
  256. $count = $this->dao->count($where);
  257. return compact('list', 'count');
  258. }
  259. /**
  260. * 经验总数
  261. * @param int $uid
  262. * @param array $where_time
  263. * @return float
  264. */
  265. public function getExpSum(int $uid = 0, $where_time = [])
  266. {
  267. $where = ['category' => ['exp'], 'pm' => 1, 'status' => 1];
  268. if ($uid) $where['uid'] = $uid;
  269. if ($where_time) $where['time'] = $where_time;
  270. return $this->dao->getBillSum($where);
  271. }
  272. /**
  273. * 获取所有经验列表
  274. * @param int $uid
  275. * @param array $where_time
  276. * @param string $field
  277. * @return array
  278. */
  279. public function getExpList(int $uid = 0, $where_time = [], string $field = '*')
  280. {
  281. [$page, $limit] = $this->getPageValue();
  282. $where = ['category' => ['exp']];
  283. $where['status'] = 1;
  284. if ($uid) $where['uid'] = $uid;
  285. if ($where_time) $where['time'] = $where_time;
  286. $list = $this->dao->getList($where, $field, $page, $limit);
  287. $count = $this->dao->count($where);
  288. return compact('list', 'count');
  289. }
  290. /**
  291. * 记录积分变化
  292. * @param int $uid
  293. * @param string $type
  294. * @param array $data
  295. * @param int $pm
  296. * @return bool
  297. * @throws Exception
  298. */
  299. public function incomeIntegral(int $uid, string $type, array $data, int $pm = 1)
  300. {
  301. $data['uid'] = $uid;
  302. $data['category'] = 'integral';
  303. $data['type'] = $type;
  304. $data['pm'] = $pm;
  305. $data['status'] = 1;
  306. $data['add_time'] = time();
  307. if (!$this->dao->save($data))
  308. throw new Exception('增加记录失败');
  309. return true;
  310. }
  311. /**
  312. * 写入用户记录
  313. * @param string $type 写入类型
  314. * @param int $uid
  315. * @param int|string|array $number
  316. * @param int|string $balance
  317. * @param int $link_id
  318. * @return bool|mixed
  319. */
  320. public function income(string $type, int $uid, $number, $balance, $link_id = 0)
  321. {
  322. $data = $this->incomeData[$type] ?? null;
  323. if (!$data) {
  324. return true;
  325. }
  326. $data['uid'] = $uid;
  327. $data['balance'] = $balance ?? 0;
  328. $data['link_id'] = $link_id;
  329. if (is_array($number)) {
  330. $key = array_keys($number);
  331. $key = array_map(function ($item) {
  332. return '{%' . $item . '%}';
  333. }, $key);
  334. $value = array_values($number);
  335. $data['number'] = $number['number'] ?? 0;
  336. $data['mark'] = str_replace($key, $value, $data['mark']);
  337. } else {
  338. $data['number'] = $number;
  339. $data['mark'] = str_replace(['{%num%}'], $number, $data['mark']);
  340. }
  341. $data['add_time'] = time();
  342. if ((float)$data['number']) {
  343. return $this->dao->save($data);
  344. }
  345. return true;
  346. }
  347. /**
  348. * 邀请新用户增加经验
  349. * @param int $spreadUid
  350. */
  351. public function inviteUserIncExp(int $spreadUid)
  352. {
  353. if (!$spreadUid) {
  354. return false;
  355. }
  356. //用户等级是否开启
  357. if (!sys_config('member_func_status', 1)) {
  358. return false;
  359. }
  360. /** @var UserServices $userService */
  361. $userService = app()->make(UserServices::class);
  362. $spread_user = $userService->getUserInfo($spreadUid);
  363. if (!$spread_user) {
  364. return false;
  365. }
  366. $exp_num = sys_config('invite_user_exp', 0);
  367. if ($exp_num) {
  368. $userService->incField($spreadUid, 'exp', (int)$exp_num);
  369. $data = [];
  370. $data['uid'] = $spreadUid;
  371. $data['number'] = $exp_num;
  372. $data['category'] = 'exp';
  373. $data['type'] = 'invite_user';
  374. $data['title'] = $data['mark'] = '邀新奖励';
  375. $data['balance'] = (int)$spread_user['exp'];
  376. $data['pm'] = 1;
  377. $data['status'] = 1;
  378. $this->dao->save($data);
  379. }
  380. //检测会员等级
  381. try {
  382. /** @var UserLevelServices $levelServices */
  383. $levelServices = app()->make(UserLevelServices::class);
  384. //检测会员升级
  385. $levelServices->detection($spreadUid);
  386. } catch (\Throwable $e) {
  387. Log::error('会员等级升级失败,失败原因:' . $e->getMessage());
  388. }
  389. return true;
  390. }
  391. /**
  392. * 获取type
  393. * @param array $where
  394. * @param string $filed
  395. */
  396. public function getBillType(array $where)
  397. {
  398. return $this->dao->getType($where);
  399. }
  400. /**
  401. * 资金类型
  402. */
  403. public function bill_type()
  404. {
  405. $where = [];
  406. $where['not_type'] = ['gain', 'system_sub', 'deduction', 'sign'];
  407. $where['not_category'] = ['exp', 'integral'];
  408. return CacheService::get('user_type_list', function () use ($where) {
  409. return ['list' => $this->dao->getType($where)];
  410. }, 600);
  411. }
  412. /**
  413. * 记录分享次数
  414. * @param int $uid 用户uid
  415. * @param int $cd 冷却时间
  416. * @return Boolean
  417. * */
  418. public function setUserShare(int $uid, $cd = 300)
  419. {
  420. /** @var UserServices $userServices */
  421. $userServices = app()->make(UserServices::class);
  422. $user = $userServices->getUserInfo($uid);
  423. if (!$user) {
  424. throw new ValidateException('用户不存在!');
  425. }
  426. $cachename = 'Share_' . $uid;
  427. if (CacheService::get($cachename)) {
  428. return false;
  429. }
  430. $data = ['title' => '用户分享记录', 'uid' => $uid, 'category' => 'share', 'type' => 'share', 'number' => 0, 'link_id' => 0, 'balance' => 0, 'mark' => date('Y-m-d H:i:s', time()) . ':用户分享'];
  431. if (!$this->dao->save($data)) {
  432. throw new ValidateException('记录分享记录失败');
  433. }
  434. CacheService::set($cachename, 1, $cd);
  435. return true;
  436. }
  437. /**
  438. * 获取积分列表
  439. * @param array $where
  440. * @param string $field
  441. * @param int $limit
  442. * @return array
  443. */
  444. public function getPointList(array $where, string $field = '*', int $limit = 0)
  445. {
  446. $where_data = [];
  447. $where_data['category'] = 'integral';
  448. if (isset($where['uid']) && $where['uid'] != '') {
  449. $where_data['uid'] = $where['uid'];
  450. }
  451. if ($where['start_time'] != '' && $where['end_time'] != '') {
  452. $where_data['time'] = $where['start_time'] . ' - ' . $where['end_time'];
  453. }
  454. if (isset($where['type']) && $where['type'] != '') {
  455. $where_data['type'] = $where['type'];
  456. }
  457. if (isset($where['nickname']) && $where['nickname'] != '') {
  458. $where_data['like'] = $where['nickname'];
  459. }
  460. if (isset($where['excel']) && $where['excel'] != '') {
  461. $where_data['excel'] = $where['excel'];
  462. } else {
  463. $where_data['excel'] = 0;
  464. }
  465. if ($limit) {
  466. [$page] = $this->getPageValue();
  467. } else {
  468. [$page, $limit] = $this->getPageValue();
  469. }
  470. $list = $this->dao->getBillList($where_data, $field, $page, $limit);
  471. foreach ($list as &$item) {
  472. $item['nickname'] = $item['user']['nickname'] ?? '';
  473. $item['number'] = intval($item['number']);
  474. $item['balance'] = intval($item['balance']);
  475. unset($item['user']);
  476. }
  477. $count = $this->dao->count($where_data);
  478. return compact('list', 'count');
  479. }
  480. /**
  481. * 积分头部信息
  482. * @param array $where
  483. * @return array[]
  484. */
  485. public function getUserPointBadgelist(array $where)
  486. {
  487. $data = [];
  488. $where_data = [];
  489. $where_data['category'] = 'integral';
  490. if ($where['start_time'] != '' && $where['end_time'] != '') {
  491. $where_data['time'] = $where['start_time'] . ' - ' . $where['end_time'];
  492. }
  493. if (isset($where['nickname']) && $where['nickname'] != '') {
  494. $where_data['like'] = $where['nickname'];
  495. }
  496. $data['SumIntegral'] = intval($this->dao->getBillSumColumn($where_data + ['pm' => 1]));
  497. $where_data['type'] = 'sign';
  498. $data['CountSign'] = $this->dao->getUserSignPoint($where_data);
  499. $data['SumSign'] = intval($this->dao->getBillSumColumn($where_data));
  500. unset($where_data['type']);
  501. $data['SumDeductionIntegral'] = intval($this->dao->getBillSumColumn($where_data + ['pm' => 0]));
  502. return [
  503. [
  504. 'col' => 6,
  505. 'count' => $data['SumIntegral'],
  506. 'name' => '总积分(个)',
  507. ],
  508. [
  509. 'col' => 6,
  510. 'count' => $data['CountSign'],
  511. 'name' => '客户签到次数(次)',
  512. ],
  513. [
  514. 'col' => 6,
  515. 'count' => $data['SumSign'],
  516. 'name' => '签到送出积分(个)',
  517. ],
  518. [
  519. 'col' => 6,
  520. 'count' => $data['SumDeductionIntegral'],
  521. 'name' => '使用积分(个)',
  522. ],
  523. ];
  524. }
  525. /**
  526. * @param $uid
  527. * @param $type
  528. * @return array
  529. */
  530. public function getUserBillList(int $uid, int $type)
  531. {
  532. $where = [];
  533. $where['uid'] = $uid;
  534. $where['category'] = 'now_money';
  535. switch ((int)$type) {
  536. case 0:
  537. $where['type'] = ['recharge', 'pay_money', 'system_add', 'pay_product_refund', 'system_sub', 'pay_member', 'offline_scan', 'lottery_use', 'lottery_add'];
  538. break;
  539. case 1:
  540. $where['type'] = ['pay_money', 'pay_member', 'offline_scan', 'user_recharge_refund', 'lottery_use'];
  541. break;
  542. case 2:
  543. $where['type'] = ['recharge', 'system_add', 'lottery_add'];
  544. break;
  545. case 3:
  546. $where['type'] = ['brokerage', 'brokerage_user'];
  547. break;
  548. case 4:
  549. $where['type'] = ['extract'];
  550. break;
  551. }
  552. $field = 'FROM_UNIXTIME(add_time,"%Y-%m") as time,group_concat(id SEPARATOR ",") ids';
  553. [$page, $limit] = $this->getPageValue();
  554. $list = $this->dao->getUserBillListByGroup($where, $field, 'time', $page, $limit);
  555. $data = [];
  556. if ($list) {
  557. $listIds = array_column($list, 'ids');
  558. $ids = [];
  559. foreach ($listIds as $id) {
  560. $ids = array_merge($ids, explode(',', $id));
  561. }
  562. $info = $this->dao->getColumn([['id', 'in', $ids]], 'FROM_UNIXTIME(add_time,"%Y-%m-%d %H:%i") as add_time,title,number,pm', 'id');
  563. foreach ($list as $item) {
  564. $value['time'] = $item['time'];
  565. $id = explode(',', $item['ids']);
  566. array_multisort($id, SORT_DESC);
  567. $value['list'] = [];
  568. foreach ($id as $v) {
  569. if (isset($info[$v])) {
  570. $value['list'][] = $info[$v];
  571. }
  572. }
  573. array_push($data, $value);
  574. }
  575. }
  576. return $data;
  577. }
  578. }