MemberController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace app\admin\controller\statistics;
  3. use app\model\Member;
  4. use app\model\MemberCert;
  5. use app\model\MemberRole;
  6. use app\model\PayDetail;
  7. use support\Db;
  8. class MemberController
  9. {
  10. public function index()
  11. {
  12. // 会员数量
  13. $memberCount = Member::where('member_is_owner', 'N')->where('member_mobile', '<>', '0000')->count();
  14. // 业主数量
  15. $ownerCount = Member::where('member_is_owner', 'Y')->count();
  16. // 用户总数
  17. $userCount = $memberCount + $ownerCount;
  18. // 今日新增用户数
  19. $todayTime = strtotime(date('Y-m-d 00:00:00'));
  20. $todayCount = Member::where('member_addtimes', '>', $todayTime)->where('member_is_owner', 'N')->count();
  21. // 本月新增用户数
  22. $monthTime = strtotime(date('Y-m-01 00:00:00'));
  23. $monthCount = Member::where('member_addtimes', '>', $monthTime)->where('member_is_owner', 'N')->count();
  24. // 折线图
  25. $newAddition = Db::select("
  26. SELECT t1.month, t1.member_count,
  27. SUM(t2.member_count) AS member_month_count
  28. FROM (
  29. SELECT DATE_FORMAT(FROM_UNIXTIME(member_addtimes), '%Y/%m') AS month,
  30. COUNT(member_id) AS member_count
  31. FROM app_member
  32. WHERE member_mobile != '0000'
  33. GROUP BY month
  34. ) t1
  35. JOIN (
  36. SELECT DATE_FORMAT(FROM_UNIXTIME(member_addtimes), '%Y/%m') AS month,
  37. COUNT(member_id) AS member_count
  38. FROM app_member
  39. WHERE member_mobile != '0000'
  40. GROUP BY month
  41. ) t2 ON t1.month >= t2.month
  42. GROUP BY t1.month;
  43. ");
  44. $newAdditionData = [];
  45. $newAddition = json_decode(json_encode($newAddition), true);
  46. foreach ($newAddition as $key => $item) {
  47. $newAddition[$key]['monthNbr'] = intval(str_replace('/', '', $item['month']));
  48. }
  49. usort($newAddition, function ($a, $b) {
  50. return $a['monthNbr'] - $b['monthNbr'];
  51. });
  52. foreach ($newAddition as $item) {
  53. $newAdditionData['category'][] = $item['month'];
  54. $newAdditionData['data'][] = $item['member_count'];
  55. $newAdditionData['month_total'][] = $item['member_month_count'];
  56. }
  57. // 饼图
  58. $newAdditionBing = Db::select("
  59. SELECT CASE
  60. WHEN TIMESTAMPDIFF(YEAR, member_cert_birth, CURDATE()) BETWEEN 0 AND 1 THEN '0'
  61. WHEN TIMESTAMPDIFF(YEAR, member_cert_birth, CURDATE()) BETWEEN 1 AND 39 THEN '39'
  62. WHEN TIMESTAMPDIFF(YEAR, member_cert_birth, CURDATE()) BETWEEN 40 AND 50 THEN '40-50'
  63. WHEN TIMESTAMPDIFF(YEAR, member_cert_birth, CURDATE()) BETWEEN 51 AND 60 THEN '51-60'
  64. WHEN TIMESTAMPDIFF(YEAR, member_cert_birth, CURDATE()) BETWEEN 61 AND 70 THEN '60-70'
  65. WHEN TIMESTAMPDIFF(YEAR, member_cert_birth, CURDATE()) BETWEEN 71 AND 80 THEN '70-80'
  66. ELSE '81以上'
  67. END AS age_group,
  68. COUNT(*) AS number_of_people
  69. FROM
  70. app_member_cert
  71. WHERE member_cert_birth != ''
  72. GROUP BY
  73. age_group
  74. ORDER BY
  75. age_group;
  76. ");
  77. $newAdditionBingData = [];
  78. foreach ($newAdditionBing as $item) {
  79. if ($item->age_group == '0') {
  80. $item->age_group = '未知';
  81. } elseif ($item->age_group == '39') {
  82. $item->age_group = '40以下';
  83. }
  84. $newAdditionBingData[] = [
  85. 'name' => $item->age_group,
  86. 'value' => $item->number_of_people
  87. ];
  88. }
  89. // 认证数
  90. $authCount = MemberCert::where('member_cert_nbr', '<>', '')
  91. ->where('member_cert_name', '<>', '')
  92. ->where('member_cert_name', '<>', null)
  93. ->count();
  94. $unAuthCount = $memberCount - $authCount;
  95. $authBingData = [
  96. [
  97. 'name' => '已认证',
  98. 'value' => $authCount
  99. ],
  100. [
  101. 'name' => '未认证',
  102. 'value' => $unAuthCount
  103. ]
  104. ];
  105. // 各身份数量
  106. $memberIdentityBingData = [];
  107. $basicMemberCount = $memberCount;
  108. // 1.等级
  109. $memberRoles = MemberRole::select('member_role_id', 'member_role_name')->get()->toArray();
  110. foreach ($memberRoles as $role) {
  111. $memberRoleCount = Member::where('join_member_role_id', $role['member_role_id'])->count();
  112. if ($memberRoleCount < 1) {
  113. continue;
  114. }
  115. $basicMemberCount -= $memberRoleCount;
  116. $isHave = false;
  117. foreach ($memberIdentityBingData as $key => $item) {
  118. if ($item['name'] == $role['member_role_name']) {
  119. $isHave = true;
  120. $memberIdentityBingData[$key]['value'] += $memberRoleCount;
  121. }
  122. }
  123. if (!$isHave) {
  124. $memberIdentityBingData[] = [
  125. 'name' => $role['member_role_name'],
  126. 'value' => $memberRoleCount
  127. ];
  128. }
  129. }
  130. // 2.康养城会员
  131. $vipCount = Member::where('member_is_vip', 'Y')->count();
  132. $basicMemberCount -= $vipCount;
  133. $memberIdentityBingData[] = [
  134. 'name' => '康养城VIP',
  135. 'value' => $vipCount
  136. ];
  137. // 3.会员合伙人
  138. $partnerCount = Member::where('member_is_partner', 'Y')->count();
  139. $basicMemberCount -= $partnerCount;
  140. $memberIdentityBingData[] = [
  141. 'name' => '会员合伙人',
  142. 'value' => $partnerCount
  143. ];
  144. // 4.康养推荐官
  145. $referrerCount = Member::where('member_is_referrer', 'Y')->count();
  146. $basicMemberCount -= $referrerCount;
  147. $memberIdentityBingData[] = [
  148. 'name' => '康养推荐官',
  149. 'value' => $referrerCount
  150. ];
  151. // 5.加盟合伙人
  152. $franchiseeCount = Member::where('member_is_franchisee', 'Y')->count();
  153. $basicMemberCount -= $franchiseeCount;
  154. $memberIdentityBingData[] = [
  155. 'name' => '加盟合伙人',
  156. 'value' => $franchiseeCount
  157. ];
  158. $memberIdentityAllBingData = $memberIdentityBingData;
  159. $memberIdentityAllBingData[] = [
  160. 'name' => '普通会员',
  161. 'value' => $basicMemberCount
  162. ];
  163. // 消费账户占比
  164. $memberConsumptionBingData = [
  165. ['name' => '未消费', 'value' => 0],
  166. ['name' => '1-500', 'value' => 0],
  167. ['name' => '501-1000', 'value' => 0],
  168. ['name' => '1001-5000', 'value' => 0],
  169. ['name' => '5001-10000', 'value' => 0],
  170. ['name' => '10001-50000', 'value' => 0],
  171. ['name' => '50000以上', 'value' => 0],
  172. ];
  173. $memberConsumptionCount = $memberCount;
  174. // 散客ID
  175. $sankeMemberId = Member::where('member_mobile', '0000')->value('member_id');
  176. $payDetails = PayDetail::selectRaw('SUM(pay_amount) as amount,join_pay_member_id')
  177. ->where('pay_amount', '>', 0)
  178. ->where('pay_status', 'SUCCESS')
  179. ->where('join_pay_member_id', '<>', $sankeMemberId)
  180. ->whereIn('pay_category', ['GOODS', 'SERVICE', 'CHNMED', 'CHNNCD', 'MEALS', 'PACKAGE'])
  181. ->groupBy('join_pay_member_id')
  182. ->get()
  183. ->toArray();
  184. foreach ($payDetails as $detail) {
  185. $memberConsumptionCount -= 1;
  186. if ($detail['amount'] >= 1 && $detail['amount'] <= 500 && isset($memberConsumptionBingData[1])) {
  187. $memberConsumptionBingData[1]['value'] += 1;
  188. } elseif ($detail['amount'] >= 501 && $detail['amount'] <= 1000 && isset($memberConsumptionBingData[2])) {
  189. $memberConsumptionBingData[2]['value'] += 1;
  190. } elseif ($detail['amount'] >= 1001 && $detail['amount'] <= 5000 && isset($memberConsumptionBingData[3])) {
  191. $memberConsumptionBingData[3]['value'] += 1;
  192. } elseif ($detail['amount'] >= 5001 && $detail['amount'] <= 10000 && isset($memberConsumptionBingData[4])) {
  193. $memberConsumptionBingData[4]['value'] += 1;
  194. } elseif ($detail['amount'] >= 10001 && $detail['amount'] <= 50000 && isset($memberConsumptionBingData[5])) {
  195. $memberConsumptionBingData[5]['value'] += 1;
  196. } elseif ($detail['amount'] >= 50001 && isset($memberConsumptionBingData[6])) {
  197. $memberConsumptionBingData[6]['value'] += 1;
  198. }
  199. }
  200. if ($memberConsumptionBingData[6]['value'] == 0) {
  201. unset($memberConsumptionBingData[6]);
  202. }
  203. $memberConsumptionBingData[0]['value'] = $memberConsumptionCount;
  204. // 充值
  205. $rechargeBingData = [];
  206. $rechargePaydetail = PayDetail::whereIn('pay_category', ['FRANCHISEE', 'RECHARGE', 'REFERRER', 'PARTNER', 'COMBINE'])
  207. ->where('pay_status', 'SUCCESS')
  208. ->where('join_pay_order_id', 'like', '%OD%');
  209. $recharge1Model = clone $rechargePaydetail;
  210. $rechargeBingData[] = [
  211. 'name' => '1-1000',
  212. 'value' => $recharge1Model->whereBetween('pay_amount', [1, 1000])->count()
  213. ];
  214. $recharge1Model = clone $rechargePaydetail;
  215. $rechargeBingData[] = [
  216. 'name' => '1001-5000',
  217. 'value' => $recharge1Model->whereBetween('pay_amount', [1001, 5000])->count()
  218. ];
  219. $recharge1Model = clone $rechargePaydetail;
  220. $rechargeBingData[] = [
  221. 'name' => '5001-10000',
  222. 'value' => $recharge1Model->whereBetween('pay_amount', [5001, 10000])->count()
  223. ];
  224. $recharge1Model = clone $rechargePaydetail;
  225. $rechargeBingData[] = [
  226. 'name' => '10001-50000',
  227. 'value' => $recharge1Model->whereBetween('pay_amount', [10001, 50000])->count()
  228. ];
  229. $recharge1Model = clone $rechargePaydetail;
  230. $rechargeBingData[] = [
  231. 'name' => '50000以上',
  232. 'value' => $recharge1Model->where('pay_amount', '>', 50000)->count()
  233. ];
  234. $data = [
  235. 'memberCount' => $memberCount,
  236. 'ownerCount' => $ownerCount,
  237. 'userCount' => $userCount,
  238. 'todayCount' => $todayCount,
  239. 'monthCount' => $monthCount,
  240. 'newAdditionData' => $newAdditionData,
  241. 'newAdditionBingData' => $newAdditionBingData,
  242. 'authBingData' => $authBingData,
  243. 'memberIdentityInfoBingData' => $memberIdentityBingData,
  244. 'memberIdentityAllBingData' => $memberIdentityAllBingData,
  245. 'memberConsumptionBingData' => $memberConsumptionBingData,
  246. 'rechargeBingData' => $rechargeBingData
  247. ];
  248. return json_success('', $data);
  249. }
  250. }