UserMoneyServices.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. * @param $where
  189. * @return array
  190. */
  191. public function getBasic($where)
  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(['pm' => 1], 'number', true);
  197. $data['sub_balance'] = $this->dao->sum(['pm' => 0], 'number', true);
  198. return $data;
  199. }
  200. /**
  201. * 余额趋势
  202. * @param $where
  203. * @return array
  204. */
  205. public function getTrend($where)
  206. {
  207. $time = explode('-', $where['time']);
  208. if (count($time) != 2) throw new AdminException(100100);
  209. $dayCount = (strtotime($time[1]) - strtotime($time[0])) / 86400 + 1;
  210. $data = [];
  211. if ($dayCount == 1) {
  212. $data = $this->trend($time, 0);
  213. } elseif ($dayCount > 1 && $dayCount <= 31) {
  214. $data = $this->trend($time, 1);
  215. } elseif ($dayCount > 31 && $dayCount <= 92) {
  216. $data = $this->trend($time, 3);
  217. } elseif ($dayCount > 92) {
  218. $data = $this->trend($time, 30);
  219. }
  220. return $data;
  221. }
  222. /**
  223. * 余额趋势
  224. * @param $time
  225. * @param $num
  226. * @param false $excel
  227. * @return array
  228. */
  229. public function trend($time, $num, $excel = false)
  230. {
  231. if ($num == 0) {
  232. $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'];
  233. $timeType = '%H';
  234. } elseif ($num != 0) {
  235. $dt_start = strtotime($time[0]);
  236. $dt_end = strtotime($time[1]);
  237. while ($dt_start <= $dt_end) {
  238. if ($num == 30) {
  239. $xAxis[] = date('Y-m', $dt_start);
  240. $dt_start = strtotime("+1 month", $dt_start);
  241. $timeType = '%Y-%m';
  242. } else {
  243. $xAxis[] = date('m-d', $dt_start);
  244. $dt_start = strtotime("+$num day", $dt_start);
  245. $timeType = '%m-%d';
  246. }
  247. }
  248. }
  249. $time[1] = date("Y-m-d", strtotime("+1 day", strtotime($time[1])));
  250. $point_add = array_column($this->dao->getBalanceTrend($time, $timeType, 'add_time', 'sum(number)', 'add'), 'num', 'days');
  251. $point_sub = array_column($this->dao->getBalanceTrend($time, $timeType, 'add_time', 'sum(number)', 'sub'), 'num', 'days');
  252. $data = $series = [];
  253. foreach ($xAxis as $item) {
  254. $data['余额积累'][] = isset($point_add[$item]) ? floatval($point_add[$item]) : 0;
  255. $data['余额消耗'][] = isset($point_sub[$item]) ? floatval($point_sub[$item]) : 0;
  256. }
  257. foreach ($data as $key => $item) {
  258. $series[] = [
  259. 'name' => $key,
  260. 'data' => $item,
  261. 'type' => 'line',
  262. ];
  263. }
  264. return compact('xAxis', 'series');
  265. }
  266. /**
  267. * 余额来源
  268. * @param $where
  269. * @return array
  270. */
  271. public function getChannel($where)
  272. {
  273. $bing_xdata = ['系统增加', '用户充值', '佣金提现', '抽奖赠送', '商品退款'];
  274. $color = ['#64a1f4', '#3edeb5', '#70869f', '#ffc653', '#fc7d6a'];
  275. $data = ['system_add', 'recharge', 'extract', 'lottery_add', 'pay_product_refund'];
  276. $bing_data = [];
  277. foreach ($data as $key => $item) {
  278. $bing_data[] = [
  279. 'name' => $bing_xdata[$key],
  280. 'value' => $this->dao->sum(['pm' => 1, 'type' => $item, 'time' => $where['time']], 'number', true),
  281. 'itemStyle' => ['color' => $color[$key]]
  282. ];
  283. }
  284. $list = [];
  285. $count = array_sum(array_column($bing_data, 'value'));
  286. foreach ($bing_data as $key => $item) {
  287. $list[] = [
  288. 'name' => $item['name'],
  289. 'value' => $item['value'],
  290. 'percent' => $count != 0 ? bcmul((string)bcdiv((string)$item['value'], (string)$count, 4), '100', 2) : 0,
  291. ];
  292. }
  293. array_multisort(array_column($list, 'value'), SORT_DESC, $list);
  294. return compact('bing_xdata', 'bing_data', 'list');
  295. }
  296. /**
  297. * 余额类型
  298. * @param $where
  299. * @return array
  300. */
  301. public function getType($where)
  302. {
  303. $bing_xdata = ['系统减少', '充值退款', '购买商品'];
  304. $color = ['#64a1f4', '#3edeb5', '#70869f'];
  305. $data = ['system_sub', 'recharge_refund', 'pay_product'];
  306. $bing_data = [];
  307. foreach ($data as $key => $item) {
  308. $bing_data[] = [
  309. 'name' => $bing_xdata[$key],
  310. 'value' => $this->dao->sum(['pm' => 0, 'type' => $item, 'time' => $where['time']], 'number', true),
  311. 'itemStyle' => ['color' => $color[$key]]
  312. ];
  313. }
  314. $list = [];
  315. $count = array_sum(array_column($bing_data, 'value'));
  316. foreach ($bing_data as $key => $item) {
  317. $list[] = [
  318. 'name' => $item['name'],
  319. 'value' => $item['value'],
  320. 'percent' => $count != 0 ? bcmul((string)bcdiv((string)$item['value'], (string)$count, 4), '100', 2) : 0,
  321. ];
  322. }
  323. array_multisort(array_column($list, 'value'), SORT_DESC, $list);
  324. return compact('bing_xdata', 'bing_data', 'list');
  325. }
  326. public function getMoneyList($uid, $type)
  327. {
  328. $where = [];
  329. $where['uid'] = $uid;
  330. [$page, $limit] = $this->getPageValue();
  331. if ($type == 1) {
  332. $where['pm'] = 0;
  333. } elseif ($type == 2) {
  334. $where['pm'] = 1;
  335. $where['not_type'] = ['pay_product_refund'];
  336. }
  337. $list = $this->dao->getList($where, $page, $limit);
  338. $count = $this->dao->count($where);
  339. $times = [];
  340. if ($list) {
  341. foreach ($list as &$item) {
  342. $item['time'] = $item['time_key'] = $item['add_time'] ? date('Y-m', (int)$item['add_time']) : '';
  343. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i', (int)$item['add_time']) : '';
  344. }
  345. $times = array_merge(array_unique(array_column($list, 'time_key')));
  346. }
  347. return ['list' => $list, 'time' => $times, 'count' => $count];
  348. }
  349. /**
  350. * 根据查询用户充值金额
  351. * @param array $where
  352. * @param string $rechargeSumField
  353. * @param string $selectType
  354. * @param string $group
  355. * @return float|mixed
  356. */
  357. public function getRechargeMoneyByWhere(array $where, string $rechargeSumField, string $selectType, string $group = "")
  358. {
  359. switch ($selectType) {
  360. case "sum" :
  361. return $this->dao->getWhereSumField($where, $rechargeSumField);
  362. case "group" :
  363. return $this->dao->getGroupField($where, $rechargeSumField, $group);
  364. }
  365. }
  366. }