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.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

@Data
public class MesSortShippingModel {

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

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

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

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

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

    public Map<String, MesWorkOrder> getMesWorkOrderMap() {
        if (CollectionUtils.isEmpty(mesWorkOrderList)) return null;
        return mesWorkOrderList.stream().collect(Collectors.toMap(MesWorkOrder::getWorkOrderNo, Function.identity(), (x, y) -> y));
    }

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

    public Map<String, MesProductionRecord> getMesProductionRecordMap() {
        if (CollectionUtils.isEmpty(mesProductionRecordList)) return null;
        return mesProductionRecordList.stream().collect(Collectors.toMap(MesProductionRecord::getWorkOrderNo, Function.identity(), (x, y) -> y));
    }

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

    public Map<String, MesPart> getMesPartMap() {
        if (CollectionUtils.isEmpty(mesPartList)) return null;
        return mesPartList.stream().filter(t -> Objects.nonNull(t.getPartNo())).collect(Collectors.toMap(MesPart::getPartNo, Function.identity(), (x, y) -> y));
    }

    public Map<String, MesMoveRule> getMesMoveRuleMap() {
        if (CollectionUtils.isEmpty(mesMoveRuleList)) return null;
        return mesMoveRuleList.stream().filter(t -> Objects.nonNull(t.getSrcType())).collect(Collectors.toMap(MesMoveRule::getSrcType, Function.identity(), (x, y) -> y));
    }

}
