فهرست منبع

修复隔离点编辑的数据和更新

pm 3 ماه پیش
والد
کامیت
51abcc802c
2فایلهای تغییر یافته به همراه86 افزوده شده و 28 حذف شده
  1. 3 2
      src/api/spm/index.ts
  2. 83 26
      src/components/SegregationPointForm.tsx

+ 3 - 2
src/api/spm/index.ts

@@ -2,6 +2,7 @@ import { request } from '../../utils/axios';
 
 // 隔离点 VO 类型
 export interface SegregationPointVO {
+  id?: number; // 详情接口返回的 id 字段
   pointId?: number;
   pointCode: string;
   pointName: string;
@@ -9,7 +10,7 @@ export interface SegregationPointVO {
   pointPicture?: string;
   pointNfc: string;
   workstationId: number;
-  lotoId: number;
+  lotoId: number | null; // lotoId 可能为 null
   powerType?: string;
   pointSerialNumber?: string;
   remark?: string;
@@ -19,7 +20,7 @@ export interface SegregationPointVO {
   lotoName?: string;
   machineryId?: number;
   machineryName?: string;
-  switchStatus?: number | string;
+  switchStatus?: number | string | null;
 }
 
 // 分页参数类型

+ 83 - 26
src/components/SegregationPointForm.tsx

@@ -25,6 +25,8 @@ const SegregationPointForm = forwardRef<SegregationPointFormRef, SegregationPoin
     const [dialogTitle, setDialogTitle] = useState('');
     const [formLoading, setFormLoading] = useState(false);
     const [formType, setFormType] = useState<'create' | 'update'>('create');
+    const [currentPointId, setCurrentPointId] = useState<number | undefined>(); // 保存当前编辑的隔离点ID
+    const [detailData, setDetailData] = useState<SegregationPointVO | null>(null); // 保存详情接口返回的完整数据
     const [form] = Form.useForm();
     const [selectedImageIndex, setSelectedImageIndex] = useState(-1);
     const [imageMap, setImageMap] = useState<Record<number, string>>({});
@@ -42,11 +44,14 @@ const SegregationPointForm = forwardRef<SegregationPointFormRef, SegregationPoin
       form.resetFields();
       setSelectedImageIndex(-1);
       setImageMap({});
+      
+      // 保存当前编辑的ID
+      setCurrentPointId(id);
 
       // 加载下拉选项数据
       await loadOptionsData();
 
-      // 修改时,设置数据
+      // 修改时,调用详情接口获取数据
       if (id) {
         setFormLoading(true);
         try {
@@ -58,21 +63,32 @@ const SegregationPointForm = forwardRef<SegregationPointFormRef, SegregationPoin
           const data = (response as any)?.data || response;
           console.log('SegregationPointForm: 处理后的数据', data);
           
-          // 设置表单值
+          // 保存详情接口返回的完整数据
+          setDetailData(data as SegregationPointVO);
+          
+          // 确保 id 存在(从详情接口返回的数据或传入的 id)
+          const currentId = data.id || id;
+          
+          // 更新当前编辑的ID(确保使用详情接口返回的ID)
+          setCurrentPointId(currentId);
+          
+          // 设置表单值(用于回显)- 使用详情接口返回的完整数据
           const formValues = {
-            pointId: data.pointId || data.id,
-            pointName: data.pointName || '',
-            pointIcon: data.pointIcon || '',
-            pointPicture: data.pointPicture || '',
-            pointNfc: data.pointNfc || '',
+            pointName: data.pointName ?? '',
+            pointIcon: data.pointIcon ?? '',
+            pointPicture: data.pointPicture ?? '',
+            pointNfc: data.pointNfc ?? '',
             workstationId: data.workstationId,
-            lotoId: data.lotoId,
-            powerType: data.powerType || '',
-            pointSerialNumber: data.pointSerialNumber || '',
-            remark: data.remark || '',
+            // lotoId 可能为 null,需要保留 null 值(不要转换为 undefined)
+            // 如果为 null 则设置为 undefined(表单 Select 组件需要 undefined 而不是 null)
+            lotoId: data.lotoId !== undefined && data.lotoId !== null ? data.lotoId : undefined,
+            powerType: data.powerType ?? '',
+            pointSerialNumber: data.pointSerialNumber ?? '',
+            remark: data.remark ?? '',
           };
           
-          console.log('SegregationPointForm: 设置表单值', formValues);
+          console.log('SegregationPointForm: 设置表单值(从详情接口获取)', formValues);
+          console.log('SegregationPointForm: 详情接口返回的原始数据', data);
           
           // 使用 setTimeout 确保表单已渲染
           setTimeout(() => {
@@ -88,6 +104,9 @@ const SegregationPointForm = forwardRef<SegregationPointFormRef, SegregationPoin
           setFormLoading(false);
         }
       } else {
+        // 新增时清空ID和详情数据
+        setCurrentPointId(undefined);
+        setDetailData(null);
         // 新增时也加载图标
         await loadIsolationIcons();
       }
@@ -184,35 +203,73 @@ const SegregationPointForm = forwardRef<SegregationPointFormRef, SegregationPoin
         
         setFormLoading(true);
         try {
-          const data: SegregationPointVO = {
-            pointId: values.pointId,
-            pointName: values.pointName,
-            pointIcon: values.pointIcon,
-            pointPicture: values.pointPicture,
-            pointNfc: values.pointNfc,
-            workstationId: values.workstationId,
-            lotoId: values.lotoId,
-            powerType: values.powerType,
-            pointSerialNumber: values.pointSerialNumber,
-            remark: values.remark,
-          };
-
           if (formType === 'create') {
+            // 新增时,使用表单数据
+            const data: SegregationPointVO = {
+              pointCode: '', // 新增时 pointCode 为空,由后端生成
+              pointName: values.pointName,
+              pointIcon: values.pointIcon,
+              pointPicture: values.pointPicture,
+              pointNfc: values.pointNfc,
+              workstationId: values.workstationId,
+              lotoId: values.lotoId,
+              powerType: values.powerType,
+              pointSerialNumber: values.pointSerialNumber,
+              remark: values.remark,
+            };
             await segregationPointApi.addinsertIsIsolationPoint(data);
             toast.success(t('common.addSuccess'));
           } else {
-            await segregationPointApi.updateIsIsolationPoint(data);
+            // 编辑时,确保有详情数据
+            if (!detailData) {
+              toast.error('缺少详情数据,无法更新');
+              setFormLoading(false);
+              return;
+            }
+            
+            // 直接使用详情接口返回的完整数据作为基础,然后用表单中修改的值覆盖
+            // 这样确保所有字段都来自详情接口,而不是写死的值
+            const updateData: any = {
+              // 先复制详情接口返回的所有字段(包括 id、lotoId: null 等所有字段)
+              ...detailData,
+              // 确保使用 id(详情接口返回的是 id,不是 pointId)
+              id: detailData.id || currentPointId,
+              // 然后用表单中用户修改的值覆盖(只覆盖表单中存在的字段)
+              pointName: values.pointName !== undefined ? values.pointName : detailData.pointName,
+              pointIcon: values.pointIcon !== undefined ? values.pointIcon : detailData.pointIcon,
+              pointPicture: values.pointPicture !== undefined ? values.pointPicture : detailData.pointPicture,
+              pointNfc: values.pointNfc !== undefined ? values.pointNfc : detailData.pointNfc,
+              workstationId: values.workstationId !== undefined ? values.workstationId : detailData.workstationId,
+              // lotoId 处理:如果表单中有值(包括 null),使用表单值;否则使用详情数据中的原值(可能是 null)
+              // 注意:表单 Select 清空时返回 undefined,此时应该保留详情数据中的 null
+              lotoId: values.lotoId !== undefined ? values.lotoId : detailData.lotoId,
+              powerType: values.powerType !== undefined ? values.powerType : detailData.powerType,
+              pointSerialNumber: values.pointSerialNumber !== undefined ? values.pointSerialNumber : detailData.pointSerialNumber,
+              remark: values.remark !== undefined ? values.remark : detailData.remark,
+            };
+            
+            // 如果详情数据中有 pointId 字段,删除它(因为详情接口返回的是 id,不是 pointId)
+            if ('pointId' in updateData) {
+              delete updateData.pointId;
+            }
+            
+            console.log('SegregationPointForm: 详情接口返回的完整数据', detailData);
+            console.log('SegregationPointForm: 表单值', values);
+            console.log('SegregationPointForm: 提交更新数据(基于详情接口完整数据)', updateData);
+            await segregationPointApi.updateIsIsolationPoint(updateData);
             toast.success(t('common.updateSuccess'));
           }
           setDialogVisible(false);
           onSuccess?.();
         } catch (error: any) {
+          console.error('SegregationPointForm: 提交失败', error);
           toast.error(error.message || t('common.operationFailed'));
         } finally {
           setFormLoading(false);
         }
       } catch (error) {
         // 表单验证失败
+        console.error('SegregationPointForm: 表单验证失败', error);
       }
     };