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.IMesProductionProcessContextStepService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProductionProcessContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.BaseStepService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.IStepService;
import cn.estsh.i3plus.mes.pcn.util.StringUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesBarcodeCountShareInfo;
import cn.estsh.i3plus.pojo.mes.bean.MesProdRouteOptParam;
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.repository.MesBarcodeCountShareInfoRepository;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import cn.estsh.impp.framework.boot.util.SpringContextsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.Optional;
import java.util.UUID;

/**
 * @Description : 保存零件条码统计OEE共享信息工步
 * @Author : wangjie
 **/
@Slf4j
@Service("mesBarcodeCountShareInfoSaveStepService")
public class MesBarcodeCountShareInfoSaveStepService extends BaseStepService {

    @Autowired
    public IMesProductionProcessContextStepService productionProcessContextStepService;

    @Autowired
    public IMesProductionDispatchContextStepService productionDispatchContextStepService;

    @Autowired
    private MesBarcodeCountShareInfoRepository barcodeCountShareInfoRepository;

    @Override
    public StepResult execute(StationRequestBean reqBean) {

        StationResultBean resultBean = new StationResultBean();

        StepResult stepResult = StepResult.getSuccessComplete();

        //获取工步参数
        Optional<Map<String, MesProdRouteOptParam>> stepParamMap = getStepParams(reqBean);

        String stepParam = (null != stepParamMap && stepParamMap.isPresent() && stepParamMap.get().containsKey(MesPcnExtConstWords.BARCODE_COUNT_SHARE_INFO)) ? stepParamMap.get().get(MesPcnExtConstWords.BARCODE_COUNT_SHARE_INFO).getParamValue() : null;

        if (StringUtils.isEmpty(stepParam) && StringUtils.isEmpty(MesExtEnumUtil.BARCODE_COUNT_SHARE_INFO.valueOfDescription(stepParam))) {
            return stepDynamicsCompleteAndSendMsgReturn(reqBean,
                    resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PROCESS.getValue()), stepResult, true,
                    MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT, String.format("未配置有效的工步参数[{}]跳过执行保存零件条码统计共享信息工步!", MesPcnExtConstWords.BARCODE_COUNT_SHARE_INFO));
        }

        Boolean isAllExec = false;
        if (MesExtEnumUtil.BARCODE_COUNT_SHARE_INFO.BARCODE_COUNT_SHARE_INFO_30.getValue().equals(stepParam)) isAllExec = true;

        //零件条码统计工步
        if (isAllExec || MesExtEnumUtil.BARCODE_COUNT_SHARE_INFO.BARCODE_COUNT_SHARE_INFO_10.getValue().equals(stepParam)) {
            ((IStepService) SpringContextsUtil.getBean("mesBarcodeCountShareProductSnSaveStepService")).executeInState(reqBean);
        }
        //保存激光装配件工步
        if (isAllExec || MesExtEnumUtil.BARCODE_COUNT_SHARE_INFO.BARCODE_COUNT_SHARE_INFO_20.getValue().equals(stepParam)) {
            ((IStepService) SpringContextsUtil.getBean("mesBarcodeCountShareAssemblySnSaveStepService")).executeInState(reqBean);
        }

        log.info("工厂{}生产线{}工位{}:FSM STATE DISPATCHER --- DO STEP --- {} EXEC --- {}={}[{}]",
                reqBean.getOrganizeCode(), reqBean.getWorkCenterCode(), reqBean.getWorkCellCode(),
                StringUtil.toLowerCaseFirst(this.getClass().getSimpleName()), MesPcnExtConstWords.BARCODE_COUNT_SHARE_INFO, stepParam, MesExtEnumUtil.BARCODE_COUNT_SHARE_INFO.valueOfDescription(stepParam));

        return stepResult;

    }

    public void saveBarcodeCountShareInfo(StationRequestBean reqBean, MesProductionProcessContext productionProcessContext, MesBarcodeCountShareInfo barcodeCountShareInfo) {
        barcodeCountShareInfo.setAreaCode(reqBean.getAreaCode());
        barcodeCountShareInfo.setWorkCenterCode(reqBean.getWorkCenterCode());
        barcodeCountShareInfo.setWorkCellCode(reqBean.getWorkCellCode());
        barcodeCountShareInfo.setProcessCode(reqBean.getProcessCode());
        barcodeCountShareInfo.setCraftCode(productionProcessContext.getCraftCode());
        barcodeCountShareInfo.setEquipmentCode(productionProcessContext.getCurCellEquip().getEquipmentCode());
        barcodeCountShareInfo.setOrganizeCode(reqBean.getOrganizeCode());
        barcodeCountShareInfo.setFid(UUID.randomUUID().toString());
        ConvertBean.serviceModelInitialize(barcodeCountShareInfo, reqBean.getUserInfo());
        barcodeCountShareInfo.setProductEndTime(barcodeCountShareInfo.getModifyDatetime());
        barcodeCountShareInfoRepository.insert(barcodeCountShareInfo);
    }

}
