UserBillServices.php 20 KB

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