HistoryData.vue 5.13 KB
<script lang="ts" setup>
  import moment from 'moment';
  import { nextTick, onMounted, onUnmounted, Ref, ref, unref } from 'vue';
  import { getDeviceDataKeys, getDeviceHistoryInfo } from '/@/api/alarm/position';
  import { Empty, Spin } from 'ant-design-vue';
  import { useECharts } from '/@/hooks/web/useECharts';
  import { dateUtil } from '/@/utils/dateUtil';
  import {
    AggregateDataEnum,
    eChartOptions,
    selectDeviceAttrSchema,
  } from '/@/views/device/localtion/config.data';
  import { useTimePeriodForm } from '/@/views/device/localtion/cpns/TimePeriodForm';
  import { defaultSchemas, QueryWay } from '/@/views/device/localtion/cpns/TimePeriodForm/config';
  import TimePeriodForm from '/@/views/device/localtion/cpns/TimePeriodForm/TimePeriodForm.vue';
  import { SchemaFiled } from '/@/views/report/config/config.data';
  import { useGridLayout } from '/@/hooks/component/useGridLayout';
  import { ColEx } from '/@/components/Form/src/types';

  interface DeviceDetail {
    tbDeviceId: string;
  }

  const props = defineProps<{
    deviceDetail: DeviceDetail;
  }>();

  const chartRef = ref();

  const deviceAttrs = ref<string[]>([]);

  const loading = ref(false);

  const isNull = ref(false);

  function getSearchParams(value: Recordable) {
    const { startTs, endTs, interval, agg, limit, keys, way } = value;
    if (way === QueryWay.LATEST) {
      return {
        entityId: props.deviceDetail.tbDeviceId,
        keys: keys ? keys : unref(deviceAttrs).join(),
        startTs: moment().subtract(startTs, 'ms').valueOf(),
        endTs: Date.now(),
        interval,
        agg,
        limit,
      };
    } else {
      return {
        entityId: props.deviceDetail.tbDeviceId,
        keys: keys ? keys : unref(deviceAttrs).join(),
        startTs: moment(startTs).valueOf(),
        endTs: moment(endTs).valueOf(),
        interval,
        agg,
        limit,
      };
    }
  }

  function hasDeviceAttr() {
    if (!unref(deviceAttrs).length) {
      return false;
    } else {
      return true;
    }
  }

  const { setOptions, getInstance } = useECharts(chartRef as Ref<HTMLDivElement>);

  function setChartOptions(data, keys?) {
    const dataArray: any[] = [];
    for (const key in data) {
      for (const item1 of data[key]) {
        let { ts, value } = item1;
        const time = dateUtil(ts).format('YYYY-MM-DD HH:mm:ss');
        value = Number(value).toFixed(2);
        dataArray.push([time, value, key]);
      }
    }
    keys = keys ? [keys] : unref(deviceAttrs);
    const series: any = keys.map((item) => {
      return {
        name: item,
        type: 'line',
        data: dataArray.filter((item1) => item1[2] === item),
      };
    });
    // 设置数据
    setOptions(eChartOptions(series, keys));
  }

  const [register, method] = useTimePeriodForm({
    schemas: [...defaultSchemas, ...selectDeviceAttrSchema],
    baseColProps: useGridLayout(2, 3, 4) as unknown as ColEx,
    async submitFunc() {
      // 表单验证
      await method.validate();
      const value = method.getFieldsValue();
      const searchParams = getSearchParams(value);

      if (!hasDeviceAttr()) return;
      // 发送请求
      loading.value = true;
      const res = await getDeviceHistoryInfo(searchParams);
      // 判断数据对象是否为空
      if (!Object.keys(res).length) {
        isNull.value = false;
        return;
      } else {
        isNull.value = true;
      }
      setChartOptions(res, value.keys);
      loading.value = false;
    },
  });

  const getDeviceDataKey = async () => {
    const { tbDeviceId } = props.deviceDetail || {};
    try {
      deviceAttrs.value = (await getDeviceDataKeys(tbDeviceId)) || [];
    } catch (error) {}
  };

  const openHistoryPanel = async () => {
    await nextTick();
    method.updateSchema({
      field: 'keys',
      componentProps: {
        options: unref(deviceAttrs).map((item) => ({ label: item, value: item })),
      },
    });

    method.setFieldsValue({
      [SchemaFiled.START_TS]: 1 * 24 * 60 * 60 * 1000,
      [SchemaFiled.LIMIT]: 7,
      [SchemaFiled.AGG]: AggregateDataEnum.NONE,
    });

    if (!hasDeviceAttr()) return;

    const res = await getDeviceHistoryInfo({
      entityId: props.deviceDetail.tbDeviceId,
      keys: unref(deviceAttrs).join(),
      startTs: Date.now() - 1 * 24 * 60 * 60 * 1000,
      endTs: Date.now(),
      agg: AggregateDataEnum.NONE,
      limit: 7,
    });

    // 判断对象是否为空
    if (!Object.keys(res).length) {
      isNull.value = false;
      return;
    } else {
      isNull.value = true;
    }
    setChartOptions(res);
  };

  onMounted(async () => {
    await getDeviceDataKey();
    await openHistoryPanel();
  });

  onUnmounted(() => {
    getInstance()?.clear();
  });
</script>

<template>
  <section class="flex flex-col p-4 h-full" style="color: #f0f2f5">
    <section class="bg-white p-3">
      <TimePeriodForm @register="register" />
    </section>
    <section class="bg-white p-3">
      <div v-show="isNull" ref="chartRef" :style="{ height: '550px', width: '100%' }">
        <Spin :spinning="loading" :absolute="true" />
      </div>
      <Empty v-show="!isNull" />
    </section>
  </section>
</template>

<style scoped></style>