UserStatisticServices.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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(100100);
  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 UserRechargeServices $recharge */
  44. $recharge = app()->make(UserRechargeServices::class);
  45. /** @var OtherOrderServices $otherOrder */
  46. $otherOrder = app()->make(OtherOrderServices::class);
  47. $toEndtime = implode('-', [0, $time[1]]);
  48. $cumulativeUserWhere = ['time' => $toEndtime, 'user_type' => $where['channel_type']];
  49. $cumulativeRechargePeopleWhere = ['time' => $toEndtime, 'timeKey' => 'pay_time', 'channel_type' => $where['channel_type']];
  50. $cumulativePayPeopleWhere = ['time' => $toEndtime, 'timeKey' => 'pay_time', 'paid' => 1, 'channel_type' => $where['channel_type']];
  51. $now['people'] = $userVisit->getDistinctCount($where, 'uid');//访客数
  52. $now['browse'] = $userVisit->count($where);//访问量
  53. $now['newUser'] = $user->count($where + ['user_type' => $where['channel_type']]);//新增用户数
  54. $now['payPeople'] = $order->getDistinctCount($where + ['paid' => 1], 'uid');//成交用户数
  55. $now['payPercent'] = bcmul((string)($now['people'] > 0 ? bcdiv($now['payPeople'], $now['people'], 4) : 0), '100', 2);//访问-付款转化率
  56. $now['payUser'] = $otherOrder->getDistinctCount($where, 'uid');//激活付费会员数
  57. $now['rechargePeople'] = $recharge->getDistinctCount($where + ['timeKey' => 'pay_time'], 'uid');//充值用户数
  58. $totalPayPrice = $order->sum($where + ['paid' => 1], 'pay_price', true);
  59. $now['payPrice'] = floatval($now['payPeople'] > 0 ? bcdiv($totalPayPrice, $now['payPeople'], 2) : 0);//客单价
  60. $now['cumulativeUser'] = $user->count($cumulativeUserWhere);//累计用户数
  61. $now['cumulativePayUser'] = count($otherOrder->getPayUserCount(strtotime($time[1]), $where['channel_type']));//到截至日期有付费会员状态的会员数
  62. $now['cumulativeRechargePeople'] = $recharge->getDistinctCount($cumulativeRechargePeopleWhere, 'uid');//累计充值用户数
  63. $now['cumulativePayPeople'] = $order->getDistinctCount($cumulativePayPeopleWhere, 'uid');//累计成交用户数
  64. $dayNum = (strtotime($time[1]) - strtotime($time[0])) / 86400 + 1;
  65. $lastTime = [
  66. date("Y/m/d", strtotime("-$dayNum days", strtotime($time[0]))),
  67. date("Y/m/d", strtotime("-1 days", strtotime($time[0])))
  68. ];
  69. $where['time'] = implode('-', $lastTime);
  70. $toEndtime = implode('-', [0, $lastTime[1]]);
  71. $last['people'] = $userVisit->getDistinctCount($where, 'uid');//访客数
  72. $last['browse'] = $userVisit->count($where);//访问量
  73. $last['newUser'] = $user->count($where + ['user_type' => $where['channel_type']]);//新增用户数
  74. $last['payPeople'] = $order->getDistinctCount($where + ['paid' => 1], 'uid');//成交用户数
  75. $last['payPercent'] = bcmul((string)($last['people'] > 0 ? bcdiv($last['payPeople'], $last['people'], 4) : 0), '100', 2);//访问-付款转化率
  76. $last['payUser'] = $otherOrder->getDistinctCount($where, 'uid');//激活付费会员数
  77. $last['rechargePeople'] = $recharge->getDistinctCount($where + ['timeKey' => 'pay_time'], 'uid');//充值用户数
  78. $totalPayPrice = $order->sum($where + ['paid' => 1], 'pay_price', true);
  79. $last['payPrice'] = floatval($last['payPeople'] > 0 ? bcdiv($totalPayPrice, $last['payPeople'], 2) : 0);//客单价
  80. $cumulativeUserWhere['time'] = $toEndtime;
  81. $last['cumulativeUser'] = $user->count($cumulativeUserWhere);//累计用户数
  82. $last['cumulativePayUser'] = count($otherOrder->getPayUserCount(strtotime($lastTime[1]) + 86400, $where['channel_type']));//到截至日期有付费会员状态的会员数
  83. $cumulativeRechargePeopleWhere['time'] = $toEndtime;
  84. $last['cumulativeRechargePeople'] = $recharge->getDistinctCount($cumulativeRechargePeopleWhere, 'uid');//累计充值用户数
  85. $cumulativePayPeopleWhere['time'] = $toEndtime;
  86. $last['cumulativePayPeople'] = $order->getDistinctCount($cumulativePayPeopleWhere, 'uid');//累计成交用户数
  87. //组合数据,计算环比
  88. $data = [];
  89. foreach ($now as $key => $item) {
  90. $data[$key]['num'] = $item;
  91. $data[$key]['last_num'] = $last[$key];
  92. $num = $last[$key] > 0 ? $last[$key] : 1;
  93. $data[$key]['percent'] = bcmul((string)bcdiv((string)($item - $last[$key]), (string)$num, 4), 100, 2);
  94. }
  95. return $data;
  96. }
  97. /**
  98. * 用户趋势
  99. * @param $where
  100. * @param $excel
  101. * @return mixed
  102. */
  103. public function getTrend($where, $excel = false)
  104. {
  105. $time = explode('-', $where['time']);
  106. $channelType = $where['channel_type'];
  107. if (count($time) != 2) throw new AdminException(100100);
  108. $dayCount = (strtotime($time[1]) - strtotime($time[0])) / 86400 + 1;
  109. $data = [];
  110. if ($dayCount == 1) {
  111. $data = $this->trend($time, $channelType, 0, $excel);
  112. } elseif ($dayCount > 1 && $dayCount <= 31) {
  113. $data = $this->trend($time, $channelType, 1, $excel);
  114. } elseif ($dayCount > 31 && $dayCount <= 92) {
  115. $data = $this->trend($time, $channelType, 3, $excel);
  116. } elseif ($dayCount > 92) {
  117. $data = $this->trend($time, $channelType, 30, $excel);
  118. }
  119. return $data;
  120. }
  121. /**
  122. * 用户趋势
  123. * @param $time
  124. * @param $channelType
  125. * @param $num
  126. * @param $excel
  127. * @return array
  128. */
  129. public function trend($time, $channelType, $num, $excel)
  130. {
  131. /** @var UserServices $user */
  132. $user = app()->make(UserServices::class);
  133. /** @var UserVisitServices $userVisit */
  134. $userVisit = app()->make(UserVisitServices::class);
  135. /** @var StoreOrderServices $order */
  136. $order = app()->make(StoreOrderServices::class);
  137. /** @var UserRechargeServices $recharge */
  138. $recharge = app()->make(UserRechargeServices::class);
  139. /** @var OtherOrderServices $otherOrder */
  140. $otherOrder = app()->make(OtherOrderServices::class);
  141. $newPeople = $visitPeople = $paidPeople = $rechargePeople = $vipPeople = [];
  142. $newPeople['name'] = '新增用户数';
  143. $visitPeople['name'] = '访客数';
  144. $paidPeople['name'] = '成交用户数';
  145. $rechargePeople['name'] = '充值用户';
  146. $vipPeople['name'] = '新增付费用户数';
  147. if ($num == 0) {
  148. $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'];
  149. $timeType = '%H';
  150. } elseif ($num != 0) {
  151. $dt_start = strtotime($time[0]);
  152. $dt_end = strtotime($time[1]);
  153. while ($dt_start <= $dt_end) {
  154. if ($num == 30) {
  155. $xAxis[] = date('Y-m', $dt_start);
  156. $dt_start = strtotime("+1 month", $dt_start);
  157. $timeType = '%Y-%m';
  158. } else {
  159. $xAxis[] = date('m-d', $dt_start);
  160. $dt_start = strtotime("+$num day", $dt_start);
  161. $timeType = '%m-%d';
  162. }
  163. }
  164. }
  165. $visitPeople = array_column($userVisit->getTrendData($time, $channelType, $timeType, 'count(distinct(uid))'), 'num', 'days');
  166. $newPeople = array_column($user->getTrendData($time, $channelType, $timeType), 'num', 'days');
  167. $paidPeople = array_column($order->getTrendData($time, $channelType, $timeType, 'count(distinct(uid))'), 'num', 'days');
  168. $rechargePeople = array_column($recharge->getTrendData($time, $channelType, $timeType), 'num', 'days');
  169. $vipPeople = array_column($otherOrder->getTrendData($time, $channelType, $timeType), 'num', 'days');
  170. if ($excel) {
  171. $data = [];
  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. $url = $exportService->userTrade($data);
  200. return compact('url');
  201. } else {
  202. $data = $series = [];
  203. foreach ($xAxis as $item) {
  204. $data['新增用户数'][] = isset($newPeople[$item]) ? intval($newPeople[$item]) : 0;
  205. $data['访客数'][] = isset($visitPeople[$item]) ? intval($visitPeople[$item]) : 0;
  206. $data['成交用户数'][] = isset($paidPeople[$item]) ? intval($paidPeople[$item]) : 0;
  207. $data['充值用户'][] = isset($rechargePeople[$item]) ? intval($rechargePeople[$item]) : 0;
  208. $data['新增付费用户数'][] = isset($vipPeople[$item]) ? intval($vipPeople[$item]) : 0;
  209. }
  210. foreach ($data as $key => $item) {
  211. $series[] = ['name' => $key, 'value' => $item];
  212. }
  213. return compact('xAxis', 'series');
  214. }
  215. }
  216. /**
  217. * 微信用户信息
  218. * @param $where
  219. * @return array
  220. */
  221. public function getWechat($where)
  222. {
  223. $time = explode('-', $where['time']);
  224. if (count($time) != 2) throw new AdminException(100100);
  225. /** @var WechatUserServices $user */
  226. $user = app()->make(WechatUserServices::class);
  227. $now['subscribe'] = $user->getCount([
  228. ['subscribe', '=', 1],
  229. ['user_type', '=', 'wechat'],
  230. ['subscribe_time', '>=', strtotime($time[0])],
  231. ['subscribe_time', '<', strtotime($time[1])]
  232. ]);
  233. $now['unSubscribe'] = $user->getCount([
  234. ['subscribe', '=', 0],
  235. ['user_type', '=', 'wechat'],
  236. ['subscribe_time', '<>', ''],
  237. ['subscribe_time', '>=', strtotime($time[0])],
  238. ['subscribe_time', '<', strtotime($time[1])]
  239. ]);
  240. $now['increaseSubscribe'] = $now['subscribe'] - $now['unSubscribe'];
  241. $now['cumulativeSubscribe'] = $user->getCount([['subscribe', '=', 1], ['user_type', '=', 'wechat']]);
  242. $now['cumulativeUnSubscribe'] = $user->getCount([
  243. ['subscribe', '=', 0],
  244. ['user_type', '=', 'wechat'],
  245. ['subscribe_time', '<>', '']
  246. ]);
  247. $dayNum = (strtotime($time[1]) - strtotime($time[0])) / 86400 + 1;
  248. $lastTime = array(
  249. date("Y/m/d", strtotime("-$dayNum days", strtotime($time[0]))),
  250. date("Y/m/d", strtotime("-1 days", strtotime($time[0])))
  251. );
  252. $last['subscribe'] = $user->getCount([
  253. ['subscribe', '=', 1],
  254. ['user_type', '=', 'wechat'],
  255. ['subscribe_time', '>=', strtotime($lastTime[0])],
  256. ['subscribe_time', '<', strtotime($lastTime[1])]
  257. ]);
  258. $last['unSubscribe'] = $user->getCount([
  259. ['subscribe', '=', 0],
  260. ['user_type', '=', 'wechat'],
  261. ['subscribe_time', '<>', ''],
  262. ['subscribe_time', '>=', strtotime($lastTime[0])],
  263. ['subscribe_time', '<', strtotime($lastTime[1])]
  264. ]);
  265. $last['increaseSubscribe'] = $last['subscribe'] - $last['unSubscribe'];
  266. $last['cumulativeSubscribe'] = $user->getCount([['subscribe', '=', 1], ['user_type', '=', 'wechat']]);
  267. $last['cumulativeUnSubscribe'] = $user->getCount([
  268. ['subscribe', '=', 0],
  269. ['user_type', '=', 'wechat'],
  270. ['subscribe_time', '<>', '']
  271. ]);
  272. //组合数据,计算环比
  273. $data = [];
  274. foreach ($now as $key => $item) {
  275. $data[$key]['num'] = $item;
  276. $num = $last[$key] ?: 1;
  277. $data[$key]['percent'] = bcmul(bcdiv(($item - $last[$key]), $num, 4), 100, 2);
  278. }
  279. return $data;
  280. }
  281. /**
  282. * 微信用户趋势
  283. * @param $where
  284. * @return array
  285. */
  286. public function getWechatTrend($where)
  287. {
  288. $time = explode('-', $where['time']);
  289. $channelType = $where['channel_type'];
  290. if (count($time) != 2) throw new AdminException(100100);
  291. $dayCount = (strtotime($time[1]) - strtotime($time[0])) / 86400 + 1;
  292. $data = [];
  293. if ($dayCount == 1) {
  294. $data = $this->wechatTrend($time, 0);
  295. } elseif ($dayCount > 1 && $dayCount <= 31) {
  296. $data = $this->wechatTrend($time, 1);
  297. } elseif ($dayCount > 31 && $dayCount <= 92) {
  298. $data = $this->wechatTrend($time, 3);
  299. } elseif ($dayCount > 92) {
  300. $data = $this->wechatTrend($time, 30);
  301. }
  302. return $data;
  303. }
  304. /**
  305. * 微信用户趋势
  306. * @param $time
  307. * @param $num
  308. * @return array
  309. */
  310. public function wechatTrend($time, $num)
  311. {
  312. /** @var WechatUserServices $user */
  313. $user = app()->make(WechatUserServices::class);
  314. $subscribe = $unSubscribe = $increaseSubscribe = $cumulativeSubscribe = $cumulativeUnSubscribe = [];
  315. if ($num == 0) {
  316. $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'];
  317. $subscribe = array_column($user->getWechatTrendData($time, [
  318. ['subscribe', '=', 1],
  319. ['subscribe_time', '>=', strtotime($time[0])],
  320. ['subscribe_time', '<', strtotime($time[1])]
  321. ], '%H', 'subscribe'), 'subscribe', 'days');
  322. $unSubscribe = array_column($user->getWechatTrendData($time, [
  323. ['subscribe', '=', 0],
  324. ['subscribe_time', '<>', ''],
  325. ['subscribe_time', '>=', strtotime($time[0])],
  326. ['subscribe_time', '<', strtotime($time[1])]
  327. ], '%H', 'unSubscribe'), 'unSubscribe', 'days');
  328. $cumulativeSubscribe = array_column($user->getWechatTrendData($time, [
  329. ['subscribe', '=', 1]
  330. ], '%H', 'cumulativeSubscribe'), 'cumulativeSubscribe', 'days');
  331. $cumulativeUnSubscribe = array_column($user->getWechatTrendData($time, [
  332. ['subscribe', '=', 0], ['subscribe_time', '<>', '']
  333. ], '%H', 'cumulativeUnSubscribe'), 'cumulativeUnSubscribe', 'days');
  334. } elseif ($num != 0) {
  335. $dt_start = strtotime($time[0]);
  336. $dt_end = strtotime($time[1]);
  337. $timeType = '%m-%d';
  338. while ($dt_start <= $dt_end) {
  339. if ($num == 30) {
  340. $xAxis[] = date('Y-m', $dt_start);
  341. $dt_start = strtotime("+1 month", $dt_start);
  342. $timeType = '%Y-%m';
  343. } else {
  344. $xAxis[] = date('m-d', $dt_start);
  345. $dt_start = strtotime("+$num day", $dt_start);
  346. $timeType = '%m-%d';
  347. }
  348. }
  349. $subscribe = array_column($user->getWechatTrendData($time, [
  350. ['subscribe', '=', 1],
  351. ['subscribe_time', '>=', strtotime($time[0])],
  352. ['subscribe_time', '<', strtotime($time[1])]
  353. ], $timeType, 'subscribe'), 'subscribe', 'days');
  354. $unSubscribe = array_column($user->getWechatTrendData($time, [
  355. ['subscribe', '=', 0],
  356. ['subscribe_time', '<>', ''],
  357. ['subscribe_time', '>=', strtotime($time[0])],
  358. ['subscribe_time', '<', strtotime($time[1])]
  359. ], $timeType, 'unSubscribe'), 'unSubscribe', 'days');
  360. $cumulativeSubscribe = array_column($user->getWechatTrendData($time, [
  361. ['subscribe', '=', 1]
  362. ], $timeType, 'cumulativeSubscribe'), 'cumulativeSubscribe', 'days');
  363. $cumulativeUnSubscribe = array_column($user->getWechatTrendData($time, [
  364. ['subscribe', '=', 0], ['subscribe_time', '<>', '']
  365. ], $timeType, 'cumulativeUnSubscribe'), 'cumulativeUnSubscribe', 'days');
  366. }
  367. $data = $series = [];
  368. foreach ($xAxis as $item) {
  369. $data['新增关注用户'][] = $subscribe[$item] ?? 0;
  370. $data['新增取关用户'][] = $unSubscribe[$item] ?? 0;
  371. $data['累计关注用户'][] = $cumulativeSubscribe[$item] ?? 0;
  372. $data['累计取关用户'][] = $cumulativeUnSubscribe[$item] ?? 0;
  373. }
  374. foreach ($data['新增关注用户'] as $keys => $items) {
  375. $data['净增用户数'][] = $data['新增关注用户'][$keys] - $data['新增取关用户'][$keys];
  376. }
  377. foreach ($data as $key => $item) {
  378. $series[] = ['name' => $key, 'value' => $item];
  379. }
  380. return compact('xAxis', 'series');
  381. }
  382. /**
  383. * 用户地域图表
  384. * @param $where
  385. * @return array
  386. */
  387. public function getRegion($where)
  388. {
  389. $time = explode('-', $where['time']);
  390. $channelType = $where['channel_type'];
  391. if (count($time) != 2) throw new AdminException(100100);
  392. /** @var UserVisitServices $userVisit */
  393. $userVisit = app()->make(UserVisitServices::class);
  394. /** @var UserServices $userService */
  395. $userService = app()->make(UserServices::class);
  396. /** @var StoreOrderServices $order */
  397. $order = app()->make(StoreOrderServices::class);
  398. /** @var WechatUserServices $user */
  399. $wechatUser = app()->make(WechatUserServices::class);
  400. $all = $wechatUser->getRegionAll($time, $channelType);
  401. $new = $wechatUser->getRegionNew($time, $channelType);
  402. $visit = $userVisit->getRegion($time, $channelType);
  403. $payPrice = $order->getRegion($time, $channelType);
  404. foreach ($all as $key1 => $item1) {
  405. foreach ($new as $key2 => $item2) {
  406. if ($item1['province'] == $item2['province']) {
  407. $all[$key1]['newNum'] = $item2['newNum'];
  408. unset($new[$key2]);
  409. }
  410. }
  411. }
  412. $all = array_merge($all, $new);
  413. foreach ($all as $key1 => $item1) {
  414. foreach ($visit as $key3 => $item3) {
  415. if ($item1['province'] == $item3['province']) {
  416. $all[$key1]['visitNum'] = $item3['visitNum'];
  417. unset($visit[$key3]);
  418. }
  419. }
  420. }
  421. $all = array_merge($all, $visit);
  422. foreach ($all as $key1 => $item1) {
  423. foreach ($payPrice as $key3 => $item3) {
  424. if ($item1['province'] == $item3['province']) {
  425. $all[$key1]['payPrice'] = $item3['payPrice'];
  426. unset($payPrice[$key3]);
  427. }
  428. }
  429. }
  430. $all = array_merge($all, $payPrice);
  431. foreach ($all as &$item) {
  432. if ($item['province'] == '') $item['province'] = '未知';
  433. if (!isset($item['allNum'])) $item['allNum'] = 0;
  434. if (!isset($item['newNum'])) $item['newNum'] = 0;
  435. if (!isset($item['visitNum'])) $item['visitNum'] = 0;
  436. if (!isset($item['payPrice'])) {
  437. $item['payPrice'] = 0;
  438. } else {
  439. $item['payPrice'] = floatval($item['payPrice']);
  440. }
  441. }
  442. $data = array_values($all);
  443. $last_names = array_column($data, $where['sort']);
  444. array_multisort($last_names, SORT_DESC, $data);
  445. return $data;
  446. }
  447. /**
  448. * 用户性别
  449. * @param $where
  450. * @return mixed
  451. */
  452. public function getSex($where)
  453. {
  454. $time = explode('-', $where['time']);
  455. $channelType = $where['channel_type'];
  456. if (count($time) != 2) throw new AdminException(100100);
  457. /** @var UserWechatuserServices $user */
  458. $wechatUser = app()->make(UserWechatuserServices::class);
  459. $data = $wechatUser->getSex($time, $channelType);
  460. $oneData = [
  461. ['value' => 0, 'name' => '未知', 'name_key' => 0],
  462. ['value' => 0, 'name' => '男', 'name_key' => 1],
  463. ['value' => 0, 'name' => '女', 'name_key' => 2],
  464. ];
  465. foreach ($oneData as &$value) {
  466. foreach ($data as $item) {
  467. if ($item['name'] == $value['name_key']) {
  468. $value['value'] = $item['value'];
  469. break;
  470. }
  471. }
  472. }
  473. return $oneData;
  474. }
  475. }