config.ts 11.8 KB
import { unref } from 'vue';
import { useSceneLinkageDrawerContext } from '../SceneLinkageDrawer/sceneLinkageDrawerContext';
import { getDeviceProfile } from '/@/api/alarm/position';
import { byOrganizationIdGetMasterDevice } from '/@/api/ruleengine/ruleengineApi';
import { findDictItemByCode } from '/@/api/system/dict';
import { FormSchema, useComponentRegister } from '/@/components/Form';
import { DictEnum } from '/@/enums/dictEnum';
import { createPickerSearch } from '/@/utils/pickerSearch';
import { DeviceTypeEnum } from '../../../dataFlow/cpns/config';
import { getDeviceAttributes } from '/@/api/dataBoard';
import { DeviceAttributeParams } from '/@/api/dataBoard/model';
import {
  TriggerValueTypeEnum,
  TriggerValueTypeNameEnum,
  TriggerEntityTypeEnum,
  TriggerEntityTypeNameEnum,
  TriggerTypeEnum,
  TriggerTypeNameEnum,
  DeviceTriggerTypeEum,
  DeviceTriggerTypeNameEum,
  FlipFlopTypeEnum,
  FlipFlopTypeNameEnum,
  TriggerUnitEnum,
  TriggerUnitNameEnum,
} from '/@/enums/linkedgeEnum';
import { DeviceModelOfMatterAttrs } from '/@/api/device/model/deviceModel';
import TriggerDurationInput from './TriggerDurationInput.vue';
import { DataTypeEnum } from '/@/enums/objectModelEnum';

export enum FormFieldEnum {
  FLIP_FLOP_TYPE = 'flipFlopType',
  DEVICE_TYPE = 'deviceType',
  DEVICE_PROFILE_ID = 'deviceProfileId',
  ENTITY_TYPE = 'entityType',
  ENTITY_ID = 'entityId',
  TRIGGER_TYPE = 'triggerType',
  CONDITION_TYPE = 'conditionType',
  CONDITION_KEY = 'conditionKey',
  CONDITION_VALUE_TYPE = 'conditionValueType',
  TRIGGER_DURATION_UNIT = 'triggerDurationUnit',
  TRIGGER_DURATION_VALUE = 'triggerDurationValue',
  TRIGGER_REPEAT_COUNT = 'triggerRepeatCount',

  CONDITION_KEY_DETAIL = 'conditionKeyDetail',
}

export enum FormFieldNameEnum {
  FLIP_FLOP_TYPE = '触发类型',
  DEVICE_TYPE = '设备类型',
  DEVICE_PROFILE_ID = '产品',
  ENTITY_TYPE = '触发设备类型',
  ENTITY_ID = '设备',
  TRIGGER_TYPE = '触发方式',
  CONDITION_TYPE = '触发条件',
  CONDITION_KEY = '属性',
  CONDITION_VALUE_TYPE = '比较类型',
  TRIGGER_DURATION_VALUE = '持续时长',
  TRIGGER_REPEAT_COUNT = '重复次数',
}

useComponentRegister('TriggerDurationInput', TriggerDurationInput);

function getTriggerValueTypeByThingsModelType(type: DataTypeEnum) {
  const map = {
    [DataTypeEnum.BOOL]: TriggerValueTypeEnum.BOOLEAN,
    [DataTypeEnum.NUMBER_DOUBLE]: TriggerValueTypeEnum.NUMERIC,
    [DataTypeEnum.NUMBER_INT]: TriggerValueTypeEnum.NUMERIC,
    [DataTypeEnum.STRING]: TriggerValueTypeEnum.STRING,
    [DataTypeEnum.STRUCT]: TriggerValueTypeEnum.STRING,
  };

  return map[type];
}

