ProductSurveillanceJobs.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. namespace App\Jobs\Manager\CollectData\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\Models\Manager\Collect\Product as ProductModel;
  10. use App\Models\Manager\Process\LowPriceGoods as ProcessLowPriceGoodsModel;
  11. use App\Models\Manager\Process\ScrapeData as ScrapeDataModel;
  12. use App\Models\Manager\WashConfig\LowPriceGoods as WashConfigLowPriceGoodsModel;
  13. use App\Models\Manager\Process\ViolationProduct as ProcessViolationProductModel;
  14. use App\Models\Manager\WashConfig\ViolationProduct as WashConfigViolationProductModel;
  15. use App\Models\Manager\Process\ViolationStore as ProcessViolationStoreModel;
  16. use App\Models\Manager\WashConfig\ViolationStore as WashConfigViolationStoreModel;
  17. use App\Models\Manager\Collect\SurveillanceWarningNotice as SurveillanceWarningNoticeModel;
  18. use App\Jobs\Manager\Process\SendEmailJobs;
  19. use Illuminate\Support\Facades\Cache;
  20. use App\Facades\Servers\Logs\Log;
  21. /**
  22. * 商品数据-监控采集清洗情况
  23. * @author: 唐远望
  24. * @version: 1.0
  25. * @date: 2026-06-03
  26. */
  27. class ProductSurveillanceJobs implements ShouldQueue
  28. {
  29. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  30. public $tries = 3; // 限制重试次数
  31. public $timeout = 600; // 10分钟超时
  32. protected $message_data;
  33. /**
  34. * Create a new job instance.
  35. *
  36. * @return void
  37. */
  38. public function __construct(array $message_data)
  39. {
  40. $this->message_data = $message_data;
  41. }
  42. /**
  43. * Execute the job.
  44. *
  45. * @return void
  46. */
  47. public function handle()
  48. {
  49. try {
  50. $ProductModel = new ProductModel();
  51. $ScrapeDataModel = new ScrapeDataModel();
  52. $ProcessLowPriceGoodsModel = new ProcessLowPriceGoodsModel();
  53. $WashConfigLowPriceGoodsModel = new WashConfigLowPriceGoodsModel();
  54. $ProcessViolationProductModel = new ProcessViolationProductModel();
  55. $WashConfigViolationProductModel = new WashConfigViolationProductModel();
  56. $ProcessViolationStoreModel = new ProcessViolationStoreModel();
  57. $WashConfigViolationStoreModel = new WashConfigViolationStoreModel();
  58. $SurveillanceWarningNoticeModel = new SurveillanceWarningNoticeModel();
  59. //获取已经开启邮箱通知的用户
  60. $email_notice_list = $SurveillanceWarningNoticeModel->where([['email', '!=', '']])->where('status', 0)->get()->toarray();
  61. if (empty($email_notice_list)) return true;
  62. $company_id = isset($this->message_data['company_id']) ? $this->message_data['company_id'] : 0; //品牌方公司ID
  63. $company_info = $this->message_data['company_info'];
  64. $start_time = $this->message_data['start_time'];
  65. $end_time = $this->message_data['end_time'];
  66. $start_time_string = date('Y-m-d H:i:s', $start_time);
  67. $end_time_string = date('Y-m-d H:i:s', $end_time);
  68. $key_name = 'LowPriceProductSurveillanceJobs_' . $company_id . '_' . $start_time . '_' . $end_time;
  69. $is_end_select = Cache::get($key_name);
  70. if (!empty($is_end_select)) return true;
  71. $config_collect_product_content = ''; //配置采集商品任务:
  72. $actual_collect_product_content = ''; //实际采集到的商品:
  73. $config_low_product_content = ''; //配置的低价清洗商品:
  74. $cleaned_low_product_content = ''; //实际清洗出来的低价商品:
  75. $config_violation_product_content = ''; //配置的违规清洗商品:
  76. $cleaned_violation_product_content = ''; //实际清洗出来的违规商品:
  77. $config_violation_store_content = ''; //配置的违规店铺:
  78. $cleaned_violation_store_content = ''; //实际清洗出来的违规店铺:
  79. $current_time = time();
  80. // 获取采集时间是星期几
  81. $now_week = date('w', $current_time);
  82. if ($now_week == 0) {
  83. $now_week = 7;
  84. }
  85. //配置采集商品任务商品查询
  86. $where = [];
  87. $where[] = ['company_id', '=', $company_id];
  88. $where[] = ['status', '=', 0];
  89. $need_config_product_data = [];
  90. $config_product_data_info = $ProductModel->WhereRaw("FIND_IN_SET(?, sampling_cycle)", [$now_week])->select(['product_name', 'sampling_start_time', 'sampling_end_time'])->where($where)->get()->toArray();
  91. if (empty($config_product_data_info)) {
  92. $config_collect_product_content .= '无' . ';';
  93. //没有采集任务不处理
  94. return true;
  95. } else {
  96. $config_collect_product_list = [];
  97. foreach ($config_product_data_info as $product_name) {
  98. $sampling_start_time = $product_name['sampling_start_time'];
  99. $sampling_end_time = $product_name['sampling_end_time'];
  100. if ($sampling_start_time != '' && $sampling_end_time != '' && ($current_time < $sampling_start_time || $current_time > $sampling_end_time)) {
  101. continue; //如果当前时间小于采集开始时间或者大于采集结束时间则跳过
  102. }
  103. $need_config_product_data[$product_name['product_name']] = $product_name;
  104. }
  105. if (empty($need_config_product_data)) {
  106. return true; //没有采集任务不处理
  107. }
  108. foreach ($need_config_product_data as $product_name) {
  109. $config_collect_product_content .= $product_name['product_name'] . ';';
  110. //实际采集到的商品
  111. $where = [];
  112. $where[] = ['insert_time', '>=', $start_time_string];
  113. $where[] = ['insert_time', '<=', $end_time_string];
  114. $where[] = ['enterprise_id', '=', $company_id];
  115. $where[] = ['product_name', 'like', '%' . $product_name['product_name'] . '%']; // 增加模糊匹配
  116. $actual_product_data_count = $ScrapeDataModel->where($where)->count();
  117. if ($actual_product_data_count < 1) {
  118. $actual_collect_product_content .= '无' . ';';
  119. } else {
  120. $actual_collect_product_content .= $product_name['product_name'] . ':数量' . $actual_product_data_count . ';';
  121. }
  122. }
  123. }
  124. //配置的低价清洗商品
  125. $where = [];
  126. $where[] = ['company_id', '=', $company_id];
  127. $where[] = ['status', '=', 0];
  128. $config_low_product_data_info = $WashConfigLowPriceGoodsModel->select('product_name')->where($where)->groupBy('product_name')->get()->toArray();
  129. if (empty($config_low_product_data_info)) {
  130. $config_low_product_content .= '无' . ';';
  131. } else {
  132. foreach ($config_low_product_data_info as $product_name) {
  133. $config_low_product_content .= $product_name['product_name'] . ';';
  134. //实际清洗出来的低价商品
  135. $where = [];
  136. $where[] = ['insert_time', '>=', strtotime($start_time_string)];
  137. $where[] = ['insert_time', '<=', strtotime($end_time_string)];
  138. $where[] = ['company_id', '=', $company_id];
  139. $where[] = ['product_name', 'like', '%' . $product_name['product_name'] . '%']; // 增加模糊匹配
  140. $cleaned_product_data_count = $ProcessLowPriceGoodsModel->where($where)->count();
  141. if ($cleaned_product_data_count < 1) {
  142. $cleaned_low_product_content .= '无' . ';';
  143. } else {
  144. $cleaned_low_product_content .= $product_name['product_name'] . ':数量' . $cleaned_product_data_count . ';';
  145. }
  146. }
  147. }
  148. //配置的违规清洗商品
  149. $where = [];
  150. $where[] = ['company_id', '=', $company_id];
  151. $where[] = ['insert_time', '>=', $start_time_string];
  152. $config_violation_product_data_info = $WashConfigViolationProductModel->select('product_name')->where($where)->groupBy('product_name')->get()->toArray();
  153. if (empty($config_violation_product_data_info)) {
  154. $config_violation_product_content .= '无' . ';';
  155. } else {
  156. foreach ($config_violation_product_data_info as $product_name) {
  157. $config_violation_product_content .= $product_name['product_name'] . ';';
  158. //实际清洗出来的违规商品
  159. $where = [];
  160. $where[] = ['insert_time', '>=', strtotime($start_time_string)];
  161. $where[] = ['insert_time', '<=', strtotime($end_time_string)];
  162. $where[] = ['company_id', '=', $company_id];
  163. $where[] = ['product_name', 'like', '%' . $product_name['product_name'] . '%']; // 增加模糊匹配
  164. $cleaned_product_data_count = $ProcessViolationProductModel->where($where)->count();
  165. if ($cleaned_product_data_count < 1) {
  166. $cleaned_violation_product_content .= '无' . ';';
  167. } else {
  168. $cleaned_violation_product_content .= $product_name['product_name'] . ':数量' . $cleaned_product_data_count . ';';
  169. }
  170. }
  171. }
  172. //配置的违规店铺
  173. $where = [];
  174. $where[] = ['company_id', '=', $company_id];
  175. $where[] = ['insert_time', '<=', $end_time_string];
  176. $config_violation_store_data_info = $WashConfigViolationStoreModel->select('store_name')->where($where)->groupBy('store_name')->get()->toArray();
  177. if (empty($config_violation_store_data_info)) {
  178. $config_violation_store_content .= '无' . ';';
  179. } else {
  180. foreach ($config_violation_store_data_info as $store_name) {
  181. $config_violation_store_content .= $store_name['store_name'] . ';';
  182. //实际清洗出来的违规店铺
  183. $where = [];
  184. $where[] = ['insert_time', '>=', strtotime($start_time_string)];
  185. $where[] = ['insert_time', '<=', strtotime($end_time_string)];
  186. $where[] = ['company_id', '=', $company_id];
  187. $where[] = ['store_name', 'like', '%' . $store_name['store_name'] . '%']; // 增加模糊匹配
  188. $cleaned_store_data_count = $ProcessViolationStoreModel->where($where)->count();
  189. if ($cleaned_store_data_count < 1) {
  190. $cleaned_violation_store_content .= '无' . ';';
  191. } else {
  192. $cleaned_violation_store_content .= $store_name['store_name'] . ':数量' . $cleaned_store_data_count . ';';
  193. }
  194. }
  195. }
  196. //合并展示内容
  197. $email_title = '【' . $company_info['company_name'] . '】' . "数据监控-采集和清洗情况通知";
  198. $email_content = $config_collect_product_content . $actual_collect_product_content . $config_low_product_content . $cleaned_low_product_content .
  199. $config_violation_product_content . $cleaned_violation_product_content . $config_violation_store_content . $cleaned_violation_store_content;
  200. $email_content_html = $this->buildEmailHtml(
  201. $config_collect_product_content,
  202. $actual_collect_product_content,
  203. $config_low_product_content,
  204. $cleaned_low_product_content,
  205. $config_violation_product_content,
  206. $cleaned_violation_product_content,
  207. $config_violation_store_content,
  208. $cleaned_violation_store_content,
  209. $start_time_string,
  210. $end_time_string
  211. );
  212. // echo $email_content_html;exit;
  213. //发送预警通知
  214. foreach ($email_notice_list as $email_notice) {
  215. $message_data = [
  216. 'email' => $email_notice['email'],
  217. 'email_content' => json_encode(['title' => $email_title, 'content' => $email_content_html]),
  218. ];
  219. SendEmailJobs::dispatch(['notice_data_info' => $message_data]);
  220. }
  221. //记录日志
  222. } catch (\Exception $e) {
  223. Log::info('job_error', '低价商品数据数据-监控采集清洗情况队列失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  224. }
  225. }
  226. /**
  227. * 构建美观的邮件HTML内容(内联样式版本)
  228. */
  229. private function buildEmailHtml(
  230. $config_collect,
  231. $actual_collect,
  232. $config_low,
  233. $cleaned_low,
  234. $config_violation_product,
  235. $cleaned_violation_product,
  236. $config_violation_store,
  237. $cleaned_violation_store,
  238. $start_time_string,
  239. $end_time_string
  240. ) {
  241. $html = '
  242. <!DOCTYPE html>
  243. <html>
  244. <head>
  245. <meta charset="UTF-8">
  246. </head>
  247. <body style="font-family: \'Microsoft YaHei\', Arial, sans-serif; background-color: #f5f7fa; margin: 0; padding: 20px;">
  248. <div style="max-width: 800px; margin: 0 auto; background-color: #ffffff; border-radius: 12px; overflow: hidden;">
  249. <div style="background: #667eea; color: white; padding: 30px 20px; text-align: center;">
  250. <h1 style="margin: 0; font-size: 24px; font-weight: 600;">数据监控报告</h1>
  251. <p style="margin: 10px 0 0; opacity: 0.9; font-size: 14px;">' . $start_time_string . ' - ' . $end_time_string . ' 采集与清洗情况通知</p>
  252. </div>
  253. <div style="padding: 30px 25px;">
  254. ' . $this->buildSectionInline('🎯 配置采集商品任务', $config_collect, '#667eea') . '
  255. ' . $this->buildSectionInline('✅ 实际采集到的商品', $actual_collect, '#48bb78') . '
  256. <div style="border-top: 1px dashed #e0e4e8; margin: 20px 0;"></div>
  257. ' . $this->buildSectionInline('⚙️ 配置的低价清洗商品', $config_low, '#ed8936') . '
  258. ' . $this->buildSectionInline('🔍 实际清洗出来的低价商品', $cleaned_low, '#ed8936') . '
  259. <div style="border-top: 1px dashed #e0e4e8; margin: 20px 0;"></div>
  260. ' . $this->buildSectionInline('⚠️ 配置的违规清洗商品', $config_violation_product, '#e53e3e') . '
  261. ' . $this->buildSectionInline('🚫 实际清洗出来的违规商品', $cleaned_violation_product, '#e53e3e') . '
  262. <div style="border-top: 1px dashed #e0e4e8; margin: 20px 0;"></div>
  263. ' . $this->buildSectionInline('🏪 配置的违规店铺', $config_violation_store, '#9b59b6') . '
  264. ' . $this->buildSectionInline('🚷 实际清洗出来的违规店铺', $cleaned_violation_store, '#9b59b6') . '
  265. </div>
  266. <div style="background-color: #f8f9fc; padding: 15px 25px; text-align: center; font-size: 12px; color: #999; border-top: 1px solid #eef2f6;">
  267. <p style="margin: 0;">此邮件由系统自动发送,请勿直接回复 | 数据统计时间:' . date('Y-m-d H:i:s') . '</p>
  268. </div>
  269. </div>
  270. </body>
  271. </html>';
  272. return $html;
  273. }
  274. /**
  275. * 构建单个数据区块(内联样式版本)
  276. */
  277. private function buildSectionInline($title, $content, $color)
  278. {
  279. $formattedContent = $this->formatContentInline($content);
  280. return '
  281. <div style="margin-bottom: 25px; border-left: 4px solid ' . $color . '; padding: 12px 20px; background-color: #f8f9fc; border-radius: 8px;">
  282. <div style="font-size: 16px; font-weight: 600; margin-bottom: 12px;">
  283. ' . htmlspecialchars($title) . '
  284. </div>
  285. <div style="font-size: 14px; color: #555; line-height: 1.8; word-break: break-all;">
  286. ' . $formattedContent . '
  287. </div>
  288. </div>';
  289. }
  290. /**
  291. * 格式化内容(内联样式版本)
  292. */
  293. private function formatContentInline($content)
  294. {
  295. // 如果内容为空或者只包含分号
  296. if (empty($content) || trim($content) === ';' || trim($content) === '无;') {
  297. return '<span style="color: #999; font-style: italic;">📭 暂无数据</span>';
  298. }
  299. // 检查是否包含"无;"的情况
  300. if (strpos($content, '无;') !== false && strlen($content) <= 6) {
  301. return '<span style="color: #999; font-style: italic;">📭 暂无数据</span>';
  302. }
  303. // 移除末尾的分号
  304. $content = rtrim($content, ';');
  305. // 按分号分割
  306. $items = explode(';', $content);
  307. $html = '';
  308. foreach ($items as $item) {
  309. $item = trim($item);
  310. if (empty($item)) continue;
  311. // 跳过"无"项
  312. if ($item === '无') continue;
  313. // 检查是否包含数量信息(格式:商品名:数量N)
  314. if (preg_match('/^(.+):数量(\d+)$/u', $item, $matches)) {
  315. $productName = trim($matches[1]);
  316. $count = $matches[2];
  317. $html .= '<span style="display: inline-block; background: #e8ecf1; padding: 4px 12px; border-radius: 20px; margin: 4px 6px 4px 0; font-size: 13px;"><strong style="color: #667eea;">' . htmlspecialchars($productName) . '</strong> <strong style="font-weight: 700; color: #667eea; font-size: 16px;">' . $count . '</strong> 件</span>';
  318. } else {
  319. // 普通商品名
  320. $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>';
  321. }
  322. }
  323. return $html ?: '<span style="color: #999; font-style: italic;">📭 暂无数据</span>';
  324. }
  325. public function failed(\Throwable $exception)
  326. {
  327. Log::info('job_error', '低价商品数据数据-监控采集清洗情况队列完全失败', ['data' => $this->message_data, 'error' => $exception->getMessage()]);
  328. }
  329. }