index.js 10.4 KB
Newer Older
1
import React, { useState, useEffect, useContext, useMemo } from 'react';
涂茜's avatar
涂茜 committed
2 3 4
import PropTypes from 'prop-types';
import Empty from '@wisdom-components/empty';
import classNames from 'classnames';
5
import { Modal, Button, Tabs, Input, Radio, ConfigProvider, message } from 'antd';
涂茜's avatar
涂茜 committed
6
import BasicTable from '@wisdom-components/basictable';
7 8 9 10 11 12
import {
  getMonitorConfig,
  getSensorType,
  getDeviceRealInfo,
  getPointAddressEntry,
  getPointAddress,
陈龙's avatar
陈龙 committed
13
} from './apis';
涂茜's avatar
涂茜 committed
14 15 16 17
import './index.less';

const { TabPane } = Tabs;

18 19 20 21 22 23 24 25 26 27 28 29 30 31
const defaultColumns = [
  {
    title: '序号',
    dataIndex: 'index',
    width: 60,
  },
  {
    title: '指标名称',
    dataIndex: 'name',
    width: 150,
  },
  {
    title: '最新指标',
    dataIndex: 'value',
32
    render: (text) => <a style={{cursor: 'default'}}>{text}</a>,
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  },
  {
    title: '单位',
    dataIndex: 'unit',
  },
  {
    title: '指标类型',
    dataIndex: 'type',
  },
  {
    title: '数据描述',
    dataIndex: 'desc',
  },
  {
    title: '更新时间',
    dataIndex: 'time',
  },
];

