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

import cn.estsh.i3plus.pojo.mes.bean.*;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description : 排序工单汇报
 * @Reference :
 * @Author : junsheng.li
 * @CreateDate 2024/9/28 12:32
 * @Modify:
 **/
@Data
public class MesWorkOrderSortReportModel {

    @ApiParam("工厂代码")
    private String organizeCode;

    @ApiParam("用户名")
    private String userName;

    @ApiParam("移库规则")
    private List<MesMoveRule> mesMoveRuleList;

    @ApiParam("加工记录")
    private List<MesProductionRecord> mesProductionRecordList;

    @ApiParam("工单")
    private List<MesWorkOrder> mesWorkOrderList;

    @ApiParam("工单零件")
    private List<MesWorkOrderPart> mesWorkOrderPartList;

    @ApiParam("零件信息-sap下发的零件")
    private List<MesPartSap> mesPartSapList;

    @ApiParam("零件信息")
    private List<MesPart> mesPartList;

    @ApiParam("生产版本")
    private List<MesProductVersion> mesProductVersionList;

    public MesWorkOrderSortReportModel() {
    }

    public MesWorkOrderSortReportModel(String organizeCode, String userName, List<MesProductionRecord> mesProductionRecordList) {
        this.organizeCode = organizeCode;
        this.userName = userName;
        this.mesProductionRecordList = mesProductionRecordList;
    }

    public List<String> getWorkOrderList() {
        if (CollectionUtils.isEmpty(mesProductionRecordList)) return null;
        return this.mesProductionRecordList.stream().map(MesProductionRecord::getWorkOrderNo).distinct().filter(t -> !StringUtils.isEmpty(t)).collect(Collectors.toList());
    }

    public List<String> getPartNoList() {
        if (CollectionUtils.isEmpty(mesProductionRecordList)) return null;
        return this.mesProductionRecordList.stream().map(MesProductionRecord::getReportPartNo).distinct().filter(t -> !StringUtils.isEmpty(t)).collect(Collectors.toList());
    }

    public List<String> getPartNoAllList() {
        List<String> partNoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(mesProductionRecordList)) partNoList.addAll(this.mesProductionRecordList.stream().map(MesProductionRecord::getReportPartNo).distinct().filter(t -> !StringUtils.isEmpty(t)).collect(Collectors.toList()));
        if (!CollectionUtils.isEmpty(mesWorkOrderPartList)) partNoList.addAll(this.mesWorkOrderPartList.stream().map(MesWorkOrderPart::getPartNo).distinct().filter(t -> !StringUtils.isEmpty(t)).collect(Collectors.toList()));
        return partNoList;
    }

    public List<String> getPartProdGroupCodeList() {
        if (CollectionUtils.isEmpty(mesWorkOrderList)) return null;
        return this.mesWorkOrderList.stream().map(MesWorkOrder::getPartProdGroupCode).distinct().filter(t -> !StringUtils.isEmpty(t)).collect(Collectors.toList());
    }

    public Map<String, List<MesProductionRecord>> getMesProductionRecordMap() {
        if (CollectionUtils.isEmpty(mesProductionRecordList)) return null;
        return mesProductionRecordList.stream().collect(Collectors.groupingBy(MesProductionRecord::getWorkOrderNo));
    }

    public Map<String, List<MesWorkOrder>> getMesWorkOrderMap() {
        if (CollectionUtils.isEmpty(mesWorkOrderList)) return null;
        return mesWorkOrderList.stream().collect(Collectors.groupingBy(MesWorkOrder::getWorkOrderNo));
    }

    public Map<String, List<MesMoveRule>> getMesMoveRuleMap() {
        if (CollectionUtils.isEmpty(mesMoveRuleList)) return null;
        return mesMoveRuleList.stream().collect(Collectors.groupingBy(MesMoveRule::getCode));
    }

    public Map<String, List<MesPartSap>> getMesPartSapMap() {
        if (CollectionUtils.isEmpty(mesPartSapList)) return null;
        return mesPartSapList.stream().collect(Collectors.groupingBy(MesPartSap::getPartNo));
    }

    public Map<String, List<MesPart>> getMesPartMap() {
        if (CollectionUtils.isEmpty(mesPartList)) return null;
        return mesPartList.stream().collect(Collectors.groupingBy(MesPart::getPartNo));
    }

    public Map<String, List<MesWorkOrderPart>> getMesWorkOrderPartMap() {
        if (CollectionUtils.isEmpty(mesWorkOrderPartList)) return null;
        return mesWorkOrderPartList.stream().collect(Collectors.groupingBy(MesWorkOrderPart::getWorkOrderNo));
    }

    public Map<String, List<MesProductVersion>> getMesProductVersionMap() {
        if (CollectionUtils.isEmpty(mesProductVersionList)) return null;
        return mesProductVersionList.stream().collect(Collectors.groupingBy(MesProductVersion::getPartNo));
    }

}
