useDrawer.vue 11 KB
<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    showFooter
    :title="getTitle"
    width="1000px"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm" />
    <div>
      <AddTriggerForm
        @eGetAddPushValueData="getAddPushValueDataValueFunc"
        ref="getTriggerChildData"
        :deviceInfo="getDeviceInfo"
      />
    </div>
    <div>
      <AddConditiForm
        @eDoConditionGetAddPushValueData="getAddDoconditionPushValueDataValueFunc"
        ref="getConditionChildData"
        :deviceInfo1="getDeviceInfo1"
      />
    </div>
    <div>
      <AddActionForm
        @eDoactionGetAddPushValueData="getAddDoActionPushValueDataValueFunc"
        ref="getChildData"
        :deviceInfo2="getDeviceInfo2"
      />
    </div>
  </BasicDrawer>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref, reactive, watch, getCurrentInstance } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form';
  import { formSchema, getData } from './config.d';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import {
    screenLinkPageAddApi,
    screenLinkPageUpdateApi,
    screenLinkPageByDeptIdGetDevice,
  } from '/@/api/ruleengine/ruleengineApi';
  import { useMessage } from '/@/hooks/web/useMessage';
  import AddTriggerForm from './addForm/trigger.vue';
  import AddActionForm from './addForm/doaction.vue';
  import AddConditiForm from './addForm/condition.vue';

  export default defineComponent({
    name: 'ConfigDrawer',
    components: { BasicDrawer, BasicForm, AddTriggerForm, AddActionForm, AddConditiForm },
    emits: ['success', 'register', 'registerForm'],
    setup(_, { emit }) {
      const { proxy } = getCurrentInstance();
      const getChildData = ref(null);
      const getTriggerChildData = ref(null);
      const getConditionChildData = ref(null);
      const { createMessage } = useMessage();
      const isUpdate = ref(true);
      let doConditionsArray: any[] = reactive([]);
      let doActionsArray: any[] = reactive([]);
      let triggersArray: any[] = reactive([]);
      let getAllFormData: any = reactive({});
      let getValuesFormData: any = reactive({});
      let getId = ref('');
      let getTenantId = ref('');
      let getDeviceInfo = ref(null);
      let getDeviceInfo1 = ref(null);
      let getDeviceInfo2 = ref(null);
      const isCountT = ref(null);
      const isAddTriggerT: any = ref([]);
      const isCountD = ref(null);
      const isAddActionD: any = ref([]);
      const isCountC = ref(null);
      const isAddConditionD: any = ref([]);

      const [registerForm, { resetFields, setFieldsValue, validateFields, getFieldsValue }] =
        useForm({
          labelWidth: 120,
          schemas: formSchema,
          showActionButtonGroup: false,
        });

      watch(getData, (newV) => {
        getDeviceInfo.value = newV;
        getDeviceInfo1.value = newV;
        getDeviceInfo2.value = newV;
      });

      const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
        await resetFields();
        setDrawerProps({ confirmLoading: false });
        isUpdate.value = !!data?.isUpdate;

        if (!unref(isUpdate)) {
          triggersArray.length = 0;
          doConditionsArray.length = 0;
          doActionsArray.length = 0;
          isAddTriggerT.value.length = 0;

          isAddActionD.value.length = 0;

          isAddConditionD.value.length = 0;
          proxy.$refs.getTriggerChildData.addClearGetAddPushValueDataArrayIsEmpty();
          proxy.$refs.getTriggerChildData.clearAddTriggerStatusFunc();
          proxy.$refs.getTriggerChildData.clearGetPushValueDataEmpty();
          proxy.$refs.getTriggerChildData.clearSelectDevice();
          proxy.$refs.getChildData.clearSelectDevice();
          proxy.$refs.getConditionChildData.clearSelectDevice();
          proxy.$refs.getChildData.funcResetFields();
          proxy.$refs.getTriggerChildData.funcResetFields();
          proxy.$refs.getConditionChildData.funcResetFields();
        }
        //编辑
        if (unref(isUpdate)) {
          console.log(data);
          getId.value = data.record.id;
          getTenantId.value = data.record.tenantId;
          await setFieldsValue({
            ...data.record,
          });
          let newMap = null;
          let newFilterMap = null;
          let newTwoFilterMap = null;
          let newFilterTwoFilterMap = null;
          const options = await screenLinkPageByDeptIdGetDevice({
            organizationId: data.record.organizationId,
          });
          newMap = options.items.map((m) => {
            try {
              newTwoFilterMap = data.record?.triggers.map((m1) => {
                if (m.id == m1.deviceId) {
                  return {
                    value: m.id,
                    label: m.name,
                  };
                }
              });

              if (m.id == data.record?.triggers[0].deviceId) {
                return {
                  value: m.id,
                  label: m.name,
                };
              }
            } catch (e) {}
          });
          newFilterMap = newMap.filter((f) => f !== undefined);
          newFilterTwoFilterMap = newTwoFilterMap.filter((f) => f !== undefined);
          proxy.$refs.getTriggerChildData.editSelectDevice(newFilterMap);
          proxy.$refs.getChildData.editSelectDevice(newFilterMap);
          proxy.$refs.getConditionChildData.editSelectDevice(newFilterMap);

          try {
            proxy.$refs.getTriggerChildData.setFieldsFormValue({
              triggerEvent: data.record.triggers[0].triggerEvent,
              attributeChoose: data.record?.triggers[0].attributeChoose,
              touchWay: data.record?.triggers[0].touchWay,
              deviceId: data.record?.triggers[0].deviceId,
              compare: data.record?.triggers[0].compare,
              value: data.record?.triggers[0].value,
              sceneLinkageId: data.record?.triggers[0].sceneLinkageId,
            });
            proxy.$refs.getConditionChildData.setFieldsFormValue({
              status: data.record?.doConditions[0].status,
              deviceId: data.record?.doConditions[0].deviceId,
              createTime: data.record?.doConditions[0].createTime,
              updateTime: data.record?.doConditions[0].updateTime,
              property: data.record?.doConditions[0].property,
              compare: data.record?.doConditions[0].compare,
              value: data.record?.doConditions[0].value,
            });
            proxy.$refs.getChildData.setFieldsFormValue({
              outTarget: data.record?.doActions[0].outTarget,
              deviceId: data.record?.doActions[0].deviceId,
              command: data.record?.doActions[0].command,
              sceneLinkageId: data.record?.doActions[0].sceneLinkageId,
            });
          } catch (e) {
            return e;
          }
        } else {
          await resetFields();
        }
      });
      const getTitle = computed(() => (!unref(isUpdate) ? '新增场景联动' : '编辑场景联动'));
      const getAddPushValueDataValueFunc = (v) => {
        if (v.count == 2) {
          isCountT.value = v.count;
          isAddTriggerT.value = v.data;
        }
      };
      const getAddDoActionPushValueDataValueFunc = (v) => {
        if (v.count == 2) {
          isCountD.value = v.count;
          isAddActionD.value = v.data;
        }
      };
      const getAddDoconditionPushValueDataValueFunc = (v) => {
        if (v.count == 2) {
          isCountC.value = v.count;
          isAddConditionD.value = v.data;
        }
      };

      async function handleSubmit() {
        if (!unref(isUpdate)) {
          triggersArray.length = 0;
          doConditionsArray.length = 0;
          doActionsArray.length = 0;
          let res = validateFields();
          if (!res) return;
          let getTriggerChildValues = proxy.$refs.getTriggerChildData.getAllFields();
          if (getTriggerChildValues.count === 1) {
            triggersArray.push(getTriggerChildValues.data);
          }
          let getconditionChildValues = proxy.$refs.getConditionChildData.getAllFields();
          if (getconditionChildValues.count === 1) {
            doConditionsArray.push(getconditionChildValues.data);
          }
          let getChildValues = proxy.$refs.getChildData.getAllFields();
          if (getChildValues.count === 1) {
            doActionsArray.push(getChildValues.data);
          }

          getValuesFormData = getFieldsValue();
          Object.assign(getAllFormData, getValuesFormData);
          getAllFormData.triggers =
            isCountT.value == 2 ? isAddTriggerT.value.concat(triggersArray) : triggersArray;
          getAllFormData.doConditions =
            isCountC.value == 2
              ? isAddConditionD.value.concat(doConditionsArray)
              : doConditionsArray;
          getAllFormData.doActions =
            isCountD.value == 2 ? isAddActionD.value.concat(doActionsArray) : doActionsArray;
          //所有的表单值
          await screenLinkPageAddApi(getAllFormData);
          createMessage.success('场景联动新增成功');
          closeDrawer();
          emit('success');
        }
        if (unref(isUpdate)) {
          triggersArray = [];
          doConditionsArray = [];
          doActionsArray = [];
          let getTriggerChildValues = proxy.$refs.getTriggerChildData.getAllFields();
          if (getTriggerChildValues.count === 1) {
            triggersArray.push(getTriggerChildValues.data);
          }
          let getconditionChildValues = proxy.$refs.getConditionChildData.getAllFields();
          if (getconditionChildValues.count === 1) {
            doConditionsArray.push(getconditionChildValues.data);
          }
          let getChildValues = proxy.$refs.getChildData.getAllFields();
          if (getChildValues.count === 1) {
            doActionsArray.push(getChildValues.data);
          }
          getValuesFormData = getFieldsValue();

          getValuesFormData.id = getId.value;
          getValuesFormData.tenantId = getTenantId.value;
          Object.assign(getAllFormData, getValuesFormData);
          getAllFormData.triggers =
            isCountT.value == 2 ? isAddTriggerT.value.concat(triggersArray) : triggersArray;
          getAllFormData.doConditions =
            isCountC.value == 2
              ? isAddConditionD.value.concat(doConditionsArray)
              : doConditionsArray;
          getAllFormData.doActions =
            isCountD.value == 2 ? isAddActionD.value.concat(doActionsArray) : doActionsArray;
          await screenLinkPageUpdateApi(getAllFormData);
          createMessage.success('场景联动编辑成功');
          closeDrawer();
          emit('success');
        }
      }
      return {
        getAddDoconditionPushValueDataValueFunc,
        getAddDoActionPushValueDataValueFunc,
        getAddPushValueDataValueFunc,
        getConditionChildData,
        getTriggerChildData,
        getChildData,
        getDeviceInfo,
        getDeviceInfo1,
        getDeviceInfo2,
        getAllFormData,
        registerDrawer,
        registerForm,
        getTitle,
        handleSubmit,
      };
    },
  });
</script>