MemberService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace app\admin\service\statistics;
  3. use app\model\Member;
  4. use support\Db;
  5. class MemberService
  6. {
  7. public static function getMember()
  8. {
  9. $data = [];
  10. // 会员数量
  11. $data['memberCount'] = Member::where('member_is_owner', 'N')->where('member_mobile', '<>', '0000')->count();
  12. // 业主数量
  13. $data['ownerCount'] = Member::where('member_is_owner', 'Y')->count();
  14. // 用户总数
  15. $data['userCount'] = $data['memberCount'] + $data['ownerCount'];
  16. // 今日新增用户数
  17. $todayTime = strtotime(date('Y-m-d 00:00:00'));
  18. $data['todayCount'] = Member::where('member_addtimes', '>', $todayTime)->where('member_is_owner', 'N')->count();
  19. // 本月新增用户数
  20. $monthTime = strtotime(date('Y-m-01 00:00:00'));
  21. $monthEndTime = strtotime(date('y-m-t 23:59:59'));
  22. $data['monthCount'] = Member::whereBetween('member_addtimes', [$monthTime, $monthEndTime])
  23. ->where('member_is_owner', 'N')
  24. ->where('member_mobile', '<>', '0000')
  25. ->count();
  26. // 折线图-会员
  27. $newAddition = Db::select("
  28. SELECT
  29. DATE_FORMAT(FROM_UNIXTIME(member_addtimes), '%Y/%m') AS `month`,
  30. count(*) as `num`
  31. FROM app_member
  32. where member_is_owner='N' and member_mobile != '0000' and member_addtimes >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 12 MONTH)) and member_addtimes <= UNIX_TIMESTAMP()
  33. group by month
  34. order by month asc ");
  35. $newAddition12Month = [];
  36. $months = month_12();
  37. foreach ($months as $key => $month) {
  38. foreach ($newAddition as $item) {
  39. if ($month == $item->month) {
  40. $newAddition12Month[$key] = $item;
  41. }
  42. }
  43. if (!isset($newAddition12Month[$key])) {
  44. $std = new \stdClass();
  45. $std->month = $month;
  46. $std->num = 0;
  47. $newAddition12Month[$key] = $std;
  48. }
  49. }
  50. $category = [];
  51. foreach ($newAddition12Month as $item) {
  52. $category[] = $item->month;
  53. // $newAdditionData['member']['category'][] = $item->month;
  54. // $newAdditionData['member']['data'][] = $item->num;
  55. }
  56. // 折线图-业主
  57. $newAdditionOwner = Db::select("SELECT DATE_FORMAT(FROM_UNIXTIME(member_addtimes), '%Y/%m') AS `month`,count(*) as `num` FROM app_member where member_is_owner='Y' group by month order by month asc ");
  58. foreach ($newAdditionOwner as $item) {
  59. if (!in_array($item->month, $category)) {
  60. $category[] = $item->month;
  61. }
  62. // $newAdditionData['owner']['category'][] = $item->month;
  63. // $newAdditionData['owner']['data'][] = $item->num;
  64. }
  65. sort($category);
  66. $newAdditionData = [];
  67. foreach ($category as $item) {
  68. $isHaveMember = false;
  69. $isHaveOwner = false;
  70. foreach ($newAddition12Month as $item2) {
  71. if ($item == $item2->month) {
  72. $isHaveMember = true;
  73. $newAdditionData['member']['category'][] = $item;
  74. $newAdditionData['member']['data'][] = $item2->num;
  75. }
  76. }
  77. foreach ($newAdditionOwner as $item3) {
  78. if ($item == $item3->month) {
  79. $isHaveOwner = true;
  80. $newAdditionData['owner']['category'][] = $item;
  81. $newAdditionData['owner']['data'][] = $item3->num;
  82. }
  83. }
  84. if (!$isHaveOwner) {
  85. $newAdditionData['owner']['category'][] = $item;
  86. $newAdditionData['owner']['data'][] = 0;
  87. }
  88. if (!$isHaveMember) {
  89. $newAdditionData['member']['category'][] = $item;
  90. $newAdditionData['member']['data'][] = 0;
  91. }
  92. }
  93. $data['addition'] = $newAdditionData;
  94. // 饼图
  95. $newAdditionBing = Db::select("
  96. SELECT CASE
  97. WHEN TIMESTAMPDIFF(YEAR, member_cert_birth, CURDATE()) BETWEEN 0 AND 1 THEN '0'
  98. WHEN TIMESTAMPDIFF(YEAR, member_cert_birth, CURDATE()) BETWEEN 1 AND 39 THEN '39'
  99. WHEN TIMESTAMPDIFF(YEAR, member_cert_birth, CURDATE()) BETWEEN 40 AND 50 THEN '41-50'
  100. WHEN TIMESTAMPDIFF(YEAR, member_cert_birth, CURDATE()) BETWEEN 51 AND 60 THEN '51-60'
  101. WHEN TIMESTAMPDIFF(YEAR, member_cert_birth, CURDATE()) BETWEEN 61 AND 70 THEN '61-70'
  102. WHEN TIMESTAMPDIFF(YEAR, member_cert_birth, CURDATE()) BETWEEN 71 AND 80 THEN '71-80'
  103. ELSE '81以上'
  104. END AS age_group,
  105. COUNT(*) AS number_of_people
  106. FROM
  107. app_member_cert
  108. GROUP BY
  109. age_group
  110. ORDER BY
  111. age_group;
  112. ");
  113. $newAdditionBingData = [];
  114. foreach ($newAdditionBing as $item) {
  115. if ($item->age_group == '0') {
  116. $item->age_group = '未知';
  117. } elseif ($item->age_group == '39') {
  118. $item->age_group = '40以下';
  119. }
  120. $newAdditionBingData[] = [
  121. 'name' => $item->age_group,
  122. 'value' => $item->number_of_people
  123. ];
  124. }
  125. $data['additionBing'] = $newAdditionBingData;
  126. return $data;
  127. }
  128. }