package com.jjt.biz.vo;

import com.jjt.utils.Tools;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 能耗趋势视图
 *
 * @author ruoyi
 * @date 2024-12-30
 */
@ApiModel(value = "YrEnergyTradeVO", description = "印染能耗趋势视图")
@Data
public class YrEnergyTradeVO {
    @ApiModelProperty("时间")
    private String date;
    @ApiModelProperty("原始时间")
    private String tips;
    @ApiModelProperty("各产线数据")
    private List<Line> line;

    public void split(BigDecimal price, BigDecimal d, BigDecimal stream, BigDecimal water) {
        BigDecimal[] avg = Tools.balancedRandomSplit(8);
        this.line = new ArrayList<>();
        Line line = new Line(0);
        line.setPrice(isNull(price).setScale(2, RoundingMode.HALF_UP));
        line.setElectricity(isNull(d));
        line.setSteam(isNull(stream));
        line.setWater(isNull(water));
        this.line.add(line);
        for (int i = 0; i < avg.length; i++) {
            Line line1 = new Line(i);
            line1.setPrice(isNull(price).multiply(BigDecimal.valueOf(8)).multiply(avg[i]).setScale(2, RoundingMode.HALF_UP));
            line1.setElectricity(isNull(d).multiply(avg[i]).setScale(2, RoundingMode.HALF_UP));
            line1.setSteam(isNull(stream).multiply(avg[i]).setScale(2, RoundingMode.HALF_UP));
            line1.setWater(isNull(water).multiply(avg[i]).setScale(2, RoundingMode.HALF_UP));
            this.line.add(line1);
        }
    }

    private BigDecimal isNull(BigDecimal v) {
        return v != null ? v : BigDecimal.ZERO;
    }

    @Data
    @NoArgsConstructor
    public static class Line {
        @ApiModelProperty("产线编号，为0时代表全部")
        private Integer line;
        @ApiModelProperty("电")
        private BigDecimal electricity;
        @ApiModelProperty("汽")
        private BigDecimal steam;
        @ApiModelProperty("水")
        private BigDecimal water;
        @ApiModelProperty("价格")
        private BigDecimal price;

        public Line(Integer line) {
            this.line = line;
        }

        public void mock() {
            Random random = new Random();
            double price = 2.0 + random.nextDouble();
            this.price = BigDecimal.valueOf(price).setScale(1, RoundingMode.HALF_UP);
            double electricity = 5000 + 1000 * random.nextDouble();
            this.electricity = BigDecimal.valueOf(electricity).setScale(1, RoundingMode.HALF_UP);
            double steam = 40 + 20 * random.nextDouble();
            this.steam = BigDecimal.valueOf(steam).setScale(1, RoundingMode.HALF_UP);
            double water = 40 + 20 * random.nextDouble();
            this.water = BigDecimal.valueOf(water).setScale(1, RoundingMode.HALF_UP);
        }
    }

    public void mock() {
        this.line = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            YrEnergyTradeVO.Line line = new YrEnergyTradeVO.Line(i);
            line.mock();
            this.line.add(line);
        }
    }
}
