startOfDay()->getTimestamp(); // 今天开始时间 00:00:00 $todayEnd = Carbon::today()->endOfDay()->getTimestamp(); // 今天结束时间 23:59:59 // 查询数据 $result = $CompanyModel->query()->get()->toArray(); // 分配数据 if (!$result) return 1; $key_index = 0; foreach ($result as $key => $item) { //获取公司配置 $company_push_time = $NoticeConfigModel->query()->where('company_id', $item['id'])->value('push_time'); if (empty($company_push_time)) continue; $company_push_time_string = date('Y-m-d', time()) . ' ' . $company_push_time . ':00'; //如果当前时间在公司配置时间前5个小时,则开始继续执行 $company_push_time_timestamp = strtotime($company_push_time_string); if (time() < $company_push_time_timestamp - 3600*5) continue; $second = 60 * $key_index; // 每个商户延迟 60 秒 // 准备数据 $message_data = ['company_id' => $item['id']]; //如果今日存在通知推送记录,则跳过统计 $sub_notice_count = $SubNoticeLogModel->query()->where([['company_id', '=', $item['id']], ['insert_time', '>=', $todayStart], ['insert_time', '<=', $todayEnd]])->count(); if ($sub_notice_count > 0) { //执行发送通知任务 SendNoticeJobs::dispatch($message_data); // SendNoticeJobs::dispatchSync($message_data); } else { $key_name = 'SubNoticeJobs_company_id_' . $item['id']; $sub_noticeJobs_data_info = Cache::get($key_name); if ($sub_noticeJobs_data_info) continue; //创建缓存 Cache::put($key_name, '1', 86400); // 每个商户每天只统计执行一次 //执行统计任务 SubNoticeJobs::dispatch($message_data)->delay($second); // SubNoticeJobs::dispatchSync($message_data)->delay($second);; } $key_index++; } // 告知结果 return 2; } }