package com.jjt.biz.vo;

import com.jjt.calc.domain.TwinCalcDayYhj;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 生产趋势视图
 *
 * @author ruoyi
 * @date 2024-12-30
 */
@ApiModel(value = "YrProdTradeVO", description = "印染生产趋势视图")
@Data
public class YrProdTradeVO {
    @ApiModelProperty("时间")
    private String date;

    @ApiModelProperty("原始时间")
    private String tips;

    @ApiModelProperty("各产线数据")
    private List<Line> line;

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

    public void split(List<TwinCalcDayYhj> yhjList, BigDecimal length, BigDecimal weight) {
        Map<Integer, TwinCalcDayYhj> yhjMap = new HashMap<>();
        if (yhjList != null) {
            yhjMap = yhjList.stream()
                    .collect(Collectors.toMap(
                            TwinCalcDayYhj::getDeviceId,
                            item -> item,
                            (existing, replacement) -> existing
                    ));
        }
        this.line = new ArrayList<>();
        Line line = new Line(0);
        line.setLength(isNull(length));
        line.setWeight(isNull(weight));
        this.line.add(line);
        for (int i = 1; i <= 8; i++) {
            Line line1 = new Line(i);
            TwinCalcDayYhj yhj = yhjMap.get(i);
            if (yhj != null) {
                line1.setLength(BigDecimal.valueOf(yhj.getLength()));
                line1.setWeight(yhj.getWeight());
            } else {
                line1.setLength(BigDecimal.ZERO);
                line1.setWeight(BigDecimal.ZERO);
            }
            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 length;
        @ApiModelProperty("重量")
        private BigDecimal weight;

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

        public void mock() {
            Random random = new Random();
            this.length = BigDecimal.valueOf(3200 + random.nextInt(1300));
            this.weight = BigDecimal.valueOf(3200 + random.nextInt(1300));
        }
    }

    public static void main(String[] args) {
        YrProdTradeVO.Line line1 = new YrProdTradeVO.Line();

    }
}
