UserMoneyServices.php 20 KB

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