package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.station.function;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProductionDispatchContextStepService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProductionPsOutContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.BaseSwsService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.IShippingDispatchService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.function.IFsmModuleFunctionService;
import cn.estsh.i3plus.platform.common.util.MesPcnConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.model.ButtonDynamicModel;
import cn.estsh.i3plus.pojo.mes.model.StationCustomDialogBean;
import cn.estsh.i3plus.pojo.mes.model.StationRequestBean;
import cn.estsh.i3plus.pojo.mes.model.StationResultBean;
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 org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description : 电子化检验弹框接口实现【电子化检验弹框】
 **/
@Service
@Slf4j
public class MesFunctionDialogElectronicInspectionService extends BaseSwsService implements IFsmModuleFunctionService {

    @Autowired
    private IShippingDispatchService shippingDispatchService;

    @Autowired
    private IMesProductionDispatchContextStepService productionDispatchContextStepService;

    @Override
    public Boolean doFunction(StationRequestBean reqBean, StationResultBean resultBean, ButtonDynamicModel buttonDynamicModel) {

        this.sendMessage(reqBean, new StationResultBean().writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PICK.getValue()).scanInfo(buttonDynamicModel.getFunctionValue()),
                String.format("生产线[%s]工位[%s]电子化检验弹框提交信息成功!提交信息[%s]", reqBean.getWorkCenterCode(), reqBean.getWorkCellCode(), StringUtils.isEmpty(buttonDynamicModel.getFunctionValue()) ? "合格" : "可疑:" + buttonDynamicModel.getFunctionValue()),
                MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);

        //functionValue=电子化检验的判断结果
        List<String> inspectionList = StringUtils.isEmpty(buttonDynamicModel.getFunctionValue()) ? null : Arrays.asList(buttonDynamicModel.getFunctionValue().split(MesPcnExtConstWords.COMMA));

        Boolean isSaveFlag = false;
        //获取上下文产出条码数据信息集合
        if (!CollectionUtils.isEmpty(inspectionList)) {
            List<MesProductionPsOutContext> productionPsOutContextList = productionDispatchContextStepService.getProductionPsOutContext(reqBean);
            if (!CollectionUtils.isEmpty(productionPsOutContextList)) {
                for (MesProductionPsOutContext productionPsOutContext : productionPsOutContextList) {
                    if (null == productionPsOutContext) continue;
                    if (productionPsOutContext.getQcStatus().compareTo(MesExtEnumUtil.PRODUCE_QC_STATUS.QUALIFIED.getValue()) != 0
                            || !inspectionList.contains(productionPsOutContext.getProductSn())) continue;
                    //电子化检验弹框判可疑
                    productionPsOutContext.setQcStatus(MesExtEnumUtil.PRODUCE_QC_STATUS.SUSPICIOUS.getValue());
                    isSaveFlag = true;
                }
                //保存上下文产出条码数据信息集合
                if (isSaveFlag) productionDispatchContextStepService.dispatchProductionPsOutContext(reqBean, productionPsOutContextList);
            }
        }

        reqBean.setClientInfo(shippingDispatchService.getActorClientInfo(reqBean));
        reqBean.setInterfaceType(MesPcnConstWords.SHIPPING);
        reqBean.setBusiType(MesPcnEnumUtil.ACTOR_RECEIVE_STRATEGY.WS_CMD_DO_SCAN.getCode());
        reqBean.setButtonCode(buttonDynamicModel.getButtonCode());
        reqBean.setStepDialogStatus(true);
        shippingDispatchService.sendScanQueueNextExec(reqBean);

        return true;

    }

    @Override
    public StationCustomDialogBean stepDialogDispatch(StationRequestBean reqBean, StationCustomDialogBean dialogBean) {

        //获取上下文产出条码数据信息集合
        List<MesProductionPsOutContext> productionPsOutContext = productionDispatchContextStepService.getProductionPsOutContext(reqBean);

        //如果当前存在产出条码列表则返回条码集合
        if (!CollectionUtils.isEmpty(productionPsOutContext)) {
            List<String> snList = productionPsOutContext.stream().filter(o -> !StringUtils.isEmpty(o.getProductSn())).map(MesProductionPsOutContext::getProductSn).distinct().collect(Collectors.toList());
            // 条码列表为空则表示不需要弹框
            if (CollectionUtils.isEmpty(snList)) return dialogBean.unDialog();
            else return dialogBean.obj(snList);
        } else return dialogBean.unDialog();

    }
}
