DeviceProfileModal.vue 12.6 KB
<template>
  <BasicModal
    v-bind="$attrs"
    width="55rem"
    @register="register"
    :title="getTitle"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <div class="step-form-form">
      <a-steps :current="current" @change="handleChange">
        <a-step title="设备配置" />
        <a-step title="传输配置" />
        <a-step title="告警配置" />
        <a-step title="告警通知" />
      </a-steps>
    </div>
    <div class="mt-5">
      <div v-show="current === 0">
        <DeviceProfileStep1 ref="DeviceProfileStep1Ref" @next="handleStepNext1"
      /></div>

      <div v-show="current === 1">
        <DeviceProfileStep2
          ref="DeviceProfileStep2Ref"
          @prev="handleStepPrev"
          @next="handleStep2Next"
      /></div>

      <div v-show="current === 2">
        <DeviceProfileStep3
          ref="DeviceProfileStep3Ref"
          @prev="handleStepPrev"
          @next="handleStep3Next"
          @redo="handleRedo"
      /></div>
      <div v-show="current === 3">
        <DeviceProfileStep4 ref="DeviceProfileStep4Ref" @prev="handleStepPrev" />
      </div>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref, getCurrentInstance, reactive } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import DeviceProfileStep1 from '/@/views/device/profile/step/DeviceProfileStep1.vue';
  import DeviceProfileStep2 from '/@/views/device/profile/step/DeviceProfileStep2.vue';
  import DeviceProfileStep3 from '/@/views/device/profile/step/DeviceProfileStep3.vue';
  import DeviceProfileStep4 from '/@/views/device/profile/step/DeviceProfileStep4.vue';
  import { Steps } from 'ant-design-vue';
  import { deviceConfigAddOrEdit, deviceConfigGetDetail } from '/@/api/device/deviceConfigApi';
  import { useMessage } from '/@/hooks/web/useMessage';

  export default defineComponent({
    name: 'DeviceModal',
    components: {
      BasicModal,
      DeviceProfileStep1,
      DeviceProfileStep2,
      DeviceProfileStep3,
      DeviceProfileStep4,
      [Steps.name]: Steps,
      [Steps.Step.name]: Steps.Step,
    },
    props: {
      userData: { type: Object },
    },
    emits: [
      'success',
      'register',
      'handleStepPrev',
      'handleStep3Next',
      'handleRedo',
      'handleStep2Next',
    ],
    setup(_, { emit }) {
      const DeviceProfileStep1Ref = ref(null);
      const DeviceProfileStep2Ref = ref(null);
      const DeviceProfileStep3Ref = ref(null);
      const DeviceProfileStep4Ref = ref(null);
      const { createMessage } = useMessage();
      // const getStepData = ref(null);
      const { proxy } = getCurrentInstance();
      const postDeviceConfogData: any = ref({});
      const getStepOneData: any = ref({});
      const getStepTwoData: any = ref({});
      const getStepThreeData = ref([]);
      const editEchoData: any = ref(null);
      const alarmProfileData: any = ref({});
      const isGetStepThreeData = reactive({
        profileData: {
          alarms: [],
        },
      });
      const postEditId = ref('');
      const current = ref(0);
      const isUpdate = ref(true);
      const getTitle = computed(() => (!unref(isUpdate) ? '新增设备配置' : '编辑设备配置'));
      const handleChange = (v) => {
        console.log(v);
      };
      const [register, { closeModal }] = useModalInner(async (data) => {
        isUpdate.value = !!data?.isUpdate;
        if (!unref(isUpdate)) {
          current.value = 0;
          proxy.$refs.DeviceProfileStep3Ref.clearProfileDataFunc();
          proxy.$refs.DeviceProfileStep3Ref.addAlarmRule();
          // proxy.$refs.DeviceProfileStep4Ref.customResetAndFunc();

          switch (current.value) {
            case 0:
              proxy.$refs.DeviceProfileStep1Ref.customResetFunc();
              proxy.$refs.DeviceProfileStep1Ref.resetIconFunc();
              break;
            case 1:
              proxy.$refs.DeviceProfileStep2Ref.customResetAndFunc();
              break;
            case 2:
              proxy.$refs.DeviceProfileStep3Ref.resetRegisterFormFunc();
              proxy.$refs.DeviceProfileStep3Ref.resetRegisterFormHighSettingmFunc();
              proxy.$refs.DeviceProfileStep3Ref.resetRegisterFormCreateAlarmFunc();
              break;
            case 3:
              proxy.$refs.DeviceProfileStep4Ref.customResetAndFunc();
              break;
          }
        }
        if (unref(isUpdate)) {
          current.value = 0;
          proxy.$refs.DeviceProfileStep3Ref.clearProfileDataFunc();
          proxy.$refs.DeviceProfileStep3Ref.addAlarmRule();
          postEditId.value = data.record.id;
          const getBackendData = await deviceConfigGetDetail(postEditId.value);
          editEchoData.value = { ...getBackendData };
          switch (current.value) {
            case 0:
              proxy.$refs.DeviceProfileStep1Ref.resetFieldsFunc(editEchoData.value);
              proxy.$refs.DeviceProfileStep1Ref.editIconFunc(editEchoData.value.icon);
              break;
          }
        }
      });
      function handleStepPrev() {
        current.value--;
      }
      function handleStepNext1(v, v1) {
        current.value++;
        getStepOneData.value = v;
        getStepOneData.value.icon = v1;
        if (unref(isUpdate)) {
          proxy.$refs.DeviceProfileStep2Ref.resetFieldsFunc({
            transportType: editEchoData.value.profileData.transportConfiguration.type,
          });
        } else {
          // proxy.$refs.DeviceProfileStep2Ref.customResetAndFunc();
        }
      }
      function handleStep2Next(v) {
        current.value++;
        getStepTwoData.value = v;
        if (unref(isUpdate)) {
          proxy.$refs.DeviceProfileStep3Ref.retryRegisterFormFunc({
            alarmType: editEchoData.value.profileData.alarms[0].alarmType,
          });
          proxy.$refs.DeviceProfileStep3Ref.retryRegisterFormHighSettingmFunc({
            propagate: editEchoData.value.profileData.alarms[0].propagate,
            propagateRelationTypes:
              editEchoData.value.profileData?.alarms[0].propagateRelationTypes[0],
          });
          const getKey = Object.keys(editEchoData.value.profileData?.alarms[0].createRules);
          proxy.$refs.DeviceProfileStep3Ref.retryRegisterFormCreateAlarmFunc({
            default: getKey[0],
          });
          const findDay = [
            { label: '等于', value: 'EQUAL' },
            { label: '不等于', value: 'NOT_EQUAL' },
            { label: '开始于', value: 'STARTS_WITH' },
            { label: '结束于', value: 'ENDS_WITH' },
            { label: '包含', value: 'CONTAINS' },
            { label: '不包含', value: 'NOT_CONTAINS' },
            { label: '等于', value: 'EQUAL' },
            { label: '不等于', value: 'NOT_EQUAL' },
            { label: '大于', value: 'GREATER' },
            { label: '小于', value: 'LESS' },
            { label: '大于或等于', value: 'GREATER_OR_EQUAL' },
            { label: '小于或等于', value: 'LESS_OR_EQUAL' },
          ];
          const findRuleByValue = findDay.find((f) => {
            if (
              f.value ==
              editEchoData.value.profileData?.alarms[0].createRules[getKey[0]].condition
                .condition[0].predicate.operation
            ) {
              return f.label;
            }
          });
          const findClearRuleByValue = findDay.find((f) => {
            if (
              f.value ==
              editEchoData.value.profileData?.alarms[0].clearRule.condition.condition[0].predicate
                .operation
            ) {
              return f.label;
            }
          });
          proxy.$refs.DeviceProfileStep3Ref.retryRulesFormDataFunc(
            `
            键名:${
              editEchoData.value.profileData?.alarms[0].createRules[getKey[0]].condition
                .condition[0].key.key
            }...操作:${findRuleByValue?.label}...值:${
              editEchoData.value.profileData?.alarms[0].createRules[getKey[0]].condition
                .condition[0].predicate.value.defaultValue
            }
            `
          );
          proxy.$refs.DeviceProfileStep3Ref.retryEnableFormDataFunc(`始终启用`);
          proxy.$refs.DeviceProfileStep3Ref.setRegisterFormChangeDetailFunc({
            alarmDetails:
              editEchoData.value.profileData?.alarms[0].createRules[getKey[0]].alarmDetails,
          });

          //清除报警
          proxy.$refs.DeviceProfileStep3Ref.retryRulesClearFormDataFunc(
            `
            键名:${editEchoData.value.profileData?.alarms[0].clearRule.condition.condition[0].key.key}...操作:${findClearRuleByValue?.label}...值:${editEchoData.value.profileData?.alarms[0].clearRule.condition.condition[0].predicate.value.defaultValue}
            `
          );
          proxy.$refs.DeviceProfileStep3Ref.retryEnableClearFormDataFunc(`始终启用`);
          proxy.$refs.DeviceProfileStep3Ref.setRegisterFormClearChangeDetailFunc({
            alarmDetails: editEchoData.value.profileData?.alarms[0].clearRule.alarmDetails,
          });
        } else {
          try {
            proxy.$refs.DeviceProfileStep3Ref.resetRegisterFormFunc();
            proxy.$refs.DeviceProfileStep3Ref.resetRegisterFormHighSettingmFunc();
            proxy.$refs.DeviceProfileStep3Ref.resetRegisterFormCreateAlarmFunc();
            proxy.$refs.DeviceProfileStep3Ref.resetRulesFormDataFunc();
            proxy.$refs.DeviceProfileStep3Ref.resetEnableFormDataFunc();
            proxy.$refs.DeviceProfileStep3Ref.resetTemplateFormDataFunc();
            proxy.$refs.DeviceProfileStep3Ref.resetRulesClearFormDataFunc();
            proxy.$refs.DeviceProfileStep3Ref.resetEnableClearFormDataFunc();
            proxy.$refs.DeviceProfileStep3Ref.resetTemplateClearFormDataFunc();
            proxy.$refs.DeviceProfileStep3Ref.resetRegisterFormChangeDetailFunc();
            proxy.$refs.DeviceProfileStep3Ref.resetRegisterFormClearChangeDetailFunc();
          } catch (e) {}
        }
      }
      function handleStep3Next(v) {
        current.value++;
        getStepThreeData.value = v;
        try {
          if (unref(isUpdate)) {
            try {
              proxy.$refs.DeviceProfileStep4Ref.resetFieldsFunc({
                id: editEchoData.value.alarmProfile.id,
                messageMode: editEchoData.value.alarmProfile.messageMode,
              });
            } catch (e) {
              return e;
            }
          } else {
            try {
              proxy.$refs.DeviceProfileStep4Ref.resetFieldsFunc({
                id: '',
                messageMode: '',
              });
            } catch (e) {
              return e;
            }
          }
        } catch (e) {
          return e;
        }
      }
      function handleRedo() {
        current.value = 0;
      }
      const handleSubmit = async () => {
        if (!unref(isUpdate)) {
          isGetStepThreeData.profileData.alarms = getStepThreeData.value;
          alarmProfileData.value.alarmProfile =
            await proxy.$refs.DeviceProfileStep4Ref.getAllFields();
          alarmProfileData.value.alarmProfile.alarmContactId.shift();
          alarmProfileData.value.alarmProfile.messageMode.shift();
          Object.assign(
            postDeviceConfogData.value,
            getStepOneData.value,
            getStepTwoData.value,
            isGetStepThreeData,
            alarmProfileData.value
          );
          await deviceConfigAddOrEdit(postDeviceConfogData.value);
          createMessage.success('新增设备配置成功');
          closeModal();
          emit('success');
        }
        if (unref(isUpdate)) {
          postDeviceConfogData.value.id = postEditId.value;
          isGetStepThreeData.profileData.alarms = getStepThreeData.value;

          alarmProfileData.value.alarmProfile =
            await proxy.$refs.DeviceProfileStep4Ref.getAllFields();
          Object.assign(
            postDeviceConfogData.value,
            getStepOneData.value,
            getStepTwoData.value,
            alarmProfileData.value,
            isGetStepThreeData
          );
          await deviceConfigAddOrEdit(postDeviceConfogData.value);
          createMessage.success('编辑设备配置成功');
          closeModal();
          emit('success');
        }
      };
      const handleCancel = () => {
        closeModal();
      };
      return {
        handleChange,
        DeviceProfileStep2Ref,
        DeviceProfileStep3Ref,
        DeviceProfileStep4Ref,
        DeviceProfileStep1Ref,
        editEchoData,
        // getStepData,
        handleStep3Next,
        handleSubmit,
        handleCancel,
        register,
        getTitle,
        current,
        handleStepPrev,
        handleStepNext1,
        handleStep2Next,
        handleRedo,
      };
    },
  });
</script>