config.ts 3.84 KB
import moment from 'moment';
import { FormSchema } from '/@/components/Form';
import { ColEx } from '/@/components/Form/src/types';
import { useGridLayout } from '/@/hooks/component/useGridLayout';
import { intervalOption } from '/@/views/device/localtion/cpns/TimePeriodForm/helper';
export enum QueryWay {
  LATEST = 'latest',
  TIME_PERIOD = 'timePeriod',
}

export enum SchemaFiled {
  DEVICE_ID = 'deviceId',
  WAY = 'way',
  TIME_PERIOD = 'timePeriod',
  KEYS = 'keys',
  DATE_RANGE = 'dataRange',
  START_TS = 'startTs',
  END_TS = 'endTs',
  INTERVAL = 'interval',
  LIMIT = 'limit',
  AGG = 'agg',
  ORDER_BY = 'orderBy',
  PAGE_SIZe = 'pageSize',
}

export enum AggregateDataEnum {
  MIN = 'MIN',
  MAX = 'MAX',
  AVG = 'AVG',
  SUM = 'SUM',
  COUNT = 'COUNT',
  NONE = 'NONE',
}
export const formSchema = (): FormSchema[] => {
  return [
    {
      field: SchemaFiled.WAY,
      label: '查询方式',
      component: 'RadioGroup',
      defaultValue: QueryWay.LATEST,
      componentProps({ formActionType }) {
        const { setFieldsValue } = formActionType;
        return {
          options: [
            { label: '最后', value: QueryWay.LATEST },
            { label: '时间段', value: QueryWay.TIME_PERIOD },
          ],
          onChange(event: ChangeEvent) {
            (event.target as HTMLInputElement).value === QueryWay.LATEST
              ? setFieldsValue({
                  [SchemaFiled.DATE_RANGE]: [],
                  [SchemaFiled.START_TS]: null,
                  [SchemaFiled.END_TS]: null,
                })
              : setFieldsValue({ [SchemaFiled.START_TS]: null });
          },
          getPopupContainer: () => document.body,
        };
      },
    },

    {
      field: SchemaFiled.PAGE_SIZe,
      label: '分页条数',
      component: 'InputNumber',
      defaultValue: 20,
      ifShow: true,
      componentProps() {
        return {
          min: 10,
          max: 50000,
          getPopupContainer: () => document.body,
        };
      },
    },
    {
      field: SchemaFiled.START_TS,
      label: '最后数据',
      component: 'Select',
      ifShow({ values }) {
        return values[SchemaFiled.WAY] === QueryWay.LATEST;
      },
      defaultValue: 2592000000,
      componentProps({ formActionType }) {
        const { setFieldsValue } = formActionType;
        return {
          options: intervalOption,
          onChange() {
            setFieldsValue({ [SchemaFiled.INTERVAL]: null });
          },
          getPopupContainer: () => document.body,
        };
      },
      colProps: useGridLayout(2, 2, 2, 2, 2, 2) as unknown as ColEx,
      rules: [{ required: true, message: '最后数据为必选项', type: 'number' }],
    },
    {
      field: SchemaFiled.DATE_RANGE,
      label: '时间段',
      component: 'RangePicker',
      ifShow({ values }) {
        return values[SchemaFiled.WAY] === QueryWay.TIME_PERIOD;
      },
      rules: [{ required: true, message: '时间段为必选项' }],
      componentProps({ formActionType }) {
        const { setFieldsValue } = formActionType;
        return {
          showTime: {
            defaultValue: [moment('00:00:00', 'HH:mm:ss'), moment('23:59:59', 'HH:mm:ss')],
          },
          onChange() {
            setFieldsValue({ [SchemaFiled.INTERVAL]: null });
          },
          getPopupContainer: () => document.body,
        };
      },
      colProps: useGridLayout(2, 2, 2, 2, 2, 2) as unknown as ColEx,
    },
    {
      field: SchemaFiled.LIMIT,
      label: '最大条数',
      component: 'InputNumber',
      ifShow({ values }) {
        return values[SchemaFiled.AGG] === AggregateDataEnum.NONE;
      },
      helpMessage: ['根据查询条件,查出的数据条数不超过这个值'],
      componentProps() {
        return {
          max: 50000,
          min: 7,
          getPopupContainer: () => document.body,
        };
      },
    },
  ];
};