123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- namespace app\jobs\product;
- use app\services\product\product\StoreProductBatchProcessServices;
- use crmeb\basic\BaseJobs;
- use crmeb\traits\QueueTrait;
- class ProductBatchJob extends BaseJobs
- {
- use QueueTrait;
-
- public static function queueName()
- {
- $default = config('queue.default');
- return config('queue.connections.' . $default . '.batch_queue');
- }
-
- public function productBatch($type, $ids, $data, $isBatch = false)
- {
- if (!$type || !$ids || !$data) {
- return true;
- }
-
- if ($isBatch) {
- $length = 30;
- } else {
- $length = 100;
- }
-
- $idsArr = array_chunk($ids, $length);
- foreach ($idsArr as $ids) {
-
- self::dispatchDo('chunkProductBatch', [$type, $ids, $data, $isBatch]);
- }
- return true;
- }
-
- public function chunkProductBatch($type, $ids, $data, $isBatch = false)
- {
- if (!$type || !$ids || !$data) {
- return true;
- }
-
- if ($isBatch) {
- self::dispatchDo('runProductBatch', [$type, $ids, $data]);
- } else {
- foreach ($ids as $id) {
- self::dispatchDo('runProductBatch', [$type, $id, $data]);
- }
- }
- return true;
- }
-
- public function runProductBatch($type, $id, $data)
- {
- if (!is_array($id)) {
- $id = (int)$id;
- }
- if (!$type || !$id || !$data) {
- return true;
- }
- try {
-
- $batchProcessServices = app()->make(StoreProductBatchProcessServices::class);
- switch ($type) {
- case 1:
- $batchProcessServices->setPrdouctCate($id, $data);
- break;
- case 4:
- $batchProcessServices->setGiveIntegralCoupon($id, $data);
- break;
- case 2:
- case 3:
- case 5:
- case 6:
- case 7:
- case 8:
- case 9:
- $batchProcessServices->runBatch($id, $data, (int)$type);
- break;
- default:
- break;
- }
- } catch (\Throwable $e) {
- response_log_write([
- 'message' => '批量操作商品,type:' . $type . ';状态失败' . ';参数:' . json_encode(['id' => $id, 'data' => $data]) . ',失败原因:' . $e->getMessage(),
- 'file' => $e->getFile(),
- 'line' => $e->getLine()
- ]);
- }
- return true;
- }
- }
|