package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.numberrule;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProductionDispatchContextStepService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProdTempDataContext;
import cn.estsh.i3plus.mes.pcn.api.iservice.busi.INumberRulePackAttributeStrategyService;
import cn.estsh.i3plus.pojo.mes.model.GenSerialNoModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

/**
 * @Description : 使用规则并截取右侧流水装配件规则截取的流水号作为条码的流水号
 * @Reference :
 * @Author : wangjie
 * @CreateDate 2024/9/26 19:24
 * @Modify:
 **/
@Component
public class MesNumberRuleStrategySubstringSerialService implements INumberRulePackAttributeStrategyService {

    @Autowired
    private IMesProductionDispatchContextStepService productionDispatchContextStepService;

    @Override
    public GenSerialNoModel execute(GenSerialNoModel genSerialNoModel) {

        //规则并截取右侧流水装配件规则, 生成零件条码工步需要使用此流水生成新的零件条码
        List<MesProdTempDataContext> prodTempDataContextList = productionDispatchContextStepService.getProdTempDataContext(genSerialNoModel.getOrganizeCode(), genSerialNoModel.getWorkCenterCode(), genSerialNoModel.getWorkCellCode());

        Optional<MesProdTempDataContext> optional = CollectionUtils.isEmpty(prodTempDataContextList) ? null :
                prodTempDataContextList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getForeignKey()) && o.getForeignKey().compareTo(genSerialNoModel.getForeignKey()) == 0)).findFirst();

        if (null != optional && optional.isPresent() && !StringUtils.isEmpty(optional.get().getSerialNo())) genSerialNoModel.appendSerialno(optional.get().getSerialNo());

        return genSerialNoModel;

    }
}
