ExportViolationProductJobs.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. <?php
  2. namespace App\Jobs\Manager\Other;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldQueue;
  5. use Illuminate\Foundation\Bus\Dispatchable;
  6. use Illuminate\Queue\InteractsWithQueue;
  7. use Illuminate\Queue\SerializesModels;
  8. use App\Models\Manager\Process\ViolationProduct as ViolationProductModel;
  9. use App\Models\Manager\Personnel\Employee as EmployeeModel;
  10. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  11. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  12. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  13. use App\Servers\Aliyun\Oss;
  14. use App\Facades\Servers\Logs\Log;
  15. use App\Models\Manager\Other\DownloadTask as DownloadTaskModel;
  16. use Illuminate\Support\Facades\Cache;
  17. use App\Models\Manager\Process\ViolationProductMember as ViolationProductMemberModel;
  18. /**
  19. * 违规处理-导出违规商品数据处理队列
  20. * @author 唐远望
  21. * @version 1.0
  22. * @date 2026-04-01
  23. */
  24. class ExportViolationProductJobs implements ShouldQueue
  25. {
  26. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  27. protected $message_data;
  28. protected $company_id;
  29. /**
  30. * Create a new job instance.
  31. *
  32. * @return void
  33. */
  34. public function __construct(array $message_data)
  35. {
  36. $this->message_data = $message_data;
  37. }
  38. /**
  39. * Execute the job.
  40. *
  41. * @return void
  42. */
  43. public function handle()
  44. {
  45. try {
  46. $fileId = $this->message_data['file_id'];
  47. $company_id = $this->message_data['company_id'];
  48. $user_id = $this->message_data['user_id'];
  49. $DownloadTaskModel = new DownloadTaskModel();
  50. $fileName = '禁止挂网商品数据' . $fileId . '.xlsx';
  51. $downloadLog = [
  52. 'insert_time' => time(),
  53. 'company_id' => $company_id,
  54. 'operator_userid' => $user_id,
  55. 'file_name' => $fileName,
  56. 'file_id' => $fileId,
  57. 'url' => '',
  58. ];
  59. // 写入数据表
  60. $DownloadTaskModel->insertGetId($downloadLog);
  61. $this->export_excel($this->message_data);
  62. } catch (\Exception $e) {
  63. Log::info('job_error', '违规数据-导出禁止挂网数据处理队列失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  64. //失败后清除缓存
  65. Cache::forget('export_violation_product_job_'. $this->company_id);
  66. }
  67. }
  68. /**
  69. * 列表导出
  70. * @author 唐远望
  71. * @version 1.0
  72. * @date 2025-06-17
  73. */
  74. public function export_excel($message_data)
  75. {
  76. $ViolationProductModel = new ViolationProductModel();
  77. $ViolationProductMemberModel = new ViolationProductMemberModel();
  78. $EmployeeModel = new EmployeeModel();
  79. $admin_company_id = $message_data['admin_company_id'];
  80. $company_id = $message_data['company_id'];
  81. $is_admin = $message_data['is_admin']; //是否管理员操作 0=是1=否
  82. $user_id = $message_data['user_id'];
  83. $file_id = $message_data['file_id'];
  84. // 查询条件
  85. $map = [];
  86. $job_page = isset($message_data['job_page']) ? $message_data['job_page'] : 1;
  87. $limit = 1000; //每次处理1000条
  88. $status = $message_data['status'] ?? '';
  89. $start_time = $message_data['start_time'] ?? '';
  90. $end_time = $message_data['end_time'] ?? '';
  91. $product_name = $message_data['product_name'] ?? '';
  92. $product_names = $message_data['product_names'] ?? '';
  93. $first_responsible_person = $message_data['first_responsible_person'] ?? '';
  94. $responsible_person = $message_data['responsible_person'] ?? '';
  95. $platform = $message_data['platform'] ?? '';
  96. $company_name = $message_data['company_name'] ?? '';
  97. $store_name = $message_data['store_name'] ?? '';
  98. $anonymous_store_name = $message_data['anonymous_store_name'] ?? '';
  99. $store_names = $message_data['store_names'] ?? '';
  100. $source_responsible_person = $message_data['source_responsible_person'] ?? '';
  101. $processing_status = $message_data['processing_status'] ?? '';
  102. $product_specs = $message_data['product_specs'] ?? '';
  103. $online_posting_count = $message_data['online_posting_count'] ?? '';
  104. $category_name = $message_data['category_name'] ?? '';
  105. $province_ids = $message_data['province_ids'] ?? '';
  106. $city_ids = $message_data['city_ids'] ?? '';
  107. $shipment_province_ids = $message_data['shipment_province_ids'] ?? '';
  108. $shipment_city_ids = $message_data['shipment_city_ids'] ?? '';
  109. $product_brand = $message_data['product_brand'];
  110. $collection_time_start_time = $message_data['collection_time_start_time'] ?? '';
  111. $collection_time_end_time = $message_data['collection_time_end_time'] ?? '';
  112. $merge_province_ids = $message_data['merge_province_ids'] ?? '';
  113. $merge_city_ids = $message_data['merge_city_ids'] ?? '';
  114. // 时间条件
  115. if ($collection_time_start_time) $map[] = ['collection_time', '>=', strtotime($collection_time_start_time)];
  116. if ($collection_time_end_time) $map[] = ['collection_time', '<=', strtotime($collection_time_end_time)];
  117. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  118. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  119. // 其他条件
  120. if ($product_name) $map[] = ['product_name', 'like', "%$product_name%"];
  121. if ($store_name) $map[] = ['store_name', 'like', "%$store_name%"];
  122. if ($anonymous_store_name) $map[] = ['anonymous_store_name', 'like', "%$anonymous_store_name%"];
  123. if ($category_name) $map[] = ['category_name', 'like', "%$category_name%"];
  124. if ($product_brand) $map[] = ['product_brand', 'like', "%$product_brand%"];
  125. if ($product_specs) $map[] = ['product_specs', 'like', "%$product_specs%"];
  126. $violation_product_where = [];
  127. // 权限判断
  128. if ($is_admin != 1 && $company_id != 0) {
  129. $violation_product_where['company_id'] = $company_id;
  130. } else {
  131. $violation_product_where['company_id'] = $admin_company_id;
  132. }
  133. $ViolationProductModel = $ViolationProductModel->where($violation_product_where);
  134. //多选平台查询
  135. if ($platform && is_string($platform)) {
  136. $platform = explode(',', $platform);
  137. $ViolationProductModel = $ViolationProductModel->whereIn('platform', $platform);
  138. }
  139. //多选处理状态查询
  140. if ($processing_status && is_string($processing_status)) {
  141. $processing_status = explode(',', $processing_status);
  142. $ViolationProductModel = $ViolationProductModel->whereIn('processing_status', $processing_status);
  143. }
  144. //多选状态查询
  145. if ($status && is_string($status)) {
  146. $status = explode(',', $status);
  147. $ViolationProductModel = $ViolationProductModel->whereIn('status', $status);
  148. }
  149. //多选店铺名称查询
  150. if ($store_names && is_string($store_names)) {
  151. $store_names = explode(',', $store_names);
  152. $ViolationProductModel = $ViolationProductModel->whereIn('store_name', $store_names);
  153. }
  154. //多选违规挂网次数查询
  155. if ($online_posting_count && is_string($online_posting_count)) {
  156. $online_posting_count = explode(',', $online_posting_count);
  157. $ViolationProductModel = $ViolationProductModel->whereIn('online_posting_count', $online_posting_count);
  158. }
  159. //多选商品查询
  160. if ($product_names && is_string($product_names)) {
  161. $product_names = explode(',', $product_names);
  162. $ViolationProductModel = $ViolationProductModel->whereIn('product_name', $product_names);
  163. }
  164. //多选公司查询
  165. if ($company_name && is_string($company_name)) {
  166. $company_name = explode(',', $company_name);
  167. $ViolationProductModel = $ViolationProductModel->whereIn('company_name', $company_name);
  168. }
  169. //多选第一责任人
  170. if ($first_responsible_person && is_string($first_responsible_person)) {
  171. $first_responsible_person = explode(',', $first_responsible_person);
  172. $subQuery = $ViolationProductMemberModel->whereIn('employee_id', $first_responsible_person)->where('duty_type',1)->distinct('violation_product_logid')->select('violation_product_logid');
  173. $ViolationProductModel = $ViolationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  174. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  175. });
  176. }
  177. //多选责任人
  178. if ($responsible_person && is_string($responsible_person)) {
  179. $responsible_person = explode(',', $responsible_person);
  180. $subQuery = $ViolationProductMemberModel->whereIn('employee_id', $responsible_person)->where('duty_type',2)->distinct('violation_product_logid')->select('violation_product_logid');
  181. $ViolationProductModel = $ViolationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  182. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  183. });
  184. }
  185. //多选溯源责任人
  186. if ($source_responsible_person && is_string($source_responsible_person)) {
  187. $source_responsible_person = explode(',', $source_responsible_person);
  188. $subQuery = $ViolationProductMemberModel->whereIn('employee_id', $source_responsible_person)->where('duty_type',3)->distinct('violation_product_logid')->select('violation_product_logid');
  189. $ViolationProductModel = $ViolationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  190. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  191. });
  192. }
  193. //多选省份
  194. if ($province_ids && is_string($province_ids)) {
  195. $province_ids = explode(',', $province_ids);
  196. $ViolationProductModel = $ViolationProductModel->whereIn('province_id', $province_ids);
  197. }
  198. //多选城市
  199. if ($city_ids && is_string($city_ids)) {
  200. $city_ids = explode(',', $city_ids);
  201. $ViolationProductModel = $ViolationProductModel->whereIn('city_id', $city_ids);
  202. }
  203. //多选发货省份
  204. if ($shipment_province_ids && is_string($shipment_province_ids)) {
  205. $shipment_province_ids = explode(',', $shipment_province_ids);
  206. $ViolationProductModel = $ViolationProductModel->whereIn('shipment_province_id', $shipment_province_ids);
  207. }
  208. //多选发货城市
  209. if ($shipment_city_ids && is_string($shipment_city_ids)) {
  210. $shipment_city_ids = explode(',', $shipment_city_ids);
  211. $ViolationProductModel = $ViolationProductModel->whereIn('shipment_city_id', $shipment_city_ids);
  212. }
  213. //多合并省份
  214. if ($merge_province_ids && is_string($merge_province_ids)) {
  215. $merge_province_ids = explode(',', $merge_province_ids);
  216. $ViolationProductModel = $ViolationProductModel->whereIn('merge_province_id', $merge_province_ids);
  217. }
  218. //多合并市
  219. if ($merge_city_ids && is_string($merge_city_ids)) {
  220. $merge_city_ids = explode(',', $merge_city_ids);
  221. $ViolationProductModel = $ViolationProductModel->whereIn('merge_city_id', $merge_city_ids);
  222. }
  223. $this->company_id = $violation_product_where['company_id'];
  224. $key_name = 'ExportViolationProductJobs_' . $violation_product_where['company_id'];
  225. //创建缓存
  226. Cache::put($key_name, [], 60 * 60 * 24);
  227. $personnel_roles_info = $EmployeeModel->leftjoin('personnel_roles', 'personnel_roles.id', '=', 'personnel_employee.role_id')
  228. ->where('personnel_employee.id', $user_id)->select(['personnel_employee.id', 'personnel_roles.identity'])
  229. ->first();
  230. //角色身份1=普通2=管理员
  231. if(!empty($personnel_roles_info) && $personnel_roles_info->identity == 2){
  232. $is_admin = 1;
  233. }
  234. $query = $ViolationProductModel->where($map);
  235. if ($is_admin != 1 && $company_id != 0) {
  236. $query = $query->where(function ($q) use ($user_id) {
  237. $q->where('first_responsible_person', 'like', "%,$user_id,%")
  238. ->orWhere('responsible_person', 'like', "%,$user_id,%")
  239. ->orWhere('source_responsible_person', 'like', "%,$user_id,%");
  240. });
  241. }
  242. $query->chunkById($limit, function ($rows) use ($key_name) {
  243. $result_data = $rows->toArray();
  244. if (empty($result_data)) {
  245. return true;
  246. }
  247. $list_data = $this->processing_responsible_person($result_data);
  248. $list_data_info = Cache::get($key_name) ?: [];
  249. $list_new_data = !empty($list_data_info) ? array_merge($list_data_info, $list_data) : $list_data;
  250. Cache::put($key_name, $list_new_data, 60 * 60 * 24);
  251. // 每处理完一个块,释放内存
  252. unset($result_data);
  253. gc_collect_cycles();
  254. return true;
  255. });
  256. $export_data_info = Cache::get($key_name) ?: [];
  257. $this->export_download($export_data_info, $violation_product_where['company_id'], $file_id);
  258. return;
  259. }
  260. /**
  261. * 处理责任人展示信息
  262. * @author 唐远望
  263. * @version 1.0
  264. * @date 2025-12-17
  265. */
  266. public function processing_responsible_person($result)
  267. {
  268. $ViolationProductMemberModel = new ViolationProductMemberModel();
  269. if (isset($result) && count($result) > 0) {
  270. foreach ($result as $key => $value) {
  271. //查询第一责任人名称
  272. $first_responsible_person = explode(',', $value['first_responsible_person']);
  273. $first_responsible_person_name = $ViolationProductMemberModel->where('violation_product_logid',$value['id'])->whereIn('employee_id', $first_responsible_person)->pluck('employee_name')->toarray();
  274. $result[$key]['first_responsible_person_name'] = $first_responsible_person_name;
  275. //查询责任人名称
  276. $responsible_person = explode(',', $value['responsible_person']);
  277. $responsible_person_name = $ViolationProductMemberModel->where('violation_product_logid',$value['id'])->whereIn('employee_id', $responsible_person)->pluck('employee_name')->toarray();
  278. $result[$key]['responsible_person_name'] = $responsible_person_name;
  279. //查询来源责任人名称
  280. $source_responsible_person = explode(',', $value['source_responsible_person']);
  281. $source_responsible_person_name = $ViolationProductMemberModel->where('violation_product_logid',$value['id'])->whereIn('employee_id', $source_responsible_person)->pluck('employee_name')->toarray();
  282. $result[$key]['source_responsible_person_name'] = $source_responsible_person_name;
  283. }
  284. }
  285. return $result;
  286. }
  287. /**
  288. * 导出下载
  289. * @author 唐远望
  290. * @version 1.0
  291. * @date 2025-06-17
  292. */
  293. public function export_download($data, $company_id, $file_id)
  294. {
  295. // 创建一个新的 Spreadsheet 对象
  296. $spreadsheet = new Spreadsheet();
  297. $sheet = $spreadsheet->getActiveSheet();
  298. //合并单元格
  299. $sheet->mergeCells('A1:W1');
  300. $sheet->setCellValue('A1', '禁止挂网商品导出(导出时间:' . date('Y-m-d H:i:s', time()) . ')'); // 设置合并后的单元格内容
  301. // 获取合并后的单元格样式对象
  302. $style = $sheet->getStyle('A1');
  303. // 设置水平居中和垂直居中
  304. $style->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
  305. // 然后设置行高以适应两行文本
  306. $sheet->getRowDimension(1)->setRowHeight(40); // 设置行高,单位是磅(point)
  307. // 设置表头
  308. $sheet->setCellValue('A2', '第一责任人');
  309. $sheet->setCellValue('B2', '责任人');
  310. $sheet->setCellValue('C2', '平台');
  311. $sheet->setCellValue('D2', '商品类型');
  312. $sheet->setCellValue('E2', '品牌名称');
  313. $sheet->setCellValue('F2', '商品名称');
  314. $sheet->setCellValue('G2', '库存');
  315. $sheet->setCellValue('H2', '销量');
  316. $sheet->setCellValue('I2', '快照URL');
  317. $sheet->setCellValue('J2', '商品规格');
  318. $sheet->setCellValue('K2', '累计挂网次数');
  319. $sheet->setCellValue('L2', '连续挂网次数');
  320. $sheet->setCellValue('M2', '链接地址');
  321. $sheet->setCellValue('N2', '店铺名称');
  322. $sheet->setCellValue('O2', '匿名店铺名称');
  323. $sheet->setCellValue('P2', '公司名称');
  324. $sheet->setCellValue('Q2', '商业类型');
  325. $sheet->setCellValue('R2', '信用代码');
  326. $sheet->setCellValue('S2', '省份');
  327. $sheet->setCellValue('T2', '城市');
  328. $sheet->setCellValue('U2', '处理状态');
  329. $sheet->setCellValue('V2', '采集时间');
  330. $sheet->setCellValue('W2', '清洗时间');
  331. //平台0=全部,1=淘宝,2=京东,3=拼多多,4=美团,5=药师帮,6=1药城,7=药九九,8=药易购,9=药帮忙,10=熊猫药药11=药房网
  332. $platform_data = [
  333. '0' => '全部',
  334. '1' => '淘宝',
  335. '2' => '京东',
  336. '3' => '拼多多',
  337. '4' => '美团',
  338. '5' => '药师帮',
  339. '6' => '1药城',
  340. '7' => '药九九',
  341. '8' => '药易购',
  342. '9' => '药帮忙',
  343. '10' => '熊猫药药',
  344. '11' => '药房网',
  345. ];
  346. $processing_status_text = [
  347. '1' => '待处理',
  348. ];
  349. $status_text = [
  350. '0' => '有效',
  351. '1' => '无效',
  352. ];
  353. // 填充数据
  354. $row = 3; // 从第2行开始
  355. foreach ($data as $item) {
  356. $first_responsible_person_name = !empty($item['first_responsible_person_name']) ? implode(',', $item['first_responsible_person_name']) : '';
  357. $responsible_person_name = !empty($item['responsible_person_name']) ? implode(',', $item['responsible_person_name']) : '';
  358. $source_responsible_person_name = !empty($item['source_responsible_person_name']) ? implode(',', $item['source_responsible_person_name']) : '';
  359. $sheet->setCellValue('A' . $row, $first_responsible_person_name);
  360. $sheet->setCellValue('B' . $row, $responsible_person_name);
  361. $sheet->setCellValue('C' . $row, isset($platform_data[$item['platform']]) ? $platform_data[$item['platform']] : '');
  362. $sheet->setCellValue('D' . $row, $item['category_name']);
  363. $sheet->setCellValue('E' . $row, $item['product_brand']);
  364. $sheet->setCellValue('F' . $row, $item['product_name']);
  365. $sheet->setCellValue('G' . $row, $item['inventory']);
  366. $sheet->setCellValue('H' . $row, $item['sales']);
  367. $sheet->setCellValue('I' . $row, $item['snapshot_url'] != '' ? $item['snapshot_url'] : '暂无');
  368. $sheet->setCellValue('J' . $row, $item['product_specs']);
  369. $sheet->setCellValue('K' . $row, $item['online_posting_count']);
  370. $sheet->setCellValue('L' . $row, $item['continuous_listing_count']);
  371. $sheet->setCellValue('M' . $row, $item['link_url']);
  372. $sheet->setCellValue('N' . $row, $item['store_name']);
  373. $sheet->setCellValue('O' . $row, $item['anonymous_store_name']);
  374. $sheet->setCellValue('P' . $row, $item['company_name']);
  375. $sheet->setCellValue('Q' . $row, $item['company_category_name']);
  376. $sheet->setCellValue('R' . $row, $item['social_credit_code']);
  377. $sheet->setCellValue('S' . $row, $item['merge_province_name']);
  378. $sheet->setCellValue('T' . $row, $item['merge_city_name']);
  379. $sheet->setCellValue('U' . $row, isset($processing_status_text[$item['processing_status']]) ? $processing_status_text[$item['processing_status']] : '');
  380. $sheet->setCellValue('V' . $row, !empty($item['collection_time']) ? date('Y-m-d H:i:s', $item['collection_time']) : '');
  381. $sheet->setCellValue('W' . $row, date('Y-m-d H:i:s', $item['insert_time']));
  382. $row++;
  383. }
  384. foreach (range('A', 'W') as $column) {
  385. $sheet->getColumnDimension($column)->setAutoSize(true);
  386. }
  387. // 创建Excel文件
  388. $filename = '禁止挂网商品数据' . $file_id . '.xlsx';
  389. $path = public_path('uploads/exports/');
  390. $fullPath = $path . $filename;
  391. if (!is_dir($path)) mkdir($path, 0777, true);
  392. // 生成 Excel 文件
  393. $writer = new Xlsx($spreadsheet);
  394. $writer->save($fullPath);
  395. // 清理
  396. $spreadsheet->disconnectWorksheets();
  397. unset($spreadsheet, $writer);
  398. //释放redis缓存
  399. Cache::forget('ExportViolationProductJobs_' . $company_id);
  400. $Oss = new Oss();
  401. $oss_url = $Oss->uploadFile($filename, $fullPath);
  402. if ($oss_url) @unlink($fullPath);
  403. // 记录下载任务
  404. $downloadTask = DownloadTaskModel::where(['file_id' => $file_id, 'company_id' => $company_id])->first();
  405. if ($downloadTask) {
  406. $downloadTask->url = $oss_url;
  407. $downloadTask->file_dir_name = $filename;
  408. $downloadTask->update_time = time();
  409. $downloadTask->status = 1;
  410. $downloadTask->save();
  411. }
  412. return $oss_url;
  413. }
  414. public function failed(\Throwable $exception)
  415. {
  416. Log::info('job_error', '违规数据-导出违规商品数据队列完全失败', ['data' => $this->message_data, 'error' => $exception]);
  417. if($this->company_id) {
  418. //失败后清除缓存
  419. Cache::forget('export_violation_product_job_'. $this->company_id);
  420. }
  421. }
  422. }