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

import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkOrderExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkOrderExtBindSn;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpSnBindOrderWorkOrderModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesWorkOrderExtBindSnRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesWorkOrderExtRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.mes.pcn.serviceimpl.base.BaseStepService;
import cn.estsh.i3plus.mes.pcn.util.LocaleUtil;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
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.MesWorkCenterRepository;
import cn.estsh.impp.framework.boot.util.ImppRedis;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-dev
 * @DESCRIPTION:工步一：EP条码关联工单-选择工单工步
 * @USER: xinwang.yi
 * @DATE: 2021-12-27 16:34
 */
@Slf4j
@Service("epSnBindOrderChooseWorkOrderStepService")
public class EpSnBindOrderChooseWorkOrderStepService extends BaseStepService {

    @Resource(name = MesPcnExtConstWords.REDIS_RES_PCN)
    private ImppRedis redisMesPcn;

    @Autowired
    private MesWorkOrderExtRepository workOrderExtRepository;

    @Autowired
    private MesWorkOrderExtBindSnRepository workOrderExtBindSnRepository;

    @Override
    public StepResult execute(StationRequestBean reqBean) {
        log.info("epSnBindOrderChooseWorkOrderStepService[execute] 参数：{}", reqBean);

        StationResultBean resultBean = new StationResultBean();
        String err;

        if(StringUtils.isEmpty(reqBean.getOrganizeCode())){
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(),"回参缺少工厂代码");
            this.sendMessage(reqBean,resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        EpSnBindOrderWorkOrderModel curWorkOrderNo = (EpSnBindOrderWorkOrderModel) doHandleCurWorkOrder(reqBean, 1, null);
//        redisMesPcn.getHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.CUR_WORK_ORDER);
        String selectWorkOrderNo = (String) doHandleSelectWorkOrderNo(reqBean, 1, null);
//        redisMesPcn.getHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA) ,MesPcnExtConstWords.CMD_WORK_ORDER);

        if(curWorkOrderNo == null && StringUtils.isEmpty(selectWorkOrderNo)){
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(),"请选择工单");
            this.sendMessage(reqBean,resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            foundExThrowNoShowMsg();
        }

        MesWorkOrderExt workOrderExt = null;
        MesWorkOrderExtBindSn workOrderExtBindSn = null;

        if(!StringUtils.isEmpty(selectWorkOrderNo)){
            doHandleSelectWorkOrderNo(reqBean, 3, selectWorkOrderNo);

            DdlPackBean packBean = DdlPackBean.getDdlPackBean(reqBean.getOrganizeCode());
            DdlPreparedPack.getStringEqualPack(selectWorkOrderNo, MesPcnExtConstWords.ORDER_NO, packBean);
            workOrderExt = workOrderExtRepository.getByProperty(packBean);

            if(workOrderExt == null){
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(),"工单[%s]不存在,选择工单失败"), selectWorkOrderNo);
                this.sendMessage(reqBean,resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }else if(StringUtils.isEmpty(workOrderExt.getWorkCenterCode()) || StringUtils.isEmpty(reqBean.getWorkCenterCode())){
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(),"工单产线[%s]和当前所选产线[%s]存在为空,选择工单失败"), workOrderExt.getWorkCenterCode(), reqBean.getWorkCenterCode());
                this.sendMessage(reqBean,resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                workOrderExt = null;
                return StepResult.getNonComplete(err);
            }else if(!workOrderExt.getWorkCenterCode().equals(reqBean.getWorkCenterCode())){
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(),"工单[%s]关联的生产线[%s]与当前生产线[%s]不一致,选择工单失败"), selectWorkOrderNo, reqBean.getWorkCenterCode());
                this.sendMessage(reqBean,resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                workOrderExt = null;
                return StepResult.getNonComplete(err);
            }else if(workOrderExt.getWorkOrderType() == MesPcnExtEnumUtil.WORK_ORDER_TYPE.REWORK_ORDER.getValue()){
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(),"工单[%s]类型是[返工工单],此工位不支持绑定,选择工单失败"), selectWorkOrderNo);
                this.sendMessage(reqBean,resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                workOrderExt=null;
            }else if(workOrderExt.getWorkOrderStatus()!= MesPcnExtEnumUtil.WORK_ORDER_STATUS.OPEN.getValue()){
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(),"工单[%s]状态为[%s],只能操作[启动]状态的工单,选择工单失败"), selectWorkOrderNo, workOrderExt.getWorkOrderStatus());
                this.sendMessage(reqBean,resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                workOrderExt=null;
                return StepResult.getNonComplete(err);
            }else if(workOrderExt.getSnCreateStatus() != MesPcnExtEnumUtil.WORK_ORDER_SN_CREATE_STATUS.NO_CREATE.getValue()){
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(),"工单[%s]条码生成状态为[%s],只能操作条码生成状态为[不生成]的工单,选择工单失败"), selectWorkOrderNo,  MesPcnExtEnumUtil.WORK_ORDER_SN_CREATE_STATUS.valueOfDescription(workOrderExt.getSnCreateStatus()));
                this.sendMessage(reqBean,resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                workOrderExt=null;
                return StepResult.getNonComplete(err);
            }else {
                packBean = DdlPackBean.getDdlPackBean(reqBean.getOrganizeCode());
                DdlPreparedPack.getStringEqualPack(selectWorkOrderNo, MesPcnExtConstWords.WORK_ORDER_NO,packBean);
                //工单条码绑定记录
                workOrderExtBindSn = workOrderExtBindSnRepository.getByProperty(packBean);

                if(workOrderExtBindSn != null){
                    if(workOrderExtBindSn.getBindQty() == workOrderExt.getQty()){
                        err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(),"工单[%s]数量为：[%s],关联的条码数量已达标,选择工单失败"), selectWorkOrderNo, workOrderExt.getQty());
                        this.sendMessage(reqBean,resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                        workOrderExt=null;
                        workOrderExtBindSn=null;
                        return StepResult.getNonComplete(err);
                    }
                }else{
                    String message = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(),"工单[%s]选择成功"), selectWorkOrderNo );
                    this.sendMessage(reqBean,resultBean, message, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
//                    return StepResult.getSuccessComplete(message);
                }
            }
        } else{
            DdlPackBean packBean = DdlPackBean.getDdlPackBean(reqBean.getOrganizeCode());
            DdlPreparedPack.getStringEqualPack(curWorkOrderNo.getWorkOrderNo(),MesPcnExtConstWords.ORDER_NO, packBean);
            workOrderExt =  workOrderExtRepository.getByProperty(packBean);
            if(workOrderExt == null){
                String message = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(),"当前执行工单[%s]不存在,请重新选择工单"), selectWorkOrderNo );
                this.sendMessage(reqBean,resultBean, message, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
                foundExThrowNoShowMsg();
            }

            if(workOrderExt.getWorkOrderStatus() != MesPcnExtEnumUtil.WORK_ORDER_STATUS.OPEN.getValue()){
                String message = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(),"当前执行工单[%s]状态为[%s],请重新选择工单"), selectWorkOrderNo,workOrderExt.getWorkOrderStatus());
                this.sendMessage(reqBean,resultBean, message, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
                foundExThrowNoShowMsg();
            }

            packBean = DdlPackBean.getDdlPackBean(reqBean.getOrganizeCode());
            DdlPreparedPack.getStringEqualPack(curWorkOrderNo.getWorkOrderNo(), MesPcnExtConstWords.WORK_ORDER_NO, packBean);
            workOrderExtBindSn = workOrderExtBindSnRepository.getByProperty(packBean);
            //判断根据当前工单的工单号查出来的条码绑定工单数据是否存在
            if(workOrderExtBindSn != null){
                if(workOrderExtBindSn.getBindQty() == workOrderExt.getQty()){
                    String message = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(),"当前执行工单[%s]不存在,请重新选择工单"), selectWorkOrderNo );
                    this.sendMessage(reqBean,resultBean, message, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
                    foundExThrowNoShowMsg();
                }
            }
        }

        if(workOrderExt != null){
            if(curWorkOrderNo == null){
                curWorkOrderNo = new EpSnBindOrderWorkOrderModel();
            }
            curWorkOrderNo.setAreaCode(workOrderExt.getAreaCode());
            curWorkOrderNo.setWorkOrderType(workOrderExt.getWorkOrderType());
            curWorkOrderNo.setWorkOrderNo(workOrderExt.getOrderNo());
            curWorkOrderNo.setPartNo(workOrderExt.getPartNo());
            curWorkOrderNo.setPartNameRdd(workOrderExt.getPartNameRdd());
            curWorkOrderNo.setWorkCenterCode(workOrderExt.getWorkCenterCode());
            curWorkOrderNo.setQty(workOrderExt.getQty());
            if(workOrderExtBindSn != null){
                curWorkOrderNo.setBindQty(workOrderExtBindSn.getBindQty());
            }else {
                curWorkOrderNo.setBindQty(Double.valueOf(0));
            }
            doHandleCurWorkOrder(reqBean, 2, curWorkOrderNo);
        }

        return StepResult.getSuccessComplete();
    }

    private synchronized Object doHandleSelectWorkOrderNo(StationRequestBean reqBean, Integer flag, String selectWorkOrderNo) {
        switch (flag) {
            case 1:
                return redisMesPcn.getHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.CMD_WORK_ORDER);
            case 2:
                return redisMesPcn.putHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.CMD_WORK_ORDER, selectWorkOrderNo, MesPcnEnumUtil.EXPIRE_TIME.NEVER.getValue());
            case 3:
                redisMesPcn.deleteHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.CMD_WORK_ORDER);
                return true;
            default:
                return null;
        }
    }

    private synchronized Object doHandleCurWorkOrder(StationRequestBean reqBean, Integer flag, EpSnBindOrderWorkOrderModel curWorkOrderNo) {
        switch (flag) {
            case 1:
                return redisMesPcn.getHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.CUR_WORK_ORDER);
            case 2:
                return redisMesPcn.putHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.CUR_WORK_ORDER, curWorkOrderNo, MesPcnEnumUtil.EXPIRE_TIME.NEVER.getValue());
            case 3:
                redisMesPcn.deleteHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.CUR_WORK_ORDER);
                return true;
            default:
                return null;
        }
    }
}
