MemberService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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')->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. $data['monthCount'] = Member::where('member_addtimes', '>', $monthTime)->where('member_is_owner', 'N')->count();
  22. // 折线图-会员
  23. $newAddition = Db::select("
  24. SELECT
  25. DATE_FORMAT(FROM_UNIXTIME(member_addtimes), '%Y/%m') AS `month`,
  26. count(*) as `num`
  27. FROM app_member
  28. where member_is_owner='N' and member_addtimes >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 12 MONTH)) and member_addtimes <= UNIX_TIMESTAMP()
  29. group by month
  30. order by month asc ");
  31. $newAddition12Month = [];
  32. $months = month_12();
  33. foreach ($months as $key => $month){
  34. foreach ($newAddition as $item){
  35. if ($month == $item->month){
  36. $newAddition12Month[$key] = $item;
  37. }
  38. }
  39. if (!isset($newAddition12Month[$key])){
  40. $std = new \stdClass();
  41. $std->month = $month;
  42. $std->num = 0;
  43. $newAddition12Month[$key] = $std;
  44. }
  45. }
  46. $category = [];
  47. foreach ($newAddition12Month as $item) {
  48. $category[] = $item->month;
  49. // $newAdditionData['member']['category'][] = $item->month;
  50. // $newAdditionData['member']['data'][] = $item->num;
  51. }
  52. // 折线图-业主
  53. $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 ");
  54. foreach ($newAdditionOwner as $item) {
  55. if (!in_array($item->month, $category)) {
  56. $category[] = $item->month;
  57. }
  58. // $newAdditionData['owner']['category'][] = $item->month;
  59. // $newAdditionData['owner']['data'][] = $item->num;
  60. }
  61. sort($category);
  62. $newAdditionData = [];
  63. foreach ($category as $item) {
  64. $isHaveMember = false;
  65. $isHaveOwner = false;
  66. foreach ($newAddition12Month as $item2) {
  67. if ($item == $item2->month) {
  68. $isHaveMember = true;
  69. $newAdditionData['member']['category'][] = $item;
  70. $newAdditionData['member']['data'][] = $item2->num;
  71. }
  72. }
  73. foreach ($newAdditionOwner as $item3) {
  74. if ($item == $item3->month) {
  75. $isHaveOwner = true;
  76. $newAdditionData['owner']['category'][] = $item;
  77. $newAdditionData['owner']['data'][] = $item3->num;
  78. }
  79. }
  80. if (!$isHaveOwner) {
  81. $newAdditionData['owner']['category'][] = $item;
  82. $newAdditionData['owner']['data'][] = 0;
  83. }
  84. if (!$isHaveMember) {
  85. $newAdditionData['member']['category'][] = $item;
  86. $newAdditionData['member']['data'][] = 0;
  87. }
  88. }
  89. $data['addition'] = $newAdditionData;
  90. // 饼图
  91. $newAdditionBing = Db::select("
  92. SELECT CASE
  93. WHEN TIMESTAMPDIFF(YEAR, member_cert_birth, CURDATE()) BETWEEN 0 AND 1 THEN '0'
  94. WHEN TIMESTAMPDIFF(YEAR, member_cert_birth, CURDATE()) BETWEEN 1 AND 39 THEN '39'
  95. WHEN TIMESTAMPDIFF(YEAR, member_cert_birth, CURDATE()) BETWEEN 40 AND 50 THEN '41-50'
  96. WHEN TIMESTAMPDIFF(YEAR, member_cert_birth, CURDATE()) BETWEEN 51 AND 60 THEN '51-60'
  97. WHEN TIMESTAMPDIFF(YEAR, member_cert_birth, CURDATE()) BETWEEN 61 AND 70 THEN '61-70'
  98. WHEN TIMESTAMPDIFF(YEAR, member_cert_birth, CURDATE()) BETWEEN 71 AND 80 THEN '71-80'
  99. ELSE '81以上'
  100. END AS age_group,
  101. COUNT(*) AS number_of_people
  102. FROM
  103. app_member_cert
  104. GROUP BY
  105. age_group
  106. ORDER BY
  107. age_group;
  108. ");
  109. $newAdditionBingData = [];
  110. foreach ($newAdditionBing as $item) {
  111. if ($item->age_group == '0') {
  112. $item->age_group = '未知';
  113. } elseif ($item->age_group == '39') {
  114. $item->age_group = '40以下';
  115. }
  116. $newAdditionBingData[] = [
  117. 'name' => $item->age_group,
  118. 'value' => $item->number_of_people
  119. ];
  120. }
  121. $data['additionBing'] = $newAdditionBingData;
  122. return $data;
  123. }
  124. }