Devicestatus.jsx 6.86 KB
Newer Older
左玲玲's avatar
左玲玲 committed
1 2 3 4 5 6 7 8 9 10 11 12
import React, { useState, useMemo } from 'react';
import styles from "./index.less";
import { useRequest } from 'ahooks';
import { doFetch } from '@/utils/doFetch';
import { DatePicker, Button } from "antd";
import dayjs from 'dayjs';
import AutoTable from '@/components/AutoTable/mtable';
import ReactEcharts from 'echarts-for-react';
import TreeRender from '@/components/TreeRender';
const { RangePicker } = DatePicker;
const statusColors = {
    报警: '#fa4659',
左玲玲's avatar
左玲玲 committed
13 14 15
    工作: '#7ac143',
    待机: '#00bce4',
    关机: '#6a737b'
左玲玲's avatar
左玲玲 committed
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
},
    format = 'YYYY-MM-DD HH:mm:ss';
const Devicestatus = ({ drawer }) => {
    const [dateTimeRange, cdate] = useState([dayjs().subtract(24, 'hours').format(format), dayjs().format(format)]),
        [deviceId, cdv] = useState('');
    const device = useRequest(async () => {
        let res = await doFetch({ url: "/lease/umLeaseLedger/querySelectbox", params: {} });
        cdv(res?.data?.dataList?.[0]?.value ?? '');
        return res?.data?.dataList?.map((it) => {
            return {
                title: it.label,
                key: it.value,
                children: []
            }
        }) ?? [];
    }),
        chartdata = useRequest(async () => {
            if (deviceId) {
                let res = await doFetch({ url: "/lease/umLeaseLedger/queryEquipmentStatus", params: { dateTimeRange, id: deviceId } });
                return res?.data ?? {};
            } else {
                return {}
            }

        }, {
            refreshDeps: [dateTimeRange, deviceId]
        });

    const onselecteTree = (selectedKeys, e, alldata) => {
        cdv(e.node.key);
    };

    const options = useMemo(() => {
        let data = chartdata?.data?.data?.datalist ?? [];
        const xAxisData = [];
        const seriesData = [];
        data.forEach((item) => {
            xAxisData.push(item.start);
            xAxisData.push(item.end);
            const statusColor = statusColors[item.status];
            seriesData.push([item.start, item.status, statusColor]);
            seriesData.push([item.end, item.status, statusColor]);
        });

        const option = {
            tooltip: {
                formatter: (params) => {
                }
            },
            xAxis: {
                type: 'category',
                name: '小时',
                boundaryGap: false,
                data: chartdata?.data?.data?.timeList ?? []
            },
            yAxis: {
                type: 'category',
                data: ['报警', '工作', '待机', '关机']
            },
            series: [
                {
                    type: 'line',
                    step: 'start',
                    lineStyle: {
                        width: 2,
                        type: 'dotted'
                    },
                    data: seriesData.map((item) => ({
                        value: [item[0], item[1]],
                        itemStyle: {
                            color: item[2],
                            borderType: 'solid'
                        },
                        symbol:
                            item[2] == '#fa4659'
                                ? 'triangle'
                                : item[2] == '#00fff5'
                                    ? 'rect'
                                    : '',
                        symbolSize: 12
                    }))
                }
            ]
        };
        return option;
    }, [chartdata?.data]);
    return <div style={{ display: "flex", justifyContent: "space-between", position: "relative" }}>
        <div style={{ width: 300, padding: '0 16px', flexShrink: 0 }}>
            <TreeRender dataSource={device?.data} onselected={onselecteTree} noaction={true} isSelectFirst={true} maxWidth={240} />
        </div>
        <div style={{ padding: 16, flex: 1, borderLeft: '1px solid #f0f0f0' }}>
            <div style={{ marginBottom: 30 }}>
                <RangePicker
                    showTime={{
                        format: 'HH:mm:ss',
                    }}
                    format={format}
                    onChange={(val, dateString) => {

                    }}
                    value={dateTimeRange?.length > 0 ? [dayjs(dateTimeRange?.[0]), dayjs(dateTimeRange?.[1])] : []}
                />
                <Button onClick={() => {
                    chartdata.run()
                }} type="primary" style={{ marginLeft: 15 }}>查询</Button>
            </div>
            <div style={{ display: 'flex' }}>
                <div className='statuspage' style={{ flex: 1, marginRight: 30 }}>
                    <AutoTable
                        columns={[
                            {
                                title: '采集时间',
                                dataIndex: 'start',
                                key: 'start',
                                search: false
                            },
                            {
                                title: '采集状态',
                                dataIndex: 'status',
                                key: 'status',
                                search: false
                            },
                        ]}
                        dataSource={chartdata?.data?.data?.datalist?.map((it, i) => {
                            return {
                                ...it,
                                id: i
                            }
                        }) ?? []}
                        resizeable={false}
                        pageextra="none"
                        options={false}
                    />
                </div>
                <div style={{ flex: 2 }}>
                    <div style={{ display: "flex", flexWrap: "wrap", gap: 15 }}>
                        {
                            Object?.keys(statusColors)?.map(it => {
左玲玲's avatar
左玲玲 committed
154
                                return <div key={it} style={{ width: '31%', display: 'flex', gap: 16, justifyContent: 'center' }} >
左玲玲's avatar
左玲玲 committed
155 156 157 158 159 160
                                    <div style={{ color: statusColors[it] }}>{it}时长</div>
                                    <div>{chartdata?.data?.data?.rateList?.filter(item => item.name == it)?.[0]?.time ?? 0}小时</div>
                                    <div>{chartdata?.data?.data?.rateList?.filter(item => item.name == it)?.[0]?.rate ?? 0}%</div>
                                </div>
                            })
                        }
左玲玲's avatar
左玲玲 committed
161
                        <div style={{ width: '31%', display: 'flex', gap: 16, justifyContent: 'center' }} >
左玲玲's avatar
左玲玲 committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175
                            <div>总时长</div>
                            <div>{chartdata?.data?.data?.timeRange}小时</div>
                        </div>
                    </div>
                    <ReactEcharts
                        style={{ height: 600, width: '100%' }}
                        option={options}
                    />
                </div>
            </div>
        </div>
    </div>
}
export default Devicestatus;