UserMoneyServices.php 20 KB

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