package com.zc.business.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.annotation.Excel;
import com.zc.business.enums.NetworkQuality;
import org.springframework.format.annotation.DateTimeFormat;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;

public class OnlineSum implements java.io.Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;//id
    private Long deviceId;//设备ID
    @Excel(name = "设备名称")
    private String deviceName;//设备名称
    @Excel(name = "IP")
    private String deviceIp;//设备IP
    @Excel(name = "桩号")
    private String stakeMark;//设备桩号
    private String direction;//方向
    private String deviceType;//设备类型
    @Excel(name = "设备状态", dictType = "dc_device_state")
    private String deviceStatus;//当前设备状态?
    @Excel(name = "使用状态", dictType = "dc_device_use")
    private String useState;
    private int totalCount;//当天总次数
    @Excel(name = "在线率(%)")
    private double onlineRate;//在线率
    @Excel(name = "离线率(%)")
    private double offlineRate;//离线率
    @Excel(name = "丢包率(%)")
    private double lossRate;//丢包率
    @Excel(name = "平均时延(ms)")
    private double rttAvg;//平均往返时延
    @Excel(name = "网络质量")
    private String networkQuality;//网络质量
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "统计日期", width = 30, dateFormat = "yyyy-MM-dd")
    private LocalDate statisticalDate;//统计日期

    private int sendCount;//发送数据包总数
    private int receiveCount;//返回数
    private int lossCount;//丢包数
    private int totalOnlineCount;//当天总在线次数
    private int totalOfflineCount;//当天总离线次数
    private Long roadId;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime lastOnlineTime;//最后在线时间

    public String getUseState() {
        return useState;
    }

    public void setUseState(String useState) {
        this.useState = useState;
    }


    public Long getRoadId() {
        return roadId;
    }

    public void setRoadId(Long roadId) {
        this.roadId = roadId;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(Long deviceId) {
        this.deviceId = deviceId;
    }

    public String getDeviceName() {
        return deviceName;
    }

    public void setDeviceName(String deviceName) {
        this.deviceName = deviceName;
    }

    public String getDeviceIp() {
        return deviceIp;
    }

    public void setDeviceIp(String deviceIp) {
        this.deviceIp = deviceIp;
    }

    public String getStakeMark() {
        return stakeMark;
    }

    public void setStakeMark(String stakeMark) {
        this.stakeMark = stakeMark;
    }

    public String getDeviceStatus() {
        return deviceStatus;
    }

    public void setDeviceStatus(String deviceStatus) {
        this.deviceStatus = deviceStatus;
    }


    public double getRttAvg() {
        return rttAvg;
    }

    public void setRttAvg(double rttAvg) {
        this.rttAvg = rttAvg;
    }

    public String getNetworkQuality() {
        return networkQuality;
    }

    public void setNetworkQuality(String networkQuality) {
        this.networkQuality = networkQuality;
    }

    public LocalDate getStatisticalDate() {
        return statisticalDate;
    }

    public void setStatisticalDate(LocalDate statisticalDate) {
        this.statisticalDate = statisticalDate;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }


    public LocalDateTime getLastOnlineTime() {
        return lastOnlineTime;
    }

    public void setLastOnlineTime(LocalDateTime lastOnlineTime) {
        this.lastOnlineTime = lastOnlineTime;
    }

    public String getDirection() {
        return direction;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }

    public String getDeviceType() {
        return deviceType;
    }

    public void setDeviceType(String deviceType) {
        this.deviceType = deviceType;
    }

    public double getOnlineRate() {
        return onlineRate;
    }

    public void setOnlineRate(double onlineRate) {
        this.onlineRate = onlineRate;
    }

    public double getOfflineRate() {
        return offlineRate;
    }

    public void setOfflineRate(double offlineRate) {
        this.offlineRate = offlineRate;
    }

    public double getLossRate() {
        return lossRate;
    }

    public void setLossRate(double lossRate) {
        this.lossRate = lossRate;
    }

    public int getSendCount() {
        return sendCount;
    }

    public void setSendCount(int sendCount) {
        this.sendCount = sendCount;
    }

    public int getReceiveCount() {
        return receiveCount;
    }

    public void setReceiveCount(int receiveCount) {
        this.receiveCount = receiveCount;
    }

    public int getLossCount() {
        return lossCount;
    }

    public void setLossCount(int lossCount) {
        this.lossCount = lossCount;
    }

    public int getTotalOnlineCount() {
        return totalOnlineCount;
    }

    public void setTotalOnlineCount(int totalOnlineCount) {
        this.totalOnlineCount = totalOnlineCount;
    }

    public int getTotalOfflineCount() {
        return totalOfflineCount;
    }

    public void setTotalOfflineCount(int totalOfflineCount) {
        this.totalOfflineCount = totalOfflineCount;
    }

    @Override
    public String toString() {
        return "OnlineSum{" +
                "id=" + id +
                ", deviceId=" + deviceId +
                ", deviceName='" + deviceName + '\'' +
                ", deviceIp='" + deviceIp + '\'' +
                ", stakeMark='" + stakeMark + '\'' +
                ", direction='" + direction + '\'' +
                ", deviceType='" + deviceType + '\'' +
                ", deviceStatus='" + deviceStatus + '\'' +
                ", totalCount=" + totalCount +
                ", totalOnlineCount=" + totalOnlineCount +
                ", totalOfflineCount=" + totalOfflineCount +
                ", onlineRate='" + onlineRate + '\'' +
                ", offlineRate='" + offlineRate + '\'' +
                ", sendCount=" + sendCount +
                ", receiveCount=" + receiveCount +
                ", lossCount=" + lossCount +
                ", lossRate='" + lossRate + '\'' +
                ", rttAvg=" + rttAvg +
                ", networkQuality='" + networkQuality + '\'' +
                ", lastOnlineTime=" + lastOnlineTime +
                ", statisticalDate=" + statisticalDate +
                '}';
    }

    /**
     * 复制日志数据
     * @param onlineLog 日志数据
     * @return @see OnlineSum
     */
    public OnlineSum copyFromLog(OnlineLog onlineLog) {
        this.deviceId = onlineLog.getDeviceId();
        this.deviceName = onlineLog.getDeviceName();
        this.deviceIp = onlineLog.getDeviceIp();
        this.stakeMark = onlineLog.getStakeMark();
        this.direction = onlineLog.getDirection();
        this.deviceType = onlineLog.getDeviceType();
        this.deviceStatus = onlineLog.getDeviceStatus();
        this.totalCount = 1;
        this.totalOnlineCount = onlineLog.getDeviceStatus().equals(DcDevice.ONLINE)?1:0;
        this.totalOfflineCount = onlineLog.getDeviceStatus().equals(DcDevice.OFFLINE)?1:0;
        this.onlineRate = (double) this.totalOnlineCount/this.totalCount*100;
        this.offlineRate = (double) this.totalOfflineCount/this.totalCount*100;
        this.sendCount = onlineLog.getSendCount();
        this.receiveCount = onlineLog.getReceiveCount();
        this.lossCount = onlineLog.getLossCount();
        this.lossRate = onlineLog.getLossRate();
        this.rttAvg = onlineLog.getRttAvg();
        this.networkQuality = onlineLog.getNetworkQuality();
        if(this.deviceStatus.equals(DcDevice.ONLINE))
            this.lastOnlineTime = onlineLog.getMonitorTime();
        this.statisticalDate = onlineLog.getMonitorTime().toLocalDate();
        return this;
    }

    /**
     *  统计汇总
     * @param onlineLog     日志数据
     * @return @see OnlineSum
     */
    public OnlineSum incrementSummary(OnlineLog onlineLog) {
        //总rtt时间
        double rtt = this.rttAvg*this.receiveCount+onlineLog.getRttAvg()*onlineLog.getReceiveCount();
        //更新设备在线状态
        this.deviceStatus = onlineLog.getDeviceStatus();
        this.totalCount += 1;
        this.totalOnlineCount += onlineLog.getDeviceStatus().equals(DcDevice.ONLINE)?1:0;
        this.totalOfflineCount += onlineLog.getDeviceStatus().equals(DcDevice.OFFLINE)?1:0;
        this.onlineRate = (this.totalCount==0?0:(double)this.totalOnlineCount/this.totalCount)*100;
        this.offlineRate = (this.totalCount==0?0:(double)this.totalOfflineCount/this.totalCount)*100;
        this.sendCount += onlineLog.getSendCount();
        this.receiveCount += onlineLog.getReceiveCount();
        this.lossCount += onlineLog.getLossCount();
        this.lossRate = (this.sendCount==0?0:(double)this.lossCount/this.sendCount)*100;
        BigDecimal bd = new BigDecimal(Double.toString(this.receiveCount==0?0:rtt/this.receiveCount));
        bd = bd.setScale(2, RoundingMode.HALF_UP);
        this.rttAvg =bd.doubleValue();
        this.networkQuality = NetworkQuality.ofNetworkQuality(this.offlineRate,this.rttAvg);
        this.lastOnlineTime = onlineLog.getMonitorTime();
        return this;
    }
}