作者 lyh

gx

@@ -443,7 +443,7 @@ class ProductController extends BaseController @@ -443,7 +443,7 @@ class ProductController extends BaseController
443 } 443 }
444 444
445 /** 445 /**
446 - * @remark :批量设置产品分类及状态 446 + * @remark :批量设置产品分类
447 * @name :batchSetCategory 447 * @name :batchSetCategory
448 * @author :lyh 448 * @author :lyh
449 * @method :post 449 * @method :post
@@ -453,11 +453,9 @@ class ProductController extends BaseController @@ -453,11 +453,9 @@ class ProductController extends BaseController
453 $this->request->validate([ 453 $this->request->validate([
454 'id'=>'required', 454 'id'=>'required',
455 'category_id'=>'required', 455 'category_id'=>'required',
456 - 'status'=>'required'  
457 ],[ 456 ],[
458 'id.required' => '产品ID不能为空', 457 'id.required' => '产品ID不能为空',
459 'category_id.required' => '分类ID不能为空', 458 'category_id.required' => '分类ID不能为空',
460 - 'status.required'=>'状态不能为空'  
461 ]); 459 ]);
462 $logic->batchSetCategory(); 460 $logic->batchSetCategory();
463 $this->response('success'); 461 $this->response('success');
@@ -335,22 +335,6 @@ class ProductLogic extends BaseLogic @@ -335,22 +335,6 @@ class ProductLogic extends BaseLogic
335 return !empty(trim($str,',')) ? ','.$str.',' : ''; 335 return !empty(trim($str,',')) ? ','.$str.',' : '';
336 } 336 }
337 337
338 - /**  
339 - * @remark :获取最后一级分类id(数组)  
340 - * @name :getLastCategory  
341 - * @author :lyh  
342 - * @method :post  
343 - * @time :2023/10/20 9:02  
344 - */  
345 - public function getLastCategoryArr($category){  
346 - $arr = [];  
347 - if(isset($category) && !empty($category)){  
348 - foreach ($category as $v){  
349 - $arr[] = $v;  
350 - }  
351 - }  
352 - return $arr;  
353 - }  
354 338
355 /** 339 /**
356 * @remark :编辑产品 340 * @remark :编辑产品
@@ -575,27 +559,29 @@ class ProductLogic extends BaseLogic @@ -575,27 +559,29 @@ class ProductLogic extends BaseLogic
575 * @time :2023/8/15 17:53 559 * @time :2023/8/15 17:53
576 */ 560 */
577 public function batchSetCategory(){ 561 public function batchSetCategory(){
578 - if(isset($this->param['category_id']) && !empty($this->param['category_id'])) {  
579 - DB::connection('custom_mysql')->beginTransaction();  
580 - $category_ids = $this->getLastCategoryArr($this->param['category_id']);  
581 - $this->param['category_id'] = ','.implode(',',$category_ids).',';  
582 - try {  
583 - //批量  
584 - $param = [  
585 - 'category_id'=>$this->param['category_id'],  
586 - 'status'=>$this->param['status']  
587 - ];  
588 - $this->model->edit($param,['id'=>['in',$this->param['id']]]); 562 + if(!isset($this->param['category_id']) || empty($this->param['category_id'])){
  563 + $this->fail('请选择分类');
  564 + }
  565 + try {
  566 + if(!isset($this->param['is_cover']) || ($this->param['is_cover'] == 1)){
  567 + $category_ids = ','.implode(',',$this->param['category_id']).',';
  568 + $this->model->edit(['category_id'=>$category_ids],['id'=>['in',$this->param['id']]]);
589 //分类关联 569 //分类关联
590 foreach ($this->param['id'] as $id){ 570 foreach ($this->param['id'] as $id){
591 - CategoryRelated::saveRelated($id, $category_ids); 571 + CategoryRelated::saveRelated($id, $this->param['category_id']);
  572 + }
  573 + }else{
  574 + foreach ($this->param['id'] as $id){
  575 + //获取当前产品的分类
  576 + $productInfo = $this->model->read(['id'=>$id],['id','category_id']);
  577 + $category_ids_arr = array_values(array_unique(array_merge($productInfo['category_id'],$this->param['category_id'])));
  578 + $category_ids = ','.implode(',',$category_ids_arr).',';
  579 + $this->model->edit(['category_id'=>$category_ids],['id'=>$id]);
  580 + CategoryRelated::saveRelated($id, $category_ids_arr);
592 } 581 }
593 - DB::connection('custom_mysql')->commit();  
594 - //对应添加关联表  
595 - }catch (\Exception $e){  
596 - DB::connection('custom_mysql')->rollBack();  
597 - $this->fail('系统错误,请联系管理员');  
598 } 582 }
  583 + }catch (\Exception $e){
  584 + $this->fail('设置分类失败,请联系管理员');
599 } 585 }
600 return $this->success(); 586 return $this->success();
601 } 587 }