CustomFormOverviewJobs.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Jobs\Company\Statistics;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldBeUnique;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use App\Servers\DB\DbService;
  10. use App\Models\OpenWork\job\Records;
  11. use App\Facades\Servers\Logs\Log;
  12. use App\Models\Company\CustomerForm\CustomerFormSubmit as CustomerFormSubmitModel;
  13. use App\Models\Company\CustomerForm\CustomerFormClickStatistics as CustomerFormClickStatisticsModel;
  14. use Illuminate\Support\Facades\Cache;
  15. /**
  16. * SAAS 自定义表单数据总览
  17. * @author 唐远望
  18. * @version 1.0
  19. * @date 2025-10-29
  20. */
  21. class CustomFormOverviewJobs implements ShouldQueue
  22. {
  23. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  24. protected $message_data;
  25. protected $Records;
  26. /**
  27. * Create a new job instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct(array $message_data)
  32. {
  33. $this->message_data = $message_data;
  34. }
  35. /**
  36. * Execute the job.
  37. *
  38. * @return void
  39. */
  40. public function handle()
  41. {
  42. try {
  43. $companyId = $this->message_data['company_id'];
  44. (new DbService())->getConnectionNameByCompanyId($companyId);
  45. // 创建任务记录
  46. $this->Records = Records::create([
  47. 'job_id' =>'SAAS_'.$companyId . '_CustomFormOverviewJobs',
  48. 'name' => static::class,
  49. 'payload' => json_encode($this->message_data),
  50. 'status' => 'processing',
  51. 'started_at' => now()
  52. ]);
  53. $id = $this->message_data['form_id'];
  54. $start_time = $this->message_data['start_time'];
  55. $end_time = $this->message_data['end_time'];
  56. $CustomerFormSubmitModel = new CustomerFormSubmitModel();
  57. $CustomerFormClickStatisticsModel = new CustomerFormClickStatisticsModel();
  58. //查询今日提交次数
  59. $today_submit_num = $CustomerFormSubmitModel->where(['form_id' => $id])->where('insert_time', '>=', $start_time)->where('insert_time', '<=', $end_time)->count();
  60. //查询今日点击人数
  61. $today_click_num = $CustomerFormClickStatisticsModel->where(['form_id' => $id])->where('insert_time', '>=', $start_time)->where('insert_time', '<=', $end_time)->distinct('custom_uid')->count();
  62. //查询今日填写人数
  63. $today_fill_num = $CustomerFormSubmitModel->where(['form_id' => $id])->where('insert_time', '>=', $start_time)->where('insert_time', '<=', $end_time)->distinct('custom_uid')->count();
  64. //查询今日提交人数
  65. $today_submit_user_num = $CustomerFormSubmitModel->where(['form_id' => $id])->where('insert_time', '>=', $start_time)->where('insert_time', '<=', $end_time)->distinct('custom_uid')->count();
  66. //查询总提交次数
  67. $total_submit_num = $CustomerFormSubmitModel->where(['form_id' => $id])->count();
  68. //查询总点击人数
  69. $total_click_num = $CustomerFormClickStatisticsModel->where(['form_id' => $id])->distinct('custom_uid')->count();
  70. //查询总填写人数
  71. $total_fill_num = $CustomerFormSubmitModel->where(['form_id' => $id])->distinct('custom_uid')->count();
  72. //查询总提交人数
  73. $total_submit_user_num = $CustomerFormSubmitModel->where(['form_id' => $id])->distinct('custom_uid')->count();
  74. //计算总填写率
  75. $total_fill_rate = $total_fill_num > 0 && $total_click_num > 0 ? round($total_fill_num / $total_click_num * 100, 2) : 0;
  76. //计算总提交率
  77. $total_submit_rate = $total_submit_num > 0 && $total_click_num > 0 ? round($total_submit_num / $total_click_num * 100, 2) : 0;
  78. $data = [
  79. 'today_submit_num' => $today_submit_num, //今日提交次数
  80. 'today_click_num' => $today_click_num, //今日点击人数
  81. 'today_fill_num' => $today_fill_num, //今日填写人数
  82. 'today_submit_user_num' => $today_submit_user_num, //今日提交人数
  83. 'total_submit_num' => $total_submit_num, //总提交次数
  84. 'total_click_num' => $total_click_num, //总点击人数
  85. 'total_fill_num' => $total_fill_num, //总填写人数
  86. 'total_submit_user_num' => $total_submit_user_num, //总提交人数
  87. 'total_fill_rate' => $total_fill_rate, //总填写率
  88. 'total_submit_rate' => $total_submit_rate, //总提交率
  89. ];
  90. //写入缓存
  91. Cache::forever('saas_custom_form_statistics_overview_'. $id. '_'. $companyId, $data);//每次查询完毕后覆盖缓存
  92. //删除任务记录
  93. $this->Records->delete();
  94. } catch (\Exception $e) {
  95. // 失败处理...
  96. if ($this->Records) {
  97. $this->Records->delete();
  98. }
  99. Log::info('job_error', '自定义表单数据总览统计失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  100. }
  101. }
  102. public function failed(\Throwable $exception)
  103. {
  104. Log::info('job_error', '自定义表单数据总览统计失败', ['data' => $this->message_data, 'error' => $exception]);
  105. // 失败处理...
  106. if ($this->Records) {
  107. $this->Records->delete();
  108. }
  109. }
  110. }