UserMoneyServices.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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\dao\user\UserMoneyDao;
  14. use app\services\BaseServices;
  15. use crmeb\services\CacheService;
  16. use crmeb\exceptions\AdminException;
  17. /**
  18. * 用户余额
  19. * Class UserMoneyServices
  20. * @package app\services\user
  21. * @mixin UserMoneyDao
  22. */
  23. class UserMoneyServices extends BaseServices
  24. {
  25. /**
  26. * 用户记录模板
  27. * @var array[]
  28. */
  29. protected $incomeData = [
  30. 'pay_product' => [
  31. 'title' => '余额支付购买商品',
  32. 'type' => 'pay_product',
  33. 'mark' => '余额支付{%num%}元购买商品',
  34. 'status' => 1,
  35. 'pm' => 0
  36. ],
  37. 'pay_product_refund' => [
  38. 'title' => '商品退款',
  39. 'type' => 'pay_product_refund',
  40. 'mark' => '订单余额退款{%num%}元',
  41. 'status' => 1,
  42. 'pm' => 1
  43. ],
  44. 'system_add' => [
  45. 'title' => '系统增加余额',
  46. 'type' => 'system_add',
  47. 'mark' => '系统增加{%num%}余额',
  48. 'status' => 1,
  49. 'pm' => 1
  50. ],
  51. 'system_sub' => [
  52. 'title' => '系统减少余额',
  53. 'type' => 'system_sub',
  54. 'mark' => '系统扣除了{%num%}余额',
  55. 'status' => 1,
  56. 'pm' => 0
  57. ],
  58. 'user_recharge' => [
  59. 'title' => '用户充值余额',
  60. 'type' => 'recharge',
  61. 'mark' => '成功充值余额{%price%}元,赠送{%give_price%}元',
  62. 'status' => 1,
  63. 'pm' => 1
  64. ],
  65. 'user_recharge_refund' => [
  66. 'title' => '用户充值退款',
  67. 'type' => 'recharge_refund',
  68. 'mark' => '退款扣除用户余额{%num%}元',
  69. 'status' => 1,
  70. 'pm' => 0
  71. ],
  72. 'brokerage_to_nowMoney' => [
  73. 'title' => '佣金提现到余额',
  74. 'type' => 'extract',
  75. 'mark' => '佣金提现到余额{%num%}元',
  76. 'status' => 1,
  77. 'pm' => 1
  78. ],
  79. 'lottery_use_money' => [
  80. 'title' => '参与抽奖使用余额',
  81. 'type' => 'lottery_use',
  82. 'mark' => '参与抽奖使用{%num%}余额',
  83. 'status' => 1,
  84. 'pm' => 0
  85. ],
  86. 'lottery_give_money' => [
  87. 'title' => '抽奖中奖赠送余额',
  88. 'type' => 'lottery_add',
  89. 'mark' => '抽奖中奖赠送{%num%}余额',
  90. 'status' => 1,
  91. 'pm' => 1
  92. ],
  93. 'newcomer_give_money' => [
  94. 'title' => '新人礼赠送余额',
  95. 'type' => 'newcomer_add',
  96. 'mark' => '新人礼赠送{%num%}余额',
  97. 'status' => 1,
  98. 'pm' => 1
  99. ],
  100. 'level_give_money' => [
  101. 'title' => '会员卡激活赠送余额',
  102. 'type' => 'level_add',
  103. 'mark' => '会员卡激活赠送{%num%}余额',
  104. 'status' => 1,
  105. 'pm' => 1
  106. ],
  107. 'pay_integral_product' => [
  108. 'title' => '余额支付购买积分商品',
  109. 'type' => 'pay_integral_product',
  110. 'mark' => '余额支付{%num%}元购买积分商品',
  111. 'status' => 1,
  112. 'pm' => 0
  113. ],
  114. 'extract_integral' => [
  115. 'title' => '积分出局',
  116. 'type' => 'extract_integral',
  117. 'mark' => '用户{%type%}出局,出局时积分价格{%price%},共出局{%integral_num%}积分,获得余额{%number%}元',
  118. 'status' => 1,
  119. 'pm' => 1
  120. ],
  121. 'extract_integral_static' => [
  122. 'title' => '参考分提取余额',
  123. 'type' => 'static_money',
  124. 'mark' => '{mark},获得余额{%number%}元',
  125. 'status' => 1,
  126. 'pm' => 1
  127. ],
  128. ];
  129. /**
  130. * 类型名称
  131. * @var string[]
  132. */
  133. protected $typeName = [
  134. 'pay_product' => '商城购物',
  135. 'pay_product_refund' => '商城购物退款',
  136. 'system_add' => '系统充值',
  137. 'system_sub' => '系统扣除',
  138. 'recharge' => '用户充值',
  139. 'recharge_refund' => '用户充值退款',
  140. 'extract' => '佣金提现充值',
  141. 'lottery_use' => '抽奖使用',
  142. 'lottery_add' => '抽奖中奖充值',
  143. 'newcomer_add' => '新人礼赠送充值',
  144. 'level_add' => '会员卡激活赠送充值'
  145. ];
  146. /**
  147. * UserMoneyServices constructor.
  148. * @param UserMoneyDao $dao
  149. */
  150. public function __construct(UserMoneyDao $dao)
  151. {
  152. $this->dao = $dao;
  153. }
  154. /**
  155. * 获取资金列表
  156. * @param array $where
  157. * @param string $field
  158. * @param int $limit
  159. * @return array
  160. */
  161. public function getMoneyList(array $where, string $field = '*', int $limit = 0)
  162. {
  163. $where_data = [];
  164. if (isset($where['uid']) && $where['uid'] != '') {
  165. $where_data['uid'] = $where['uid'];
  166. }
  167. if ($where['start_time'] != '' && $where['end_time'] != '') {
  168. $where_data['time'] = str_replace('-', '/', $where['start_time']) . ' - ' . str_replace('-', '/', $where['end_time']);
  169. }
  170. if (isset($where['type']) && $where['type'] != '') {
  171. $where_data['type'] = $where['type'];
  172. }
  173. if (isset($where['nickname']) && $where['nickname'] != '') {
  174. $where_data['like'] = $where['nickname'];
  175. }
  176. if (isset($where['excel']) && $where['excel'] != '') {
  177. $where_data['excel'] = $where['excel'];
  178. } else {
  179. $where_data['excel'] = 0;
  180. }
  181. if ($limit) {
  182. [$page] = $this->getPageValue();
  183. } else {
  184. [$page, $limit] = $this->getPageValue();
  185. }
  186. $data = $this->dao->getList($where_data, $field, $page, $limit, [
  187. 'user' => function ($query) {
  188. $query->field('uid,nickname');
  189. }]);
  190. foreach ($data as &$item) {
  191. $item['nickname'] = $item['user']['nickname'] ?? '';
  192. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
  193. unset($item['user']);
  194. }
  195. $count = $this->dao->count($where_data);
  196. return compact('data', 'count');
  197. }
  198. /**
  199. * 用户|所有资金变动列表
  200. * @param int $uid
  201. * @param array $where_time
  202. * @param string $field
  203. * @return array
  204. */
  205. public function getUserMoneyList(int $uid = 0, $where_time = [], string $field = '*')
  206. {
  207. [$page, $limit] = $this->getPageValue();
  208. $where = [];
  209. if ($uid) $where['uid'] = $uid;
  210. if ($where_time) $where['add_time'] = $where_time;
  211. $list = $this->dao->getList($where, $field, $page, $limit);
  212. $count = $this->dao->count($where);
  213. foreach ($list as &$item) {
  214. $value = array_filter($this->incomeData, function ($value) use ($item) {
  215. if ($item['type'] == $value['type']) {
  216. return $item['title'];
  217. }
  218. });
  219. $item['type_title'] = $value[$item['type']]['title'] ?? '未知类型';
  220. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
  221. }
  222. return compact('list', 'count');
  223. }
  224. /**
  225. * 获取用户的充值总数
  226. * @param int $uid
  227. * @return float
  228. */
  229. public function getRechargeSum(int $uid = 0, $time = [])
  230. {
  231. $where = ['uid' => $uid, 'pm' => 1, 'status' => 1, 'type' => ['system_add', 'recharge', 'extract', 'lottery_add', 'newcomer_add', 'level_add']];
  232. if ($time) $where['add_time'] = $time;
  233. return $this->dao->sum($where, 'number', true);
  234. }
  235. /**
  236. * 用户|所有充值列表
  237. * @param int $uid
  238. * @param string $field
  239. * @return array
  240. */
  241. public function getRechargeList(int $uid = 0, $where_time = [], string $field = '*')
  242. {
  243. [$page, $limit] = $this->getPageValue();
  244. $where = [];
  245. if ($uid) $where['uid'] = $uid;
  246. if ($where_time) $where['add_time'] = $where_time;
  247. $list = $this->dao->getList($where, $field, $page, $limit);
  248. $count = $this->dao->count($where);
  249. return compact('list', 'count');
  250. }
  251. /**
  252. * 写入用户记录
  253. * @param string $type 写入类型
  254. * @param int $uid
  255. * @param int|string|array $number
  256. * @param int|string $balance
  257. * @param int $link_id
  258. * @return bool|mixed
  259. */
  260. public function income(string $type, int $uid, $number, $balance, $link_id = 0)
  261. {
  262. $data = $this->incomeData[$type] ?? null;
  263. if (!$data) {
  264. return true;
  265. }
  266. $data['uid'] = $uid;
  267. $data['balance'] = $balance ?? 0;
  268. $data['link_id'] = $link_id;
  269. if (is_array($number)) {
  270. $key = array_keys($number);
  271. $key = array_map(function ($item) {
  272. return '{%' . $item . '%}';
  273. }, $key);
  274. $value = array_values($number);
  275. $data['number'] = $number['number'] ?? 0;
  276. $data['mark'] = str_replace($key, $value, $data['mark']);
  277. } else {
  278. $data['number'] = $number;
  279. $data['mark'] = str_replace(['{%num%}'], $number, $data['mark']);
  280. }
  281. $data['add_time'] = time();
  282. if ((float)$data['number']) {
  283. return $this->dao->save($data);
  284. }
  285. return true;
  286. }
  287. /**
  288. * 资金类型
  289. * @return bool|mixed|null
  290. */
  291. public function bill_type()
  292. {
  293. return CacheService::get('user_money_type_list', function () {
  294. return ['list' => $this->dao->getMoneyType([])];
  295. }, 600);
  296. }
  297. /**
  298. * 用户余额记录列表
  299. * @param int $uid
  300. * @param int $type
  301. * @param array $data
  302. * @return array
  303. */
  304. public function userMoneyList(int $uid, int $type, array $data = [])
  305. {
  306. $where = [];
  307. $where['uid'] = $uid;
  308. switch ($type) {
  309. case 1:
  310. $where['pm'] = 0;
  311. break;
  312. case 2:
  313. $where['pm'] = 1;
  314. break;
  315. case 0:
  316. default:
  317. break;
  318. }
  319. [$page, $limit] = $this->getPageValue();
  320. if ((isset($data['start']) && $data['start']) || (isset($data['stop']) && $data['stop'])) {
  321. $where['time'] = [$data['start'], $data['stop']];
  322. }
  323. $list = $this->dao->getList($where, '*', $page, $limit);
  324. $count = $this->dao->count($where);
  325. $times = [];
  326. if ($list) {
  327. $typeName = $this->typeName;
  328. foreach ($list as &$item) {
  329. $item['time_key'] = $item['time'] = $item['add_time'] ? date('Y-m', (int)$item['add_time']) : '';
  330. $item['day'] = $item['add_time'] ? date('Y-m-d', (int)$item['add_time']) : '';
  331. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i', (int)$item['add_time']) : '';
  332. $item['type_name'] = $typeName[$item['type'] ?? ''] ?? '未知类型';
  333. }
  334. $times = array_merge(array_unique(array_column($list, 'time_key')));
  335. }
  336. return ['count' => $count, 'list' => $list, 'time' => $times];
  337. }
  338. /**
  339. * 根据查询用户充值金额
  340. * @param array $where
  341. * @param string $rechargeSumField
  342. * @param string $selectType
  343. * @param string $group
  344. * @return float|mixed
  345. */
  346. public function getRechargeMoneyByWhere(array $where, string $rechargeSumField, string $selectType, string $group = "")
  347. {
  348. switch ($selectType) {
  349. case "sum" :
  350. return $this->dao->getWhereSumField($where, $rechargeSumField);
  351. case "group" :
  352. return $this->dao->getGroupField($where, $rechargeSumField, $group);
  353. }
  354. }
  355. /**
  356. * 新人礼赠送余额
  357. * @param int $uid
  358. * @return bool
  359. */
  360. public function newcomerGiveMoney(int $uid)
  361. {
  362. if (!sys_config('newcomer_status')) {
  363. return false;
  364. }
  365. $status = sys_config('register_money_status');
  366. if (!$status) {//未开启
  367. return true;
  368. }
  369. $money = (int)sys_config('register_give_money', []);
  370. if (!$money) {
  371. return true;
  372. }
  373. /** @var UserServices $userServices */
  374. $userServices = app()->make(UserServices::class);
  375. $userInfo = $userServices->getUserInfo($uid);
  376. if (!$userInfo) {
  377. return true;
  378. }
  379. $balance = bcadd((string)$userInfo['now_money'], (string)$money);
  380. $this->income('newcomer_give_money', $uid, $money, $balance);
  381. $userServices->update($uid, ['now_money' => $balance]);
  382. return true;
  383. }
  384. /**
  385. * 会员卡激活赠送余额
  386. * @param int $uid
  387. * @return bool
  388. */
  389. public function levelGiveMoney(int $uid)
  390. {
  391. $status = sys_config('level_activate_status');
  392. if (!$status) {//是否需要激活
  393. return true;
  394. }
  395. $status = sys_config('level_money_status');
  396. if (!$status) {//未开启
  397. return true;
  398. }
  399. $money = (int)sys_config('level_give_money', []);
  400. if (!$money) {
  401. return true;
  402. }
  403. /** @var UserServices $userServices */
  404. $userServices = app()->make(UserServices::class);
  405. $userInfo = $userServices->getUserInfo($uid);
  406. if (!$userInfo) {
  407. return true;
  408. }
  409. $balance = bcadd((string)$userInfo['now_money'], (string)$money);
  410. $this->income('level_give_money', $uid, $money, $balance);
  411. $userServices->update($uid, ['now_money' => $balance]);
  412. return true;
  413. }
  414. /**
  415. * 余额统计基础
  416. * @param $where
  417. * @return array
  418. */
  419. public function getBasic($where)
  420. {
  421. /** @var UserServices $userServices */
  422. $userServices = app()->make(UserServices::class);
  423. $data['now_balance'] = $userServices->sum(['status' => 1], 'now_money', true);
  424. $data['add_balance'] = $this->dao->sum(['pm' => 1], 'number', true);
  425. $data['sub_balance'] = $this->dao->sum(['pm' => 0], 'number', true);
  426. return $data;
  427. }
  428. /**
  429. * 余额趋势
  430. * @param $where
  431. * @return array
  432. */
  433. public function getTrend($where)
  434. {
  435. $time = explode('-', $where['time']);
  436. if (count($time) != 2) throw new AdminException('参数错误');
  437. $dayCount = (strtotime($time[1]) - strtotime($time[0])) / 86400 + 1;
  438. $data = [];
  439. if ($dayCount == 1) {
  440. $data = $this->trend($time, 0);
  441. } elseif ($dayCount > 1 && $dayCount <= 31) {
  442. $data = $this->trend($time, 1);
  443. } elseif ($dayCount > 31 && $dayCount <= 92) {
  444. $data = $this->trend($time, 3);
  445. } elseif ($dayCount > 92) {
  446. $data = $this->trend($time, 30);
  447. }
  448. return $data;
  449. }
  450. /**
  451. * 余额趋势
  452. * @param $time
  453. * @param $num
  454. * @param false $excel
  455. * @return array
  456. */
  457. public function trend($time, $num, $excel = false)
  458. {
  459. if ($num == 0) {
  460. $xAxis = ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23'];
  461. $timeType = '%H';
  462. } elseif ($num != 0) {
  463. $dt_start = strtotime($time[0]);
  464. $dt_end = strtotime($time[1]);
  465. while ($dt_start <= $dt_end) {
  466. if ($num == 30) {
  467. $xAxis[] = date('Y-m', $dt_start);
  468. $dt_start = strtotime("+1 month", $dt_start);
  469. $timeType = '%Y-%m';
  470. } else {
  471. $xAxis[] = date('m-d', $dt_start);
  472. $dt_start = strtotime("+$num day", $dt_start);
  473. $timeType = '%m-%d';
  474. }
  475. }
  476. }
  477. $time[1] = date("Y-m-d", strtotime("+1 day", strtotime($time[1])));
  478. $point_add = array_column($this->dao->getBalanceTrend($time, $timeType, 'add_time', 'sum(number)', 'add'), 'num', 'days');
  479. $point_sub = array_column($this->dao->getBalanceTrend($time, $timeType, 'add_time', 'sum(number)', 'sub'), 'num', 'days');
  480. $data = $series = [];
  481. foreach ($xAxis as $item) {
  482. $data['余额积累'][] = isset($point_add[$item]) ? floatval($point_add[$item]) : 0;
  483. $data['余额消耗'][] = isset($point_sub[$item]) ? floatval($point_sub[$item]) : 0;
  484. }
  485. foreach ($data as $key => $item) {
  486. $series[] = [
  487. 'name' => $key,
  488. 'data' => $item,
  489. 'type' => 'line',
  490. ];
  491. }
  492. return compact('xAxis', 'series');
  493. }
  494. /**
  495. * 余额来源
  496. * @param $where
  497. * @return array
  498. */
  499. public function getChannel($where)
  500. {
  501. $bing_xdata = ['系统增加', '用户充值', '佣金提现', '抽奖赠送', '商品退款'];
  502. $color = ['#64a1f4', '#3edeb5', '#70869f', '#ffc653', '#fc7d6a'];
  503. $data = ['system_add', 'recharge', 'extract', 'lottery_add', 'pay_product_refund'];
  504. $bing_data = [];
  505. foreach ($data as $key => $item) {
  506. $bing_data[] = [
  507. 'name' => $bing_xdata[$key],
  508. 'value' => $this->dao->sum(['pm' => 1, 'type' => $item, 'time' => $where['time']], 'number', true),
  509. 'itemStyle' => ['color' => $color[$key]]
  510. ];
  511. }
  512. $list = [];
  513. $count = array_sum(array_column($bing_data, 'value'));
  514. foreach ($bing_data as $key => $item) {
  515. $list[] = [
  516. 'name' => $item['name'],
  517. 'value' => $item['value'],
  518. 'percent' => $count != 0 ? bcmul((string)bcdiv((string)$item['value'], (string)$count, 4), '100', 2) : 0,
  519. ];
  520. }
  521. array_multisort(array_column($list, 'value'), SORT_DESC, $list);
  522. return compact('bing_xdata', 'bing_data', 'list');
  523. }
  524. /**
  525. * 余额类型
  526. * @param $where
  527. * @return array
  528. */
  529. public function getType($where)
  530. {
  531. $bing_xdata = ['系统减少', '充值退款', '购买商品'];
  532. $color = ['#64a1f4', '#3edeb5', '#70869f'];
  533. $data = ['system_sub', 'recharge_refund', 'pay_product'];
  534. $bing_data = [];
  535. foreach ($data as $key => $item) {
  536. $bing_data[] = [
  537. 'name' => $bing_xdata[$key],
  538. 'value' => $this->dao->sum(['pm' => 0, 'type' => $item, 'time' => $where['time']], 'number', true),
  539. 'itemStyle' => ['color' => $color[$key]]
  540. ];
  541. }
  542. $list = [];
  543. $count = array_sum(array_column($bing_data, 'value'));
  544. foreach ($bing_data as $key => $item) {
  545. $list[] = [
  546. 'name' => $item['name'],
  547. 'value' => $item['value'],
  548. 'percent' => $count != 0 ? bcmul((string)bcdiv((string)$item['value'], (string)$count, 4), '100', 2) : 0,
  549. ];
  550. }
  551. array_multisort(array_column($list, 'value'), SORT_DESC, $list);
  552. return compact('bing_xdata', 'bing_data', 'list');
  553. }
  554. }