UserPointServices.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace app\services\activity\integral;
  3. use app\dao\user\UserBillDao;
  4. use app\services\BaseServices;
  5. use app\services\order\StoreOrderServices;
  6. use app\services\user\UserServices;
  7. use crmeb\exceptions\AdminException;
  8. class UserPointServices extends BaseServices
  9. {
  10. /**
  11. * UserBillServices constructor.
  12. * @param UserBillDao $dao
  13. */
  14. public function __construct(UserBillDao $dao)
  15. {
  16. $this->dao = $dao;
  17. }
  18. /**
  19. * 积分记录
  20. * @param $where
  21. * @return array
  22. */
  23. public function pointRecord($where)
  24. {
  25. $where['category'] = 'integral';
  26. $status = [
  27. 'invite_user' => '邀新奖励',
  28. 'system_add' => '系统增加积分',
  29. 'system_sub' => '系统减少积分',
  30. 'gain' => '下单赠送积分',
  31. 'product_gain' => '购买商品赠送积分',
  32. 'deduction' => '下单积分抵扣',
  33. 'lottery_use' => '参与抽奖使用积分',
  34. 'lottery_add' => '抽奖中奖赠送积分',
  35. 'order_deduction' => '扣除订单下单赠送积分',
  36. 'storeIntegral_use' => '积分兑换商品',
  37. 'pay_product_integral_back' => '返还下单使用积分',
  38. 'sign' => '签到获得积分',
  39. ];
  40. [$page, $limit] = $this->getPageValue();
  41. $list = $this->dao->getList($where, '*', $page, $limit);
  42. //关联用户
  43. /** @var UserServices $userServices */
  44. $userServices = app()->make(UserServices::class);
  45. $uids = array_column($list, 'uid');
  46. $nicknameArr = $userServices->getColumn([['uid', 'in', $uids]], 'nickname', 'uid');
  47. //关联订单
  48. /** @var StoreOrderServices $orderServices */
  49. $orderServices = app()->make(StoreOrderServices::class);
  50. /** @var StoreIntegralOrderServices $integralOrderServices */
  51. $integralOrderServices = app()->make(StoreIntegralOrderServices::class);
  52. foreach ($list as &$item) {
  53. $item['nickname'] = $nicknameArr[$item['uid']] ?? '未知用户';
  54. if ($item['type'] == 'gain' || $item['type'] == 'deduction' || $item['type'] == 'product_deduction' || $item['type'] == 'pay_product_integral_back') {
  55. $item['relation'] = $orderServices->value(['id' => $item['link_id']], 'order_id');
  56. } elseif ($item['type'] == 'storeIntegral_use') {
  57. $item['relation'] = $integralOrderServices->value(['id' => $item['link_id']], 'order_id');
  58. } else {
  59. $item['relation'] = $status[$item['type']];
  60. }
  61. $item['type_name'] = $status[$item['type']];
  62. }
  63. $count = $this->dao->count($where);
  64. return compact('list', 'count', 'status');
  65. }
  66. /**
  67. * 积分记录备注
  68. * @param $data
  69. * @return bool
  70. */
  71. public function recordRemark($id, $mark)
  72. {
  73. if (!$id) throw new AdminException('缺少参数');
  74. if ($mark === '') throw new AdminException('请填写备注');
  75. if ($this->dao->update($id, ['mark' => $mark])) {
  76. return true;
  77. } else {
  78. throw new AdminException('备注失败');
  79. }
  80. }
  81. /**
  82. * 订单统计基础
  83. * @param $where
  84. * @return array
  85. */
  86. public function getBasic($where)
  87. {
  88. /** @var UserServices $userServices */
  89. $userServices = app()->make(UserServices::class);
  90. $data['now_point'] = $userServices->sum(['status' => 1], 'integral', true);
  91. $data['all_point'] = $this->dao->sum(['category' => 'integral', 'pm' => 1, 'time' => $where['time']], 'number', true);
  92. $data['pay_point'] = $this->dao->sum(['category' => 'integral', 'pm' => 0, 'time' => $where['time']], 'number', true);
  93. return $data;
  94. }
  95. /**
  96. * 订单趋势
  97. * @param $where
  98. * @return array
  99. */
  100. public function getTrend($where)
  101. {
  102. $time = explode('-', $where['time']);
  103. if (count($time) != 2) throw new AdminException('数据错误');
  104. $dayCount = (strtotime($time[1]) - strtotime($time[0])) / 86400 + 1;
  105. $data = [];
  106. if ($dayCount == 1) {
  107. $data = $this->trend($time, 0);
  108. } elseif ($dayCount > 1 && $dayCount <= 31) {
  109. $data = $this->trend($time, 1);
  110. } elseif ($dayCount > 31 && $dayCount <= 92) {
  111. $data = $this->trend($time, 3);
  112. } elseif ($dayCount > 92) {
  113. $data = $this->trend($time, 30);
  114. }
  115. return $data;
  116. }
  117. /**
  118. * 订单趋势
  119. * @param $time
  120. * @param $num
  121. * @param false $excel
  122. * @return array
  123. */
  124. public function trend($time, $num, $excel = false)
  125. {
  126. if ($num == 0) {
  127. $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'];
  128. $timeType = '%H';
  129. } elseif ($num != 0) {
  130. $dt_start = strtotime($time[0]);
  131. $dt_end = strtotime($time[1]);
  132. while ($dt_start <= $dt_end) {
  133. if ($num == 30) {
  134. $xAxis[] = date('Y-m', $dt_start);
  135. $dt_start = strtotime("+1 month", $dt_start);
  136. $timeType = '%Y-%m';
  137. } else {
  138. $xAxis[] = date('m-d', $dt_start);
  139. $dt_start = strtotime("+$num day", $dt_start);
  140. $timeType = '%m-%d';
  141. }
  142. }
  143. }
  144. $point_add = array_column($this->dao->getPointTrend($time, $timeType, 'add_time', 'sum(number)', 'add'), 'num', 'days');
  145. $point_sub = array_column($this->dao->getPointTrend($time, $timeType, 'add_time', 'sum(number)', 'sub'), 'num', 'days');
  146. $data = $series = [];
  147. foreach ($xAxis as $item) {
  148. $data['积分积累'][] = isset($point_add[$item]) ? floatval($point_add[$item]) : 0;
  149. $data['积分消耗'][] = isset($point_sub[$item]) ? floatval($point_sub[$item]) : 0;
  150. }
  151. foreach ($data as $key => $item) {
  152. $series[] = [
  153. 'name' => $key,
  154. 'data' => $item,
  155. 'type' => 'line',
  156. ];
  157. }
  158. return compact('xAxis', 'series');
  159. }
  160. /**
  161. * 订单来源
  162. * @param $where
  163. * @return array
  164. */
  165. public function getChannel($where)
  166. {
  167. $bing_xdata = ['订单赠送', '商品赠送', '后台赠送', '签到获得', '九宫格抽奖'];
  168. $color = ['#64a1f4', '#3edeb5', '#70869f', '#ffc653', '#fc7d6a'];
  169. $data = ['gain', 'product_gain', 'system_add', 'sign', 'lottery_add'];
  170. $bing_data = [];
  171. foreach ($data as $key => $item) {
  172. $bing_data[] = [
  173. 'name' => $bing_xdata[$key],
  174. 'value' => $this->dao->sum(['pm' => 1, 'type' => $item, 'category' => 'integral', 'time' => $where['time']], 'number', true),
  175. 'itemStyle' => ['color' => $color[$key]]
  176. ];
  177. }
  178. $list = [];
  179. $count = array_sum(array_column($bing_data, 'value'));
  180. foreach ($bing_data as $key => $item) {
  181. $list[] = [
  182. 'name' => $item['name'],
  183. 'value' => $item['value'],
  184. 'percent' => $count != 0 ? bcmul((string)bcdiv((string)$item['value'], (string)$count, 4), '100', 1) : 0,
  185. ];
  186. }
  187. array_multisort(array_column($list, 'value'), SORT_DESC, $list);
  188. return compact('bing_xdata', 'bing_data', 'list');
  189. }
  190. /**
  191. * 订单类型
  192. * @param $where
  193. * @return array
  194. */
  195. public function getType($where)
  196. {
  197. $bing_xdata = ['订单抵扣', '九宫格抽奖', '后台减少', '退款退回', '兑换商品'];
  198. $color = ['#64a1f4', '#3edeb5', '#70869f', '#ffc653', '#fc7d6a'];
  199. $data = ['deduction', 'lottery_use', 'system_sub', 'order_deduction', 'storeIntegral_use'];
  200. $bing_data = [];
  201. foreach ($data as $key => $item) {
  202. $bing_data[] = [
  203. 'name' => $bing_xdata[$key],
  204. 'value' => $this->dao->sum(['pm' => 0, 'type' => $item, 'category' => 'integral', 'time' => $where['time']], 'number', true),
  205. 'itemStyle' => ['color' => $color[$key]]
  206. ];
  207. }
  208. $list = [];
  209. $count = array_sum(array_column($bing_data, 'value'));
  210. foreach ($bing_data as $key => $item) {
  211. $list[] = [
  212. 'name' => $item['name'],
  213. 'value' => $item['value'],
  214. 'percent' => $count != 0 ? bcmul((string)bcdiv((string)$item['value'], (string)$count, 4), '100', 1) : 0,
  215. ];
  216. }
  217. array_multisort(array_column($list, 'value'), SORT_DESC, $list);
  218. return compact('bing_xdata', 'bing_data', 'list');
  219. }
  220. }