package com.jjt.calc.domain;

import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.jjt.common.annotation.Excel;
import com.jjt.common.core.domain.BaseEntity;
import com.jjt.common.utils.DateUtils;
import com.jjt.utils.Tools;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.poi.ss.usermodel.Cell;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

/**
 * 日统计数据对象 TWIN_CALC_DAY
 *
 * @author wukai
 * @date 2025-01-18
 */
@ApiModel(value = "TwinCalcDay", description = "日统计数据")
@Data
@NoArgsConstructor
public class TwinCalcDay extends BaseEntity {
    private static final long serialVersionUID = 1L;

    public TwinCalcDay(Date date) {
        this.time = date;
        this.length = BigDecimal.ZERO;
        this.weight = BigDecimal.ZERO;
        this.kwh = BigDecimal.ZERO;

        this.lengthA = BigDecimal.ZERO;
        this.weightA = BigDecimal.ZERO;
        this.kwhA = BigDecimal.ZERO;

        this.lengthB = BigDecimal.ZERO;
        this.weightB = BigDecimal.ZERO;
        this.kwhB = BigDecimal.ZERO;

        this.alarm = 0L;
        this.openTimeA = BigDecimal.ZERO;
        this.openTimeB = BigDecimal.ZERO;
        this.closeTimeA = BigDecimal.ZERO;
        this.closeTimeB = BigDecimal.ZERO;
        this.stop1A = 0L;
        this.stop1B = 0L;
        this.stop2A = 0L;
        this.stop2B = 0L;
        this.stop3A = 0L;
        this.stop3B = 0L;

        this.setEfficiencyA(BigDecimal.ZERO);
        this.setEfficiencyB(BigDecimal.ZERO);
    }

    /**
     * ID
     */
    @ApiModelProperty("ID")
    @TableId
    private Long id;

