basicConfiguration.ts 3.07 KB
import { byOganizationIdGetMasterDevice, getAttribute } from '/@/api/ruleengine/ruleengineApi';
import { getOrganizationList } from '/@/api/system/system';
import { FormSchema } from '/@/components/Form';
import { copyTransFun } from '/@/utils/fnUtils';
import { to } from '/@/utils/to';

export enum DataSourceField {
  DEVICE_ID = 'deviceId',
}

export const basicSchema: FormSchema[] = [
  {
    field: 'name',
    label: '组件名称',
    component: 'Input',
    componentProps: {
      placeholder: '请输入组件名称',
    },
  },
  {
    field: 'remark',
    label: '组件备注',
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入组件备注',
    },
  },
];

export const dataSourceSchema: FormSchema[] = [
  {
    field: 'organizationId',
    component: 'ApiTreeSelect',
    label: '组织',
    colProps: { span: 6 },
    componentProps({ formActionType }) {
      const { setFieldsValue } = formActionType;
      return {
        placeholder: '请选择组织',
        api: async () => {
          const data = await getOrganizationList();
          copyTransFun(data as any as any[]);
          return data;
        },
        onChange() {
          setFieldsValue({ device: null, attr: null });
        },
        getPopupContainer: () => document.body,
      };
    },
  },
  {
    field: 'deviceId',
    component: 'ApiSelect',
    label: '设备',
    colProps: { span: 5 },
    componentProps({ formModel, formActionType }) {
      const { setFieldsValue } = formActionType;
      const orgId = formModel['organizationId'];
      return {
        api: async () => {
          if (orgId) {
            const [, data] = await to<Record<'id' | 'name', string>[]>(
              byOganizationIdGetMasterDevice(orgId)
            );
            if (data) return data.map((item) => ({ label: item.name, value: item.id }));
          }
          return [];
        },
        onChange() {
          setFieldsValue({ attr: null });
        },
        placeholder: '请选择设备',
        getPopupContainer: () => document.body,
      };
    },
  },
  {
    field: 'attribute',
    component: 'ApiSelect',
    label: '属性',
    colProps: { span: 5 },
    componentProps({ formModel }) {
      const orgId = formModel['organizationId'];
      const deviceId = formModel['deviceId'];
      return {
        api: async () => {
          if (orgId && deviceId) {
            const [, data] = await to<string[]>(getAttribute(orgId, deviceId));
            // TODO attribute exist null
            if (data) return data.filter(Boolean).map((item) => ({ label: item, value: item }));
          }
          return [];
        },
        placeholder: '请选择属性',
        getPopupContainer: () => document.body,
      };
    },
  },
  {
    field: 'deviceRename',
    component: 'Input',
    label: '设备',
    colProps: { span: 4 },
    componentProps: {
      placeholder: '设备重命名',
    },
  },
  {
    field: 'attributeRename',
    component: 'Input',
    label: '属性',
    colProps: { span: 4 },
    componentProps: {
      placeholder: '属性重命名',
    },
  },
];