UserMoneyServices.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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. ];
  87. /**
  88. * UserMoneyServices constructor.
  89. * @param UserMoneyDao $dao
  90. */
  91. public function __construct(UserMoneyDao $dao)
  92. {
  93. $this->dao = $dao;
  94. }
  95. /**
  96. * 写入用户记录
  97. * @param string $type 写入类型
  98. * @param int $uid
  99. * @param int|string|array $number
  100. * @param int|string $balance
  101. * @param $linkId
  102. * @return bool|mixed
  103. */
  104. public function income(string $type, int $uid, $number, $balance, $linkId)
  105. {
  106. $data = $this->incomeData[$type] ?? null;
  107. if (!$data) {
  108. return true;
  109. }
  110. $data['uid'] = $uid;
  111. $data['balance'] = $balance ?? 0;
  112. $data['link_id'] = $linkId;
  113. if (is_array($number)) {
  114. $key = array_keys($number);
  115. $key = array_map(function ($item) {
  116. return '{%' . $item . '%}';
  117. }, $key);
  118. $value = array_values($number);
  119. $data['number'] = $number['number'] ?? 0;
  120. $data['mark'] = str_replace($key, $value, $data['mark']);
  121. } else {
  122. $data['number'] = $number;
  123. $data['mark'] = str_replace(['{%num%}'], $number, $data['mark']);
  124. }
  125. $data['add_time'] = time();
  126. return $this->dao->save($data);
  127. }
  128. /**
  129. * 余额记录
  130. * @param $where
  131. * @return array
  132. */
  133. public function balanceList($where)
  134. {
  135. $status = [];
  136. foreach ($this->incomeData as $value) {
  137. $status[$value['type']] = $value['title'];
  138. }
  139. [$page, $limit] = $this->getPageValue();
  140. $list = $this->dao->getList($where, $page, $limit);
  141. //关联用户
  142. /** @var UserServices $userServices */
  143. $userServices = app()->make(UserServices::class);
  144. $uids = array_column($list, 'uid');
  145. $nicknameArr = $userServices->getColumn([['uid', 'in', $uids]], 'nickname', 'uid');
  146. //关联订单
  147. /** @var StoreOrderServices $orderServices */
  148. $orderServices = app()->make(StoreOrderServices::class);
  149. /** @var UserRechargeServices $rechargeServices */
  150. $rechargeServices = app()->make(UserRechargeServices::class);
  151. foreach ($list as &$item) {
  152. $item['nickname'] = $nicknameArr[$item['uid']];
  153. if ($item['type'] == 'pay_product' || $item['type'] == 'pay_product_refund') {
  154. $item['relation'] = $orderServices->value(['id' => $item['link_id']], 'order_id');
  155. } elseif ($item['type'] == 'recharge' || $item['type'] == 'recharge_refund') {
  156. $item['relation'] = $rechargeServices->value(['id' => $item['link_id']], 'order_id');
  157. } else {
  158. $item['relation'] = $status[$item['type']];
  159. }
  160. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  161. $item['type_name'] = $status[$item['type']];
  162. }
  163. $count = $this->dao->count($where);
  164. return compact('list', 'count', 'status');
  165. }
  166. /**
  167. * 余额记录备注
  168. * @param $data
  169. * @return bool
  170. */
  171. public function recordRemark($id, $mark)
  172. {
  173. if ($this->dao->update($id, ['mark' => $mark])) {
  174. return true;
  175. } else {
  176. throw new AdminException(100025);
  177. }
  178. }
  179. /**
  180. * 余额统计基础
  181. * @param $where
  182. * @return array
  183. */
  184. public function getBasic($where)
  185. {
  186. /** @var UserServices $userServices */
  187. $userServices = app()->make(UserServices::class);
  188. $data['now_balance'] = $userServices->sum(['status' => 1], 'now_money', true);
  189. $data['add_balance'] = $this->dao->sum(['pm' => 1], 'number', true);
  190. $data['sub_balance'] = $this->dao->sum(['pm' => 0], 'number', true);
  191. return $data;
  192. }
  193. /**
  194. * 余额趋势
  195. * @param $where
  196. * @return array
  197. */
  198. public function getTrend($where)
  199. {
  200. $time = explode('-', $where['time']);
  201. if (count($time) != 2) throw new AdminException(100100);
  202. $dayCount = (strtotime($time[1]) - strtotime($time[0])) / 86400 + 1;
  203. $data = [];
  204. if ($dayCount == 1) {
  205. $data = $this->trend($time, 0);
  206. } elseif ($dayCount > 1 && $dayCount <= 31) {
  207. $data = $this->trend($time, 1);
  208. } elseif ($dayCount > 31 && $dayCount <= 92) {
  209. $data = $this->trend($time, 3);
  210. } elseif ($dayCount > 92) {
  211. $data = $this->trend($time, 30);
  212. }
  213. return $data;
  214. }
  215. /**
  216. * 余额趋势
  217. * @param $time
  218. * @param $num
  219. * @param false $excel
  220. * @return array
  221. */
  222. public function trend($time, $num, $excel = false)
  223. {
  224. if ($num == 0) {
  225. $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'];
  226. $timeType = '%H';
  227. } elseif ($num != 0) {
  228. $dt_start = strtotime($time[0]);
  229. $dt_end = strtotime($time[1]);
  230. while ($dt_start <= $dt_end) {
  231. if ($num == 30) {
  232. $xAxis[] = date('Y-m', $dt_start);
  233. $dt_start = strtotime("+1 month", $dt_start);
  234. $timeType = '%Y-%m';
  235. } else {
  236. $xAxis[] = date('m-d', $dt_start);
  237. $dt_start = strtotime("+$num day", $dt_start);
  238. $timeType = '%m-%d';
  239. }
  240. }
  241. }
  242. $time[1] = date("Y-m-d", strtotime("+1 day", strtotime($time[1])));
  243. $point_add = array_column($this->dao->getBalanceTrend($time, $timeType, 'add_time', 'sum(number)', 'add'), 'num', 'days');
  244. $point_sub = array_column($this->dao->getBalanceTrend($time, $timeType, 'add_time', 'sum(number)', 'sub'), 'num', 'days');
  245. $data = $series = [];
  246. foreach ($xAxis as $item) {
  247. $data['余额积累'][] = isset($point_add[$item]) ? floatval($point_add[$item]) : 0;
  248. $data['余额消耗'][] = isset($point_sub[$item]) ? floatval($point_sub[$item]) : 0;
  249. }
  250. foreach ($data as $key => $item) {
  251. $series[] = [
  252. 'name' => $key,
  253. 'data' => $item,
  254. 'type' => 'line',
  255. ];
  256. }
  257. return compact('xAxis', 'series');
  258. }
  259. /**
  260. * 余额来源
  261. * @param $where
  262. * @return array
  263. */
  264. public function getChannel($where)
  265. {
  266. $bing_xdata = ['系统增加', '用户充值', '佣金提现', '抽奖赠送', '商品退款'];
  267. $color = ['#64a1f4', '#3edeb5', '#70869f', '#ffc653', '#fc7d6a'];
  268. $data = ['system_add', 'recharge', 'extract', 'lottery_add', 'pay_product_refund'];
  269. $bing_data = [];
  270. foreach ($data as $key => $item) {
  271. $bing_data[] = [
  272. 'name' => $bing_xdata[$key],
  273. 'value' => $this->dao->sum(['pm' => 1, 'type' => $item, 'time' => $where['time']], 'number', true),
  274. 'itemStyle' => ['color' => $color[$key]]
  275. ];
  276. }
  277. $list = [];
  278. $count = array_sum(array_column($bing_data, 'value'));
  279. foreach ($bing_data as $key => $item) {
  280. $list[] = [
  281. 'name' => $item['name'],
  282. 'value' => $item['value'],
  283. 'percent' => $count != 0 ? bcmul((string)bcdiv((string)$item['value'], (string)$count, 4), '100', 2) : 0,
  284. ];
  285. }
  286. array_multisort(array_column($list, 'value'), SORT_DESC, $list);
  287. return compact('bing_xdata', 'bing_data', 'list');
  288. }
  289. /**
  290. * 余额类型
  291. * @param $where
  292. * @return array
  293. */
  294. public function getType($where)
  295. {
  296. $bing_xdata = ['系统减少', '充值退款', '购买商品'];
  297. $color = ['#64a1f4', '#3edeb5', '#70869f'];
  298. $data = ['system_sub', 'recharge_refund', 'pay_product'];
  299. $bing_data = [];
  300. foreach ($data as $key => $item) {
  301. $bing_data[] = [
  302. 'name' => $bing_xdata[$key],
  303. 'value' => $this->dao->sum(['pm' => 0, 'type' => $item, 'time' => $where['time']], 'number', true),
  304. 'itemStyle' => ['color' => $color[$key]]
  305. ];
  306. }
  307. $list = [];
  308. $count = array_sum(array_column($bing_data, 'value'));
  309. foreach ($bing_data as $key => $item) {
  310. $list[] = [
  311. 'name' => $item['name'],
  312. 'value' => $item['value'],
  313. 'percent' => $count != 0 ? bcmul((string)bcdiv((string)$item['value'], (string)$count, 4), '100', 2) : 0,
  314. ];
  315. }
  316. array_multisort(array_column($list, 'value'), SORT_DESC, $list);
  317. return compact('bing_xdata', 'bing_data', 'list');
  318. }
  319. public function getMoneyList($uid, $type)
  320. {
  321. $where = [];
  322. $where['uid'] = $uid;
  323. [$page, $limit] = $this->getPageValue();
  324. if ($type == 1) {
  325. $where['pm'] = 0;
  326. } elseif ($type == 2) {
  327. $where['pm'] = 1;
  328. $where['not_type'] = ['pay_product_refund'];
  329. }
  330. $list = $this->dao->getList($where, $page, $limit);
  331. $count = $this->dao->count($where);
  332. $times = [];
  333. if ($list) {
  334. foreach ($list as &$item) {
  335. $item['time'] = $item['time_key'] = $item['add_time'] ? date('Y-m', (int)$item['add_time']) : '';
  336. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i', (int)$item['add_time']) : '';
  337. }
  338. $times = array_merge(array_unique(array_column($list, 'time_key')));
  339. }
  340. return ['list' => $list, 'time' => $times, 'count' => $count];
  341. }
  342. /**
  343. * 根据查询用户充值金额
  344. * @param array $where
  345. * @param string $rechargeSumField
  346. * @param string $selectType
  347. * @param string $group
  348. * @return float|mixed
  349. */
  350. public function getRechargeMoneyByWhere(array $where, string $rechargeSumField, string $selectType, string $group = "")
  351. {
  352. switch ($selectType) {
  353. case "sum" :
  354. return $this->dao->getWhereSumField($where, $rechargeSumField);
  355. case "group" :
  356. return $this->dao->getGroupField($where, $rechargeSumField, $group);
  357. }
  358. }
  359. }