涂茜's avatar
涂茜 committed
52 53 54 55
const RealTimeInfo = (props) => {
  const { getPrefixCls } = useContext(ConfigProvider.ConfigContext);
  const prefixCls = getPrefixCls('realtime-info');

程恺文's avatar
程恺文 committed
56 57
  const { deviceParams, infoData, user, placeholder, defaultTargetValue, modalTitle, buttonText } =
    props;
涂茜's avatar
涂茜 committed
58 59 60 61 62 63

  const [isModalVisible, setIsModalVisible] = useState(false);
  const [targetValue, setTargetValue] = useState(defaultTargetValue); // 重点/全部
  const [searchValue, setSearchValue] = useState(''); // 搜索框内容
  const [tabKey, setTabKey] = useState('');
  const [guid, setGuid] = useState('');
涂茜's avatar
涂茜 committed
64
  const [deviceConf, setDeviceConf] = useState([]); // 设备配置
涂茜's avatar
涂茜 committed
65
  const [deviceInfo, setDeviceInfo] = useState({}); // 设备实时数据
66
  const [sensorType, setSensorType] = useState([]); // sensorType
涂茜's avatar
涂茜 committed
67 68
  const [pointAddress, setPointAddress] = useState([]); // pointAddress

69 70
  const tabData = useMemo(() => {
    const data = deviceInfo;
涂茜's avatar
涂茜 committed
71
    let tData = [];
72
    if (!data) return tData;
涂茜's avatar
涂茜 committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    tData.push({
      key: data.code,
      title: data.name,
      guid: data.guid,
      versionID: data.versionID,
      deviceType: data.aName,
    });
    if (data.child && data.child.length) {
      data.child.forEach((child) => {
        tData.push({
          key: child.code,
          title: data.name + child.name,
          guid: child.guid,
          versionID: child.versionID,
          deviceType: child.aName,
        });
      });
    }
91 92
    return tData;
  }, [deviceInfo]);
涂茜's avatar
涂茜 committed
93

94 95
  // 过滤重点指标(带搜索)
  const filterEmphasis = (dataSource, searchValue) => {
涂茜's avatar
涂茜 committed
96 97 98 99
    const cur = tabData.filter((item) => item.key === tabKey);
    const conf =
      cur.length > 0 ? deviceConf.filter((item) => item.deviceType === cur[0].deviceType) : [];
    const dPoints = conf.length > 0 && conf[0].dPoints ? conf[0].dPoints.split(',') : [];
100 101 102 103 104 105 106 107 108 109 110
    const data = dataSource.filter((item) =>
      searchValue
        ? dPoints.indexOf(searchValue) > -1 && dPoints.includes(item.name)
        : dPoints.includes(item.name),
    );
    return data;
  };

  // 过滤普通指标(带搜索)
  const filterSearch = (dataSource, searchValue) => {
    return !searchValue ? dataSource : dataSource.filter((item) => item.name.includes(searchValue));
涂茜's avatar
涂茜 committed
111 112
  };

113
  const getData = () => {
徐乐's avatar
徐乐 committed
114
    let deviceType = '';
徐乐's avatar
徐乐 committed
115 116 117
    if (infoData) {
      let devices = [infoData.aName];
      infoData.child?.forEach(function (val) {
程恺文's avatar
程恺文 committed
118 119 120
        let k = devices.find(function (a) {
          return a == val.aName;
        });
徐乐's avatar
徐乐 committed
121 122 123
        if (!k) {
          devices.push(val.aName);
        }
程恺文's avatar
程恺文 committed
124
      });
徐乐's avatar
徐乐 committed
125 126 127 128 129 130 131 132
      deviceType = devices.join(',');
    } else {
      deviceType =
        deviceParams.length > 0
          ? Array.from(new Set(deviceParams.map((item) => item.deviceType))).join(',')
          : '二供泵房,二供机组';
    }

133 134 135 136
    const configReq = getMonitorConfig({
      params: {
        user,
        showAll: true,
徐乐's avatar
徐乐 committed
137
        deviceType: deviceType,
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
      },
    });
    const sensorReq = getSensorType();
    const realDataReq = getDeviceRealInfo({
      data: {
        pageIndex: 1,
        pageSize: 200,
        param: deviceParams,
        useID: user,
      },
    });
    Promise.all([configReq, sensorReq, realDataReq])
      .then((results) => {
        if (results.some((item) => item.code !== 0)) message.error('获取数据失败');
        const [configRes, sensorRes, realDataRes] = results;
        setSensorType(sensorRes.data);
        setDeviceConf(configRes.data);
        setDeviceInfo(realDataRes.data.list[0]);
        setTabKey(realDataRes?.data?.list?.[0]?.code);
      })
      .catch((err) => {
        message.error('获取数据失败');
      });
  };

  useEffect(() => {
    if (!isModalVisible || !deviceParams || !deviceParams.length) return;
    getData();
  }, [deviceParams, user, isModalVisible]);

  const GetPointAddressEntry = () => {
    const cur = tabData.filter((item) => item.key === tabKey);
    !!cur.length &&
      getPointAddressEntry({
        params: {
          versionId: cur[0].versionID,
        },
      }).then((res) => {
        if (res.code === 0 && res.data.length) {
          setPointAddress(res.data);
涂茜's avatar
涂茜 committed
178 179 180 181
        }
      });
  };

182 183 184
  useEffect(() => {
    if (!tabKey || !tabData || !tabData.length) return;
    GetPointAddressEntry();
185 186
    const g = tabData.filter((item) => item.key === tabKey);
    setGuid(g?.[0]?.guid);
187 188 189 190 191
  }, [tabKey]);

  const formatData = (data = [], sensorType = [], deviceInfo = {}) => {
    if (!deviceInfo) return [];
    let time = deviceInfo.pt;
涂茜's avatar
涂茜 committed
192
    if (time) time = time.slice(5, 19).replace('-', '/');
193
    let newData = data.map((item, index) => {
涂茜's avatar
涂茜 committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207
      return {
        id: item.id,
        key: item.id,
        index: index + 1,
        name: item.name,
        value: 0,
        unit: '--',
        type: '--',
        time: time,
        desc: item.valDesc || '--',
        ...item,
      };
    });
    newData.forEach((item) => {
208 209
      let curData1 = sensorType.filter((child) => child.id == item.sensorTypeID);
      let curData2 = deviceInfo.dataList.filter((child) => child.paid == item.id);
涂茜's avatar
涂茜 committed
210 211 212 213 214
      if (curData1.length) {
        item.unit = curData1[0].unit || '--';
        item.type = curData1[0].name || '--';
      }
      if (curData2.length) {
215
        item.value = curData2[0].pv || curData2[0].pv === 0 ? curData2[0].pv : '--';
涂茜's avatar
涂茜 committed
216 217
      }
    });
218
    return newData;
涂茜's avatar
涂茜 committed
219 220
  };

221 222 223
  const tableData = useMemo(() => {
    // 过滤数据
    let points = [];
涂茜's avatar
涂茜 committed
224
    if (targetValue === 'emphasis') {
225
      points = filterEmphasis(pointAddress, searchValue);
涂茜's avatar
涂茜 committed
226
    } else {
227
      points = filterSearch(pointAddress, searchValue);
涂茜's avatar
涂茜 committed
228
    }
229 230 231 232 233 234 235 236 237 238 239 240

    const deviceData =
      deviceInfo.code === tabKey
        ? deviceInfo
        : deviceInfo?.child?.find((item) => item.code === tabKey);
    const newData = formatData(points, sensorType, deviceData);
    return newData;
  }, [targetValue, searchValue, pointAddress, deviceInfo]);

  const columns = useMemo(() => {
    return defaultColumns;
  }, []);
涂茜's avatar
涂茜 committed
241 242 243 244 245 246 247 248 249 250 251 252 253

  const showModal = () => {
    setIsModalVisible(true);
  };

  const handleOk = () => {
    setIsModalVisible(false);
  };

  const handleCancel = () => {
    setIsModalVisible(false);
  };

254 255 256 257 258
  const changeSearch = (e) => {
    e.target && e.target.value === '' && setSearchValue('');
  };

  const onSearch = (value) => {
涂茜's avatar
涂茜 committed
259
    // 前端搜索
260
    setSearchValue(value);
涂茜's avatar
涂茜 committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
  };

  const onTabChange = (key) => {
    setTabKey(key);
  };

  const onRadioChange = (e) => {
    setTargetValue(e.target.value);
  };

  const renderTitle = () => {
    return (
      <div className={classNames(`${prefixCls}-modal-title`)}>
        <Tabs
          tabBarExtraContent={{ left: <h3 style={{ fontWeight: 'bold' }}>{modalTitle}</h3> }}
          activeKey={tabKey}
          onChange={onTabChange}
          centered
        >
          {tabData.map((item) => (
            <TabPane tab={item.title} key={item.key} />
          ))}
        </Tabs>
      </div>
    );
  };

  return (
    <div className={classNames(prefixCls)}>
      <Button type="link" onClick={showModal}>
        {buttonText}
      </Button>
      <Modal
        className={classNames(`${prefixCls}-modal`)}
        width={915}
        title={renderTitle()}
        footer={null}
298
        open={isModalVisible}
涂茜's avatar
涂茜 committed
299 300 301 302 303 304 305
        onOk={handleOk}
        onCancel={handleCancel}
      >
        <div className={classNames(`${prefixCls}-modal-content`)}>
          <div className={classNames(`${prefixCls}-search-wrap`)}>
            <div className={classNames(`${prefixCls}-search`)}>
              <div className={classNames(`${prefixCls}-label`)}>搜索:</div>
306
              <Input.Search placeholder={placeholder} onSearch={onSearch} onChange={changeSearch} />
涂茜's avatar
涂茜 committed
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
            </div>
            <div className={classNames(`${prefixCls}-target`)}>
              <div className={classNames(`${prefixCls}-label`)}>指标:</div>
              <Radio.Group onChange={onRadioChange} defaultValue={targetValue}>
                <Radio.Button value="emphasis">重点指标</Radio.Button>
                <Radio.Button value="all">全部</Radio.Button>
              </Radio.Group>
            </div>
          </div>
          <div className={classNames(`${prefixCls}-code-wrap`)}>
            <div>采集编码:{guid || '--'}</div>
            <div>更新时间:{(tableData.length && tableData[0].time) || '--'}</div>
          </div>
          <div className={classNames(`${prefixCls}-modal-table`)}>
            <BasicTable
              bordered
              columns={columns}
324
              locale={{ empty: false }}
涂茜's avatar
涂茜 committed
325
              pagination={false}
326
              dataSource={tableData}
涂茜's avatar
涂茜 committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340
              {...props}
            />
          </div>
        </div>
      </Modal>
    </div>
  );
};

