| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415 |
- <?php
- namespace App\Jobs\Manager\CollectData\Statistics;
- use Illuminate\Bus\Queueable;
- use Illuminate\Contracts\Queue\ShouldBeUnique;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Bus\Dispatchable;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Queue\SerializesModels;
- use App\Models\Manager\Collect\Product as ProductModel;
- use App\Models\Manager\Process\LowPriceGoods as ProcessLowPriceGoodsModel;
- use App\Models\Manager\Process\ScrapeData as ScrapeDataModel;
- use App\Models\Manager\WashConfig\LowPriceGoods as WashConfigLowPriceGoodsModel;
- use App\Models\Manager\Process\ViolationProduct as ProcessViolationProductModel;
- use App\Models\Manager\WashConfig\ViolationProduct as WashConfigViolationProductModel;
- use App\Models\Manager\Process\ViolationStore as ProcessViolationStoreModel;
- use App\Models\Manager\WashConfig\ViolationStore as WashConfigViolationStoreModel;
- use App\Models\Manager\Collect\SurveillanceWarningNotice as SurveillanceWarningNoticeModel;
- use App\Jobs\Manager\Process\SendEmailJobs;
- use Illuminate\Support\Facades\Cache;
- use App\Facades\Servers\Logs\Log;
- /**
- * 商品数据-监控采集清洗情况
- * @author: 唐远望
- * @version: 1.0
- * @date: 2026-06-03
- */
- class ProductSurveillanceJobs implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- public $tries = 3; // 限制重试次数
- public $timeout = 600; // 10分钟超时
- protected $message_data;
- /**
- * Create a new job instance.
- *
- * @return void
- */
- public function __construct(array $message_data)
- {
- $this->message_data = $message_data;
- }
- /**
- * Execute the job.
- *
- * @return void
- */
- public function handle()
- {
- try {
- $ProductModel = new ProductModel();
- $ScrapeDataModel = new ScrapeDataModel();
- $ProcessLowPriceGoodsModel = new ProcessLowPriceGoodsModel();
- $WashConfigLowPriceGoodsModel = new WashConfigLowPriceGoodsModel();
- $ProcessViolationProductModel = new ProcessViolationProductModel();
- $WashConfigViolationProductModel = new WashConfigViolationProductModel();
- $ProcessViolationStoreModel = new ProcessViolationStoreModel();
- $WashConfigViolationStoreModel = new WashConfigViolationStoreModel();
- $SurveillanceWarningNoticeModel = new SurveillanceWarningNoticeModel();
- //获取已经开启邮箱通知的用户
- $email_notice_list = $SurveillanceWarningNoticeModel->where([['email', '!=', '']])->where('status', 0)->get()->toarray();
- if (empty($email_notice_list)) return true;
- $company_id = isset($this->message_data['company_id']) ? $this->message_data['company_id'] : 0; //品牌方公司ID
- $company_info = $this->message_data['company_info'];
- $start_time = $this->message_data['start_time'];
- $end_time = $this->message_data['end_time'];
- $start_time_string = date('Y-m-d H:i:s', $start_time);
- $end_time_string = date('Y-m-d H:i:s', $end_time);
- $key_name = 'LowPriceProductSurveillanceJobs_' . $company_id . '_' . $start_time . '_' . $end_time;
- $is_end_select = Cache::get($key_name);
- if (!empty($is_end_select)) return true;
- $config_collect_product_content = ''; //配置采集商品任务:
- $actual_collect_product_content = ''; //实际采集到的商品:
- $config_low_product_content = ''; //配置的低价清洗商品:
- $cleaned_low_product_content = ''; //实际清洗出来的低价商品:
- $config_violation_product_content = ''; //配置的违规清洗商品:
- $cleaned_violation_product_content = ''; //实际清洗出来的违规商品:
- $config_violation_store_content = ''; //配置的违规店铺:
- $cleaned_violation_store_content = ''; //实际清洗出来的违规店铺:
- $current_time = time();
- // 获取采集时间是星期几
- $now_week = date('w', $current_time);
- if ($now_week == 0) {
- $now_week = 7;
- }
- //配置采集商品任务商品查询
- $where = [];
- $where[] = ['company_id', '=', $company_id];
- $where[] = ['status', '=', 0];
- $need_config_product_data = [];
- $config_product_data_info = $ProductModel->WhereRaw("FIND_IN_SET(?, sampling_cycle)", [$now_week])->select(['product_name', 'platform', 'sampling_start_time', 'sampling_end_time'])->where($where)->get()->toArray();
- if (empty($config_product_data_info)) {
- $config_collect_product_content .= '无' . ';';
- //没有采集任务不处理
- return true;
- } else {
- //实际采集到的所有商品
- $where = [];
- $where[] = ['insert_time', '>=', $start_time_string];
- $where[] = ['insert_time', '<=', $end_time_string];
- $where[] = ['enterprise_id', '=', $company_id];
- $scrape_data_list = $ScrapeDataModel->where($where)->get()->toarray();
- foreach ($config_product_data_info as $product_data) {
- $sampling_start_time = $product_data['sampling_start_time'];
- $sampling_end_time = $product_data['sampling_end_time'];
- $platform_id_data = explode(',', $product_data['platform']);
- if ($sampling_start_time != '' && $sampling_end_time != '' && ($current_time < $sampling_start_time || $current_time > $sampling_end_time)) {
- continue; //如果当前时间小于采集开始时间或者大于采集结束时间则跳过
- }
- if (isset($need_config_product_data[$product_data['product_name']])) {
- $platform_id_data_merge = array_merge($need_config_product_data[$product_data['product_name']], $platform_id_data);
- $platform_id_data_merge = array_unique($platform_id_data_merge);
- $need_config_product_data[$product_data['product_name']] = $platform_id_data_merge;
- } else {
- $need_config_product_data[$product_data['product_name']] = $platform_id_data;
- }
- }
- if (empty($need_config_product_data)) {
- return true; //没有采集任务不处理
- }
- //更新平台名称
- foreach ($need_config_product_data as $product_name => $platform_id_data) {
- $platform_name_list = '';
- $platform_product_name_sting = '';
- foreach ($platform_id_data as $platform_id) {
- $platform_name = $this->platform($platform_id);
- //实际采集到的商品
- $where = [];
- $where[] = ['insert_time', '>=', $start_time_string];
- $where[] = ['insert_time', '<=', $end_time_string];
- $where[] = ['enterprise_id', '=', $company_id];
- $where[] = ['platform_id', '=', $platform_id];
- $where[] = ['product_name', 'like', '%' . $product_name . '%']; // 增加模糊匹配
- $actual_product_data_count = 0;
- if (!empty($scrape_data_list)) {
- foreach ($scrape_data_list as $scrape_data) {
- if ($scrape_data['platform_id'] == $platform_id && strpos($scrape_data['product_name'], $product_name) !== false) {
- $actual_product_data_count++;
- }
- }
- }
- $platform_product_name_sting .= '【' . $platform_name . '】' . ':' . $actual_product_data_count . '条';
- $platform_name_list .= $platform_name . '/';
- }
- $actual_collect_product_content .= $product_name . $platform_product_name_sting . ';';
- $product_name_sting = $product_name . '【' . $platform_name_list . '】';
- $config_collect_product_content .= $product_name_sting . ';';
- }
- }
- //配置的低价清洗商品
- $where = [];
- $where[] = ['company_id', '=', $company_id];
- $where[] = ['status', '=', 0];
- $config_low_product_data_info = $WashConfigLowPriceGoodsModel->select('product_name')->where($where)->groupBy('product_name')->get()->toArray();
- if (empty($config_low_product_data_info)) {
- $config_low_product_content .= '无' . ';';
- } else {
- //实际清洗出来的低价商品
- $where = [];
- $where[] = ['insert_time', '>=', strtotime($start_time_string)];
- $where[] = ['insert_time', '<=', strtotime($end_time_string)];
- $where[] = ['company_id', '=', $company_id];
- $process_low_priceGoods = $ProcessLowPriceGoodsModel->where($where)->get()->toarray();
- foreach ($config_low_product_data_info as $product_name) {
- $config_low_product_content .= $product_name['product_name'] . ';';
- $cleaned_product_data_count = 0;
- if (!empty($process_low_priceGoods)) {
- foreach ($process_low_priceGoods as $process_low_priceGood) {
- if (strpos($process_low_priceGood['product_name'], $product_name['product_name']) !== false) {
- $cleaned_product_data_count++;
- }
- }
- }
- $cleaned_low_product_content .= $product_name['product_name'] . ':数量' . $cleaned_product_data_count . '条;';
- }
- }
- //配置的违规清洗商品
- $where = [];
- $where[] = ['company_id', '=', $company_id];
- $where[] = ['insert_time', '>=', $start_time_string];
- $config_violation_product_data_info = $WashConfigViolationProductModel->select('product_name')->where($where)->groupBy('product_name')->get()->toArray();
- if (empty($config_violation_product_data_info)) {
- $config_violation_product_content .= '无' . ';';
- } else {
- //实际清洗出来的违规商品
- $where = [];
- $where[] = ['insert_time', '>=', strtotime($start_time_string)];
- $where[] = ['insert_time', '<=', strtotime($end_time_string)];
- $where[] = ['company_id', '=', $company_id];
- $process_violation_products = $ProcessViolationProductModel->where($where)->get()->toarray();
- foreach ($config_violation_product_data_info as $product_name) {
- $config_violation_product_content .= $product_name['product_name'] . ';';
- $cleaned_product_data_count = 0;
- if (!empty($process_violation_products)) {
- foreach ($process_violation_products as $process_violation_product) {
- if (strpos($process_violation_product['product_name'], $product_name['product_name']) !== false) {
- $cleaned_product_data_count++;
- }
- }
- $cleaned_violation_product_content .= $product_name['product_name'] . ':数量' . $cleaned_product_data_count . '条;';
- }
- }
- }
- //配置的违规店铺
- $where = [];
- $where[] = ['company_id', '=', $company_id];
- $where[] = ['insert_time', '<=', $end_time_string];
- $config_violation_store_data_info = $WashConfigViolationStoreModel->select('store_name')->where($where)->groupBy('store_name')->get()->toArray();
- if (empty($config_violation_store_data_info)) {
- $config_violation_store_content .= '无' . ';';
- } else {
- //实际清洗出来的违规店铺
- $where = [];
- $where[] = ['insert_time', '>=', strtotime($start_time_string)];
- $where[] = ['insert_time', '<=', strtotime($end_time_string)];
- $where[] = ['company_id', '=', $company_id];
- $process_violation_store = $ProcessViolationStoreModel->where($where)->get()->toarray();
- foreach ($config_violation_store_data_info as $store_name) {
- $config_violation_store_content .= $store_name['store_name'] . ';';
- $cleaned_store_data_count = 0;
- if (!empty($process_violation_store)) {
- foreach ($process_violation_store as $process_violation_store) {
- if (strpos($process_violation_store['store_name'], $store_name['store_name']) !== false) {
- $cleaned_store_data_count++;
- }
- }
- }
- $cleaned_violation_store_content .= $store_name['store_name'] . ':数量' . $cleaned_store_data_count . '条;';
- }
- }
- //合并展示内容
- $email_title = '【' . $company_info['company_name'] . '】' . "数据监控-采集和清洗情况通知";
- $email_content = $config_collect_product_content . $actual_collect_product_content . $config_low_product_content . $cleaned_low_product_content .
- $config_violation_product_content . $cleaned_violation_product_content . $config_violation_store_content . $cleaned_violation_store_content;
- $email_content_html = $this->buildEmailHtml(
- $config_collect_product_content,
- $actual_collect_product_content,
- $config_low_product_content,
- $cleaned_low_product_content,
- $config_violation_product_content,
- $cleaned_violation_product_content,
- $config_violation_store_content,
- $cleaned_violation_store_content,
- $start_time_string,
- $end_time_string
- );
- // echo $email_content_html;
- //发送预警通知
- foreach ($email_notice_list as $email_notice) {
- $message_data = [
- 'email' => $email_notice['email'],
- 'email_content' => json_encode(['title' => $email_title, 'content' => $email_content_html]),
- ];
- SendEmailJobs::dispatch(['notice_data_info' => $message_data]);
- }
- //记录日志
- } catch (\Exception $e) {
- Log::info('job_error', '低价商品数据数据-监控采集清洗情况队列失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
- }
- }
- /**
- * platform 平台ID定义
- *
- */
- private function platform($platform_id)
- {
- //平台0=全部,1=淘宝,2=京东,3=拼多多,4=美团,5=药师帮,6=1药城,7=药久久,8=药易购,9=药帮忙,10=熊猫药药11=药房网
- switch ($platform_id) {
- case 1:
- return '淘宝';
- case 2:
- return '京东';
- case 3:
- return '拼多多';
- case 4:
- return '美团';
- case 5:
- return '药师帮';
- case 6:
- return '1药城';
- case 7:
- return '药久久';
- case 8:
- return '药易购';
- case 9:
- return '药帮忙';
- case 10:
- return '熊猫药药';
- case 11:
- return '药房网';
- default:
- return '';
- }
- }
- /**
- * 构建美观的邮件HTML内容(内联样式版本)
- */
- private function buildEmailHtml(
- $config_collect,
- $actual_collect,
- $config_low,
- $cleaned_low,
- $config_violation_product,
- $cleaned_violation_product,
- $config_violation_store,
- $cleaned_violation_store,
- $start_time_string,
- $end_time_string
- ) {
- $html = '
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- </head>
- <body style="font-family: \'Microsoft YaHei\', Arial, sans-serif; background-color: #f5f7fa; margin: 0; padding: 20px;">
- <div style="mix-width: 800px; width: 96%; margin: 0 auto; background-color: #ffffff; border-radius: 12px; overflow: hidden;">
- <div style="background: #667eea; color: white; padding: 30px 20px; text-align: center;">
- <h1 style="margin: 0; font-size: 24px; font-weight: 600;">数据监控报告</h1>
- <p style="margin: 10px 0 0; opacity: 0.9; font-size: 14px;">' . $start_time_string . ' - ' . $end_time_string . ' 采集与清洗情况通知</p>
- </div>
- <div style="padding: 30px 25px;">
- ' . $this->buildSectionInline('🎯 配置采集商品任务', $config_collect, '#667eea') . '
- ' . $this->buildSectionInline('✅ 实际采集到的商品', $actual_collect, '#48bb78') . '
- <div style="border-top: 1px dashed #e0e4e8; margin: 20px 0;"></div>
- ' . $this->buildSectionInline('⚙️ 配置的低价清洗商品', $config_low, '#ed8936') . '
- ' . $this->buildSectionInline('🔍 实际清洗出来的低价商品', $cleaned_low, '#ed8936') . '
- <div style="border-top: 1px dashed #e0e4e8; margin: 20px 0;"></div>
- ' . $this->buildSectionInline('⚠️ 配置的违规清洗商品', $config_violation_product, '#e53e3e') . '
- ' . $this->buildSectionInline('🚫 实际清洗出来的违规商品', $cleaned_violation_product, '#e53e3e') . '
- <div style="border-top: 1px dashed #e0e4e8; margin: 20px 0;"></div>
- ' . $this->buildSectionInline('🏪 配置的违规店铺', $config_violation_store, '#9b59b6') . '
- ' . $this->buildSectionInline('🚷 实际清洗出来的违规店铺', $cleaned_violation_store, '#9b59b6') . '
- </div>
- <div style="background-color: #f8f9fc; padding: 15px 25px; text-align: center; font-size: 12px; color: #999; border-top: 1px solid #eef2f6;">
- <p style="margin: 0;">此邮件由系统自动发送,请勿直接回复 | 数据统计时间:' . date('Y-m-d H:i:s') . '</p>
- </div>
- </div>
- </body>
- </html>';
- return $html;
- }
- /**
- * 构建单个数据区块(内联样式版本)
- */
- private function buildSectionInline($title, $content, $color)
- {
- $formattedContent = $this->formatContentInline($content);
- return '
- <div style="margin-bottom: 25px; border-left: 4px solid ' . $color . '; padding: 12px 20px; background-color: #f8f9fc; border-radius: 8px;">
- <div style="font-size: 16px; font-weight: 600; margin-bottom: 12px;">
- ' . htmlspecialchars($title) . '
- </div>
- <div style="font-size: 14px; color: #555; line-height: 1.8; word-break: break-all;">
- ' . $formattedContent . '
- </div>
- </div>';
- }
- /**
- * 格式化内容(内联样式版本)
- */
- private function formatContentInline($content)
- {
- // 如果内容为空或者只包含分号
- if (empty($content) || trim($content) === ';' || trim($content) === '无;') {
- return '<span style="color: #999; font-style: italic;">📭 暂无数据</span>';
- }
- // 检查是否包含"无;"的情况
- if (strpos($content, '无;') !== false && strlen($content) <= 6) {
- return '<span style="color: #999; font-style: italic;">📭 暂无数据</span>';
- }
- // 移除末尾的分号
- $content = rtrim($content, ';');
- // 按分号分割
- $items = explode(';', $content);
- $html = '';
- foreach ($items as $item) {
- $item = trim($item);
- if (empty($item)) continue;
- // 跳过"无"项
- if ($item === '无') continue;
- // 普通商品名
- $html .= '<span style="display: inline-block; background: #e8ecf1; padding: 4px 12px; border-radius: 20px; margin: 4px 6px 4px 0; font-size: 13px;">' . htmlspecialchars($item) . '</span>';
- }
- return $html ?: '<span style="color: #999; font-style: italic;">📭 暂无数据</span>';
- }
- public function failed(\Throwable $exception)
- {
- Log::info('job_error', '低价商品数据数据-监控采集清洗情况队列完全失败', ['data' => $this->message_data, 'error' => $exception->getMessage()]);
- }
- }
|