Files
huangjingfen/pro_v3.5.1/app/jobs/product/ProductBatchJob.php
panchengyong c1e74d8e68 chore(php): 统一 ScottPan 文件头与注释域名替换
- 按 docs/renew-code-comment.md 将 PHP 文件头改为带边框的 Author 注释\n- 注释中的 crmeb.com 替换为 uj345.cn(代码字符串中的外链未改)\n- 新增 docs/renew-code-comment.md 说明

Made-with: Cursor
2026-03-29 11:22:58 +08:00

132 lines
3.6 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | Author: ScottPan Team
// +----------------------------------------------------------------------
namespace app\jobs\product;
use app\services\product\product\StoreProductBatchProcessServices;
use crmeb\basic\BaseJobs;
use crmeb\traits\QueueTrait;
/**
* 商品批量任务队列
* Class ProductBatchJob
* @package app\jobs\product
*/
class ProductBatchJob extends BaseJobs
{
use QueueTrait;
/**
* @return mixed
*/
public static function queueName()
{
$default = config('queue.default');
return config('queue.connections.' . $default . '.batch_queue');
}
/**
* 商品批量队列
* @param $type
* @param $ids
* @param $data
* @param $isBatch
* @return bool
*/
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;
}
/**
* 拆分分批队列
* @param $type
* @param $ids
* @param $data
* @param $isBatch
* @return bool
*/
public function chunkProductBatch($type, $ids, $data, $isBatch = false)
{
if (!$type || !$ids || !$data) {
return true;
}
//是否批量多个修改
if ($isBatch) {
self::dispatchDo('runProductBatch', [$type, $ids, $data]);
} else {//拆分id,单个队列执行
foreach ($ids as $id) {
self::dispatchDo('runProductBatch', [$type, $id, $data]);
}
}
return true;
}
/**
* 实际执行商品操作队列
* @param $type
* @param $ids
* @param $data
* @return bool
*/
public function runProductBatch($type, $id, $data)
{
if (!is_array($id)) {
$id = (int)$id;
}
if (!$type || !$id || !$data) {
return true;
}
try {
/** @var StoreProductBatchProcessServices $batchProcessServices */
$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://商品品牌
case 10://商品佣金
$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;
}
}