ExportLowPriceGoodsJobs.php 24 KB

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