作者 lyh

gx

... ... @@ -443,7 +443,7 @@ class ProductController extends BaseController
}
/**
* @remark :批量设置产品分类及状态
* @remark :批量设置产品分类
* @name :batchSetCategory
* @author :lyh
* @method :post
... ... @@ -453,11 +453,9 @@ class ProductController extends BaseController
$this->request->validate([
'id'=>'required',
'category_id'=>'required',
'status'=>'required'
],[
'id.required' => '产品ID不能为空',
'category_id.required' => '分类ID不能为空',
'status.required'=>'状态不能为空'
]);
$logic->batchSetCategory();
$this->response('success');
... ...
... ... @@ -335,22 +335,6 @@ class ProductLogic extends BaseLogic
return !empty(trim($str,',')) ? ','.$str.',' : '';
}
/**
* @remark :获取最后一级分类id(数组)
* @name :getLastCategory
* @author :lyh
* @method :post
* @time :2023/10/20 9:02
*/
public function getLastCategoryArr($category){
$arr = [];
if(isset($category) && !empty($category)){
foreach ($category as $v){
$arr[] = $v;
}
}
return $arr;
}
/**
* @remark :编辑产品
... ... @@ -575,27 +559,29 @@ class ProductLogic extends BaseLogic
* @time :2023/8/15 17:53
*/
public function batchSetCategory(){
if(isset($this->param['category_id']) && !empty($this->param['category_id'])) {
DB::connection('custom_mysql')->beginTransaction();
$category_ids = $this->getLastCategoryArr($this->param['category_id']);
$this->param['category_id'] = ','.implode(',',$category_ids).',';
try {
//批量
$param = [
'category_id'=>$this->param['category_id'],
'status'=>$this->param['status']
];
$this->model->edit($param,['id'=>['in',$this->param['id']]]);
if(!isset($this->param['category_id']) || empty($this->param['category_id'])){
$this->fail('请选择分类');
}
try {
if(!isset($this->param['is_cover']) || ($this->param['is_cover'] == 1)){
$category_ids = ','.implode(',',$this->param['category_id']).',';
$this->model->edit(['category_id'=>$category_ids],['id'=>['in',$this->param['id']]]);
//分类关联
foreach ($this->param['id'] as $id){
CategoryRelated::saveRelated($id, $category_ids);
CategoryRelated::saveRelated($id, $this->param['category_id']);
}
}else{
foreach ($this->param['id'] as $id){
//获取当前产品的分类
$productInfo = $this->model->read(['id'=>$id],['id','category_id']);
$category_ids_arr = array_values(array_unique(array_merge($productInfo['category_id'],$this->param['category_id'])));
$category_ids = ','.implode(',',$category_ids_arr).',';
$this->model->edit(['category_id'=>$category_ids],['id'=>$id]);
CategoryRelated::saveRelated($id, $category_ids_arr);
}
DB::connection('custom_mysql')->commit();
//对应添加关联表
}catch (\Exception $e){
DB::connection('custom_mysql')->rollBack();
$this->fail('系统错误,请联系管理员');
}
}catch (\Exception $e){
$this->fail('设置分类失败,请联系管理员');
}
return $this->success();
}
... ...