    /**
     * 时间
     */
    @ApiModelProperty("时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Excel(name = "时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date time;

    /**
     * 设备ID
     */
    @ApiModelProperty("设备ID")
    @Excel(name = "设备ID")
    private Long deviceId;

    /**
     * 总米长
     */
    @ApiModelProperty("总米长")
    @Excel(name = "总米长")
    private BigDecimal length;

    /**
     * 总重量
     */
    @ApiModelProperty("总重量")
    @Excel(name = "总重量")
    private BigDecimal weight;

    /**
     * 稼动率
     */
    @ApiModelProperty("稼动率")
    @Excel(name = "稼动率")
    private BigDecimal efficiency;

    /**
     * 总电量
     */
    @ApiModelProperty("总电量")
    private BigDecimal kwh;

    /**
     * 告警数量
     */
    @ApiModelProperty("告警数量")
    private Long alarm;

    /**
     * A班米长
     */
    @ApiModelProperty("A班米长")
    @Excel(name = "A班米长")
    private BigDecimal lengthA;

    /**
     * A班重量
     */
    @ApiModelProperty("A班重量")
    @Excel(name = "A班重量")
    private BigDecimal weightA;

    /**
     * A班稼动率
     */
    @ApiModelProperty("A班稼动率")
    @Excel(name = "A班稼动率")
    private BigDecimal efficiencyA;

    /**
     * A班开机时间
     */
    @ApiModelProperty("A班开机时间")
    @Excel(name = "A班开机时间")
    private BigDecimal openTimeA;

    /**
     * A班停机时间
     */
    @ApiModelProperty("A班停机时间")
    @Excel(name = "A班停机时间")
    private BigDecimal closeTimeA;

    /**
     * A班电量
     */
    @ApiModelProperty("A班电量")
    private BigDecimal kwhA;

    /**
     * A班停经片停机
     */
    @ApiModelProperty("A班停经片停机")
    @Excel(name = "A班停经片停机")
    private Long stop1A;

    /**
     * A班CCD停机
     */
    @ApiModelProperty("A班CCD停机")
    @Excel(name = "A班CCD停机")
    private Long stop2A;

    /**
     * A班人工停机
     */
    @ApiModelProperty("A班人工停机")
    @Excel(name = "A班人工停机")
    private Long stop3A;

    /**
     * B班米长
     */
    @ApiModelProperty("B班米长")
    @Excel(name = "B班米长")
    private BigDecimal lengthB;

    /**
     * B班重量
     */
    @ApiModelProperty("B班重量")
    @Excel(name = "B班重量")
    private BigDecimal weightB;

    /**
     * B班稼动率
     */
    @ApiModelProperty("B班稼动率")
    @Excel(name = "B班稼动率")
    private BigDecimal efficiencyB;

    /**
     * B班开机时间
     */
    @ApiModelProperty("B班开机时间")
    @Excel(name = "B班开机时间")
    private BigDecimal openTimeB;

    /**
     * B班停机时间
     */
    @ApiModelProperty("B班停机时间")
    @Excel(name = "B班停机时间")
    private BigDecimal closeTimeB;

    /**
     * B班电量
     */
    @ApiModelProperty("B班电量")
    private BigDecimal kwhB;

    /**
     * B班停经片停机
     */
    @ApiModelProperty("B班停经片停机")
    @Excel(name = "B班停经片停机")
    private Long stop1B;

    /**
     * B班CCD停机
     */
    @ApiModelProperty("B班CCD停机")
    @Excel(name = "B班CCD停机")
    private Long stop2B;

    /**
     * B班人工停机
     */
    @ApiModelProperty("B班人工停机")
    @Excel(name = "B班人工停机")
    private Long stop3B;

    /**
     * 创建人
     */
    @ApiModelProperty("创建人")
    @Excel(name = "创建人")
    private String createdBy;

    /**
     * 创建时间
     */
    @ApiModelProperty("创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Excel(name = "创建时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date createdTime;

    /**
     * 更新人
     */
    @ApiModelProperty("更新人")
    @Excel(name = "更新人")
    private String updatedBy;

    /**
     * 更新时间
     */
    @ApiModelProperty("更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Excel(name = "更新时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date updatedTime;
    @ApiModelProperty("开机率")
    private Double runningRatio;


    /**
     * 计算产量数据
     *
     * @param hours 1小时统计数据
     */
    public void calcHours(List<TwinCalcHour> hours) {
        hours.forEach(hour -> {
            this.length = length.add(hour.getLength());
            this.weight = weight.add(hour.getWeight());
            this.kwh = kwh.add(hour.getKwh());
            if (hour.getHour() >= 7 && hour.getHour() < 19) {
                //A班
                this.lengthA = lengthA.add(hour.getLength());
                this.weightA = weightA.add(hour.getWeight());
                this.kwhA = kwhA.add(hour.getKwh());
                this.openTimeA = openTimeA.add(BigDecimal.valueOf(hour.getOpenTime()));
                this.closeTimeA = closeTimeA.add(BigDecimal.valueOf(hour.getCloseTime()));
            } else {
                //B班
                this.lengthB = lengthB.add(hour.getLength());
                this.weightB = weightB.add(hour.getWeight());
                this.kwhB = kwhB.add(hour.getKwh());
                this.openTimeB = openTimeB.add(BigDecimal.valueOf(hour.getOpenTime()));
                this.closeTimeB = closeTimeB.add(BigDecimal.valueOf(hour.getCloseTime()));
            }
        });
    }

    /**
     * 计算天统计数据
     *
     * @param days 天数据
     */
    public void calcDays(List<TwinCalcDay> days) {
        days.forEach(day -> {
            this.weight = weight.add(day.getWeight());
            this.weightA = weightA.add(day.getWeightA());
            this.weightB = weightB.add(day.getWeightB());

            this.length = length.add(day.getLength()).setScale(2, RoundingMode.HALF_UP);
            this.lengthA = lengthA.add(day.getLengthA()).setScale(2, RoundingMode.HALF_UP);
            this.lengthB = lengthB.add(day.getLengthB()).setScale(2, RoundingMode.HALF_UP);

            this.kwh = kwh.add(day.getKwh());
            this.kwhA = kwhA.add(day.getKwhA());
            this.kwhB = kwhB.add(day.getKwhB());

            this.stop1A += day.getStop1A();
            this.stop1B += day.getStop1B();
            this.stop2A += day.getStop2A();
            this.stop2B += day.getStop2B();
            this.stop3A += day.getStop3A();
            this.stop3B += day.getStop3B();

            this.alarm += day.getAlarm();
            this.closeTimeA = closeTimeA.add(day.getCloseTimeA());
            this.closeTimeB = closeTimeB.add(day.getCloseTimeB());
            this.openTimeA = openTimeA.add(day.getOpenTimeA());
            this.openTimeB = openTimeB.add(day.getOpenTimeB());
        });
        BigDecimal teamTimeA = this.openTimeA.add(this.closeTimeA);
        BigDecimal teamTimeB = this.openTimeB.add(this.closeTimeB);
        BigDecimal totalTeamTime = teamTimeA.add(teamTimeB);
        BigDecimal totalOpenTime = this.openTimeA.add(openTimeB);
        if (teamTimeA.intValue() == 0) {
            this.efficiencyA = BigDecimal.ZERO;
        } else {
            this.efficiencyA = openTimeA.divide(teamTimeA, 4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
        }
        if (teamTimeB.intValue() == 0) {
            this.efficiencyB = BigDecimal.ZERO;
        } else {
            this.efficiencyB = openTimeB.divide(teamTimeB, 4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
        }
        if (totalTeamTime.intValue() == 0) {
            this.efficiency = BigDecimal.ZERO;
        } else {
            this.efficiency = totalOpenTime.divide(totalTeamTime, 4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
        }
    }

    public void setCells(Cell[] cells) {
        //设备名称
        cells[0].setCellValue(this.getRemark());
        //时间
        cells[1].setCellValue(DateUtils.parseDateToStr(this.getTime()));
        //总长
        cells[2].setCellValue(this.getLengthA().add(this.getLengthB()).floatValue());
        //总重
        cells[3].setCellValue(this.getWeightA().add(this.getWeightB()).floatValue());
        //总稼动率
        BigDecimal totalOpenTime = this.getOpenTimeA().add(this.getOpenTimeB());
        BigDecimal totalCloseTime = this.getCloseTimeA().add(this.getCloseTimeB());
        cells[4].setCellValue(Tools.calcPercent(totalOpenTime, totalCloseTime).floatValue());
        // 总电量
        cells[5].setCellValue(this.getKwh().floatValue());
        //告警次数
        cells[6].setCellValue(this.getAlarm());
        //A班米长
        cells[7].setCellValue(this.getLengthA().floatValue());
        //A班重量
        cells[8].setCellValue(this.getWeightA().floatValue());
        //A班开机时间
        cells[9].setCellValue(this.getOpenTimeA().floatValue());
        //A班停机时间
        cells[10].setCellValue(this.getCloseTimeA().floatValue());
        //A班稼动率
        cells[11].setCellValue(Tools.calcPercent(this.getOpenTimeA(), this.getCloseTimeA()).floatValue());
        //A班电量
        cells[12].setCellValue(this.getKwhA().floatValue());
        //A班停经片停机次数
        cells[13].setCellValue(this.getStop1A());
        //A班CCD停机次数
        cells[14].setCellValue(this.getStop2A());
        //A班人工停机次数
        cells[15].setCellValue(this.getStop3A());
        //B班米长
        cells[16].setCellValue(this.getLengthB().floatValue());
        //B班重量
        cells[17].setCellValue(this.getWeightB().floatValue());
        //B班开机时间
        cells[18].setCellValue(this.getOpenTimeB().floatValue());
        //B班停机时间
        cells[19].setCellValue(this.getCloseTimeB().floatValue());
        //B班稼动率
        cells[20].setCellValue(Tools.calcPercent(this.getOpenTimeB(), this.getCloseTimeB()).floatValue());
        //B班电量
        cells[21].setCellValue(this.getKwhB().floatValue());
        //B班停经片停机次数
        cells[22].setCellValue(this.getStop1B());
        //B班CCD停机次数
        cells[23].setCellValue(this.getStop2B());
        //B班人工停机次数
        cells[24].setCellValue(this.getStop3B());
    }

}
