import React, { Fragment, useRef, useMemo, useEffect, useState } from 'react'; import { Table, Card, Modal, Form, Input, Button, Row, Col, Timeline, message, Tabs, Image } from 'antd'; import ToolBar from './toolbar'; import apiurl from '../../../../service/apiurl'; import usePageTable from '../../../../components/crud/usePageTable2'; import { createCrudService } from '../../../../components/crud/_'; import { httppost5, httppost2 } from '../../../../utils/request'; import { exportFile } from '../../../../utils/tools.js'; import './index.less' const url = "http://223.75.53.141:9102/test.by-lyf.tmp" const Page = () => { const projectType = { 0: '闸后流量', 1: '雨量', 2: '闸前水位', 3: '闸后水位' } const bbType = { 0: '日报表', 1: '年报表', 2:'月报表' } const [searchVal, setSearchVal] = useState(false) const [requsetUrlObj, setRequsetUrlObj] = useState({}) const requsetUrlObjRef = useRef(null) requsetUrlObjRef.current = requsetUrlObj; const timeColumns = [ { title: '日期', key: 'date', dataIndex: 'date', width: 120, align: "center", fixed: "left" }, ...Array(16).fill(0).map((item, index) => ({ title: `${index + 9}时`, key: `rzH${index + 9}`, dataIndex: `rzH${index + 9}`, width: 80, align: "center", sorter: (a, b) => a[`rzH${index + 9}`] - b[`rzH${index + 9}`] })), ...Array(8).fill(0).map((item, index) => ({ title: `${index + 1}时`, key: `rzH${index + 1}`, dataIndex: `rzH${index + 1}`, width: 80, align: "center", sorter: (a, b) => a[`rzH${index + 1}`] - b[`rzH${index + 1}`] })), { title: '平均', key: 'rzAvg ', dataIndex: 'rzAvg', width: 100, align: "center", sorter: (a, b) => a.rzAvg - b.rzAvg }, ]; const dayColumns = [ { title: '日期', key: 'date', dataIndex: 'date', width: 120, align: "center", render: (v, r, i) => {v ? `${v}号`: ''}}, {title: '监测值', key: 'data', dataIndex: 'data', width: 120, align: "center",} ]; const yearColumns = [ { title: '日期', key: 'date', dataIndex: 'date', width: 120, align: "center", fixed: "left" }, ...Array(12).fill(0).map((item, index) => ({ title: `${index + 1}月`, key: `drpM${index + 1}`, dataIndex: `drpM${index + 1}`, width: 100, align: "center", })) ]; const newTimeColumns = useMemo(() => { const changeColumns = JSON.parse(JSON.stringify(timeColumns)); changeColumns[changeColumns.length - 1] = { title: '日累计', key: 'drpD', dataIndex: 'drpD', width: 100, align: 'center', sorter: (a, b) => a.drpD - b.drpD } const cols = [changeColumns[0]]; changeColumns.forEach((item, i) => { if (i > 0) { if (i < 17) { item.sorter = (a, b) => a[`drpH${i + 8}`] - b[`drpH${i + 8}`] } else { item.sorter = (a, b) => a[`drpH${i - 16}`] - b[`drpH${i - 16}`] } cols.push({ ...item, key: item.key.replace('rz', 'drp'), dataIndex: item.dataIndex.replace('rz', 'drp'), }) } }) return searchVal.type == 1 ? cols : timeColumns }, [searchVal]) const width = useMemo(() => { let columns = searchVal.type1 == 0 ? timeColumns : searchVal.type1 == 1 ? yearColumns : yearColumns; return columns.reduce((total, cur) => total + (cur.width), 0) }, [searchVal]); const [staData, setStaData] = useState({}) const [data, setData] = useState([]) const getData = async (params) => { try { const res = await httppost2(requsetUrlObjRef.current?.page, params) if (res.code == 200) { setData(res.data) } } catch (error) { console.log(error); } } // 年报表总结栏 const getStaData = async (params) => { let obj = {}; try { const res = await httppost2(requsetUrlObjRef.current?.yearSummary, params) if (res.code == 200) { res.data.list?.forEach(item => { if (!obj[item.date]) { let arr = Object.keys(item) params.type == 1 ? arr.splice(0, 1) : arr.splice(0, 2) obj[item.date] = arr.map(o => ({ [o]: item[o] })) } }) setStaData({ ...res.data, list: obj }) } } catch (error) { console.log(error); } } // 导出 const exportExcel = () => { let params = { ...searchVal, } const fileName = (searchVal?.type1 == 0 ? `${formatDate(searchVal?.dateRangeSo?.start)} - ${formatDate(searchVal?.dateRangeSo?.end)}` : searchVal?.type1 == 1 ?formatDate(searchVal.year): formatDate(searchVal.yearMonth)) + projectType[searchVal.type] + bbType[searchVal.type1] httppost5(requsetUrlObjRef.current?.export, params).then(res => { exportFile(`${fileName}.xlsx`, res.data) }) } const getUrl = (name, type) => { let urlObj; let obj = { page: type == 0 ? apiurl.gcyx.tjbb.swrbb : apiurl.gcyx.tjbb.swnbb, export: type == 0 ? apiurl.gcyx.tjbb.swrbbExport : type == 1 ?apiurl.gcyx.tjbb.swnbbExport: apiurl.gcyx.tjbb.swybbExport, yearSummary: type == 1 ? apiurl.gcyx.tjbb.swnbbSummary : '' } switch (name) { case 0: urlObj = obj break; case 1: urlObj = { page: type == 0 ? apiurl.gcyx.tjbb.jyrbb : apiurl.gcyx.tjbb.jynbb, export: type == 0 ? apiurl.gcyx.tjbb.jyrbbExport :type == 1 ? apiurl.gcyx.tjbb.jynbbExport: apiurl.gcyx.tjbb.swybbExport, yearSummary: type == 1 ? apiurl.gcyx.tjbb.jynbbSummary : '' } break; case 2: urlObj = obj break; case 3: urlObj = obj break; default: break; } return urlObj } const [monthData, setMonthData] = useState([]) const getMonthData = async (params) => { try { const res = await httppost2(apiurl.gcyx.tjbb.swybb, params) if (res.code == 200) { const silceData = res.data.slice(-3) setData(res.data.slice(0,-3)) setMonthData(silceData) } } catch (error) { console.log(error); } } const formatDate = (date1) => { const date = new Date(date1); const year = date.getFullYear(); const month = date.getMonth() + 1; const day = date.getDate(); const res = searchVal.type1 == 0 ? year + '年' + month + '月' + day + '日' : searchVal.type1 == 1 ? year + '年' : year + '年' + month + '月' return res; } useEffect(() => { if (searchVal && searchVal.type1 != 1 && searchVal.type1 != 2) { const params = { ...searchVal } setRequsetUrlObj(getUrl(searchVal.type, searchVal.type1)) requsetUrlObjRef.current = getUrl(searchVal.type, searchVal.type1) getData(params) } if (searchVal.type1 == 1) { getStaData(searchVal) } if (searchVal.type1 == 2) { getMonthData(searchVal) } }, [searchVal]) return ( <>