export const getFormSchemas = (): FormSchema[] => {
  const { organizationId } = useSceneLinkageDrawerContext();

  return [
    {
      field: FormFieldEnum.FLIP_FLOP_TYPE,
      label: '',
      component: 'Select',
      rules: [{ required: true, message: `请选择${FormFieldNameEnum.FLIP_FLOP_TYPE}` }],
      defaultValue: FlipFlopTypeEnum.SIMPLE,
      componentProps: () => {
        return {
          options: Object.keys(FlipFlopTypeEnum).map((value) => ({
            label: FlipFlopTypeNameEnum[value],
            value,
          })),
          placeholder: `请选择${FormFieldNameEnum.FLIP_FLOP_TYPE}`,
        };
      },
    },
    {
      field: FormFieldEnum.DEVICE_TYPE,
      label: '',
      component: 'ApiSelect',
      defaultValue: DeviceTypeEnum.SENSOR,
      rules: [{ required: true, message: `请选择${FormFieldNameEnum.DEVICE_TYPE}` }],
      componentProps: ({ formActionType }) => {
        const { setFieldsValue } = formActionType;
        return {
          api: findDictItemByCode,
          params: {
            dictCode: DictEnum.DEVICE_TYPE,
          },
          labelField: 'itemText',
          valueField: 'itemValue',
          placeholder: `请选择${FormFieldNameEnum.DEVICE_TYPE}`,
          onChange() {
            setFieldsValue({
              [FormFieldEnum.DEVICE_PROFILE_ID]: null,
              [FormFieldEnum.ENTITY_ID]: [],
              [FormFieldEnum.CONDITION_KEY]: null,
            });
          },
        };
      },
    },
    {
      field: FormFieldEnum.DEVICE_PROFILE_ID,
      label: '',
      component: 'ApiSelect',
      rules: [{ required: true, message: `请选择${FormFieldNameEnum.DEVICE_PROFILE_ID}` }],
      componentProps: ({ formModel, formActionType }) => {
        const deviceType = formModel[FormFieldEnum.DEVICE_TYPE];
        const { setFieldsValue } = formActionType;
        return {
          api: async () => {
            return await getDeviceProfile(deviceType);
          },
          labelField: 'name',
          valueField: 'id',
          placeholder: `请选择${FormFieldNameEnum.DEVICE_PROFILE_ID}`,
          ...createPickerSearch(),
          onChange() {
            setFieldsValue({
              [FormFieldEnum.ENTITY_ID]: [],
              [FormFieldEnum.CONDITION_KEY]: null,
            });
          },
        };
      },
    },
    {
      field: FormFieldEnum.ENTITY_TYPE,
      label: '',
      component: 'Select',
      rules: [{ required: true, message: `请选择${FormFieldNameEnum.ENTITY_TYPE}` }],
      componentProps: () => {
        return {
          options: Object.keys(TriggerEntityTypeEnum).map((value) => ({
            label: TriggerEntityTypeNameEnum[value],
            value,
          })),
          placeholder: `请选择${FormFieldNameEnum.ENTITY_TYPE}`,
        };
      },
    },
    {
      field: FormFieldEnum.TRIGGER_DURATION_UNIT,
      label: '',
      component: 'Input',
      ifShow: false,
      defaultValue: TriggerUnitEnum.SECONDS,
    },
    {
      field: FormFieldEnum.TRIGGER_DURATION_VALUE,
      label: '',
      component: 'TriggerDurationInput',
      rules: [
        {
          required: true,
          message: `请输入${FormFieldNameEnum.TRIGGER_DURATION_VALUE}`,
          type: 'number',
        },
      ],
      changeEvent: 'update:value',
      ifShow: ({ model }) => model[FormFieldEnum.FLIP_FLOP_TYPE] === FlipFlopTypeEnum.DURATION,
      componentProps: ({ formActionType, formModel }) => {
        const { setFieldsValue } = formActionType;
        return {
          placeholder: `请输入${FormFieldNameEnum.TRIGGER_DURATION_VALUE}`,
          unit: formModel[FormFieldEnum.TRIGGER_DURATION_UNIT],
          unitOptions: Object.values(TriggerUnitEnum).map((value) => ({
            label: TriggerUnitNameEnum[value],
            value,
          })),
          onUnitChange(value: TriggerUnitEnum) {
            setFieldsValue({ [FormFieldEnum.TRIGGER_DURATION_UNIT]: value });
          },
        };
      },
    },
    {
      field: FormFieldEnum.TRIGGER_REPEAT_COUNT,
      label: '',
      component: 'InputNumber',
      rules: [{ required: true, message: `请输入${FormFieldNameEnum.TRIGGER_REPEAT_COUNT}` }],
      ifShow: ({ model }) => model[FormFieldEnum.FLIP_FLOP_TYPE] === FlipFlopTypeEnum.REPEATING,
      componentProps: {
        placeholder: `请输入${FormFieldNameEnum.TRIGGER_REPEAT_COUNT}`,
        min: 0,
      },
    },
    {
      field: FormFieldEnum.ENTITY_ID,
      label: '',
      component: 'ApiSelect',
      rules: [{ required: true, type: 'array', message: `请选择${FormFieldNameEnum.ENTITY_ID}` }],
      ifShow: ({ model }) => model[FormFieldEnum.ENTITY_TYPE] === TriggerEntityTypeEnum.PART,
      componentProps: ({ formModel }) => {
        return {
          api: async (params: Record<'organizationId' | 'deviceProfileId', string>) => {
            if (params.deviceProfileId && params.organizationId) {
              const result = await byOrganizationIdGetMasterDevice(params);
              if (result) {
                return result.map((item) => ({
                  ...item,
                  label: item.alias || item.name,
                  value: item.tbDeviceId,
                }));
              }
            }
            return [];
          },
          mode: 'multiple',
          maxTagCount: 3,
          params: {
            organizationId: unref(organizationId),
            deviceProfileId: formModel[FormFieldEnum.DEVICE_PROFILE_ID],
          },
          placeholder: `请选择${FormFieldNameEnum.ENTITY_ID}`,
          ...createPickerSearch(),
        };
      },
    },
    {
      field: FormFieldEnum.TRIGGER_TYPE,
      label: '',
      component: 'Select',
      rules: [{ required: true, message: `请选择${FormFieldNameEnum.TRIGGER_TYPE}` }],
      defaultValue: TriggerTypeEnum.DEVICE_TRIGGER,
      componentProps: () => {
        return {
          options: [
            { label: TriggerTypeNameEnum.DEVICE_TRIGGER, value: TriggerTypeEnum.DEVICE_TRIGGER },
          ],
        };
      },
    },
    {
      field: FormFieldEnum.CONDITION_TYPE,
      label: '',
      component: 'Select',
      rules: [{ required: true, message: `请选择${FormFieldNameEnum.CONDITION_TYPE}` }],
      defaultValue: DeviceTriggerTypeEum.TIME_SERIES,
      componentProps: () => {
        return {
          options: [
            {
              label: DeviceTriggerTypeNameEum.TIME_SERIES,
              value: DeviceTriggerTypeEum.TIME_SERIES,
            },
          ],
        };
      },
    },
    {
      field: FormFieldEnum.CONDITION_KEY_DETAIL,
      label: '',
      component: 'Input',
      ifShow: false,
    },
    {
      field: FormFieldEnum.CONDITION_KEY,
      label: '',
      component: 'ApiSelect',
      rules: [{ required: true, message: `请选择${FormFieldNameEnum.CONDITION_KEY}` }],
      componentProps: ({ formModel, formActionType }) => {
        const { setFieldsValue } = formActionType;
        return {
          api: async (params: DeviceAttributeParams) => {
            if (params.deviceProfileId) {
              return await getDeviceAttributes(params);
            }
            return [];
          },
          labelField: 'name',
          valueField: 'identifier',
          params: {
            deviceProfileId: formModel[FormFieldEnum.DEVICE_PROFILE_ID],
          },
          placeholder: `请选择${FormFieldNameEnum.CONDITION_KEY}`,
          onChange(
            value: string,
            option: DeviceModelOfMatterAttrs & Record<'label' | 'value', string>
          ) {
            setFieldsValue({
              [FormFieldEnum.CONDITION_KEY_DETAIL]: value
                ? { ...option, identifier: option?.value, name: option?.label }
                : {},
              [FormFieldEnum.CONDITION_VALUE_TYPE]:
                value && option?.detail?.dataType?.type
                  ? getTriggerValueTypeByThingsModelType(option.detail.dataType?.type)
                  : null,
            });
          },
          onOptionsChange(
            options: (DeviceModelOfMatterAttrs & Record<'label' | 'value', string>)[]
          ) {
            const conditionKey = formModel[FormFieldEnum.CONDITION_KEY];
            const res = options.find((item) => item.value === conditionKey);
            res &&
              setFieldsValue({
                [FormFieldEnum.CONDITION_KEY_DETAIL]: {
                  ...res,
                  identifier: res?.value,
                  name: res?.label,
                },
              });
          },
        };
      },
    },
    {
      field: FormFieldEnum.CONDITION_VALUE_TYPE,
      label: '',
      component: 'Select',
      rules: [{ required: true, message: `请选择${FormFieldNameEnum.CONDITION_VALUE_TYPE}` }],
      componentProps: () => {
        return {
          options: Object.keys(TriggerValueTypeEnum).map((value) => ({
            label: TriggerValueTypeNameEnum[value],
            value,
          })),
          placeholder: `请选择${FormFieldNameEnum.CONDITION_VALUE_TYPE}`,
        };
      },
    },
    {
      field: 'conditionFilter',
      label: '',
      component: 'Input',
      colProps: { span: 24, xxl: 24, xl: 24, lg: 24, md: 24, sm: 24, xs: 24 },
      ifShow: ({ model }) =>
        model[FormFieldEnum.CONDITION_KEY] && model[FormFieldEnum.CONDITION_VALUE_TYPE],
      colSlot: 'conditionFilter',
    },
  ];
};