basicConfiguration.ts 9.26 KB
import { getAllDeviceByOrg, getDeviceAttributes, getGatewaySlaveDevice } from '/@/api/dataBoard';
import { getOrganizationList } from '/@/api/system/system';
import { FormSchema } from '/@/components/Form';
import { copyTransFun } from '/@/utils/fnUtils';
import { DeviceAttributeParams, MasterDeviceList } from '/@/api/dataBoard/model';
import { FrontComponent } from '../../const/const';

export enum BasicConfigField {
  NAME = 'name',
  REMARK = 'remark',
}

const getDeviceAttribute = async (params: DeviceAttributeParams) => {
  try {
    const data = await getDeviceAttributes(params);
    if (data) return data.map((item) => ({ label: item.name, value: item.identifier }));
  } catch (error) {}
  return [];
};

export enum DataSourceField {
  IS_GATEWAY_DEVICE = 'gatewayDevice',
  ORIGINATION_ID = 'organizationId',
  DEVICE_ID = 'deviceId',
  SLAVE_DEVICE_ID = 'slaveDeviceId',
  DEVICE_PROFILE_ID = 'deviceProfileId',
  SLAVE_DEVICE_PROFILE_ID = 'slaveDeviceProfileId',
  ATTRIBUTE = 'attribute',
  ATTRIBUTE_RENAME = 'attributeRename',
  DEVICE_NAME = 'deviceName',
  DEVICE_RENAME = 'deviceRename',
  LONGITUDE_ATTRIBUTE = 'longitudeAttribute',
  LATITUDE_ATTRIBUTE = 'latitudeAttribute',
}

const isControlComponent = (frontId: FrontComponent) => {
  const list = [
    FrontComponent.CONTROL_COMPONENT_SLIDING_SWITCH,
    FrontComponent.CONTROL_COMPONENT_SWITCH_WITH_ICON,
    FrontComponent.CONTROL_COMPONENT_TOGGLE_SWITCH,
  ];
  if (list.includes(frontId)) {
    return true;
  }
  return false;
};

export const basicSchema: FormSchema[] = [
  {
    field: BasicConfigField.NAME,
    label: '组件名称',
    component: 'Input',
    // rules: [{ required: true, message: '组件名称为必填项' }],
    componentProps: {
      placeholder: '请输入组件名称',
    },
  },
  {
    field: BasicConfigField.REMARK,
    label: '组件备注',
    component: 'InputTextArea',
    // rules: [{ required: true, message: '组件备注为必填项' }],
    componentProps: {
      placeholder: '请输入组件备注',
    },
  },
];