RealTimeInfo.defaultProps = {
  buttonText: '查看更多',
  modalTitle: '实时指标监控',
  placeholder: '输入指标名称等',
  defaultTargetValue: 'emphasis',
涂茜's avatar
涂茜 committed
341
  user: null,
342
  deviceParams: [],
涂茜's avatar
涂茜 committed
343
  deviceRealInfoParams: {},
程恺文's avatar
程恺文 committed
344 345 346 347
  deviceConfService: () => {},
  pointAddressEntryService: () => {},
  sensorTypeService: () => {},
  deviceRealInfoService: () => {},
涂茜's avatar
涂茜 committed
348 349 350 351 352 353 354
};

RealTimeInfo.propTypes = {
  buttonText: PropTypes.string,
  modalTitle: PropTypes.string,
  placeholder: PropTypes.string,
  defaultTargetValue: PropTypes.string,
355 356
  user: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
  deviceParams: PropTypes.array,
涂茜's avatar
涂茜 committed
357 358 359 360 361 362 363 364
  deviceRealInfoParams: PropTypes.object,
  deviceConfService: PropTypes.func,
  pointAddressEntryService: PropTypes.func,
  sensorTypeService: PropTypes.func,
  deviceRealInfoService: PropTypes.func,
};

export default RealTimeInfo;