Browse Source

fix:验货类型

super-yimizi 1 month ago
parent
commit
43cac892b1

+ 64 - 27
application/admin/controller/inspection/Type.php

@@ -10,7 +10,7 @@ use Exception;
 use app\common\model\inspection\Item as ItemModel;
 use app\common\model\inspection\Item as ItemModel;
 use app\admin\model\inspection\TypeItem as TypeItemModel;
 use app\admin\model\inspection\TypeItem as TypeItemModel;
 use fast\Arr;
 use fast\Arr;
-
+use app\common\enum\StatusEnum;
 /**
 /**
  * 验货类型
  * 验货类型
  *
  *
@@ -94,7 +94,7 @@ class Type extends Backend
                     'name' => $item['name'],
                     'name' => $item['name'],
                     'standard_desc' => $item['standard_desc'],
                     'standard_desc' => $item['standard_desc'],
                     'is_required' => $item['is_required'],
                     'is_required' => $item['is_required'],
-                    'status' => 'normal'
+                    'status' => StatusEnum::ENABLED
                 ]);
                 ]);
                 $itemResult[] = $newItem;
                 $itemResult[] = $newItem;
             }
             }
@@ -170,11 +170,16 @@ class Type extends Backend
         }
         }
         
         
         // 处理fieldlist格式的验货选项数据
         // 处理fieldlist格式的验货选项数据
-        $arrItems = $params['items'] ?? [];
-        if(empty($arrItems)){
+        $jsonItems = $params['items'] ?? [];
+        if(empty($jsonItems)){
             $this->error(__('Parameter %s can not be empty', 'items'));
             $this->error(__('Parameter %s can not be empty', 'items'));
         }
         }
-        
+        $arrItems = json_decode($jsonItems, true); 
+        // 删除调试代码
+        // echo "<pre>";
+        // print_r($arrItems);
+        // die();
+
         $params = $this->preExcludeFields($params);
         $params = $this->preExcludeFields($params);
         $result = false;
         $result = false;
         Db::startTrans();
         Db::startTrans();
@@ -191,34 +196,66 @@ class Type extends Backend
             $itemModel = new ItemModel();
             $itemModel = new ItemModel();
             $typeItemModel = new TypeItemModel();
             $typeItemModel = new TypeItemModel();
             
             
-
+            // 1. 查询当前类型关联的所有验货选项
+            $existingTypeItems = $typeItemModel->where('type_id', $row->id)->select();
+            $existingItemIds = array_column(collection($existingTypeItems)->toArray(), 'item_id');
             
             
-            // 处理前端传来的选项数据 - 由于fieldlist不包含id,我们采用替换策略
-            // 先删除所有现有关联
-            $typeItemModel->where('type_id', $row->id)->delete();
+            // 2. 从前端数据中提取ID
+            $frontendItemIds = [];
+            $frontendItemsById = [];
+            foreach($arrItems as $item) {
+                if(isset($item['id']) && !empty($item['id'])) {
+                    $frontendItemIds[] = $item['id'];
+                    $frontendItemsById[$item['id']] = $item;
+                }
+            }
             
             
-            // 重新创建所有选项和关联
-            $newItemIds = [];
-            foreach($arrItems as $item){
-                // 创建新选项
-                $newItem = $itemModel->create([
-                    'name' => $item['name'],
-                    'standard_desc' => $item['standard_desc'],
-                    'is_required' => $item['is_required'],
-                    'status' => 'normal'
+            // 3. 判断需要删除的选项 (存在于数据库但不存在于前端数据中)
+            $itemsToDelete = array_diff($existingItemIds, $frontendItemIds);
+            if(!empty($itemsToDelete)) {
+                // 删除关联关系
+                $typeItemModel->where('type_id', $row->id)->where('item_id', 'in', $itemsToDelete)->delete();
+                // 删除选项本身
+                $itemModel->where('id', 'in', $itemsToDelete)->delete();
+            }
+            
+            // 4. 判断需要修改的选项 (存在于数据库且存在于前端数据中)
+            $itemsToUpdate = array_intersect($existingItemIds, $frontendItemIds);
+            foreach($itemsToUpdate as $itemId) {
+                $itemData = $frontendItemsById[$itemId];
+                $itemModel->where('id', $itemId)->update([
+                    'name' => $itemData['name'],
+                    'standard_desc' => $itemData['standard_desc'],
+                    'is_required' => $itemData['is_required'],
                 ]);
                 ]);
-                $newItemIds[] = $newItem->id;
             }
             }
             
             
-            // 创建新的关联关系
-            $arrTypeItem = [];
-            foreach($newItemIds as $itemId){
-                $arrTypeItem[] = [
-                    'item_id' => $itemId,
-                    'type_id' => $row->id,
-                ];
+            // 5. 判断需要新增的选项 (不存在于数据库但存在于前端数据中,或者没有ID的新选项)
+            $newItems = [];
+            foreach($arrItems as $item) {
+                // 如果没有ID或者ID不在现有ID列表中,则为新增
+                if(!isset($item['id']) || empty($item['id']) || !in_array($item['id'], $existingItemIds)) {
+                    $newItem = $itemModel->create([
+                        'name' => $item['name'],
+                        'standard_desc' => $item['standard_desc'],
+                        'is_required' => $item['is_required'],
+                        'status' => StatusEnum::ENABLED
+                    ]);
+                    $newItems[] = $newItem;
+                }
+            }
+            
+            // 6. 为新增的选项创建关联关系
+            if(!empty($newItems)) {
+                $arrTypeItem = [];
+                foreach($newItems as $newItem) {
+                    $arrTypeItem[] = [
+                        'item_id' => $newItem->id,
+                        'type_id' => $row->id,
+                    ];
+                }
+                $typeItemModel->saveAll($arrTypeItem);
             }
             }
-            $typeItemModel->saveAll($arrTypeItem);
             
             
             Db::commit();
             Db::commit();
         } catch (ValidateException|PDOException|Exception $e) {
         } catch (ValidateException|PDOException|Exception $e) {

+ 1 - 1
application/api/controller/inspection/Index.php

@@ -21,9 +21,9 @@ class Index extends Base
         if(intval($inspectId) <= 0){
         if(intval($inspectId) <= 0){
             $this->error('验货类型ID不能为空');
             $this->error('验货类型ID不能为空');
         }
         }
-        $params['inspection_type_id'] = $inspectId;
         $params = [
         $params = [
             'status' => StatusEnum::ENABLED,
             'status' => StatusEnum::ENABLED,
+            'inspection_type_id' => $inspectId,
         ];
         ];
         $options = InspectService::getInspectionItems($params);
         $options = InspectService::getInspectionItems($params);
         $this->success('获取成功', $options);
         $this->success('获取成功', $options);

+ 0 - 1
application/common/Service/InspectService.php

@@ -39,7 +39,6 @@ class InspectService
         if (isset($params['name']) && $params['name'] !== '') {
         if (isset($params['name']) && $params['name'] !== '') {
             $where['name'] = ['like', '%' . $params['name'] . '%'];
             $where['name'] = ['like', '%' . $params['name'] . '%'];
         }
         }
-
         if (isset($params['inspection_type_id']) && $params['inspection_type_id'] !== '') {
         if (isset($params['inspection_type_id']) && $params['inspection_type_id'] !== '') {
             // 通过关联表查询ID
             // 通过关联表查询ID
             $typeItem = TypeItem::where('type_id', $params['inspection_type_id'])->select();
             $typeItem = TypeItem::where('type_id', $params['inspection_type_id'])->select();