config.ts 2.5 KB
import { formatToDateTime } from '/@/utils/dateUtil';
import { DataComponentRecord, DataSource } from '/@/api/dataBoard/model';
export interface TextComponentLayout {
  id: string;
  base?: boolean;
  showUpdate?: boolean;
  showIcon?: boolean;
  showUnit?: boolean;
}

export interface TextComponentValue {
  name: string;
  value: number;
  icon?: string;
  unit?: string;
  updateTime?: string;
  fontColor?: string;
  iconColor?: string;
}

export type TextComponentType =
  | 'text-component-1'
  | 'text-component-2'
  | 'text-component-3'
  | 'text-component-4'
  | 'text-component-5';

type TextComponentDefault = TextComponentLayout & { value: TextComponentValue };

export const TextComponent1Config: TextComponentDefault = {
  id: 'text-component-1',
  base: true,
  value: { value: 123, name: '温度' },
};

export const TextComponent2Config: TextComponentDefault = {
  id: 'text-component-2',
  base: false,
  value: { value: 123, name: '温度' },
};
export const TextComponent3Config: TextComponentDefault = {
  id: 'text-component-3',
  base: false,
  showUpdate: true,
  value: {
    value: 123,
    name: '温度',
    updateTime: formatToDateTime(new Date(), 'YYYY-MM-DD HH:mm:ss'),
  },
};
export const TextComponent4Config: TextComponentDefault = {
  id: 'text-component-4',
  base: false,
  showIcon: true,
  showUpdate: true,
  showUnit: true,
  value: {
    value: 123,
    name: '温度',
    updateTime: formatToDateTime(new Date(), 'YYYY-MM-DD HH:mm:ss'),
    unit: '℃',
  },
};
export const TextComponent5Config: TextComponentDefault = {
  id: 'text-component-5',
  base: false,
  showIcon: true,
  showUnit: true,
  value: { value: 123, name: '温度', unit: '℃' },
};

export const textComponentConfig: TextComponentDefault[] = [
  TextComponent1Config,
  TextComponent2Config,
  TextComponent3Config,
  TextComponent4Config,
  TextComponent5Config,
];

export const transformTextComponentConfig = (
  config: TextComponentDefault,
  record: DataComponentRecord,
  dataSourceRecord: DataSource
) => {
  return {
    layout: {
      ...config,
    } as TextComponentLayout,
    value: {
      name: dataSourceRecord.attributeRename || dataSourceRecord.attribute,
      value: dataSourceRecord.componentInfo.value,
      icon: dataSourceRecord.componentInfo.icon,
      unit: dataSourceRecord.componentInfo.unit,
      updateTime: dataSourceRecord.componentInfo.updateTime,
      fontColor: dataSourceRecord.componentInfo.fontColor,
      iconColor: dataSourceRecord.componentInfo.iconColor,
    } as TextComponentValue,
  };
};