ExportViolationProductJobs.php 20 KB

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