index.js 9.02 KB
Newer Older
1 2
import React, { useContext, useEffect, useRef, useState, useMemo } from 'react';
import { ConfigProvider, Modal, Radio, Select, InputNumber, Spin, message } from 'antd';
3
import classNames from 'classnames';
4 5 6 7 8
import { BasicChart } from '@wisdom-components/basicchart';
import { getStatisticsInfo } from '../apis';
import { getOptions, experiTypeOptions, compareOptions, valTakeOptions } from './utils';
import moment from 'moment';
import _ from 'lodash';
9 10
import './index.less';

11 12
const FORMAT = 'yyyy-MM-DD HH:mm:ss';
const PredictionCurve = (props) => {
13 14 15
  const { getPrefixCls } = useContext(ConfigProvider.ConfigContext);
  const prefixCls = getPrefixCls('empirical-curve');

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
  const {
    width,
    deviceCode,
    sensors,
    deviceType,
    getContainer,
    title,
    visible,
    onClose,
    valTakeType = '按偏移量',
    compareType = '同比',
    experiType = 'h',
    highLimit = 2,
    higherLimit = 0,
  } = props;
  const [loading, setLoading] = useState(false);
  const [experiVal, setExperiVal] = useState(experiType); // 经验值类型
  const [compareVal, setCompareVal] = useState(compareType); // 对比目标
  const [valTake, setValTake] = useState(valTakeType); // 取值方式
  const [realData, setRealData] = useState([]); // 原始数据
  const [compareData, setCompareData] = useState([]); // 对比数据
  const [highLimitVal, setHighLimitVal] = useState(highLimit); // 高限值
  const [higherLimitVal, setHigherLimitVal] = useState(higherLimit); // 高限值
  const [sensorData, setSensorData] = useState({ sensorName: sensors }); // 指标配置数据
  const chartRef = useRef(null);
  const dayStart = moment().format('yyyy-MM-DD 00:00:00');
  const dayEnd = moment().format('yyyy-MM-DD 23:59:59');
  const monthStart = moment().startOf('month').format('yyyy-MM-DD 00:00:00');
  const monthEnd = moment().endOf('month').format('yyyy-MM-DD 23:59:59');

  // 当前时间
  const [dateFrom1, dateTo1] = useMemo(() => {
    if (experiVal === 'h') {
      return [dayStart, dayEnd];
    } else {
      return [monthStart, monthEnd];
    }
  }, [experiVal]);

  // 对比时间
  const [dateFrom2, dateTo2] = useMemo(() => {
    // 小时-同比:今日0点-24点对应昨日0点-24点
    if (experiVal === 'h' && compareVal === '同比') {
      return [
        moment(dayStart).subtract(1, 'days').format(FORMAT),
        moment(dayEnd).subtract(1, 'days').format(FORMAT),
      ];
    }
    // 小时-环比:今日0点-24点对比当前时间往前推1小时
    if (experiVal === 'h' && compareVal === '环比') {
      return [
        moment(dayStart).subtract(1, 'hours').format(FORMAT),
        moment(dayEnd).subtract(1, 'hours').format(FORMAT),
      ];
    }

    // 日-同比:本月1日-31日对比上个月1日-31日
    if (experiVal === 'day' && compareVal === '同比') {
      return [
        moment(monthStart).subtract(1, 'months').format(FORMAT),
        moment(monthEnd).subtract(1, 'months').format(FORMAT),
      ];
    }

    // 日-环比:本月1日-31日对比当前时间往前推一天
    if (experiVal === 'day' && compareVal === '环比') {
      return [
        moment(monthStart).subtract(1, 'days').format(FORMAT),
        moment(monthEnd).subtract(1, 'days').format(FORMAT),
      ];
    }

    return ['', ''];
  }, [experiVal, compareVal]);

  //chart options
  const options = useMemo(() => {
    const _options = getOptions(
      realData,
      compareData,
      experiVal,
      compareVal,
      highLimitVal,
      higherLimitVal,
      valTake,
      sensorData,
    );
    return _options;
  }, [realData, compareData, highLimitVal, higherLimitVal, , valTake, sensorData]);

  // 确定
  const onOk = () => {
    onClose();
  };

  // 取消
  const onCancel = () => {
    onClose();
  };

  // 获取统计服务数据
  const getStatisticsData = async () => {
    try {
      const params1 = {
        pageIndex: 1,
        pageSize: 1,
        q_DeviceReports: [
          {
            accountName: deviceType,
            nameTypeList: [
              {
                name: sensors,
                type: sensors.includes('累计') ? 'Sub' : 'Avg',
              },
            ],
            dateType: experiVal,
            dateFrom: dateFrom1,
            dateTo: dateTo1,
            deviceCode,
          },
        ],
        dateType: experiVal,
      };
      const params2 = _.cloneDeep(params1);
      params2.q_DeviceReports[0].dateFrom = dateFrom2;
      params2.q_DeviceReports[0].dateTo = dateTo2;
      setLoading(true);
      const req1 = getStatisticsInfo(params1);
      const req2 = getStatisticsInfo(params2);
      const results = await Promise.all([req1, req2]);
      setLoading(false);
      const [res1, res2] = results;
      if (res1.code === 0 && res2.code === 0) {
        let _realData = res1?.data?.list?.[0]?.dNameDataList?.[0]?.nameDate ?? [];
        let _compareData = res2?.data?.list?.[0]?.dNameDataList?.[0]?.nameDate ?? [];
        _realData = _realData.map((item) => ({
          ...item,
          value: typeof item.value === 'number' ? item.value.toFixed(2) * 1 : 0,
        }));
        _compareData = _compareData.map((item) => ({
          ...item,
          value: typeof item.value === 'number' ? item.value.toFixed(2) * 1 : 0,
        }));
        setRealData(_realData);
        setCompareData(_compareData);
        setSensorData({
          ...sensorData,
          unit: res1?.data?.list?.[0]?.dNameDataList?.[0]?.unit ?? '',
        });
      } else {
        setRealData([]);
        setCompareData([]);
      }
    } catch (error) {
      setLoading(false);
      console.log(error);
    }
  };

  useEffect(() => {
    getStatisticsData();
  }, [experiVal, dateFrom1, dateTo1, dateFrom2, dateTo2]);

179 180
  return (
    <>
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
      <Modal
        title={title}
        centered
        okText={'确定'}
        width={width || '1200px'}
        cancelText={'取消'}
        open={visible}
        onOk={onOk}
        onCancel={onCancel}
        wrapClassName={classNames(`${prefixCls}`)}
        getContainer={getContainer || document.body}
      >
        <Spin spinning={loading}>
          <div className={classNames(`${prefixCls}-box`)}>
            <div className={classNames(`${prefixCls}-header`)}>
              <div className={classNames(`${prefixCls}-header-list`)}>
                <span className={classNames(`${prefixCls}-header-item`)}>
                  经验值类型:
                  <Radio.Group
                    options={experiTypeOptions}
                    optionType={'button'}
                    value={experiVal}
                    onChange={(e) => {
                      setExperiVal(e.target.value);
                    }}
                  />
                </span>
                <span className={classNames(`${prefixCls}-header-item`)}>
                  对比目标:
                  <Radio.Group
                    options={compareOptions}
                    optionType={'button'}
                    value={compareVal}
                    onChange={(e) => {
                      setCompareVal(e.target.value);
                    }}
                  />
                </span>
                <span className={classNames(`${prefixCls}-header-item`)}>
                  取值方式:
                  <Select options={valTakeOptions} value={valTake} onChange={setValTake} />
                </span>
                <span className={classNames(`${prefixCls}-header-item`)}>
                  偏差范围: 高限
                  <InputNumber
                    addonAfter={valTake === '按偏移率' ? '%' : ''}
                    style={{
                      marginLeft: '2px',
                      marginRight: '8px',
                      width: valTake === '按偏移率' ? '110px' : '90px',
                    }}
                    value={highLimitVal}
                    onChange={(val) => {
                      if (val && higherLimitVal && val > higherLimitVal)
                        return message.info('高限值不能大于或等于高高限值!');
                      setHighLimitVal(val);
                    }}
                    min={0}
                  />
                  高高限
                  <InputNumber
                    addonAfter={valTake === '按偏移率' ? '%' : ''}
                    style={{ marginLeft: '2px', width: valTake === '按偏移率' ? '110px' : '90px' }}
                    value={higherLimitVal}
                    onChange={(val) => {
                      if (val && highLimitVal && val <= highLimitVal)
                        return message.info('高高限值不能小于或等于高限值!');
                      setHigherLimitVal(val);
                    }}
                    min={0}
                  />
                </span>
              </div>
            </div>
            <div className={classNames(`${prefixCls}-content`)}>
              <BasicChart
                ref={chartRef}
                option={options}
                notMerge
                style={{ width: '100%', height: '100%' }}
              />
            </div>
          </div>
        </Spin>
      </Modal>
266 267 268 269
    </>
  );
};

270
export default PredictionCurve;