xieyonghong 3 роки тому
батько
коміт
af9016e941

+ 22 - 31
src/pages/rule-engine/Scene/Save/action/index.tsx

@@ -63,32 +63,27 @@ export default (props: ActionsProps) => {
                       parallel={false}
                       parallel={false}
                       actions={parallelThens.length ? parallelThens[0].actions : []}
                       actions={parallelThens.length ? parallelThens[0].actions : []}
                       onAdd={(actionItem) => {
                       onAdd={(actionItem) => {
-                        // console.log('parallelThens', parallelThens, actionItem);
-                        if (parallelThens[0]) {
-                          if (
-                            parallelThens[0].actions.some((aItem) => aItem.key === actionItem.key)
-                          ) {
-                            parallelThens[0].actions = parallelThens[0].actions.map((aItem) => {
-                              if (aItem.key === actionItem.key) {
-                                return actionItem;
-                              }
-                              return aItem;
-                            });
+                        const thenIndex = props.thenOptions.findIndex((item) => !item.parallel);
+                        if (thenIndex !== -1) {
+                          const indexOf = props.thenOptions[thenIndex].actions?.findIndex(
+                            (aItem) => aItem.key === actionItem.key,
+                          );
+                          if (indexOf !== -1) {
+                            props.thenOptions[thenIndex].actions.splice(indexOf, 1, actionItem);
                           } else {
                           } else {
-                            parallelThens[0].actions.push(actionItem);
+                            props.thenOptions[thenIndex].actions.push(actionItem);
                           }
                           }
-                          set(FormModel.current, ['branches', props.name, 'then'], parallelThens);
                         } else {
                         } else {
-                          parallelThens.push({
+                          props.thenOptions.push({
                             parallel: false,
                             parallel: false,
                             actions: [actionItem],
                             actions: [actionItem],
                           });
                           });
-                          set(FormModel.current, ['branches', props.name, 'then'], parallelThens);
                         }
                         }
+                        set(FormModel.current, ['branches', props.name, 'then'], props.thenOptions);
                       }}
                       }}
                       onDelete={(_index) => {
                       onDelete={(_index) => {
                         parallelThens[0].actions.splice(_index, 1);
                         parallelThens[0].actions.splice(_index, 1);
-                        set(FormModel.current, ['branches', props.name, 'then'], parallelThens);
+                        set(FormModel.current, ['branches', props.name, 'then'], props.thenOptions);
                       }}
                       }}
                     />
                     />
                   );
                   );
@@ -115,31 +110,27 @@ export default (props: ActionsProps) => {
                       parallel={true}
                       parallel={true}
                       actions={parallelThens.length ? parallelThens[0].actions : []}
                       actions={parallelThens.length ? parallelThens[0].actions : []}
                       onAdd={(actionItem) => {
                       onAdd={(actionItem) => {
-                        if (parallelThens[0]) {
-                          if (
-                            parallelThens[0].actions.some((aItem) => aItem.key === actionItem.key)
-                          ) {
-                            parallelThens[0].actions = parallelThens[0].actions.map((aItem) => {
-                              if (aItem.key === actionItem.key) {
-                                return actionItem;
-                              }
-                              return aItem;
-                            });
+                        const thenIndex = props.thenOptions.findIndex((item) => item.parallel);
+                        if (thenIndex !== -1) {
+                          const indexOf = props.thenOptions[thenIndex].actions?.findIndex(
+                            (aItem) => aItem.key === actionItem.key,
+                          );
+                          if (indexOf !== -1) {
+                            props.thenOptions[thenIndex].actions.splice(indexOf, 1, actionItem);
                           } else {
                           } else {
-                            parallelThens[0].actions.push(actionItem);
+                            props.thenOptions[thenIndex].actions.push(actionItem);
                           }
                           }
-                          set(FormModel.current, ['branches', props.name, 'then'], parallelThens);
                         } else {
                         } else {
-                          parallelThens.push({
+                          props.thenOptions.push({
                             parallel: true,
                             parallel: true,
                             actions: [actionItem],
                             actions: [actionItem],
                           });
                           });
-                          set(FormModel.current, ['branches', props.name, 'then'], parallelThens);
                         }
                         }
+                        set(FormModel.current, ['branches', props.name, 'then'], props.thenOptions);
                       }}
                       }}
                       onDelete={(_index) => {
                       onDelete={(_index) => {
                         parallelThens[0].actions.splice(_index, 1);
                         parallelThens[0].actions.splice(_index, 1);
-                        set(FormModel.current, ['branches', props.name, 'then'], parallelThens);
+                        set(FormModel.current, ['branches', props.name, 'then'], props.thenOptions);
                       }}
                       }}
                     />
                     />
                   );
                   );

