config.ts 3.34 KB
import { TaskTargetEnum } from '../../config';
import { getMeetTheConditionsDevice } from '/@/api/dataBoard';
import { getDevicesByDeviceIds } from '/@/api/device/deviceManager';
import { TaskRecordType } from '/@/api/task/model';
import { FormSchema } from '/@/components/Form';
import { useI18n } from '/@/hooks/web/useI18n';
import { createPickerSearch } from '/@/utils/pickerSearch';

export interface FormValue extends Record<FormFieldsEnum, any> {
  [FormFieldsEnum.TASK_RECORD]: string;
}

export enum FormFieldsEnum {
  EXECUTE_TARGET_TYPE = 'executeTarget',
  TASK_TYPE = 'taskType',
  TARGET_IDS = 'targetIds',
  TASK_RECORD = 'taskRecord',
  TB_DEVICE_ID = 'tbDeviceId',
}

export enum TargetType {
  ALL = 'ALL',
  ASSIGN = 'ASSIGN',
}

const { t } = useI18n();
export const formSchemas: FormSchema[] = [
  {
    field: 'taskName',
    component: 'Input',
    label: t('taskCenter.task.form.name'),
    slot: 'taskName',
  },
  {
    field: FormFieldsEnum.TASK_TYPE,
    component: 'Input',
    label: t('taskCenter.task.detail.taskType'),
    slot: 'taskType',
  },
  {
    field: FormFieldsEnum.TASK_RECORD,
    component: 'Input',
    label: t('taskCenter.task.detail.taskDetail'),
    show: false,
  },
  {
    field: FormFieldsEnum.EXECUTE_TARGET_TYPE,
    component: 'RadioGroup',
    label: t('taskCenter.task.runTask.selectTargetType'),
    helpMessage: [t('taskCenter.task.runTask.typeMessage')],
    defaultValue: TargetType.ALL,
    componentProps: {
      options: [
        { label: t('taskCenter.task.runTask.allDevice'), value: TargetType.ALL },
        { label: t('taskCenter.task.runTask.targetDevice'), value: TargetType.ASSIGN },
      ],
    },
  },
  {
    field: FormFieldsEnum.TB_DEVICE_ID,
    component: 'Input',
    label: '',
    show: false,
  },
  {
    field: FormFieldsEnum.TARGET_IDS,
    component: 'ApiSelect',
    label: t('taskCenter.task.runTask.targetDevice'),
    ifShow: ({ model }) => model[FormFieldsEnum.EXECUTE_TARGET_TYPE] === TargetType.ASSIGN,
    rules: [{ required: true, type: 'array' }],
    componentProps: ({ formModel }) => {
      const record = JSON.parse(formModel[FormFieldsEnum.TASK_RECORD]) as TaskRecordType;
      const tbDeviceId = formModel[FormFieldsEnum.TB_DEVICE_ID];
      const isDevices = record.targetType === TaskTargetEnum.DEVICES;
      const { executeTarget } = record;
      const { organizationId, data } = executeTarget;
      return {
        api: async () => {
          try {
            if (isDevices) {
              const result = await getDevicesByDeviceIds(data!);
              return result.data.map((item) => ({
                label: item.alias || item.name,
                value: item.tbDeviceId,
                disabled: tbDeviceId ? item.tbDeviceId !== tbDeviceId : false,
              }));
            } else {
              const result = await getMeetTheConditionsDevice({
                organizationId,
                deviceProfileId: data![0],
              });
              return result.map((item) => ({
                label: item.alias || item.name,
                value: item.tbDeviceId,
              }));
            }
          } catch (error) {
            return [];
          }
        },
        mode: 'multiple',
        ...createPickerSearch(),
        placeholder: t('common.chooseText') + t('business.deviceText'),
        getPopupContainer: () => document.body,
      };
    },
  },
];