package cn.estsh.i3plus.pojo.mes.model.mgn.bj.edi;

import cn.estsh.i3plus.pojo.mes.model.mgn.bj.edi.Part;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Wynne.Lu
 * @date 2020/6/27 15:39
 * @desc
 */
@Data
@NoArgsConstructor
public class MgnBjJitPlanModel {

    private String org;

    private String userInfo;

    private List<Part> flPnParts;

    private String flPnRecipe;

    private List<Part> frPnParts;

    private String frPnRecipe;

    private List<Part> rlPnParts;

    private String rlPnRecipe;

    private List<Part> rrPnParts;

    private String rrPnRecipe;

    private Map<String, List<Part>> groupPartsMap = new HashMap<>();

    public void setFlPnParts(List<Part> flPnParts) {
        this.groupPartsMap.put("FL", flPnParts);
        this.flPnRecipe = getGroupRecipe(flPnParts);
        this.flPnParts = flPnParts;
    }

    public void setFrPnParts(List<Part> frPnParts) {
        this.groupPartsMap.put("FR", frPnParts);
        this.frPnRecipe = getGroupRecipe(frPnParts);
        this.frPnParts = frPnParts;
    }

    public void setRlPnParts(List<Part> rlPnParts) {
        this.groupPartsMap.put("RL", rlPnParts);
        this.rlPnRecipe = getGroupRecipe(rlPnParts);
        this.rlPnParts = rlPnParts;
    }

    public void setRrPnParts(List<Part> rrPnParts) {
        this.groupPartsMap.put("RR", rrPnParts);
        this.rrPnRecipe = getGroupRecipe(rrPnParts);
        this.rrPnParts = rrPnParts;
    }

    public MgnBjJitPlanModel(String org, String userInfo) {
        this.org = org;
        this.userInfo = userInfo;
    }

    private String getGroupRecipe(List<Part> groupParts) {
        List<String> groupPartsRecipe = groupParts.stream().filter(d -> StringUtils.isNotBlank(d.getRecipeSeq()))
                .sorted(Comparator.comparing(Part::getRecipeSeq))
                .map(Part::getRecipeItem)
                .collect(Collectors.toList());
        return String.join("", groupPartsRecipe);
    }
}
