UserStatisticServices.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\UserAddressServices;
  17. use app\services\user\UserRechargeServices;
  18. use app\services\user\UserServices;
  19. use app\services\user\UserVisitServices;
  20. use app\services\user\UserWechatuserServices;
  21. use app\services\wechat\WechatUserServices;
  22. use crmeb\exceptions\AdminException;
  23. /**
  24. * Class UserStatisticServices
  25. * @package app\services\statistic
  26. */
  27. class UserStatisticServices extends BaseServices
  28. {
  29. /**
  30. * 基本概况
  31. * @param $where
  32. * @return mixed
  33. */
  34. public function getBasic($where)
  35. {
  36. $time = explode('-', $where['time']);
  37. if (count($time) != 2) throw new AdminException('参数错误');
  38. /** @var UserVisitServices $userVisit */
  39. $userVisit = app()->make(UserVisitServices::class);
  40. /** @var UserServices $user */
  41. $user = app()->make(UserServices::class);
  42. /** @var StoreOrderServices $order */
  43. $order = app()->make(StoreOrderServices::class);
  44. /** @var UserRechargeServices $recharge */
  45. $recharge = app()->make(UserRechargeServices::class);
  46. /** @var OtherOrderServices $otherOrder */
  47. $otherOrder = app()->make(OtherOrderServices::class);
  48. $toEndtime = [0, $time[1]];
  49. $cumulativeUserWhere = [['add_time', 'between', $toEndtime], ['user_type', '=', $where['channel_type']]];
  50. $cumulativeRechargePeopleWhere = ['paid' => 1, 'time' => $toEndtime, 'timeKey' => 'pay_time', 'channel_type' => $where['channel_type']];
  51. $cumulativePayPeopleWhere = ['time' => $toEndtime, 'timeKey' => 'pay_time', 'paid' => 1, 'channel_type' => $where['channel_type']];
  52. $now['people'] = $userVisit->getDistinctCount($where, 'uid');//访客数
  53. $now['browse'] = $userVisit->count($where);//访问量
  54. $now['newUser'] = $user->getWithTrashedCount($where['channel_type'] ? ['user_type' => $where['channel_type']] : [], $time);//新增用户数
  55. $now['payPeople'] = $order->getDistinctCount($where + ['paid' => 1, 'timeKey' => 'pay_time'], 'uid');//成交用户数
  56. $now['payPercent'] = bcmul((string)($now['people'] > 0 ? bcdiv($now['payPeople'], $now['people'], 4) : 0), '100', 2);//访问-付款转化率
  57. $now['payUser'] = $otherOrder->getDistinctCount($where + ['paid' => 1], 'uid');//激活付费会员数
  58. $now['rechargePeople'] = $recharge->getDistinctCount($where + ['paid' => 1, 'timeKey' => 'pay_time'], 'uid');//充值用户数
  59. $totalPayPrice = $order->sum($where + ['paid' => 1], 'pay_price', true);
  60. $now['payPrice'] = floatval($now['payPeople'] > 0 ? bcdiv($totalPayPrice, $now['payPeople'], 2) : 0);//客单价
  61. $now['cumulativeUser'] = $user->getWithTrashedCount($where['channel_type'] ? ['user_type' => $where['channel_type']] : [], $toEndtime);//累计用户数
  62. $now['cumulativePayUser'] = count($otherOrder->getPayUserCount(strtotime($time[1]) + 86400, $where['channel_type']));//到截至日期有付费会员状态的会员数
  63. $now['cumulativeRechargePeople'] = $recharge->getDistinctCount($cumulativeRechargePeopleWhere, 'uid');//累计充值用户数
  64. $now['cumulativePayPeople'] = $order->getDistinctCount($cumulativePayPeopleWhere, 'uid');//累计成交用户数
  65. $dayNum = (strtotime($time[1]) - strtotime($time[0])) / 86400 + 1;
  66. $lastTime = [
  67. date("Y/m/d", strtotime("-$dayNum days", strtotime($time[0]))),
  68. date("Y/m/d", strtotime("-1 days", strtotime($time[0])))
  69. ];
  70. $where['time'] = implode('-', $lastTime);
  71. $toEndtime = implode('-', [0, $lastTime[1]]);
  72. $last['people'] = $userVisit->getDistinctCount($where, 'uid');//访客数
  73. $last['browse'] = $userVisit->count($where);//访问量
  74. $last['newUser'] = $user->count($where + ['user_type' => $where['channel_type']]);//新增用户数
  75. $last['payPeople'] = $order->getDistinctCount($where + ['paid' => 1, 'timeKey' => 'pay_time'], 'uid');//成交用户数
  76. $last['payPercent'] = bcmul((string)($last['people'] > 0 ? bcdiv($last['payPeople'], $last['people'], 4) : 0), '100', 2);//访问-付款转化率
  77. $last['payUser'] = $otherOrder->getDistinctCount($where + ['paid' => 1], 'uid');//激活付费会员数
  78. $last['rechargePeople'] = $recharge->getDistinctCount($where + ['paid' => 1, 'timeKey' => 'pay_time'], 'uid');//充值用户数
  79. $totalPayPrice = $order->sum($where + ['paid' => 1], 'pay_price', true);
  80. $last['payPrice'] = floatval($last['payPeople'] > 0 ? bcdiv($totalPayPrice, $last['payPeople'], 2) : 0);//客单价
  81. $cumulativeUserWhere['time'] = $toEndtime;
  82. $last['cumulativeUser'] = $user->count($cumulativeUserWhere);//累计用户数
  83. $last['cumulativePayUser'] = count($otherOrder->getPayUserCount(strtotime($lastTime[1]) + 86400, $where['channel_type']));//到截至日期有付费会员状态的会员数
  84. $cumulativeRechargePeopleWhere['time'] = $toEndtime;
  85. $last['cumulativeRechargePeople'] = $recharge->getDistinctCount($cumulativeRechargePeopleWhere, 'uid');//累计充值用户数
  86. $cumulativePayPeopleWhere['time'] = $toEndtime;
  87. $last['cumulativePayPeople'] = $order->getDistinctCount($cumulativePayPeopleWhere, 'uid');//累计成交用户数
  88. //组合数据,计算环比
  89. $data = [];
  90. foreach ($now as $key => $item) {
  91. $data[$key]['num'] = $item;
  92. $data[$key]['last_num'] = $last[$key];
  93. $num = $last[$key] > 0 ? $last[$key] : 1;
  94. $data[$key]['percent'] = bcmul((string)bcdiv((string)($item - $last[$key]), (string)$num, 4), 100, 2);
  95. }
  96. return $data;
  97. }
  98. /**
  99. * 用户趋势
  100. * @param $where
  101. * @param $excel
  102. * @return mixed
  103. */
  104. public function getTrend($where, $excel = false)
  105. {
  106. $time = explode('-', $where['time']);
  107. $channelType = $where['channel_type'];
  108. if (count($time) != 2) throw new AdminException('参数错误');
  109. $dayCount = (strtotime($time[1]) - strtotime($time[0])) / 86400 + 1;
  110. $data = [];
  111. if ($dayCount == 1) {
  112. $data = $this->trend($time, $channelType, 0, $excel);
  113. } elseif ($dayCount > 1 && $dayCount <= 31) {
  114. $data = $this->trend($time, $channelType, 1, $excel);
  115. } elseif ($dayCount > 31 && $dayCount <= 92) {
  116. $data = $this->trend($time, $channelType, 3, $excel);
  117. } elseif ($dayCount > 92) {
  118. $data = $this->trend($time, $channelType, 30, $excel);
  119. }
  120. return $data;
  121. }
  122. /**
  123. * 用户趋势
  124. * @param $time
  125. * @param $channelType
  126. * @param $num
  127. * @param $excel
  128. * @return array
  129. */
  130. public function trend($time, $channelType, $num, $excel)
  131. {
  132. /** @var UserServices $user */
  133. $user = app()->make(UserServices::class);
  134. /** @var UserVisitServices $userVisit */
  135. $userVisit = app()->make(UserVisitServices::class);
  136. /** @var StoreOrderServices $order */
  137. $order = app()->make(StoreOrderServices::class);
  138. /** @var UserRechargeServices $recharge */
  139. $recharge = app()->make(UserRechargeServices::class);
  140. /** @var OtherOrderServices $otherOrder */
  141. $otherOrder = app()->make(OtherOrderServices::class);
  142. $newPeople = $visitPeople = $paidPeople = $rechargePeople = $vipPeople = [];
  143. $newPeople['name'] = '新增用户数';
  144. $visitPeople['name'] = '访客数';
  145. $paidPeople['name'] = '成交用户数';
  146. $rechargePeople['name'] = '充值用户';
  147. $vipPeople['name'] = '新增付费用户数';
  148. if ($num == 0) {
  149. $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'];
  150. $timeType = '%H';
  151. } elseif ($num != 0) {
  152. $dt_start = strtotime($time[0]);
  153. $dt_end = strtotime($time[1]);
  154. while ($dt_start <= $dt_end) {
  155. if ($num == 30) {
  156. $xAxis[] = date('Y-m', $dt_start);
  157. $dt_start = strtotime("+1 month", $dt_start);
  158. $timeType = '%Y-%m';
  159. } else {
  160. $xAxis[] = date('Y-m-d', $dt_start);
  161. $dt_start = strtotime("+$num day", $dt_start);
  162. $timeType = '%Y-%m-%d';
  163. }
  164. }
  165. }
  166. $visitPeople = array_column($userVisit->getTrendData($time, $channelType, $timeType, 'count(distinct(uid))'), 'num', 'days');
  167. $newPeople = array_column($user->getTrendData($time, $channelType, $timeType), 'num', 'days');
  168. $paidPeople = array_column($order->getTrendData($time, $channelType, $timeType, 'count(distinct(uid))'), 'num', 'days');
  169. $rechargePeople = array_column($recharge->getTrendData($time, $channelType, $timeType, 'count(distinct(uid))'), 'num', 'days');
  170. $vipPeople = array_column($otherOrder->getTrendData($time, $channelType, $timeType, 'count(distinct(uid))'), 'num', 'days');
  171. if ($excel) {
  172. $browsePeople = array_column($userVisit->getTrendData($time, $channelType, $timeType, 'count(id)'), 'num', 'days');
  173. $paidPrice = array_column($order->getTrendData($time, $channelType, $timeType, 'sum(pay_price)'), 'num', 'days');
  174. foreach ($xAxis as &$item) {
  175. if (isset($paidPeople[$item]) && isset($visitPeople[$item])) {
  176. $changes = bcmul(bcdiv((string)$paidPeople[$item], (string)$visitPeople[$item], 4), 100, 2);
  177. $changes = $changes > 100 ? 100 : $changes;
  178. } else {
  179. $changes = 0;
  180. }
  181. $data[] = [
  182. 'time' => $item,
  183. 'user' => $visitPeople[$item] ?? 0,
  184. 'browse' => $browsePeople[$item] ?? 0,
  185. 'new' => $newPeople[$item] ?? 0,
  186. 'paid' => $paidPeople[$item] ?? 0,
  187. 'changes' => $changes,
  188. 'vip' => $vipPeople[$item] ?? 0,
  189. 'recharge' => $rechargePeople[$item] ?? 0,
  190. 'payPrice' => isset($paidPrice[$item]) && isset($paidPeople[$item]) ? bcdiv((string)$paidPrice[$item], (string)$paidPeople[$item], 2) : 0,
  191. // 'cumulativeUser' => $user->getCount($userWhere),
  192. // 'cumulativeVip' => $otherOrder->getPayUserCount($endTime, $channelType),
  193. // 'cumulativeRecharge' => $recharge->getCount($rechargeWhere),
  194. // 'cumulativePaid' => $order->getCount($payWhere),
  195. ];
  196. }
  197. /** @var ExportServices $exportService */
  198. $exportService = app()->make(ExportServices::class);
  199. return $exportService->userTrade($data);
  200. } else {
  201. $data = $series = [];
  202. foreach ($xAxis as $item) {
  203. $data['新增用户数'][] = isset($newPeople[$item]) ? intval($newPeople[$item]) : 0;
  204. $data['访客数'][] = isset($visitPeople[$item]) ? intval($visitPeople[$item]) : 0;
  205. $data['成交用户数'][] = isset($paidPeople[$item]) ? intval($paidPeople[$item]) : 0;
  206. $data['充值用户'][] = isset($rechargePeople[$item]) ? intval($rechargePeople[$item]) : 0;
  207. $data['新增付费用户数'][] = isset($vipPeople[$item]) ? intval($vipPeople[$item]) : 0;
  208. }
  209. foreach ($data as $key => $item) {
  210. $series[] = ['name' => $key, 'value' => $item];
  211. }
  212. return compact('xAxis', 'series');
  213. }
  214. }
  215. /**
  216. * 微信用户信息
  217. * @param $where
  218. * @return array
  219. */
  220. public function getWechat($where)
  221. {
  222. $time = explode('-', $where['time']);
  223. if (count($time) != 2) throw new AdminException('参数错误');
  224. /** @var WechatUserServices $user */
  225. $user = app()->make(WechatUserServices::class);
  226. $now['subscribe'] = $user->getCount([
  227. ['subscribe', '=', 1],
  228. ['user_type', '=', 'wechat'],
  229. ['subscribe_time', '>=', strtotime($time[0])],
  230. ['subscribe_time', '<', strtotime($time[1]) + 86400]
  231. ]);
  232. $now['unSubscribe'] = $user->getCount([
  233. ['subscribe', '=', 0],
  234. ['user_type', '=', 'wechat'],
  235. ['subscribe_time', '<>', ''],
  236. ['subscribe_time', '>=', strtotime($time[0])],
  237. ['subscribe_time', '<', strtotime($time[1]) + 86400]
  238. ]);
  239. $now['increaseSubscribe'] = $now['subscribe'] - $now['unSubscribe'];
  240. $now['cumulativeSubscribe'] = $user->getCount([['subscribe', '=', 1], ['user_type', '=', 'wechat']]);
  241. $now['cumulativeUnSubscribe'] = $user->getCount([
  242. ['subscribe', '=', 0],
  243. ['user_type', '=', 'wechat'],
  244. ['subscribe_time', '<>', '']
  245. ]);
  246. $dayNum = (strtotime($time[1]) - strtotime($time[0])) / 86400 + 1;
  247. $lastTime = array(
  248. date("Y/m/d", strtotime("-$dayNum days", strtotime($time[0]))),
  249. date("Y/m/d", strtotime("-1 days", strtotime($time[0])))
  250. );
  251. $last['subscribe'] = $user->getCount([
  252. ['subscribe', '=', 1],
  253. ['user_type', '=', 'wechat'],
  254. ['subscribe_time', '>=', strtotime($lastTime[0])],
  255. ['subscribe_time', '<', strtotime($lastTime[1]) + 86400]
  256. ]);
  257. $last['unSubscribe'] = $user->getCount([
  258. ['subscribe', '=', 0],
  259. ['user_type', '=', 'wechat'],
  260. ['subscribe_time', '<>', ''],
  261. ['subscribe_time', '>=', strtotime($lastTime[0])],
  262. ['subscribe_time', '<', strtotime($lastTime[1]) + 86400]
  263. ]);
  264. $last['increaseSubscribe'] = $last['subscribe'] - $last['unSubscribe'];
  265. $last['cumulativeSubscribe'] = $user->getCount([['subscribe', '=', 1], ['user_type', '=', 'wechat']]);
  266. $last['cumulativeUnSubscribe'] = $user->getCount([
  267. ['subscribe', '=', 0],
  268. ['user_type', '=', 'wechat'],
  269. ['subscribe_time', '<>', '']
  270. ]);
  271. //组合数据,计算环比
  272. $data = [];
  273. foreach ($now as $key => $item) {
  274. $data[$key]['num'] = $item;
  275. $num = $last[$key] ?: 1;
  276. $data[$key]['percent'] = bcmul(bcdiv(($item - $last[$key]), $num, 4), 100, 2);
  277. }
  278. return $data;
  279. }
  280. /**
  281. * 微信用户趋势
  282. * @param $where
  283. * @return array
  284. */
  285. public function getWechatTrend($where)
  286. {
  287. $time = explode('-', $where['time']);
  288. $channelType = $where['channel_type'];
  289. if (count($time) != 2) throw new AdminException('参数错误');
  290. $dayCount = (strtotime($time[1]) - strtotime($time[0])) / 86400 + 1;
  291. $data = [];
  292. if ($dayCount == 1) {
  293. $data = $this->wechatTrend($time, 0);
  294. } elseif ($dayCount > 1 && $dayCount <= 31) {
  295. $data = $this->wechatTrend($time, 1);
  296. } elseif ($dayCount > 31 && $dayCount <= 92) {
  297. $data = $this->wechatTrend($time, 3);
  298. } elseif ($dayCount > 92) {
  299. $data = $this->wechatTrend($time, 30);
  300. }
  301. return $data;
  302. }
  303. /**
  304. * 微信用户趋势
  305. * @param $time
  306. * @param $num
  307. * @return array
  308. */
  309. public function wechatTrend($time, $num)
  310. {
  311. /** @var WechatUserServices $user */
  312. $user = app()->make(WechatUserServices::class);
  313. $subscribe = $unSubscribe = $increaseSubscribe = $cumulativeSubscribe = $cumulativeUnSubscribe = [];
  314. if ($num == 0) {
  315. $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'];
  316. $subscribe = array_column($user->getWechatTrendData($time, [
  317. ['subscribe', '=', 1],
  318. ['subscribe_time', '>=', strtotime($time[0])],
  319. ['subscribe_time', '<', strtotime($time[1]) + 86400]
  320. ], '%H', 'subscribe'), 'subscribe', 'days');
  321. $unSubscribe = array_column($user->getWechatTrendData($time, [
  322. ['subscribe', '=', 0],
  323. ['subscribe_time', '<>', ''],
  324. ['subscribe_time', '>=', strtotime($time[0])],
  325. ['subscribe_time', '<', strtotime($time[1]) + 86400]
  326. ], '%H', 'unSubscribe'), 'unSubscribe', 'days');
  327. $cumulativeSubscribe = array_column($user->getWechatTrendData($time, [
  328. ['subscribe', '=', 1]
  329. ], '%H', 'cumulativeSubscribe'), 'cumulativeSubscribe', 'days');
  330. $cumulativeUnSubscribe = array_column($user->getWechatTrendData($time, [
  331. ['subscribe', '=', 0], ['subscribe_time', '<>', '']
  332. ], '%H', 'cumulativeUnSubscribe'), 'cumulativeUnSubscribe', 'days');
  333. } elseif ($num != 0) {
  334. $dt_start = strtotime($time[0]);
  335. $dt_end = strtotime($time[1]);
  336. while ($dt_start <= $dt_end) {
  337. if ($num == 30) {
  338. $xAxis[] = date('Y-m', $dt_start);
  339. $dt_start = strtotime("+1 month", $dt_start);
  340. $timeType = '%Y-%m';
  341. } else {
  342. $xAxis[] = date('m-d', $dt_start);
  343. $dt_start = strtotime("+$num day", $dt_start);
  344. $timeType = '%m-%d';
  345. }
  346. }
  347. $subscribe = array_column($user->getWechatTrendData($time, [
  348. ['subscribe', '=', 1],
  349. ['subscribe_time', '>=', strtotime($time[0])],
  350. ['subscribe_time', '<', strtotime($time[1]) + 86400]
  351. ], $timeType, 'subscribe'), 'subscribe', 'days');
  352. $unSubscribe = array_column($user->getWechatTrendData($time, [
  353. ['subscribe', '=', 0],
  354. ['subscribe_time', '<>', ''],
  355. ['subscribe_time', '>=', strtotime($time[0])],
  356. ['subscribe_time', '<', strtotime($time[1]) + 86400]
  357. ], $timeType, 'unSubscribe'), 'unSubscribe', 'days');
  358. $cumulativeSubscribe = array_column($user->getWechatTrendData($time, [
  359. ['subscribe', '=', 1]
  360. ], $timeType, 'cumulativeSubscribe'), 'cumulativeSubscribe', 'days');
  361. $cumulativeUnSubscribe = array_column($user->getWechatTrendData($time, [
  362. ['subscribe', '=', 0], ['subscribe_time', '<>', '']
  363. ], $timeType, 'cumulativeUnSubscribe'), 'cumulativeUnSubscribe', 'days');
  364. }
  365. $data = $series = [];
  366. foreach ($xAxis as $item) {
  367. $data['新增关注用户'][] = isset($subscribe[$item]) ? $subscribe[$item] : 0;
  368. $data['新增取关用户'][] = isset($unSubscribe[$item]) ? $unSubscribe[$item] : 0;
  369. $data['累计关注用户'][] = isset($cumulativeSubscribe[$item]) ? $cumulativeSubscribe[$item] : 0;
  370. $data['累计取关用户'][] = isset($cumulativeUnSubscribe[$item]) ? $cumulativeUnSubscribe[$item] : 0;
  371. }
  372. foreach ($data['新增关注用户'] as $keys => $items) {
  373. $data['净增用户数'][] = $data['新增关注用户'][$keys] - $data['新增取关用户'][$keys];
  374. }
  375. foreach ($data as $key => $item) {
  376. $series[] = ['name' => $key, 'value' => $item];
  377. }
  378. return compact('xAxis', 'series');
  379. }
  380. /**
  381. * 用户地域图表
  382. * @param $where
  383. * @return array
  384. */
  385. public function getRegionV1($where)
  386. {
  387. $time = explode('-', $where['time']);
  388. $channelType = $where['channel_type'];
  389. if (count($time) != 2) throw new AdminException('参数错误');
  390. /** @var UserVisitServices $userVisit */
  391. $userVisit = app()->make(UserVisitServices::class);
  392. /** @var StoreOrderServices $order */
  393. $order = app()->make(StoreOrderServices::class);
  394. /** @var UserAddressServices $useAddress */
  395. $useAddress = app()->make(UserAddressServices::class);
  396. $all = $useAddress->getRegionAll($time, $channelType);
  397. $new = $useAddress->getRegionNew($time, $channelType);
  398. $visit = $userVisit->getRegion($time, $channelType);
  399. $payPrice = $order->getRegion($time, $channelType);
  400. if ($new) $new = array_combine(array_column($new, 'province'), $new);
  401. if ($visit) $visit = array_combine(array_column($visit, 'province'), $visit);
  402. $newPayPrice = [];
  403. if ($payPrice) {
  404. foreach ($payPrice as $item) {
  405. if (isset($newPayPrice[$item['province']])) {
  406. $newPayPrice[$item['province']]['payPrice'] = bcadd((string)$newPayPrice[$item['province']]['payPrice'], (string)$item['payPrice'], 2);
  407. } else {
  408. $newPayPrice[$item['province']] = ['province' => $item['province'], 'payPrice' => $item['payPrice']];
  409. }
  410. }
  411. }
  412. if (isset($visit[""])) {
  413. array_push($all, ["province" => '']);
  414. }
  415. $handleAddress = function ($p, $visitArr) {
  416. return $visitArr[$p.'市'] ?? $visitArr[$p.'省'] ?? $visitArr[$p.'自治区'] ?? [];
  417. };
  418. foreach ($all as &$item) {
  419. if (!isset($item['allNum'])) $item['allNum'] = 0;
  420. $item['newNum'] = intval($new[$item['province']]['newNum'] ?? 0);
  421. $item['visitNum'] = intval($handleAddress($item['province'], $visit)['visitNum'] ?? 0);
  422. $item['payPrice'] = floatval($newPayPrice[$item['province']]['payPrice'] ?? 0);
  423. if ($item['province'] == '') $item['province'] = '未知';
  424. }
  425. $data = array_values($all);
  426. $last_names = array_column($data, $where['sort']);
  427. array_multisort($last_names, SORT_DESC, $data);
  428. return $data;
  429. }
  430. /**
  431. * 用户地域图表
  432. * @param $where
  433. * @return array
  434. */
  435. public function getRegion($where)
  436. {
  437. $time = explode('-', $where['time']);
  438. $channelType = $where['channel_type'];
  439. if (count($time) != 2) throw new AdminException('参数错误');
  440. /** @var UserVisitServices $userVisit */
  441. $userVisit = app()->make(UserVisitServices::class);
  442. /** @var UserServices $userService */
  443. $userService = app()->make(UserServices::class);
  444. /** @var StoreOrderServices $order */
  445. $order = app()->make(StoreOrderServices::class);
  446. /** @var UserWechatuserServices $wechatUser */
  447. $wechatUser = app()->make(UserWechatuserServices::class);
  448. $all = $wechatUser->getRegionAll($time, $channelType);
  449. $new = $wechatUser->getRegionNew($time, $channelType);
  450. $visit = $userVisit->getRegion($time, $channelType);
  451. $payPrice = $order->getRegion($time, $channelType);
  452. foreach ($all as $key1 => $item1) {
  453. foreach ($new as $key2 => $item2) {
  454. if ($item1['province'] == $item2['province']) {
  455. $all[$key1]['newNum'] = $item2['newNum'];
  456. unset($new[$key2]);
  457. }
  458. }
  459. }
  460. $all = array_merge($all, $new);
  461. foreach ($all as $key1 => $item1) {
  462. foreach ($visit as $key3 => $item3) {
  463. if ($item1['province'] == $item3['province']) {
  464. $all[$key1]['visitNum'] = $item3['visitNum'];
  465. unset($visit[$key3]);
  466. }
  467. }
  468. }
  469. $all = array_merge($all, $visit);
  470. foreach ($all as $key1 => $item1) {
  471. foreach ($payPrice as $key3 => $item3) {
  472. if ($item1['province'] == $item3['province']) {
  473. $all[$key1]['payPrice'] = $item3['payPrice'];
  474. unset($payPrice[$key3]);
  475. }
  476. }
  477. }
  478. $all = array_merge($all, $payPrice);
  479. foreach ($all as &$item) {
  480. if ($item['province'] == '') $item['province'] = '未知';
  481. if (!isset($item['allNum'])) $item['allNum'] = 0;
  482. if (!isset($item['newNum'])) $item['newNum'] = 0;
  483. if (!isset($item['visitNum'])) $item['visitNum'] = 0;
  484. if (!isset($item['payPrice'])) {
  485. $item['payPrice'] = 0;
  486. } else {
  487. $item['payPrice'] = floatval($item['payPrice']);
  488. }
  489. }
  490. $data = array_values($all);
  491. $last_names = array_column($data, $where['sort']);
  492. array_multisort($last_names, SORT_DESC, $data);
  493. return $data;
  494. }
  495. /**
  496. * 用户性别
  497. * @param $where
  498. * @return mixed
  499. */
  500. public function getSex($where)
  501. {
  502. $time = explode('-', $where['time']);
  503. $channelType = $where['channel_type'];
  504. if (count($time) != 2) throw new AdminException('参数错误');
  505. /** @var UserWechatuserServices $wechatUser */
  506. $wechatUser = app()->make(UserWechatuserServices::class);
  507. $data = $wechatUser->getSex($time, $channelType);
  508. $oneData = [
  509. ['value' => 0, 'name' => '未知', 'name_key' => 0],
  510. ['value' => 0, 'name' => '男', 'name_key' => 1],
  511. ['value' => 0, 'name' => '女', 'name_key' => 2],
  512. ];
  513. foreach ($data as $item) {
  514. $u_name = (int)($item['u_name'] ?? 0);
  515. $u_name = $u_name > 2 ? 0 : $u_name;
  516. if ($u_name > 0) {
  517. $oneData[$u_name]['value'] = (int)bcadd((string)$oneData[$u_name]['value'], '1');
  518. } else {
  519. $w_name = (int)($item['w_name'] ?? 0);
  520. $w_name = $w_name > 2 ? 0 : $w_name;
  521. $oneData[$w_name]['value'] = (int)bcadd((string)$oneData[$w_name]['value'], '1');
  522. }
  523. }
  524. return $oneData;
  525. }
  526. }