package com.jjt.biz.vo;

import com.jjt.biz.domain.TwinDevice;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * 周曲线
 *
 * @author wukai
 * @date 2024/5/8 15:50
 */
@ApiModel(value = "FormulaTotal", description = "配方-统计数据")
@Data
@NoArgsConstructor
public class FormulaDetail {
    @ApiModelProperty("设备信息")
    private String device;

    @ApiModelProperty("设备ID")
    private Long deviceId;
    @ApiModelProperty("配方号")
    private Integer formula_data_1;
    @ApiModelProperty("机台号")
    private Integer formula_data_2;
    @ApiModelProperty("米克重")
    private Integer formula_data_3;
    @ApiModelProperty("L1、L5纱线规格D")
    private Integer formula_data_4;
    @ApiModelProperty("L2、L4纱线规格D")
    private Integer formula_data_5;
    @ApiModelProperty("L3纱线规格D")
    private Integer formula_data_6;
    @ApiModelProperty("L1、L5纱线规格F")
    private Integer formula_data_7;
    @ApiModelProperty("L2、L4纱线规格F")
    private Integer formula_data_8;
    @ApiModelProperty("L3纱线规格F")
    private Integer formula_data_9;
    @ApiModelProperty("L1、L5盘头根数")
    private Integer formula_data_10;
    @ApiModelProperty("L2、L4盘头根数")
    private Integer formula_data_11;
    @ApiModelProperty("L3盘头根数")
    private Integer formula_data_12;
    @ApiModelProperty("卷曲幅宽")
    private Float formula_data_13;
    @ApiModelProperty("下布长度")
    private Float formula_data_14;
    @ApiModelProperty("毛高")
    private Float formula_data_15;
    @ApiModelProperty("成品长")
    private Float formula_data_16;
    @ApiModelProperty("成品宽")
    private Float formula_data_17;
    @ApiModelProperty("成品重")
    private Float formula_data_18;
    @ApiModelProperty("GB1送经量")
    private String formula_data_19;
    @ApiModelProperty("GB2送经量")
    private String formula_data_20;
    @ApiModelProperty("GB3送经量")
    private String formula_data_21;
    @ApiModelProperty("GB4送经量")
    private String formula_data_22;
    @ApiModelProperty("GB5送经量")
    private String formula_data_23;
    @ApiModelProperty("牵拉密度")
    private Float formula_data_24;
    @ApiModelProperty("卷曲张力系数")
    private Float formula_data_25;
    @ApiModelProperty("GB1盘头剩余周长")
    private String capacity_data_10;
    @ApiModelProperty("GB2盘头剩余周长")
    private String capacity_data_11;
    @ApiModelProperty("GB3盘头剩余周长")
    private String capacity_data_12;
    @ApiModelProperty("GB4盘头剩余周长")
    private String capacity_data_13;
    @ApiModelProperty("GB5盘头剩余周长")
    private String capacity_data_14;
    @ApiModelProperty("GB1盘头剩余圈数")
    private String capacity_data_15;
    @ApiModelProperty("GB2盘头剩余圈数")
    private String capacity_data_16;
    @ApiModelProperty("GB3盘头剩余圈数")
    private String capacity_data_17;
    @ApiModelProperty("GB4盘头剩余圈数")
    private String capacity_data_18;
    @ApiModelProperty("GB5盘头剩余圈数")
    private String capacity_data_19;
    @ApiModelProperty("纱线规格D")
    private String yarnD;
    @ApiModelProperty("纱线规格D底纱")
    private String yarnDd;
    @ApiModelProperty("纱线规格F")
    private String yarnF;
    @ApiModelProperty("盘头根数信息")
    private String yarnPt;
    public FormulaDetail(boolean isSmall, Map<String, Object> map) {

        TwinDevice twinDevice = (TwinDevice) map.get("device");
        this.deviceId = twinDevice.getDeviceId();
        this.device = twinDevice.getDeviceName();
        if (isSmall) {
            setSmall(map);
        } else {
            setBig(map);
        }
        this.yarnDd = this.formula_data_4 + "/" + formula_data_5;
        this.yarnD = this.formula_data_4 + "/" + formula_data_5 + "/" + formula_data_6;
        this.yarnF = this.formula_data_7 + "/" + formula_data_8 + "/" + formula_data_9;
        this.yarnPt = this.formula_data_10 + "/" + formula_data_11 + "/" + formula_data_12 + "/" + formula_data_15;

    }

