wzyyy 3 лет назад
Родитель
Сommit
07b98a056e

+ 43 - 40
src/pages/device/Instance/Detail/ChildDevice/SaveChild/index.tsx

@@ -35,46 +35,48 @@ const SaveChild = (props: Props) => {
 
   useEffect(() => {
     if (props.childData.id) {
-      const item = productList.filter((i: any) => i.id === props.childData.productId)[0];
-      console.log(props.childData, item);
-      // const metadata = JSON.parse(item.metadata|| []).properties?.map((item: any) => ({
-      //   metadataId: item.id,
-      //   metadataName: `${item.name}(${item.id})`,
-      //   metadataType: 'property',
-      // }));
-      // if (metadata && metadata.length !== 0) {
-      //   service
-      //     .getMap(props.data.id, {
-      //       deviceId: props.childData.id,
-      //       query: {},
-      //     })
-      //     .then((res) => {
-      //       if (res.status === 200) {
-      //         // console.log(res.result)
-      //         //合并物模型
-      //         const array = res.result[0]?.reduce((x: any, y: any) => {
-      //           const metadataId = metadata.find((item: any) => item.metadataId === y.metadataId);
-      //           if (metadataId) {
-      //             Object.assign(metadataId, y);
-      //           } else {
-      //             x.push(y);
-      //           }
-      //           return x;
-      //         }, metadata);
-      //         //删除物模型
-      //         const items = array.filter((item: any) => item.metadataName);
-      //         setMetaData(items);
-      //         const delList = array.filter((a: any) => !a.metadataName).map((b: any) => b.id);
-      //         //删除后解绑
-      //         if (delList && delList.length !== 0) {
-      //           service.removeMap(props.data.id, {
-      //             deviceId: props.childData.id,
-      //             idList: [...delList],
-      //           });
-      //         }
-      //       }
-      //     });
-      // }
+      const product = productList.filter((i: any) => i.id === props.childData.productId)[0];
+      // console.log(JSON.parse(item.metadata || []));
+      if (product && product.metadata) {
+        const metadata = JSON.parse(product?.metadata || {})?.properties?.map((item: any) => ({
+          metadataId: item.id,
+          metadataName: `${item.name}(${item.id})`,
+          metadataType: 'property',
+        }));
+        if (metadata && metadata.length !== 0) {
+          service
+            .getMap(props.data.id, {
+              deviceId: props.childData.id,
+              query: {},
+            })
+            .then((res) => {
+              if (res.status === 200) {
+                // console.log(res.result)
+                //合并物模型
+                const array = res.result[0]?.reduce((x: any, y: any) => {
+                  const metadataId = metadata.find((item: any) => item.metadataId === y.metadataId);
+                  if (metadataId) {
+                    Object.assign(metadataId, y);
+                  } else {
+                    x.push(y);
+                  }
+                  return x;
+                }, metadata);
+                //删除物模型
+                const items = array.filter((item: any) => item.metadataName);
+                setMetaData(items);
+                const delList = array.filter((a: any) => !a.metadataName).map((b: any) => b.id);
+                //删除后解绑
+                if (delList && delList.length !== 0) {
+                  service.removeMap(props.data.id, {
+                    deviceId: props.childData.id,
+                    idList: [...delList],
+                  });
+                }
+              }
+            });
+        }
+      }
     }
   }, [productList]);
 
@@ -113,6 +115,7 @@ const SaveChild = (props: Props) => {
               rules={[{ required: true, message: '请选择产品名称' }]}
             >
               <Select
+                disabled={props.childData.id}
                 onChange={(e) => {
                   if (e) {
                     setVisible(true);

+ 5 - 4
src/pages/device/Instance/Detail/EdgeMap/mapTable/index.tsx

@@ -226,19 +226,20 @@ const MapTable = (props: Props) => {
           parentId: edgeId,
           id: deviceId ? deviceId : undefined,
         };
-        console.log(formData);
-        const res = await service.addDevice(formData);
+        const res = deviceId
+          ? await service.editDevice(formData)
+          : await service.addDevice(formData);
         if (res.status === 200) {
           const array = mapValue.requestList.filter((item: any) => item.channelId);
           const submitData = {
-            deviceId: res.result.id,
+            deviceId: deviceId ? deviceId : res.result.id,
             provider: array?.[0]?.provider,
             requestList: array,
           };
           save(submitData);
         }
       }
-      console.log(value, mapValue);
+      // console.log(value, mapValue);
     }
   };
 

+ 5 - 0
src/pages/device/Instance/Detail/EdgeMap/service.ts

@@ -62,6 +62,11 @@ class Service extends BaseService<any> {
     });
   addDevice = (params: any) =>
     request(`/${SystemConst.API_BASE}/device-instance`, {
+      method: 'POST',
+      data: params,
+    });
+  editDevice = (params: any) =>
+    request(`/${SystemConst.API_BASE}/device-instance`, {
       method: 'PATCH',
       data: params,
     });