UserBillServices.php 19 KB

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