    private void setSmall(Map<String, Object> map) {
        // 整数类型字段赋值
        int[] intFields = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
        for (int i : intFields) {
            int value = (int) map.get("Formula_data_" + i);
            try {
                Field field = this.getClass().getDeclaredField("formula_data_" + i);
                field.set(this, value);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // 浮点类型字段赋值
        int[] floatFields = {13, 14, 15, 16, 17, 18, 24, 25};
        for (int i : floatFields) {
            float value = (float) map.get("Formula_data_" + i);
            try {
                Field field = this.getClass().getDeclaredField("formula_data_" + i);
                field.set(this, value);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // 字符串类型字段赋值(整数转字符串)
        int[] stringFormulaFields = {19, 20, 21, 22, 23};
        for (int i : stringFormulaFields) {
            try {
                int value = (int) map.get("Formula_data_" + i);
                this.getClass().getDeclaredField("formula_data_" + i).set(this, value + "");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // 剩余周长，剩余圈数
        int[] capacityFloatFields = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
        for (int i : capacityFloatFields) {
            try {
                Number value = (Number) map.get("Capacity_data_" + i);
                this.getClass().getDeclaredField("capacity_data_" + i).set(this, value.intValue() + "");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void setBig(Map<String, Object> map) {
        this.formula_data_1 = (int) map.get("Formula_data_41");
        this.formula_data_2 = (int) map.get("Formula_data_42");
        this.formula_data_3 = (int) map.get("Formula_data_43");
        this.formula_data_4 = (int) map.get("Formula_data_44");
        this.formula_data_5 = (int) map.get("Formula_data_45");
        this.formula_data_6 = (int) map.get("Formula_data_46");
        this.formula_data_7 = (int) map.get("Formula_data_47");
        this.formula_data_8 = (int) map.get("Formula_data_48");
        this.formula_data_9 = (int) map.get("Formula_data_49");
        this.formula_data_10 = (int) map.get("Formula_data_50");
        this.formula_data_11 = (int) map.get("Formula_data_51");
        this.formula_data_12 = (int) map.get("Formula_data_52");
        this.formula_data_13 = (float) map.get("Formula_data_53");
        this.formula_data_14 = (float) map.get("Formula_data_54");
        this.formula_data_15 = (float) map.get("Formula_data_55");
        this.formula_data_16 = (float) map.get("Formula_data_56");
        this.formula_data_17 = (float) map.get("Formula_data_57");
        this.formula_data_18 = (float) map.get("Formula_data_58");
        //送经量
        this.formula_data_19 = String.format("%d,%d",
                ((Number) map.get("Formula_data_1")).intValue(),
                ((Number) map.get("Formula_data_2")).intValue());
        this.formula_data_20 = String.format("%d,%d",
                ((Number) map.get("Formula_data_3")).intValue(),
                ((Number) map.get("Formula_data_4")).intValue());
        this.formula_data_21 = String.format("%d,%d",
                ((Number) map.get("Formula_data_5")).intValue(),
                ((Number) map.get("Formula_data_6")).intValue());
        this.formula_data_22 = String.format("%d,%d",
                ((Number) map.get("Formula_data_7")).intValue(),
                ((Number) map.get("Formula_data_8")).intValue());
        this.formula_data_23 = String.format("%d,%d",
                ((Number) map.get("Formula_data_9")).intValue(),
                ((Number) map.get("Formula_data_10")).intValue());
        // 优化处理剩余周长和剩余圈数赋值逻辑
        int[][] fieldConfigs = {
                {10, 11, 12, 13, 14},  // zcFields
                {3, 2},                 // zcIndex和步长
                {15, 16, 17, 18, 19},   // qsFields
                {15, 2}                 // qsIndex和步长
        };

        for (int config = 0; config < fieldConfigs.length; config += 2) {
            int[] fields = fieldConfigs[config];
            int startIndex = fieldConfigs[config + 1][0];
            int step = fieldConfigs[config + 1][1];

            for (int i = 0; i < fields.length; i++) {
                int fieldNum = fields[i];
                int index1 = startIndex + i * step;
                int index2 = index1 + 1;
                String value = String.format("%d,%d",
                        ((Number) map.get("Capacity_data_" + index1)).intValue(),
                        ((Number) map.get("Capacity_data_" + index2)).intValue());

                try {
                    Field field = this.getClass().getDeclaredField("capacity_data_" + fieldNum);
                    field.set(this, value);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        this.formula_data_24 = (float) map.get("Capacity_data_112");
//        int index = 10;
//        this.bigSj = new Float[index];
//        this.bigZc = new Float[index];
//        this.bigQs = new Integer[index];
//        for (int i = 0; i < index; i++) {
//            int sjIndex = 1;
//            bigSj[i] = (float) map.get("Formula_data_" + (i + sjIndex));
//            bigZc[i] = (float) map.get("Capacity_data_" + (i + zcIndex));
//            bigQs[i] = (int) map.get("Capacity_data_" + (i + qsIndex));
//        }
    }

    public void setCells(Cell[] cells) {
        cells[0].setCellValue(this.device);
        cells[1].setCellValue(this.formula_data_2);
        cells[2].setCellValue(this.formula_data_15);
        cells[3].setCellValue(this.formula_data_1);
        cells[4].setCellValue(this.formula_data_3);
        cells[5].setCellValue(this.formula_data_19);
        cells[6].setCellValue(this.formula_data_20);
        cells[7].setCellValue(this.formula_data_21);
        cells[8].setCellValue(this.formula_data_22);
        cells[9].setCellValue(this.formula_data_23);
        cells[10].setCellValue(this.formula_data_4);
        cells[11].setCellValue(this.formula_data_5);
        cells[12].setCellValue(this.formula_data_6);
        cells[13].setCellValue(this.formula_data_7);
        cells[14].setCellValue(this.formula_data_8);
        cells[15].setCellValue(this.formula_data_9);
        cells[16].setCellValue(this.formula_data_10);
        cells[17].setCellValue(this.formula_data_11);
        cells[18].setCellValue(this.formula_data_12);
        cells[19].setCellValue(this.formula_data_13);
        cells[20].setCellValue(this.formula_data_14);
        cells[21].setCellValue(this.formula_data_16);
        cells[22].setCellValue(this.formula_data_17);
        cells[23].setCellValue(this.formula_data_18);
        cells[24].setCellValue(this.formula_data_24);
        if (this.formula_data_25 != null) {
            cells[25].setCellValue(this.formula_data_25);
        }
    }


    /**
     * 设置纱线规格 表格
     *
     * @param cells cell
     * @param p2    格式
     */
    public void setYarnCells(Cell[] cells, CellStyle p2) {
        cells[0].setCellValue(this.formula_data_4);
        cells[1].setCellValue(this.formula_data_5);
        cells[2].setCellValue(this.formula_data_6);
        cells[3].setCellValue(this.device);
        cells[4].setCellValue(this.formula_data_2);
        cells[5].setCellValue(this.formula_data_15);
        cells[6].setCellValue(this.formula_data_1);
        cells[7].setCellValue(this.formula_data_3);
        cells[8].setCellValue(this.formula_data_13);
        cells[9].setCellValue(this.formula_data_14);
        cells[10].setCellValue(this.formula_data_16);
        cells[11].setCellValue(this.formula_data_17);
        cells[12].setCellValue(this.formula_data_18);
        cells[13].setCellValue(this.formula_data_24);
        if (this.formula_data_25 != null) {
            cells[14].setCellValue(this.formula_data_25);
        }

        cells[15].setCellValue(this.capacity_data_10);
        cells[16].setCellValue(this.capacity_data_11);
        cells[17].setCellValue(this.capacity_data_12);
        cells[18].setCellValue(this.capacity_data_13);
        cells[19].setCellValue(this.capacity_data_14);
        cells[20].setCellValue(this.capacity_data_15);
        cells[21].setCellValue(this.capacity_data_16);
        cells[22].setCellValue(this.capacity_data_17);
        cells[23].setCellValue(this.capacity_data_18);
        cells[24].setCellValue(this.capacity_data_19);

        cells[25].setCellValue(this.formula_data_19);
        cells[26].setCellValue(this.formula_data_20);
        cells[27].setCellValue(this.formula_data_21);
        cells[28].setCellValue(this.formula_data_22);
        cells[29].setCellValue(this.formula_data_23);
        cells[30].setCellValue(this.formula_data_4);
        cells[31].setCellValue(this.formula_data_5);
        cells[32].setCellValue(this.formula_data_6);
        cells[33].setCellValue(this.formula_data_7);
        cells[34].setCellValue(this.formula_data_8);
        cells[35].setCellValue(this.formula_data_9);
        cells[36].setCellValue(this.formula_data_10);
        cells[37].setCellValue(this.formula_data_11);
        cells[38].setCellValue(this.formula_data_12);

        cells[8].setCellStyle(p2);
        cells[13].setCellStyle(p2);
        cells[14].setCellStyle(p2);
        cells[15].setCellStyle(p2);
        cells[16].setCellStyle(p2);
        cells[17].setCellStyle(p2);
        cells[18].setCellStyle(p2);
        cells[19].setCellStyle(p2);
    }

}
