企微短剧业务系统

CustomerHourStat.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Log;
  4. use App\Models\Customer;
  5. use App\Models\CustomerDetails;
  6. use App\Models\CustomerHourRecord;
  7. use App\Models\AuthorizeCorp;
  8. use App\Models\WxOfficialAccountFanRecord;
  9. use App\Models\DjOrder;
  10. use App\Models\OfficialAccount;
  11. use App\RedisModel;
  12. use App\Support\EmailQueue;
  13. use Illuminate\Console\Command;
  14. class CustomerHourStat extends Command
  15. {
  16. protected $signature = 'CustomerHourStat';
  17. protected $description = '客户数据每日分小时统计';
  18. public function handle()
  19. {
  20. \DB::connection()->disableQueryLog();
  21. $this->info(date('m-d H:i:s') . ' 开始整理');
  22. $this->stat();
  23. $this->info(date('m-d H:i:s') . ' 整理结束');
  24. }
  25. public static function stat()
  26. {
  27. try {
  28. //查企微账号
  29. $accounts = AuthorizeCorp::select('corpid')->where('enable', 1)->get();
  30. if($accounts->isEmpty()){
  31. return;
  32. }
  33. $start = date('Y-m-d');
  34. $hour = (int)date('H');
  35. //汇总
  36. $data_total = array();
  37. foreach($accounts as $account){
  38. $corpid = $account->corpid;
  39. # 查询企微未流失的总客户数
  40. $cust_total_info = CustomerDetails::suffix($corpid)
  41. ->where('corpid', $corpid)
  42. ->where('loss_status', 1)
  43. ->selectRaw('count(distinct(external_userid)) as cust_total')
  44. ->first();
  45. $cust_total_uc = $cust_total_info->cust_total ?? 0;
  46. # 查询当日新添加的客户信息
  47. $cust_add_uc = CustomerDetails::suffix($corpid)
  48. ->selectRaw("external_userid, min(createtime) as add_time")
  49. ->where('corpid', $corpid)
  50. ->where('loss_status', 1)
  51. ->groupBy(['external_userid'])
  52. ->having("add_time", '>=', strtotime($start))
  53. ->get()
  54. ->count();
  55. # 当日流失用户信息
  56. $cust_tloss = CustomerDetails::suffix($corpid)
  57. ->where('loss_time', '>=', $start)
  58. ->where('loss_status', 0)
  59. ->where('corpid', $corpid)
  60. ->select('external_userid')
  61. ->distinct()
  62. ->pluck('external_userid')
  63. ->all();
  64. #过滤真正流失的
  65. if( empty($cust_tloss) ){
  66. $cust_loss_uc = 0;
  67. } else {
  68. $cust_noloss = CustomerDetails::suffix($corpid)
  69. ->where('loss_status', 1)
  70. ->where('corpid', $corpid)
  71. ->whereIn('external_userid', $cust_tloss)
  72. ->select('external_userid')
  73. ->distinct()
  74. ->pluck('external_userid')
  75. ->all();
  76. # 当日流失客户数 = 当日流失总数 - 还关注了其它客服的流失客户
  77. $cust_loss_uc = count($cust_tloss) - count($cust_noloss);
  78. }
  79. //先查关联公众号
  80. $official_account_uc = null;
  81. $appids = OfficialAccount::where('corp_id', $corpid)->pluck('mp_app_id')->all();
  82. if(!empty($appids)){
  83. $official_info = WxOfficialAccountFanRecord::whereIn('app_id', $appids)
  84. ->where('record_at', date('Y-m-d H:00:00', strtotime('-1 hour') ))
  85. ->selectRaw('sum(follow_uv) as follow_uv')
  86. ->first();
  87. $official_account_uc = $official_info->follow_uv ?? null;// 公众号关注数
  88. }
  89. $cust_pay_uc = null;// 付费客户数
  90. $cust_pay_amount = null; //付费金额
  91. $cust_pay = DjOrder::where('system_corpid', $corpid)
  92. ->where('pay_status', 1)
  93. ->where('order_pay_time', '>=', strtotime($start) * 1000)
  94. ->where('enable', 1)
  95. ->selectRaw('count(distinct(external_userid)) as cust_uc, sum(pay_money) as pay_amount')
  96. ->first();
  97. if( isset($cust_pay->cust_uc) ){
  98. $cust_pay_uc = $cust_pay->cust_uc;
  99. $cust_pay_amount = round($cust_pay->pay_amount/10000, 2);
  100. }
  101. $cust_pay_uc_total = null;// 总付费客户数
  102. $cust_pay_total = DjOrder::where('system_corpid', $corpid)
  103. ->where('pay_status', 1)
  104. ->where('enable', 1)
  105. ->selectRaw('count(distinct(external_userid)) as cust_uc, sum(pay_money) as pay_amount')
  106. ->first();
  107. if( isset($cust_pay_total->cust_uc) ){
  108. $cust_pay_uc_total = $cust_pay_total->cust_uc;
  109. $cust_pay_amount_total = round($cust_pay_total->pay_amount/10000, 2);
  110. }
  111. # 总客户数
  112. $cust_total_info = CustomerDetails::suffix($corpid)->where('corpid', $corpid)
  113. ->selectRaw("count(distinct(external_userid)) as total")
  114. ->first();
  115. $cust_total = $cust_total_info->total ?? 0;
  116. # 所有流失客户信息
  117. $cust_loss_uc_total = $cust_total - $cust_total_uc;
  118. $data_total[] = [
  119. 'idate' => $start,
  120. 'corpid' => $corpid,
  121. 'hour' => $hour,
  122. 'cust_total_uc' => $cust_total_uc, // 未流失总客户数
  123. 'cust_add_uc' => $cust_add_uc, // 当日新增客户数
  124. 'cust_loss_uc' => $cust_loss_uc, // 当日客户流失
  125. 'official_account_uc' => $official_account_uc, // 今日公众号关注数
  126. 'cust_pay_uc' => $cust_pay_uc, // 付费客户数
  127. 'cust_pay_amount' => $cust_pay_amount, // 付费金额
  128. 'cust_pay_uc_total' => $cust_pay_uc_total, // 总付费客户数
  129. 'cust_loss_uc_total' => $cust_loss_uc_total, // 总流失客户数
  130. 'cust_pay_amount_total' => $cust_pay_amount_total // 总付费金额
  131. ];
  132. }
  133. CustomerHourRecord::insert($data_total);
  134. } catch (\Exception $e) {
  135. EmailQueue::rPush('统计分日用户数据发生异常', $e->getTraceAsString(), ['xiaohua.hou@kuxuan-inc.com'], '统计分日用户数据发生异常');
  136. Log::logInfo('统计分日用户数据发生异常:', ['line' => $e->getLine(), 'msg' => $e->getMessage()], 'CustomerHourStat');
  137. }
  138. }
  139. }