UserMoneyServices.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\user;
  12. use app\dao\user\UserMoneyDao;
  13. use app\services\BaseServices;
  14. use app\services\order\StoreOrderServices;
  15. use crmeb\exceptions\AdminException;
  16. class UserMoneyServices extends BaseServices
  17. {
  18. /**
  19. * 用户记录模板
  20. * @var array[]
  21. */
  22. protected $incomeData = [
  23. 'pay_product' => [
  24. 'title' => '余额支付购买商品',
  25. 'type' => 'pay_product',
  26. 'mark' => '余额支付{%num%}元购买商品',
  27. 'status' => 1,
  28. 'pm' => 0
  29. ],
  30. 'pay_product_refund' => [
  31. 'title' => '商品退款',
  32. 'type' => 'pay_product_refund',
  33. 'mark' => '订单退款到余额{%num%}元',
  34. 'status' => 1,
  35. 'pm' => 1
  36. ],
  37. 'system_add' => [
  38. 'title' => '系统增加余额',
  39. 'type' => 'system_add',
  40. 'mark' => '系统增加{%num%}余额',
  41. 'status' => 1,
  42. 'pm' => 1
  43. ],
  44. 'system_sub' => [
  45. 'title' => '系统减少余额',
  46. 'type' => 'system_sub',
  47. 'mark' => '系统扣除{%num%}余额',
  48. 'status' => 1,
  49. 'pm' => 0
  50. ],
  51. 'user_recharge' => [
  52. 'title' => '用户充值余额',
  53. 'type' => 'recharge',
  54. 'mark' => '成功充值余额{%price%}元,赠送{%give_price%}元',
  55. 'status' => 1,
  56. 'pm' => 1
  57. ],
  58. 'user_recharge_refund' => [
  59. 'title' => '用户充值退款',
  60. 'type' => 'recharge_refund',
  61. 'mark' => '退款扣除余额{%num%}元',
  62. 'status' => 1,
  63. 'pm' => 0
  64. ],
  65. 'brokerage_to_nowMoney' => [
  66. 'title' => '佣金提现到余额',
  67. 'type' => 'extract',
  68. 'mark' => '佣金提现到余额{%num%}元',
  69. 'status' => 1,
  70. 'pm' => 1
  71. ],
  72. 'lottery_use_money' => [
  73. 'title' => '参与抽奖使用余额',
  74. 'type' => 'lottery_use',
  75. 'mark' => '参与抽奖使用{%num%}余额',
  76. 'status' => 1,
  77. 'pm' => 0
  78. ],
  79. 'lottery_give_money' => [
  80. 'title' => '抽奖中奖赠送余额',
  81. 'type' => 'lottery_add',
  82. 'mark' => '抽奖中奖赠送{%num%}余额',
  83. 'status' => 1,
  84. 'pm' => 1
  85. ],
  86. 'register_system_add' => [
  87. 'title' => '新用户注册赠送余额',
  88. 'type' => 'register_system_add',
  89. 'mark' => '新用户注册赠送{%num%}余额',
  90. 'status' => 1,
  91. 'pm' => 1
  92. ],
  93. ];
  94. /**
  95. * UserMoneyServices constructor.
  96. * @param UserMoneyDao $dao
  97. */
  98. public function __construct(UserMoneyDao $dao)
  99. {
  100. $this->dao = $dao;
  101. }
  102. /**
  103. * 写入用户记录
  104. * @param string $type 写入类型
  105. * @param int $uid
  106. * @param int|string|array $number
  107. * @param int|string $balance
  108. * @param $linkId
  109. * @return bool|mixed
  110. */
  111. public function income(string $type, int $uid, $number, $balance, $linkId)
  112. {
  113. $data = $this->incomeData[$type] ?? null;
  114. if (!$data) {
  115. return true;
  116. }
  117. $data['uid'] = $uid;
  118. $data['balance'] = $balance ?? 0;
  119. $data['link_id'] = $linkId;
  120. if (is_array($number)) {
  121. $key = array_keys($number);
  122. $key = array_map(function ($item) {
  123. return '{%' . $item . '%}';
  124. }, $key);
  125. $value = array_values($number);
  126. $data['number'] = $number['number'] ?? 0;
  127. $data['mark'] = str_replace($key, $value, $data['mark']);
  128. } else {
  129. $data['number'] = $number;
  130. $data['mark'] = str_replace(['{%num%}'], $number, $data['mark']);
  131. }
  132. $data['add_time'] = time();
  133. return $this->dao->save($data);
  134. }
  135. /**
  136. * 余额记录
  137. * @param $where
  138. * @return array
  139. */
  140. public function balanceList($where)
  141. {
  142. $status = [];
  143. foreach ($this->incomeData as $value) {
  144. $status[$value['type']] = $value['title'];
  145. }
  146. [$page, $limit] = $this->getPageValue();
  147. $list = $this->dao->getList($where, $page, $limit);
  148. //关联用户
  149. /** @var UserServices $userServices */
  150. $userServices = app()->make(UserServices::class);
  151. $uids = array_column($list, 'uid');
  152. $nicknameArr = $userServices->getColumn([['uid', 'in', $uids]], 'nickname', 'uid');
  153. //关联订单
  154. /** @var StoreOrderServices $orderServices */
  155. $orderServices = app()->make(StoreOrderServices::class);
  156. /** @var UserRechargeServices $rechargeServices */
  157. $rechargeServices = app()->make(UserRechargeServices::class);
  158. foreach ($list as &$item) {
  159. $item['nickname'] = $nicknameArr[$item['uid']];
  160. if ($item['type'] == 'pay_product' || $item['type'] == 'pay_product_refund') {
  161. $item['relation'] = $orderServices->value(['id' => $item['link_id']], 'order_id');
  162. } elseif ($item['type'] == 'recharge' || $item['type'] == 'recharge_refund') {
  163. $item['relation'] = $rechargeServices->value(['id' => $item['link_id']], 'order_id');
  164. } else {
  165. $item['relation'] = $status[$item['type']];
  166. }
  167. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  168. $item['type_name'] = $status[$item['type']];
  169. }
  170. $count = $this->dao->count($where);
  171. return compact('list', 'count', 'status');
  172. }
  173. /**
  174. * 余额记录备注
  175. * @param $data
  176. * @return bool
  177. */
  178. public function recordRemark($id, $mark)
  179. {
  180. if ($this->dao->update($id, ['mark' => $mark])) {
  181. return true;
  182. } else {
  183. throw new AdminException(100025);
  184. }
  185. }
  186. /**
  187. * 余额统计基础
  188. * @return array
  189. * @throws \ReflectionException
  190. */
  191. public function getBasic()
  192. {
  193. /** @var UserServices $userServices */
  194. $userServices = app()->make(UserServices::class);
  195. $data['now_balance'] = $userServices->sum(['status' => 1], 'now_money', true);
  196. $data['add_balance'] = $this->dao->sum([
  197. ['pm', '=', 1],
  198. ['type', 'in', ['system_add', 'recharge', 'extract', 'lottery_add', 'register_system_add']]
  199. ], 'number', false);
  200. $data['sub_balance'] = bcsub($data['add_balance'], $data['now_balance'], 2);
  201. return $data;
  202. }
  203. /**
  204. * 余额趋势
  205. * @param $where
  206. * @return array
  207. */
  208. public function getTrend($where)
  209. {
  210. $time = explode('-', $where['time']);
  211. if (count($time) != 2) throw new AdminException('请选择时间');
  212. $dayCount = (strtotime($time[1]) - strtotime($time[0])) / 86400 + 1;
  213. $data = [];
  214. if ($dayCount == 1) {
  215. $data = $this->trend($time, 0);
  216. } elseif ($dayCount > 1 && $dayCount <= 31) {
  217. $data = $this->trend($time, 1);
  218. } elseif ($dayCount > 31 && $dayCount <= 92) {
  219. $data = $this->trend($time, 3);
  220. } elseif ($dayCount > 92) {
  221. $data = $this->trend($time, 30);
  222. }
  223. return $data;
  224. }
  225. /**
  226. * 余额趋势
  227. * @param $time
  228. * @param $num
  229. * @param false $excel
  230. * @return array
  231. */
  232. public function trend($time, $num, $excel = false)
  233. {
  234. if ($num == 0) {
  235. $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'];
  236. $timeType = '%H';
  237. } elseif ($num != 0) {
  238. $dt_start = strtotime($time[0]);
  239. $dt_end = strtotime($time[1]);
  240. while ($dt_start <= $dt_end) {
  241. if ($num == 30) {
  242. $xAxis[] = date('Y-m', $dt_start);
  243. $dt_start = strtotime("+1 month", $dt_start);
  244. $timeType = '%Y-%m';
  245. } else {
  246. $xAxis[] = date('m-d', $dt_start);
  247. $dt_start = strtotime("+$num day", $dt_start);
  248. $timeType = '%m-%d';
  249. }
  250. }
  251. }
  252. $time[1] = date("Y-m-d", strtotime("+1 day", strtotime($time[1])));
  253. $point_add = array_column($this->dao->getBalanceTrend($time, $timeType, 'add_time', 'sum(number)', 'add'), 'num', 'days');
  254. $point_sub = array_column($this->dao->getBalanceTrend($time, $timeType, 'add_time', 'sum(number)', 'sub'), 'num', 'days');
  255. $data = $series = [];
  256. foreach ($xAxis as $item) {
  257. $data['余额积累'][] = isset($point_add[$item]) ? floatval($point_add[$item]) : 0;
  258. $data['余额消耗'][] = isset($point_sub[$item]) ? floatval($point_sub[$item]) : 0;
  259. }
  260. foreach ($data as $key => $item) {
  261. $series[] = [
  262. 'name' => $key,
  263. 'data' => $item,
  264. 'type' => 'line',
  265. ];
  266. }
  267. return compact('xAxis', 'series');
  268. }
  269. /**
  270. * 余额来源
  271. * @param $where
  272. * @return array
  273. */
  274. public function getChannel($where)
  275. {
  276. $bing_xdata = ['系统增加', '用户充值', '佣金提现', '抽奖赠送', '商品退款'];
  277. $color = ['#64a1f4', '#3edeb5', '#70869f', '#ffc653', '#fc7d6a'];
  278. $data = ['system_add', 'recharge', 'extract', 'lottery_add', 'pay_product_refund'];
  279. $bing_data = [];
  280. foreach ($data as $key => $item) {
  281. $bing_data[] = [
  282. 'name' => $bing_xdata[$key],
  283. 'value' => $this->dao->sum(['pm' => 1, 'type' => $item, 'time' => $where['time']], 'number', true),
  284. 'itemStyle' => ['color' => $color[$key]]
  285. ];
  286. }
  287. $list = [];
  288. $count = array_sum(array_column($bing_data, 'value'));
  289. foreach ($bing_data as $key => $item) {
  290. $list[] = [
  291. 'name' => $item['name'],
  292. 'value' => $item['value'],
  293. 'percent' => $count != 0 ? bcmul((string)bcdiv((string)$item['value'], (string)$count, 4), '100', 2) : 0,
  294. ];
  295. }
  296. array_multisort(array_column($list, 'value'), SORT_DESC, $list);
  297. return compact('bing_xdata', 'bing_data', 'list');
  298. }
  299. /**
  300. * 余额类型
  301. * @param $where
  302. * @return array
  303. */
  304. public function getType($where)
  305. {
  306. $bing_xdata = ['系统减少', '充值退款', '购买商品'];
  307. $color = ['#64a1f4', '#3edeb5', '#70869f'];
  308. $data = ['system_sub', 'recharge_refund', 'pay_product'];
  309. $bing_data = [];
  310. foreach ($data as $key => $item) {
  311. $bing_data[] = [
  312. 'name' => $bing_xdata[$key],
  313. 'value' => $this->dao->sum(['pm' => 0, 'type' => $item, 'time' => $where['time']], 'number', true),
  314. 'itemStyle' => ['color' => $color[$key]]
  315. ];
  316. }
  317. $list = [];
  318. $count = array_sum(array_column($bing_data, 'value'));
  319. foreach ($bing_data as $key => $item) {
  320. $list[] = [
  321. 'name' => $item['name'],
  322. 'value' => $item['value'],
  323. 'percent' => $count != 0 ? bcmul((string)bcdiv((string)$item['value'], (string)$count, 4), '100', 2) : 0,
  324. ];
  325. }
  326. array_multisort(array_column($list, 'value'), SORT_DESC, $list);
  327. return compact('bing_xdata', 'bing_data', 'list');
  328. }
  329. public function getMoneyList($uid, $type)
  330. {
  331. $where = [];
  332. $where['uid'] = $uid;
  333. [$page, $limit] = $this->getPageValue();
  334. if ($type == 1) {
  335. $where['pm'] = 0;
  336. } elseif ($type == 2) {
  337. $where['pm'] = 1;
  338. $where['not_type'] = ['pay_product_refund'];
  339. }
  340. $list = $this->dao->getList($where, $page, $limit);
  341. $count = $this->dao->count($where);
  342. $times = [];
  343. if ($list) {
  344. foreach ($list as &$item) {
  345. $item['time'] = $item['time_key'] = $item['add_time'] ? date('Y-m', (int)$item['add_time']) : '';
  346. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i', (int)$item['add_time']) : '';
  347. }
  348. $times = array_merge(array_unique(array_column($list, 'time_key')));
  349. }
  350. return ['list' => $list, 'time' => $times, 'count' => $count];
  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. }