package cn.estsh.i3plus.ext.mes.pojo.model.bu3;

import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @DESCRIPTION:
 * @USER: wangjie
 * @DATE: 2022-11-01 15:54
 */
@Data
@ApiModel("非作业时间分析报表数据MODEL")
public class SxNonWorkingTimeAnalysisModel extends SxEChartsReportCommonDataModel implements Serializable {

    private static final long serialVersionUID = 5101759774632562164L;

    @ApiParam(value = "生产线代码")
    private String workCenterCodeArr;

    @ApiParam("产品别")
    private String categoryName;

    @ApiParam("计划比例")
    private String planRate;

    @ApiParam("作业时间")
    private String workTime;

    @ApiParam("当前月份")
    private String curMonth;

    @ApiParam("标题")
    public String title;

    @ApiParam("头部信息")
    private List<SxDataModel> headDataList;

    @ApiParam("各组别非作业比率")
    private List<SxDataModel> centerDataList;

    @ApiParam("损失别非作业比率")
    private List<SxDataModel> itemDataList;

    @ApiParam("各系别非作业时间比率")
    private List<SxDataModel> pieDataList;

    @ApiParam("非作业时间推移")
    private List<SxDataModel> barDataList;

    @ApiParam("表字段")
    private List<SxDataModel> tableNameList;

    @ApiParam("表数据")
    private List<Map<String, Object>> tableDataList;

    public SxNonWorkingTimeAnalysisModel title(String title) {
        this.title = title;
        return this;
    }

    public SxNonWorkingTimeAnalysisModel addHeadData(String key, String value) {
        if (CollectionUtils.isEmpty(this.headDataList)) this.headDataList = new LinkedList<>();
        this.headDataList.add(new SxDataModel(key, value));
        return this;
    }

    public SxNonWorkingTimeAnalysisModel addCenterData(String key, BigDecimal value) {
        if (CollectionUtils.isEmpty(this.centerDataList)) this.centerDataList = new LinkedList<>();
        this.centerDataList.add(new SxDataModel(key, value).valueToSort(value.doubleValue()));
        return this;
    }

    public SxNonWorkingTimeAnalysisModel addItemData(String key, BigDecimal value) {
        if (CollectionUtils.isEmpty(this.itemDataList)) this.itemDataList = new LinkedList<>();
        this.itemDataList.add(new SxDataModel(key, value).valueToSort(value.doubleValue()));
        return this;
    }

    public SxNonWorkingTimeAnalysisModel addPieData(String key, BigDecimal value, String value2) {
        if (CollectionUtils.isEmpty(this.pieDataList)) this.pieDataList = new LinkedList<>();
        this.pieDataList.add(new SxDataModel(key, value, value2));
        return this;
    }

    public SxNonWorkingTimeAnalysisModel addBarData(String key, BigDecimal value, BigDecimal value2, BigDecimal value3) {
        if (CollectionUtils.isEmpty(this.barDataList)) this.barDataList = new LinkedList<>();
        this.barDataList.add(new SxDataModel(key, value, value2, value3));
        return this;
    }

    public SxNonWorkingTimeAnalysisModel addTableName(String field, String name) {
        if (CollectionUtils.isEmpty(this.tableNameList)) this.tableNameList = new LinkedList<>();
        this.tableNameList.add(new SxDataModel(field, name));
        return this;
    }

    public SxNonWorkingTimeAnalysisModel addTableData(Map<String, Object> tableDataMap) {
        if (CollectionUtils.isEmpty(this.tableDataList)) this.tableDataList = new LinkedList<>();
        this.tableDataList.add(tableDataMap);
        return this;
    }

    public SxNonWorkingTimeAnalysisModel sortReversedMoreDataList() {
        this.centerDataList = sortReversedValue(this.centerDataList);
        this.itemDataList = sortReversedValue(this.itemDataList);
        return this;
    }

    public SxNonWorkingTimeAnalysisModel sortReversedTableDataList() {
        this.tableDataList = CollectionUtils.isEmpty(this.tableDataList) ? null :
                this.tableDataList.stream().sorted(Comparator.comparing(SxNonWorkingTimeAnalysisModel::comparingDouble).reversed()).collect(Collectors.toList());
        return this;
    }

    private static Double comparingDouble(Map<String, Object> map) {
        return Double.valueOf(map.get(MesExtConstWords.TOTAL).toString());
    }

}
