formatData.ts 9.19 KB
import { formatToDateTime } from '/@/utils/dateUtil';

// 生成触发器或执行条件JSON数据
export const genTriggerOrConditionData = (triggerData) => {
  const {
    triggerType,
    entityId,
    type1,
    type2,
    device,
    detail,
    predicate,
    operationType,
    triggered,
    schedule,
  } = triggerData;
  const mapPredicate = predicate?.map((item) => {
    return {
      key: {
        type: type1,
        key: type2,
      },
      valueType: operationType,
      value: null,
      predicate: {
        type: operationType === 'DATE_TIME' ? 'NUMERIC' : operationType,
        operation: item.operation,
        value: {
          defaultValue:
            operationType === 'DATE_TIME' ? Number(formatToDateTime(item.value, 'x')) : item.value,
          userValue: null,
          dynamicValue: null,
        },
      },
    };
  });
  return {
    triggerType,
    entityType: device,
    entityId: entityId?.length ? entityId : null,
    triggerCondition: {
      alarmDetails: detail,
      condition: {
        condition: mapPredicate,
        spec: {
          type: triggered,
          // unit: 'SECONDS',
          // predicate: {
          //   defaultValue: 30,
          //   userValue: null,
          //   dynamicValue: null,
          // },
        },
      },
      schedule: {
        type: schedule,
        // timezone: 'Asia/Shanghai',
        // daysOfWeek: [2, 3],
        // startsOn: 8700000,
        // endsOn: 30300000,
      },
    },
  };
};

export const genActionData = (conditionData) => {
  const {
    alarm_config,
    alarm_level,
    detail,
    device,
    doContext,
    operationType,
    outTarget,
    predicate,
    triggerType,
    triggered,
    type1,
    type2,
    schedule,
    entityId,
    deviceId,
    checked,
  } = conditionData;
  const mapPredicate = predicate?.map((item) => {
    return {
      key: {
        type: type1,
        key: type2,
      },
      valueType: operationType,
      value: null,
      predicate: {
        type: operationType,
        operation: item.operation,
        value: {
          defaultValue: item.value,
          userValue: null,
          dynamicValue: null,
        },
      },
    };
  });
  return [
    {
      alarmProfileId: alarm_config,
      outTarget,
      entityType: device,
      entityId: entityId?.length ? entityId : null,
      deviceId,
      doContext: mapPredicate?.length
        ? {
            alarmLevel: alarm_level,
            clearRule: {
              triggerType,
              triggerCondition: {
                alarmDetails: detail,
                condition: {
                  condition: mapPredicate,
                  spec: {
                    type: triggered,
                    // unit: 'SECONDS',
                    // predicate: {
                    //   defaultValue: 30,
                    //   userValue: null,
                    //   dynamicValue: null,
                    // },
                  },
                },
                schedule: {
                  type: schedule,
                  // timezone: 'Asia/Shanghai',
                  // daysOfWeek: [2, 3],
                  // startsOn: 8700000,
                  // endsOn: 30300000,
                },
              },
            },
          }
        : {
            ...doContext,
            alarmLevel: outTarget === 'MSG_NOTIFY' ? alarm_level : undefined,
          },
    },
  ];
};

import { Number_Operation, String_Operation, Boolean_Operation } from '/@/enums/operationEnum';

export const operationNumber_OR_TIME = [
  { label: '等于', value: Number_Operation.EQUAL },
  { label: '不等于', value: Number_Operation.NOT_EQUAL },
  { label: '小于', value: Number_Operation.LESS },
  { label: '小于等于', value: Number_Operation.LESS_OR_EQUAL },
  { label: '大于', value: Number_Operation.GREATER },
  { label: '大于等于', value: Number_Operation.GREATER_OR_EQUAL },
];

export const operationString = [
  { label: '等于', value: String_Operation.EQUAL },
  { label: '不等于', value: String_Operation.NOT_EQUAL },
  { label: '开始于', value: String_Operation.BEGAN_IN },
  { label: '结束于', value: String_Operation.END_IN },
  { label: '包含', value: String_Operation.INCLUDE },
  { label: '不包含', value: String_Operation.NOT_INCLUDE },
];

export const operationBoolean = [
  { label: '等于', value: Boolean_Operation.EQUAL },
  { label: '不等于', value: Boolean_Operation.NOT_EQUAL },
];

