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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProductionDispatchContextStepService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.recyclablepackage.IMesRecyclablePackageBindingService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProductionRecyclablePackageContext;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.BaseStepService;
import cn.estsh.i3plus.pojo.mes.bean.recyclablepackage.MesRecyclablePackageBinding;
import cn.estsh.i3plus.pojo.mes.bean.recyclablepackage.MesRecyclablePackageBindingDetail;
import cn.estsh.i3plus.pojo.mes.model.StationRequestBean;
import cn.estsh.i3plus.pojo.mes.model.StationResultBean;
import cn.estsh.i3plus.pojo.mes.model.StepResult;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * @Description : 包装零件解绑
 * @Reference :
 * @Author : junsheng.li
 * @CreateDate 2024/9/13 18:08
 * @Modify:
 **/
@Slf4j
@Service("mesRecyclablePackageUnBindingStepService")
public class MesRecyclablePackageUnBindingStepService extends BaseStepService {

    @Autowired
    private IMesProductionDispatchContextStepService productionDispatchContextStepService;

    @Autowired
    private IMesRecyclablePackageBindingService mesRecyclablePackageBindingService;

    @Override
    public StepResult execute(StationRequestBean reqBean) {

        StationResultBean resultBean = new StationResultBean();

        StepResult stepResult = StepResult.getSuccessComplete();
        //验证进料可回用包装是否存在
        if (!productionDispatchContextStepService.checkProductionRecyclablePackageContextIsExist(reqBean)){
            return stepResult;
        }
        //获取进料可回用包装信息
        List<MesProductionRecyclablePackageContext> productionRecyclablePackageContext = productionDispatchContextStepService.getProductionRecyclablePackageContext(reqBean);
        //获取包装绑定主表
        MesRecyclablePackageBinding recyclablePackageBinding = mesRecyclablePackageBindingService.getMesRecyclablePackageBindingByPackageSn(reqBean.getOrganizeCode(), productionRecyclablePackageContext.iterator().next().getPackageSn());
        if (Objects.isNull(recyclablePackageBinding)) {
            return stepNonCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog(), stepResult, String.format("回收包装绑定[%s]信息不存在", productionRecyclablePackageContext.iterator().next().getPackageSn()));
        }
        //校验状态
        if (MesExtEnumUtil.RECYCLABLE_PACKAGE_PACKAGE_BINDING_STATUS.CLOSED.getValue() != recyclablePackageBinding.getStatus()) {
            return stepNonCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog(), stepResult, String.format("回收包装绑定[%s]状态不为[已关箱]", productionRecyclablePackageContext.iterator().next().getPackageSn()));
        }
        //获取绑定明细信息
        List<MesRecyclablePackageBindingDetail> bindingDetailList = mesRecyclablePackageBindingService.findMesRecyclablePackageBindingDetailByPid(reqBean.getOrganizeCode(), recyclablePackageBinding.getId());
        if (CollectionUtils.isEmpty(bindingDetailList)) {
            return stepNonCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog(), stepResult, String.format("回收包装绑定[%s]明细信息不存在", productionRecyclablePackageContext.iterator().next().getPackageSn()));
        }
        //解绑
        mesRecyclablePackageBindingService.doMesRecyclablePackageUnBinding(recyclablePackageBinding, bindingDetailList, reqBean.getUserInfo());

        return stepSuccessCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog(), stepResult, String.format("可回用包装条码%s解绑成功!", productionRecyclablePackageContext.iterator().next().getPackageSn()));
    }


}
