config.ts 3.94 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';
import { useI18n } from '/@/hooks/web/useI18n';
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',
}

const { t } = useI18n();
export const formSchema = (): FormSchema[] => {
  return [
    {
      field: SchemaFiled.WAY,
      label: t('visual.board.queryMethodText'),
      component: 'RadioGroup',
      defaultValue: QueryWay.LATEST,
      componentProps({ formActionType }) {
        const { setFieldsValue } = formActionType;
        return {
          options: [
            { label: t('visual.board.finalText'), value: QueryWay.LATEST },
            { label: t('visual.board.timeSlotText'), 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: t('visual.board.numberPackText'),
      component: 'InputNumber',
      defaultValue: 20,
      ifShow: true,
      componentProps() {
        return {
          min: 10,
          max: 50000,
          getPopupContainer: () => document.body,
        };
      },
    },
    {
      field: SchemaFiled.START_TS,
      label: t('visual.board.finalDataText'),
      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, type: 'number' }],
    },
    {
      field: SchemaFiled.DATE_RANGE,
      label: t('visual.board.timeSlotText'),
      component: 'RangePicker',
      ifShow({ values }) {
        return values[SchemaFiled.WAY] === QueryWay.TIME_PERIOD;
      },
      rules: [{ required: true }],
      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: t('visual.board.maxEntriesText'),
      component: 'InputNumber',
      ifShow({ values }) {
        return values[SchemaFiled.AGG] === AggregateDataEnum.NONE;
      },
      helpMessage: [t('visual.board.limitMEssage')],
      componentProps() {
        return {
          max: 50000,
          min: 7,
          getPopupContainer: () => document.body,
        };
      },
    },
  ];
};