// 查找操作符
export function findOperation(valueType, operation) {
  switch (valueType) {
    case 'NUMERIC' || 'DATE_TIME':
      return operationNumber_OR_TIME.find((item) => item.value === operation);
    case 'STRING':
      return operationString.find((item) => item.value === operation);
    case 'BOOLEAN':
      return operationBoolean.find((item) => item.value === operation);
  }
}

export function isType(operationType) {
  switch (operationType) {
    case 'NUMERIC':
      return [
        {
          field: 'operation',
          label: '执行操作',
          component: 'Select',
          required: true,
          componentProps: {
            options: operationNumber_OR_TIME,
          },
          colProps: {
            span: 8,
          },
        },
        {
          field: 'value',
          label: '操作值',
          required: true,
          component: 'InputNumber',
          colProps: {
            span: 8,
          },
        },
      ];
    case 'STRING':
      return [
        {
          field: 'ignoreCase',
          label: '忽略大小写',
          component: 'Checkbox',
          labelWidth: 150,
          colProps: {
            span: 7,
          },
        },
        {
          field: 'operation',
          label: '执行操作',
          component: 'Select',
          required: true,
          componentProps: {
            options: operationString,
          },
          colProps: {
            span: 7,
          },
        },
        {
          field: 'value',
          label: '操作值',
          required: true,
          component: 'Input',
          colProps: {
            span: 7,
          },
        },
      ];
    case 'BOOLEAN':
      return [
        {
          field: 'operation',
          label: '执行操作',
          component: 'Select',
          required: true,
          componentProps: {
            options: operationBoolean,
          },
          colProps: {
            span: 8,
          },
        },
        {
          field: 'value',
          label: '操作值',
          component: 'Select',
          required: true,
          componentProps: {
            options: [
              {
                label: '真',
                value: 'true',
              },
              {
                label: '假',
                value: 'false',
              },
            ],
          },
          colProps: {
            span: 8,
          },
        },
      ];
    case 'DATE_TIME':
      return [
        {
          field: 'operation',
          label: '执行操作',
          required: true,
          component: 'Select',
          componentProps: {
            options: operationNumber_OR_TIME,
          },
          colProps: {
            span: 8,
          },
        },
        {
          field: 'value',
          label: '操作值',
          required: true,
          component: 'DatePicker',
          componentProps: {
            showTime: true,
          },
          colProps: {
            span: 8,
          },
        },
      ];
  }
}
export function conditionPreView(data, operationType) {
  if (operationType === 'NUMERIC' || operationType === 'DATE_TIME') {
    const { EQUAL, NOT_EQUAL, LESS, LESS_OR_EQUAL, GREATER, GREATER_OR_EQUAL } = Number_Operation;
    return data.map((item) => {
      return {
        operation:
          item?.operation === EQUAL
            ? '等于'
            : item?.operation === NOT_EQUAL
            ? '不等于'
            : item?.operation === LESS
            ? '小于'
            : item?.operation === LESS_OR_EQUAL
            ? '小于等于'
            : item?.operation === GREATER
            ? '大于'
            : item?.operation === GREATER_OR_EQUAL
            ? '大于等于'
            : '',
        value: item.value,
        attribute: item.attribute,
      };
    });
  } else if (operationType === 'STRING') {
    const { EQUAL, NOT_EQUAL, BEGAN_IN, END_IN, INCLUDE, NOT_INCLUDE } = String_Operation;
    return data.map((item) => {
      return {
        operation:
          item?.operation === EQUAL
            ? '等于'
            : item?.operation === NOT_EQUAL
            ? '不等于'
            : item?.operation === BEGAN_IN
            ? '开始于'
            : item?.operation === END_IN
            ? '结束于'
            : item?.operation === INCLUDE
            ? '包含'
            : item?.operation === NOT_INCLUDE
            ? '不包含'
            : '',
        value: item.value,
        attribute: item.attribute,
      };
    });
  } else if (operationType === 'BOOLEAN') {
    const { EQUAL, NOT_EQUAL } = Boolean_Operation;
    return data.map((item) => {
      return {
        operation:
          item?.operation === EQUAL ? '等于' : item?.operation === NOT_EQUAL ? '不等于' : '',
        value: item.value,
        attribute: item.attribute,
      };
    });
  }
}