企微短剧业务系统

CustomerConversationReportDataRepair.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2022/11/22
  6. * Time: 16:12
  7. */
  8. namespace App\Console\Repair;
  9. use App\Models\CustomerDetails;
  10. use App\Models\Report\DjCustomerConversationReport;
  11. use Illuminate\Console\Command;
  12. class CustomerConversationReportDataRepair extends Command
  13. {
  14. protected $signature = 'CustomerConversationReportDataRepair';
  15. protected $description = '员工联系客户数据统计历史数据修复';
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. }
  20. public function handle()
  21. {
  22. $this->info(date('H:i') . ' 开始执行');
  23. $corpList = DjCustomerConversationReport::query()
  24. ->select(['corpid', 'date'])
  25. ->where('enable', 1)
  26. ->where('new_contact_cnt', 0)
  27. ->where('date', '<=', '2021-12-31')
  28. ->where('date', '>=', '2021-01-01')
  29. ->groupBy(['corpid', 'date'])
  30. ->orderBy('date', 'desc')
  31. ->get();
  32. foreach($corpList as $info) {
  33. $this->info(date('H:i') . ' 开始处理企微 ' . $info->corpid . ' 在 ' . $info->date . ' 日的数据');
  34. $this->dealCorpData($info->corpid, $info->date);
  35. }
  36. $this->info(date('H:i') . ' 执行完成');
  37. }
  38. public function dealCorpData($corpid, $date)
  39. {
  40. # 提取该企微在当日所有的客服
  41. $userList = DjCustomerConversationReport::query()
  42. ->where('date', $date)
  43. ->where('enable', 1)
  44. ->where('corpid', $corpid)
  45. ->pluck('user_id')
  46. ->toArray();
  47. # 批量查询当日所有客服的流失客户数
  48. // $lossCustomerData = CustomerDetails::suffix($corpid)
  49. // ->where('corpid', $corpid)
  50. // ->whereIn('user_id', $userList)
  51. // ->where('loss_status', 0)
  52. // ->whereBetween('loss_time', [$date . ' 00:00:00', $date . ' 23:59:59'])
  53. // ->selectRaw('user_id, sum(1) as loss_contact_cnt')
  54. // ->groupBy('user_id')
  55. // ->get();
  56. $addCustomerData = CustomerDetails::suffix($corpid)
  57. ->where('corpid', $corpid)
  58. ->whereIn('user_id', $userList)
  59. ->whereIn('loss_status', [0, 1])
  60. ->whereBetween('createtime', [strtotime($date . ' 00:00:00'), strtotime($date . ' 23:59:59')])
  61. ->selectRaw('user_id, sum(1) as new_contact_cnt')
  62. ->groupBy('user_id')
  63. ->get();
  64. foreach($userList as $userId) {
  65. // $lossData = $lossCustomerData->where('user_id', $userId)->first();
  66. $addData = $addCustomerData->where('user_id', $userId)->first();
  67. $newContactCnt = $addData->new_contact_cnt ?? 0;
  68. if(!empty($newContactCnt)) {
  69. $this->info(date('H:i').' corpid:'.$corpid." user_id:".$userId." date:".$date);
  70. DjCustomerConversationReport::query()
  71. ->where('corpid', $corpid)
  72. ->where('date', $date)
  73. ->where('user_id', $userId)
  74. ->update([
  75. // 'loss_contact_cnt' => $lossData->loss_contact_cnt ?? 0,
  76. 'new_contact_cnt' => $newContactCnt,
  77. ]);
  78. }
  79. }
  80. }
  81. }