MemberController.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace app\admin\controller\statistics;
  3. use app\model\Member;
  4. use support\Db;
  5. class MemberController
  6. {
  7. public function index()
  8. {
  9. // 会员数量
  10. $memberCount = Member::where('member_is_owner', 'N')->where('member_mobile','<>','0000')->count();
  11. // 业主数量
  12. $ownerCount = Member::where('member_is_owner', 'Y')->count();
  13. // 用户总数
  14. $userCount = $memberCount + $ownerCount;
  15. // 今日新增用户数
  16. $todayTime = strtotime(date('Y-m-d 00:00:00'));
  17. $todayCount = Member::where('member_addtimes', '>', $todayTime)->where('member_is_owner', 'N')->count();
  18. // 本月新增用户数
  19. $monthTime = strtotime(date('Y-m-01 00:00:00'));
  20. $monthCount = Member::where('member_addtimes', '>', $monthTime)->where('member_is_owner', 'N')->count();
  21. // 折线图
  22. $newAddition = Db::select("SELECT DATE_FORMAT(FROM_UNIXTIME(member_addtimes), '%Y/%m') AS `month`,count(*) as `num` FROM app_member group by month");
  23. $newAdditionData = [];
  24. foreach ($newAddition as $item) {
  25. $newAdditionData['category'][] = $item->month;
  26. $newAdditionData['data'][] = $item->num;
  27. }
  28. // 饼图
  29. $newAdditionBing = Db::select("
  30. SELECT CASE
  31. WHEN TIMESTAMPDIFF(YEAR, member_cert_birth, CURDATE()) BETWEEN 0 AND 1 THEN '0'
  32. WHEN TIMESTAMPDIFF(YEAR, member_cert_birth, CURDATE()) BETWEEN 1 AND 39 THEN '39'
  33. WHEN TIMESTAMPDIFF(YEAR, member_cert_birth, CURDATE()) BETWEEN 40 AND 50 THEN '40-50'
  34. WHEN TIMESTAMPDIFF(YEAR, member_cert_birth, CURDATE()) BETWEEN 51 AND 60 THEN '51-60'
  35. WHEN TIMESTAMPDIFF(YEAR, member_cert_birth, CURDATE()) BETWEEN 61 AND 70 THEN '60-70'
  36. WHEN TIMESTAMPDIFF(YEAR, member_cert_birth, CURDATE()) BETWEEN 71 AND 80 THEN '70-80'
  37. ELSE '81以上'
  38. END AS age_group,
  39. COUNT(*) AS number_of_people
  40. FROM
  41. app_member_cert
  42. WHERE member_cert_birth != ''
  43. GROUP BY
  44. age_group
  45. ORDER BY
  46. age_group;
  47. ");
  48. $newAdditionBingData = [];
  49. foreach ($newAdditionBing as $item) {
  50. if ($item->age_group == '0') {
  51. $item->age_group = '未知';
  52. }elseif ($item->age_group == '39'){
  53. $item->age_group = '40以下';
  54. }
  55. $newAdditionBingData[] = [
  56. 'name' => $item->age_group,
  57. 'value' => $item->number_of_people
  58. ];
  59. }
  60. $data = [
  61. 'memberCount' => $memberCount,
  62. 'ownerCount' => $ownerCount,
  63. 'userCount' => $userCount,
  64. 'todayCount' => $todayCount,
  65. 'monthCount' => $monthCount,
  66. 'newAdditionData' => $newAdditionData,
  67. 'newAdditionBingData' => $newAdditionBingData
  68. ];
  69. return json_success('', $data);
  70. }
  71. }