+ 19 - 20
src/pages/rule-engine/Scene/Save/components/Buttons/Dropdown.tsx

@@ -1,5 +1,5 @@
 import { useCallback, useEffect, useMemo, useState } from 'react';
 import { useCallback, useEffect, useMemo, useState } from 'react';
-import { Dropdown, Tree } from 'antd';
+import { Dropdown, Empty, Tree } from 'antd';
 import classNames from 'classnames';
 import classNames from 'classnames';
 import styles from './index.less';
 import styles from './index.less';
 import { onlyMessage } from '@/utils/util';
 import { onlyMessage } from '@/utils/util';
@@ -98,25 +98,24 @@ const DropdownButton = (props: DropdownButtonProps) => {
     );
     );
   }, [props.options, myValue]);
   }, [props.options, myValue]);
 
 
-  const _options = !props.isTree ? { menu: menuOptions } : { dropdownRender: () => DropdownRender };
-
-  // const findLabel = (value: string, data: DropdownButtonOptions[]): boolean => {
-  //   let isLabel = false;
-  //   return data.some((item) => {
-  //     if (item.key === value) {
-  //       let titleKey = 'title';
-  //       if (props.showLabelKey) {
-  //         titleKey = props.showLabelKey;
-  //       }
-  //       setLabel(item[titleKey]);
-  //       setLoading(false);
-  //       isLabel = true;
-  //     } else if (item.children) {
-  //       isLabel = findLabel(value, item.children);
-  //     }
-  //     return isLabel;
-  //   });
-  // };
+  const _options = !props.isTree
+    ? {
+        menu: menuOptions,
+        dropdownRender: (menu: any) => {
+          if (!menuOptions.items.length) {
+            return (
+              <div
+                className={styles['dropdown-content']}
+                style={{ background: '#fff', padding: '0 16px' }}
+              >
+                <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />
+              </div>
+            );
+          }
+          return menu;
+        },
+      }
+    : { dropdownRender: () => DropdownRender };
 
 
   useEffect(() => {
   useEffect(() => {
     setMyValue(props.value);
     setMyValue(props.value);

+ 1 - 2
src/pages/rule-engine/Scene/Save/components/Buttons/ParamsDropdown.tsx

@@ -188,7 +188,6 @@ export default (props: ParamsDropdownProps) => {
 
 
   const valueLabel = useCallback(
   const valueLabel = useCallback(
     (v: any, type: string) => {
     (v: any, type: string) => {
-      console.log(type, v);
       switch (type) {
       switch (type) {
         case 'boolean':
         case 'boolean':
           setLabel(v ? '是' : '否');
           setLabel(v ? '是' : '否');
@@ -237,7 +236,7 @@ export default (props: ParamsDropdownProps) => {
 
 
   useEffect(() => {
   useEffect(() => {
     if (props.BuiltInOptions) {
     if (props.BuiltInOptions) {
-      let _value = props.value.value;
+      let _value = props.value?.value;
       if ('name' in props) {
       if ('name' in props) {
         _value = props.value?.value[props.name!];
         _value = props.value?.value[props.name!];
       }
       }

+ 16 - 2
src/pages/rule-engine/Scene/Save/index.tsx

@@ -129,8 +129,7 @@ export default observer(() => {
         if (resp.status === 200) {
         if (resp.status === 200) {
           console.log('defaultBranches', defaultBranches);
           console.log('defaultBranches', defaultBranches);
           const branches = resp.result.branches || defaultBranches;
           const branches = resp.result.branches || defaultBranches;
-          if (branches[0]?.when?.length) {
-            // 设备
+          if (resp.result.branches && !resp.result.branches.length) {
             branches.push({
             branches.push({
               when: [],
               when: [],
               key: 'branches_' + new Date().getTime(),
               key: 'branches_' + new Date().getTime(),
@@ -142,6 +141,21 @@ export default observer(() => {
               },
               },
               then: [],
               then: [],
             });
             });
+          } else {
+            if (branches[0]?.when?.length) {
+              // 设备
+              branches.push({
+                when: [],
+                key: 'branches_' + new Date().getTime(),
+                shakeLimit: {
+                  enabled: false,
+                  time: 0,
+                  threshold: 0,
+                  alarmFirst: false,
+                },
+                then: [],
+              });
+            }
           }
           }
           FormModel.current = {
           FormModel.current = {
             ...resp.result,
             ...resp.result,

+ 39 - 20
src/pages/rule-engine/Scene/Save/terms/index.tsx

@@ -5,10 +5,10 @@ import { model } from '@formily/reactive';
 import { FormModel, defaultBranches } from '@/pages/rule-engine/Scene/Save';
 import { FormModel, defaultBranches } from '@/pages/rule-engine/Scene/Save';
 import BranchItem from './branchItem';
 import BranchItem from './branchItem';
 import { service } from '@/pages/rule-engine/Scene/index';
 import { service } from '@/pages/rule-engine/Scene/index';
-import { Switch } from 'antd'
+import { Switch } from 'antd';
 import type { TriggerType } from '@/pages/rule-engine/Scene/typings';
 import type { TriggerType } from '@/pages/rule-engine/Scene/typings';
 import Actions from '@/pages/rule-engine/Scene/Save/action';
 import Actions from '@/pages/rule-engine/Scene/Save/action';
-import {cloneDeep} from "lodash";
+import { cloneDeep, set } from 'lodash';
 
 
 interface TermsModelProps {
 interface TermsModelProps {
   columnOptions: any[];
   columnOptions: any[];
@@ -19,15 +19,14 @@ export const TermsModel = model<TermsModelProps>({
 });
 });
 
 
 export default observer(() => {
 export default observer(() => {
-
-  const [open, setOpen] = useState(true)
+  const [open, setOpen] = useState(true);
 
 
   useEffect(() => {
   useEffect(() => {
-    console.log('terms-effect',FormModel.current.branches)
+    console.log('terms-effect', FormModel.current.branches);
     if (FormModel.current.branches && FormModel.current.branches.length === 1) {
     if (FormModel.current.branches && FormModel.current.branches.length === 1) {
-      setOpen(false)
+      setOpen(false);
     }
     }
-  }, [FormModel.current.branches])
+  }, [FormModel.current.branches]);
 
 
   const queryColumn = (data: TriggerType) => {
   const queryColumn = (data: TriggerType) => {
     service.getParseTerm({ trigger: data }).then((res: any) => {
     service.getParseTerm({ trigger: data }).then((res: any) => {
@@ -36,8 +35,8 @@ export default observer(() => {
   };
   };
 
 
   const openChange = (checked: boolean) => {
   const openChange = (checked: boolean) => {
-    console.log('terms-effect-change')
-    setOpen(checked)
+    console.log('terms-effect-change');
+    setOpen(checked);
     if (checked) {
     if (checked) {
       FormModel.current.branches = cloneDeep([
       FormModel.current.branches = cloneDeep([
         ...defaultBranches,
         ...defaultBranches,
@@ -52,12 +51,21 @@ export default observer(() => {
           },
           },
           then: [],
           then: [],
         },
         },
-      ])
+      ]);
+      set(FormModel.current.options!, 'when', [
+        {
+          terms: [
+            {
+              terms: [],
+            },
+          ],
+        },
+      ]);
     } else {
     } else {
       FormModel.current.branches = [
       FormModel.current.branches = [
         {
         {
           when: [],
           when: [],
-          key: 'branches_'+new Date().getTime(),
+          key: 'branches_' + new Date().getTime(),
           shakeLimit: {
           shakeLimit: {
             enabled: false,
             enabled: false,
             time: 0,
             time: 0,
@@ -65,10 +73,11 @@ export default observer(() => {
             alarmFirst: false,
             alarmFirst: false,
           },
           },
           then: [],
           then: [],
-        }
-      ]
+        },
+      ];
+      set(FormModel.current.options!, 'when', []);
     }
     }
-  }
+  };
 
 
   useEffect(() => {
   useEffect(() => {
     if (FormModel.current.trigger?.device) {
     if (FormModel.current.trigger?.device) {
@@ -82,12 +91,17 @@ export default observer(() => {
         style={{ fontSize: 14 }}
         style={{ fontSize: 14 }}
         data={
         data={
           <span>
           <span>
-            触发条件 <Switch checked={open} onChange={openChange} checkedChildren={'开'} unCheckedChildren={'关'}/>
+            触发条件{' '}
+            <Switch
+              checked={open}
+              onChange={openChange}
+              checkedChildren={'开'}
+              unCheckedChildren={'关'}
+            />
           </span>
           </span>
         }
         }
       />
       />
-      {
-        open ?
+      {open ? (
         <Observer>
         <Observer>
           {() =>
           {() =>
             FormModel.current.branches?.map((item, index) => {
             FormModel.current.branches?.map((item, index) => {
@@ -106,9 +120,14 @@ export default observer(() => {
               );
               );
             })
             })
           }
           }
-        </Observer> :
-          <Actions openShakeLimit={true} name={0} thenOptions={FormModel.current.branches ? FormModel.current.branches[0].then : []} />
-      }
+        </Observer>
+      ) : (
+        <Actions
+          openShakeLimit={true}
+          name={0}
+          thenOptions={FormModel.current.branches ? FormModel.current.branches[0].then : []}
+        />
+      )}
     </div>
     </div>
   );
   );
 });
 });