export const dataSourceSchema = (frontId?: FrontComponent): FormSchema[] => {
  return [
    {
      field: DataSourceField.IS_GATEWAY_DEVICE,
      component: 'Switch',
      label: '是否是网关设备',
      show: false,
    },
    {
      field: DataSourceField.DEVICE_NAME,
      component: 'Input',
      label: '设备名',
      show: false,
    },
    {
      field: DataSourceField.ORIGINATION_ID,
      component: 'ApiTreeSelect',
      label: '组织',
      colProps: { span: 8 },
      rules: [{ required: true, message: '组织为必填项' }],
      componentProps({ formActionType }) {
        const { setFieldsValue } = formActionType;
        return {
          placeholder: '请选择组织',
          api: async () => {
            const data = await getOrganizationList();
            copyTransFun(data as any as any[]);
            return data;
          },
          onChange() {
            setFieldsValue({
              [DataSourceField.DEVICE_ID]: null,
              [DataSourceField.ATTRIBUTE]: null,
              [DataSourceField.SLAVE_DEVICE_ID]: null,
              [DataSourceField.IS_GATEWAY_DEVICE]: false,
              [DataSourceField.DEVICE_PROFILE_ID]: null,
              [DataSourceField.SLAVE_DEVICE_PROFILE_ID]: null,
            });
          },
          getPopupContainer: () => document.body,
        };
      },
    },
    {
      field: DataSourceField.DEVICE_PROFILE_ID,
      component: 'Input',
      label: '',
      show: false,
    },
    {
      field: DataSourceField.DEVICE_ID,
      component: 'ApiSelect',
      label: '设备',
      colProps: { span: 8 },
      rules: [{ required: true, message: '设备名称为必填项' }],
      componentProps({ formModel, formActionType }) {
        const { setFieldsValue } = formActionType;
        const organizationId = formModel[DataSourceField.ORIGINATION_ID];
        const deviceId = formModel[DataSourceField.DEVICE_ID];
        return {
          api: async () => {
            if (organizationId) {
              try {
                const data = await getAllDeviceByOrg(organizationId);
                if (deviceId) {
                  const record = data.find((item) => item.id === deviceId);
                  setFieldsValue({ [DataSourceField.DEVICE_PROFILE_ID]: record?.deviceProfileId });
                }
                if (data)
                  return data.map((item) => ({
                    ...item,
                    label: item.name,
                    value: item.id,
                    deviceType: item.deviceType,
                  }));
              } catch (error) {}
            }
            return [];
          },

          onChange(_value, record: MasterDeviceList) {
            setFieldsValue({
              [DataSourceField.ATTRIBUTE]: null,
              [DataSourceField.IS_GATEWAY_DEVICE]: record?.deviceType === 'GATEWAY',
              [DataSourceField.DEVICE_PROFILE_ID]: record?.deviceProfileId,
              [DataSourceField.SLAVE_DEVICE_ID]: null,
              [DataSourceField.SLAVE_DEVICE_PROFILE_ID]: null,
              [DataSourceField.DEVICE_NAME]: record?.label,
            });
          },
          placeholder: '请选择设备',
          getPopupContainer: () => document.body,
        };
      },
    },
    {
      field: DataSourceField.SLAVE_DEVICE_PROFILE_ID,
      component: 'Input',
      label: '',
      show: false,
    },
    {
      field: DataSourceField.SLAVE_DEVICE_ID,
      label: '网关子设备',
      component: 'ApiSelect',
      colProps: { span: 8 },
      rules: [{ required: true, message: '网关子设备为必填项' }],
      ifShow({ model }) {
        return model[DataSourceField.IS_GATEWAY_DEVICE];
      },
      dynamicRules({ model }) {
        return [
          { required: model[DataSourceField.IS_GATEWAY_DEVICE], message: '请选择网关子设备' },
        ];
      },
      componentProps({ formModel, formActionType }) {
        const { setFieldsValue } = formActionType;
        const organizationId = formModel[DataSourceField.ORIGINATION_ID];
        const isGatewayDevice = formModel[DataSourceField.IS_GATEWAY_DEVICE];
        const deviceId = formModel[DataSourceField.DEVICE_ID];
        const slaveDeviceId = formModel[DataSourceField.SLAVE_DEVICE_ID];
        return {
          api: async () => {
            if (organizationId && isGatewayDevice) {
              try {
                const data = await getGatewaySlaveDevice({ organizationId, masterId: deviceId });
                if (slaveDeviceId) {
                  const record = data.find((item) => item.id === slaveDeviceId);
                  setFieldsValue({ [DataSourceField.DEVICE_PROFILE_ID]: record?.deviceProfileId });
                }
                if (data)
                  return data.map((item) => ({
                    ...item,
                    label: item.name,
                    value: item.id,
                    deviceType: item.deviceType,
                  }));
              } catch (error) {}
            }
            return [];
          },
          onChange(_value, record: MasterDeviceList) {
            setFieldsValue({
              [DataSourceField.ATTRIBUTE]: null,
              [DataSourceField.SLAVE_DEVICE_PROFILE_ID]: record.deviceProfileId,
              [DataSourceField.DEVICE_NAME]: record?.label,
            });
          },
          placeholder: '请选择网关子设备',
          getPopupContainer: () => document.body,
        };
      },
    },
    {
      field: DataSourceField.ATTRIBUTE,
      component: 'ApiSelect',
      label: '属性',
      colProps: { span: 8 },
      rules: [{ required: true, message: '属性为必填项' }],
      componentProps({ formModel }) {
        const isGatewayDevice = formModel[DataSourceField.IS_GATEWAY_DEVICE];
        const deviceId = formModel[DataSourceField.DEVICE_ID];
        const deviceProfileId = formModel[DataSourceField.DEVICE_PROFILE_ID];
        const slaveDeviceId = formModel[DataSourceField.SLAVE_DEVICE_ID];
        const slaveDeviceProfileId = formModel[DataSourceField.SLAVE_DEVICE_PROFILE_ID];

        return {
          api: async () => {
            if (deviceId) {
              try {
                if (isGatewayDevice && slaveDeviceId && slaveDeviceProfileId) {
                  return await getDeviceAttribute({
                    deviceProfileId: slaveDeviceProfileId,
                    dataType: isControlComponent(frontId!) ? 'BOOL' : undefined,
                  });
                }
                if (!isGatewayDevice && deviceProfileId) {
                  return await getDeviceAttribute({
                    deviceProfileId,
                    dataType: isControlComponent(frontId!) ? 'BOOL' : undefined,
                  });
                }
              } catch (error) {}
            }
            return [];
          },
          placeholder: '请选择属性',
          getPopupContainer: () => document.body,
        };
      },
    },
    {
      field: DataSourceField.DEVICE_RENAME,
      component: 'Input',
      label: '设备',
      colProps: { span: 8 },
      componentProps: {
        placeholder: '设备重命名',
      },
    },
    {
      field: DataSourceField.ATTRIBUTE_RENAME,
      component: 'Input',
      label: '属性',
      colProps: { span: 8 },
      componentProps: {
        placeholder: '属性重命名',
      },
    },
  ];
};