UserBillServices.php 18 KB

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