UserStatisticServices.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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\statistic;
  12. use app\services\BaseServices;
  13. use app\services\other\export\ExportServices;
  14. use app\services\order\OtherOrderServices;
  15. use app\services\order\StoreOrderServices;
  16. use app\services\user\UserRechargeServices;
  17. use app\services\user\UserServices;
  18. use app\services\user\UserVisitServices;
  19. use app\services\user\UserWechatuserServices;
  20. use app\services\wechat\WechatUserServices;
  21. use crmeb\exceptions\AdminException;
  22. /**
  23. * Class UserStatisticServices
  24. * @package app\services\statistic
  25. */
  26. class UserStatisticServices extends BaseServices
  27. {
  28. /**
  29. * 基本概况
  30. * @param $where
  31. * @return mixed
  32. */
  33. public function getBasic($where)
  34. {
  35. $time = explode('-', $where['time']);
  36. if (count($time) != 2) throw new AdminException('请选择时间');
  37. /** @var UserVisitServices $userVisit */
  38. $userVisit = app()->make(UserVisitServices::class);
  39. /** @var UserServices $user */
  40. $user = app()->make(UserServices::class);
  41. /** @var StoreOrderServices $order */
  42. $order = app()->make(StoreOrderServices::class);
  43. /** @var OtherOrderServices $otherOrder */
  44. $otherOrder = app()->make(OtherOrderServices::class);
  45. $toEndTime = implode('-', [0, $time[1]]);
  46. $cumulativeUserWhere = ['time' => $toEndTime, 'user_type' => $where['channel_type']];
  47. $now['people'] = $userVisit->getDistinctCount($where, 'uid');//访客数
  48. $now['browse'] = $userVisit->count($where);//访问量
  49. $now['newUser'] = $user->count($where + ['user_type' => $where['channel_type']]);//新增用户数
  50. $now['payPeople'] = $order->getDistinctCount($where + ['paid' => 1], 'uid');//成交用户数
  51. $now['payUser'] = $otherOrder->getDistinctCount($where, 'uid');//激活付费会员数
  52. $now['cumulativeUser'] = $user->count($cumulativeUserWhere);//累计用户数
  53. $dayNum = (strtotime($time[1]) - strtotime($time[0])) / 86400 + 1;
  54. $lastTime = [
  55. date("Y/m/d", strtotime("-$dayNum days", strtotime($time[0]))),
  56. date("Y/m/d", strtotime("-1 days", strtotime($time[0])))
  57. ];
  58. $where['time'] = implode('-', $lastTime);
  59. $toEndTime = implode('-', [0, $lastTime[1]]);
  60. $last['people'] = $userVisit->getDistinctCount($where, 'uid');//访客数
  61. $last['browse'] = $userVisit->count($where);//访问量
  62. $last['newUser'] = $user->count($where + ['user_type' => $where['channel_type']]);//新增用户数
  63. $last['payPeople'] = $order->getDistinctCount($where + ['paid' => 1], 'uid');//成交用户数
  64. $last['payUser'] = $otherOrder->getDistinctCount($where, 'uid');//激活付费会员数
  65. $cumulativeUserWhere['time'] = $toEndTime;
  66. $last['cumulativeUser'] = $user->count($cumulativeUserWhere);//累计用户数
  67. //组合数据,计算环比
  68. $data = [];
  69. foreach ($now as $key => $item) {
  70. $data[$key]['num'] = $item;
  71. $data[$key]['last_num'] = $last[$key];
  72. $num = $last[$key] > 0 ? $last[$key] : 1;
  73. $data[$key]['percent'] = bcmul((string)bcdiv((string)($item - $last[$key]), (string)$num, 4), 100, 2);
  74. }
  75. return $data;
  76. }
  77. /**
  78. * 用户趋势
  79. * @param $where
  80. * @param $excel
  81. * @return mixed
  82. */
  83. public function getTrend($where, $excel = false)
  84. {
  85. $time = explode('-', $where['time']);
  86. $channelType = $where['channel_type'];
  87. if (count($time) != 2) throw new AdminException(100100);
  88. $dayCount = bcadd(bcdiv(bcsub(strtotime($time[1]), strtotime($time[0])), '86400'), '1');
  89. $data = [];
  90. if ($dayCount == 1) {
  91. $data = $this->trend($time, $channelType, 0, $excel);
  92. } elseif ($dayCount > 1 && $dayCount <= 31) {
  93. $data = $this->trend($time, $channelType, 1, $excel);
  94. } elseif ($dayCount > 31 && $dayCount <= 92) {
  95. $data = $this->trend($time, $channelType, 3, $excel);
  96. } elseif ($dayCount > 92) {
  97. $data = $this->trend($time, $channelType, 30, $excel);
  98. }
  99. return $data;
  100. }
  101. /**
  102. * 用户趋势
  103. * @param $time
  104. * @param $channelType
  105. * @param $num
  106. * @param $excel
  107. * @return array
  108. */
  109. public function trend($time, $channelType, $num, $excel)
  110. {
  111. /** @var UserServices $user */
  112. $user = app()->make(UserServices::class);
  113. /** @var UserVisitServices $userVisit */
  114. $userVisit = app()->make(UserVisitServices::class);
  115. /** @var StoreOrderServices $order */
  116. $order = app()->make(StoreOrderServices::class);
  117. /** @var OtherOrderServices $otherOrder */
  118. $otherOrder = app()->make(OtherOrderServices::class);
  119. $newPeople = $visitPeople = $paidPeople = $rechargePeople = $vipPeople = [];
  120. $newPeople['name'] = '新增用户数';
  121. $visitPeople['name'] = '访客数';
  122. $paidPeople['name'] = '成交用户数';
  123. $rechargePeople['name'] = '充值用户';
  124. $vipPeople['name'] = '新增付费用户数';
  125. if ($num == 0) {
  126. $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'];
  127. $timeType = '%H';
  128. } elseif ($num != 0) {
  129. $dt_start = strtotime($time[0]);
  130. $dt_end = strtotime($time[1]);
  131. while ($dt_start <= $dt_end) {
  132. if ($num == 30) {
  133. $xAxis[] = date('Y-m', $dt_start);
  134. $dt_start = strtotime("+1 month", $dt_start);
  135. $timeType = '%Y-%m';
  136. } else {
  137. $xAxis[] = date('m-d', $dt_start);
  138. $dt_start = strtotime("+$num day", $dt_start);
  139. $timeType = '%m-%d';
  140. }
  141. }
  142. }
  143. $visitPeople = array_column($userVisit->getTrendData($time, $channelType, $timeType, 'count(distinct(uid))'), 'num', 'days');
  144. $newPeople = array_column($user->getTrendData($time, $channelType, $timeType), 'num', 'days');
  145. $paidPeople = array_column($order->getTrendData($time, $channelType, $timeType, 'count(distinct(uid))'), 'num', 'days');
  146. $visitNum = array_column($userVisit->getTrendData($time, $channelType, $timeType, 'count(id)'), 'num', 'days');
  147. $vipPeople = array_column($otherOrder->getTrendData($time, $channelType, $timeType), 'num', 'days');
  148. if ($excel) {
  149. $data = [];
  150. $browsePeople = array_column($userVisit->getTrendData($time, $channelType, $timeType, 'count(id)'), 'num', 'days');
  151. foreach ($xAxis as &$item) {
  152. $data[] = [
  153. 'time' => $item,
  154. 'user' => $visitPeople[$item] ?? 0,
  155. 'browse' => $browsePeople[$item] ?? 0,
  156. 'new' => $newPeople[$item] ?? 0,
  157. 'paid' => $paidPeople[$item] ?? 0,
  158. 'vip' => $vipPeople[$item] ?? 0,
  159. ];
  160. }
  161. /** @var ExportServices $exportService */
  162. $exportService = app()->make(ExportServices::class);
  163. $url = $exportService->userTrade($data);
  164. return compact('url');
  165. } else {
  166. $data = $series = [];
  167. foreach ($xAxis as $item) {
  168. $data['新增用户数'][] = isset($newPeople[$item]) ? intval($newPeople[$item]) : 0;
  169. $data['访客数'][] = isset($visitPeople[$item]) ? intval($visitPeople[$item]) : 0;
  170. $data['浏览量'][] = isset($visitNum[$item]) ? intval($visitNum[$item]) : 0;
  171. $data['成交用户数'][] = isset($paidPeople[$item]) ? intval($paidPeople[$item]) : 0;
  172. $data['新增付费用户数'][] = isset($vipPeople[$item]) ? intval($vipPeople[$item]) : 0;
  173. }
  174. foreach ($data as $key => $item) {
  175. $series[] = ['name' => $key, 'value' => $item];
  176. }
  177. return compact('xAxis', 'series');
  178. }
  179. }
  180. /**
  181. * 微信用户信息
  182. * @param $where
  183. * @return array
  184. */
  185. public function getWechat($where)
  186. {
  187. $time = explode('-', $where['time']);
  188. if (count($time) != 2) throw new AdminException(100100);
  189. /** @var WechatUserServices $user */
  190. $user = app()->make(WechatUserServices::class);
  191. $now['subscribe'] = $user->getCount([
  192. ['subscribe', '=', 1],
  193. ['user_type', '=', 'wechat'],
  194. ['subscribe_time', '>=', strtotime($time[0])],
  195. ['subscribe_time', '<', strtotime($time[1])]
  196. ]);
  197. $now['unSubscribe'] = $user->getCount([
  198. ['subscribe', '=', 0],
  199. ['user_type', '=', 'wechat'],
  200. ['subscribe_time', '<>', ''],
  201. ['subscribe_time', '>=', strtotime($time[0])],
  202. ['subscribe_time', '<', strtotime($time[1])]
  203. ]);
  204. $now['increaseSubscribe'] = $now['subscribe'] - $now['unSubscribe'];
  205. $now['cumulativeSubscribe'] = $user->getCount([['subscribe', '=', 1], ['user_type', '=', 'wechat']]);
  206. $now['cumulativeUnSubscribe'] = $user->getCount([
  207. ['subscribe', '=', 0],
  208. ['user_type', '=', 'wechat'],
  209. ['subscribe_time', '<>', '']
  210. ]);
  211. $dayNum = bcadd(bcdiv(bcsub(strtotime($time[1]), strtotime($time[0])), '86400'), '1');
  212. $lastTime = array(
  213. date("Y/m/d", strtotime("-$dayNum days", strtotime($time[0]))),
  214. date("Y/m/d", strtotime("-1 days", strtotime($time[0])))
  215. );
  216. $last['subscribe'] = $user->getCount([
  217. ['subscribe', '=', 1],
  218. ['user_type', '=', 'wechat'],
  219. ['subscribe_time', '>=', strtotime($lastTime[0])],
  220. ['subscribe_time', '<', strtotime($lastTime[1])]
  221. ]);
  222. $last['unSubscribe'] = $user->getCount([
  223. ['subscribe', '=', 0],
  224. ['user_type', '=', 'wechat'],
  225. ['subscribe_time', '<>', ''],
  226. ['subscribe_time', '>=', strtotime($lastTime[0])],
  227. ['subscribe_time', '<', strtotime($lastTime[1])]
  228. ]);
  229. $last['increaseSubscribe'] = $last['subscribe'] - $last['unSubscribe'];
  230. $last['cumulativeSubscribe'] = $user->getCount([['subscribe', '=', 1], ['user_type', '=', 'wechat']]);
  231. $last['cumulativeUnSubscribe'] = $user->getCount([
  232. ['subscribe', '=', 0],
  233. ['user_type', '=', 'wechat'],
  234. ['subscribe_time', '<>', '']
  235. ]);
  236. //组合数据,计算环比
  237. $data = [];
  238. foreach ($now as $key => $item) {
  239. $data[$key]['num'] = $item;
  240. $num = $last[$key] ?: 1;
  241. $data[$key]['percent'] = bcmul(bcdiv(($item - $last[$key]), $num, 4), 100, 2);
  242. }
  243. return $data;
  244. }
  245. /**
  246. * 微信用户趋势
  247. * @param $where
  248. * @return array
  249. */
  250. public function getWechatTrend($where)
  251. {
  252. $time = explode('-', $where['time']);
  253. if (count($time) != 2) throw new AdminException(100100);
  254. $dayCount = bcadd(bcdiv(bcsub(strtotime($time[1]), strtotime($time[0])), '86400'), '1');
  255. $data = [];
  256. if ($dayCount == 1) {
  257. $data = $this->wechatTrend($time, 0);
  258. } elseif ($dayCount > 1 && $dayCount <= 31) {
  259. $data = $this->wechatTrend($time, 1);
  260. } elseif ($dayCount > 31 && $dayCount <= 92) {
  261. $data = $this->wechatTrend($time, 3);
  262. } elseif ($dayCount > 92) {
  263. $data = $this->wechatTrend($time, 30);
  264. }
  265. return $data;
  266. }
  267. /**
  268. * 微信用户趋势
  269. * @param $time
  270. * @param $num
  271. * @return array
  272. */
  273. public function wechatTrend($time, $num)
  274. {
  275. /** @var WechatUserServices $user */
  276. $user = app()->make(WechatUserServices::class);
  277. $subscribe = $unSubscribe = $increaseSubscribe = $cumulativeSubscribe = $cumulativeUnSubscribe = [];
  278. if ($num == 0) {
  279. $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'];
  280. $subscribe = array_column($user->getWechatTrendData($time, [
  281. ['subscribe', '=', 1],
  282. ['subscribe_time', '>=', strtotime($time[0])],
  283. ['subscribe_time', '<', strtotime($time[1])]
  284. ], '%H', 'subscribe'), 'subscribe', 'days');
  285. $unSubscribe = array_column($user->getWechatTrendData($time, [
  286. ['subscribe', '=', 0],
  287. ['subscribe_time', '<>', ''],
  288. ['subscribe_time', '>=', strtotime($time[0])],
  289. ['subscribe_time', '<', strtotime($time[1])]
  290. ], '%H', 'unSubscribe'), 'unSubscribe', 'days');
  291. $cumulativeSubscribe = array_column($user->getWechatTrendData($time, [
  292. ['subscribe', '=', 1]
  293. ], '%H', 'cumulativeSubscribe'), 'cumulativeSubscribe', 'days');
  294. $cumulativeUnSubscribe = array_column($user->getWechatTrendData($time, [
  295. ['subscribe', '=', 0], ['subscribe_time', '<>', '']
  296. ], '%H', 'cumulativeUnSubscribe'), 'cumulativeUnSubscribe', 'days');
  297. } elseif ($num != 0) {
  298. $dt_start = strtotime($time[0]);
  299. $dt_end = strtotime($time[1]);
  300. $timeType = '%m-%d';
  301. while ($dt_start <= $dt_end) {
  302. if ($num == 30) {
  303. $xAxis[] = date('Y-m', $dt_start);
  304. $dt_start = strtotime("+1 month", $dt_start);
  305. $timeType = '%Y-%m';
  306. } else {
  307. $xAxis[] = date('m-d', $dt_start);
  308. $dt_start = strtotime("+$num day", $dt_start);
  309. $timeType = '%m-%d';
  310. }
  311. }
  312. $subscribe = array_column($user->getWechatTrendData($time, [
  313. ['subscribe', '=', 1],
  314. ['subscribe_time', '>=', strtotime($time[0])],
  315. ['subscribe_time', '<', strtotime($time[1])]
  316. ], $timeType, 'subscribe'), 'subscribe', 'days');
  317. $unSubscribe = array_column($user->getWechatTrendData($time, [
  318. ['subscribe', '=', 0],
  319. ['subscribe_time', '<>', ''],
  320. ['subscribe_time', '>=', strtotime($time[0])],
  321. ['subscribe_time', '<', strtotime($time[1])]
  322. ], $timeType, 'unSubscribe'), 'unSubscribe', 'days');
  323. $cumulativeSubscribe = array_column($user->getWechatTrendData($time, [
  324. ['subscribe', '=', 1]
  325. ], $timeType, 'cumulativeSubscribe'), 'cumulativeSubscribe', 'days');
  326. $cumulativeUnSubscribe = array_column($user->getWechatTrendData($time, [
  327. ['subscribe', '=', 0], ['subscribe_time', '<>', '']
  328. ], $timeType, 'cumulativeUnSubscribe'), 'cumulativeUnSubscribe', 'days');
  329. }
  330. $data = $series = [];
  331. foreach ($xAxis as $item) {
  332. $data['新增关注用户'][] = $subscribe[$item] ?? 0;
  333. $data['新增取关用户'][] = $unSubscribe[$item] ?? 0;
  334. $data['累计关注用户'][] = $cumulativeSubscribe[$item] ?? 0;
  335. $data['累计取关用户'][] = $cumulativeUnSubscribe[$item] ?? 0;
  336. }
  337. foreach ($data['新增关注用户'] as $keys => $items) {
  338. $data['净增用户数'][] = $data['新增关注用户'][$keys] - $data['新增取关用户'][$keys];
  339. }
  340. foreach ($data as $key => $item) {
  341. $series[] = ['name' => $key, 'value' => $item];
  342. }
  343. return compact('xAxis', 'series');
  344. }
  345. /**
  346. * 用户地域图表
  347. * @param $where
  348. * @return array
  349. */
  350. public function getRegion($where)
  351. {
  352. $time = explode('-', $where['time']);
  353. $channelType = $where['channel_type'];
  354. if (count($time) != 2) throw new AdminException(100100);
  355. /** @var UserVisitServices $userVisit */
  356. $userVisit = app()->make(UserVisitServices::class);
  357. /** @var UserServices $userService */
  358. $userService = app()->make(UserServices::class);
  359. /** @var StoreOrderServices $order */
  360. $order = app()->make(StoreOrderServices::class);
  361. /** @var WechatUserServices $user */
  362. $wechatUser = app()->make(WechatUserServices::class);
  363. $all = $wechatUser->getRegionAll($time, $channelType);
  364. $new = $wechatUser->getRegionNew($time, $channelType);
  365. $visit = $userVisit->getRegion($time, $channelType);
  366. $payPrice = $order->getRegion($time, $channelType);
  367. foreach ($all as $key1 => $item1) {
  368. foreach ($new as $key2 => $item2) {
  369. if ($item1['province'] == $item2['province']) {
  370. $all[$key1]['newNum'] = $item2['newNum'];
  371. unset($new[$key2]);
  372. }
  373. }
  374. }
  375. $all = array_merge($all, $new);
  376. foreach ($all as $key1 => $item1) {
  377. foreach ($visit as $key3 => $item3) {
  378. if ($item1['province'] == $item3['province']) {
  379. $all[$key1]['visitNum'] = $item3['visitNum'];
  380. unset($visit[$key3]);
  381. }
  382. }
  383. }
  384. $all = array_merge($all, $visit);
  385. foreach ($all as $key1 => $item1) {
  386. foreach ($payPrice as $key3 => $item3) {
  387. if ($item1['province'] == $item3['province']) {
  388. $all[$key1]['payPrice'] = $item3['payPrice'];
  389. unset($payPrice[$key3]);
  390. }
  391. }
  392. }
  393. $all = array_merge($all, $payPrice);
  394. foreach ($all as &$item) {
  395. if ($item['province'] == '') $item['province'] = '未知';
  396. if (!isset($item['allNum'])) $item['allNum'] = 0;
  397. if (!isset($item['newNum'])) $item['newNum'] = 0;
  398. if (!isset($item['visitNum'])) $item['visitNum'] = 0;
  399. if (!isset($item['payPrice'])) {
  400. $item['payPrice'] = 0;
  401. } else {
  402. $item['payPrice'] = floatval($item['payPrice']);
  403. }
  404. }
  405. $data = array_values($all);
  406. $last_names = array_column($data, $where['sort']);
  407. array_multisort($last_names, SORT_DESC, $data);
  408. return $data;
  409. }
  410. /**
  411. * 用户性别
  412. * @param $where
  413. * @return mixed
  414. */
  415. public function getSex($where)
  416. {
  417. $time = explode('-', $where['time']);
  418. $channelType = $where['channel_type'];
  419. if (count($time) != 2) throw new AdminException(100100);
  420. /** @var UserWechatuserServices $user */
  421. $wechatUser = app()->make(UserWechatuserServices::class);
  422. $data = $wechatUser->getSex($time, $channelType);
  423. $oneData = [
  424. ['value' => 0, 'name' => '未知', 'name_key' => 0],
  425. ['value' => 0, 'name' => '男', 'name_key' => 1],
  426. ['value' => 0, 'name' => '女', 'name_key' => 2],
  427. ];
  428. foreach ($oneData as &$value) {
  429. foreach ($data as $item) {
  430. if ($item['name'] == $value['name_key']) {
  431. $value['value'] = $item['value'];
  432. break;
  433. }
  434. }
  435. }
  436. return $oneData;
  437. }
  438. }