package com.xisc.pm.app.service.strategy.secondarydesign;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.xisc.pm.api.dto.PassQualifiedProDTO;
import com.xisc.pm.api.dto.PmProContFilterRequestDTO;
import com.xisc.pm.api.dto.PmProContYieldRequestDTO;
import com.xisc.pm.api.dto.PmProContYieldResposeDTO;
import com.xisc.pm.api.dto.ctevent.PmSlabApplyMakeDTO;
import com.xisc.pm.domain.entity.PmCmdCont;
import com.xisc.pm.domain.entity.PmCmdLargePlt;
import com.xisc.pm.domain.entity.PmPdContSum;
import com.xisc.pm.domain.entity.PmPdPlate;
import com.xisc.pm.domain.entity.PmPdPlateInfo;
import com.xisc.pm.domain.entity.PmProContProdTtr;
import com.xisc.pm.domain.entity.PmSmallPltPos;
import com.xisc.pm.domain.repository.MmPlateRepository;
import com.xisc.pm.domain.repository.PmPdPlateInfoRepository;
import com.xisc.pm.domain.repository.PmPdPlateRepository;
import com.xisc.pm.domain.vo.EventFeignVO;
import com.xisc.pm.domain.vo.MatStatusModifyVO;
import com.xisc.pm.domain.vo.MmPlateVO;
import com.xisc.pm.domain.vo.PmCmdVO;
import com.xisc.pm.infra.client.EventClient;
import com.xisc.pm.infra.client.MmEventClient;
import com.xisc.pm.infra.constant.Constants;
import com.xisc.pm.infra.constant.Constants.CcType;
import com.xisc.pm.infra.enums.PdMatType;
import com.xisc.pm.infra.enums.PdProdFlag;
import com.xisc.pm.infra.enums.PdRuleSetType;
import com.xisc.pm.infra.util.Utils;
import io.choerodon.core.exception.CommonException;
import io.choerodon.core.oauth.DetailsHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.hzero.core.base.BaseConstants;
import org.hzero.mybatis.domian.Condition;
import org.hzero.mybatis.util.Sqls;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * (余材)二次设计
 * 
 * @author jun.zeng
 *
 */
@Slf4j
@Component
public class PlateSecondaryDesignStrategy extends DefaultSecondaryDesignStrategy<PmPdPlate> {

    @Autowired
    private PmPdPlateRepository pmPdPlateRepository;

    @Autowired
    private PmPdPlateInfoRepository pmPdPlateInfoRepository;

    @Autowired
    private MmPlateRepository mmPlateRepository;

    @Autowired
    private MmEventClient mmEventClient;

    @Autowired
    private EventClient eventClient;

    private static final ThreadLocal<Map<String, PmPdPlateInfo>> PM_PD_SLAB_INFO_LOCAL = new ThreadLocal<>();
    
    @Override
    protected boolean hasPmCmdSlab() {
        return false;
    }

    @Override
    protected void remove() {
        PM_PD_SLAB_INFO_LOCAL.remove();
    }

    @Override
    protected Map<String, PmPdPlate> selectByMatNo(List<String> virtualSlabNo, Long tenantId) {
        List<PmPdPlate> pmPdPlates = pmPdPlateRepository.selectByCondition(Condition.builder(PmPdPlate.class)
                .andWhere(Sqls.custom().andEqualTo(PmPdPlate.FIELD_TENANT_ID, tenantId).andIn(PmPdPlate.FIELD_PLATE_NO,
                        virtualSlabNo))
                .build());
        Map<String, PmPdPlate> pmpdPlateMap = Maps.newHashMap();

        if (pmPdPlates == null || pmPdPlates.isEmpty()) {
            return pmpdPlateMap;
        }
        for (PmPdPlate pmPdPlate : pmPdPlates) {
            pmpdPlateMap.put(pmPdPlate.getPlateNo(), pmPdPlate);
        }
        List<PmPdPlateInfo> pmPdPlateInfos = pmPdPlateInfoRepository.selectByCondition(Condition
                .builder(PmPdPlateInfo.class).andWhere(Sqls.custom().andEqualTo(PmPdPlate.FIELD_TENANT_ID, tenantId)
                        .andIn(PmPdPlateInfo.FIELD_PLATE_NO, virtualSlabNo))
                .build());
        Map<String, PmPdPlateInfo> pmPdPlateInfoMap = Maps.uniqueIndex(pmPdPlateInfos, PmPdPlateInfo::getPlateNo);
        PM_PD_SLAB_INFO_LOCAL.set(pmPdPlateInfoMap);
        return pmpdPlateMap;
    }

    @Override
    protected PmProContYieldResposeDTO yiledCalc(PmCmdLargePlt pmCmdLargePlt, PmProContYieldRequestDTO pmProContYield,
            PmProContProdTtr axisPmProContProdTtr) {
        PmProContYieldResposeDTO pmProContYieldRespose = super.yiledCalc(pmCmdLargePlt, pmProContYield,
                axisPmProContProdTtr);
        MmPlateVO mmPlateVO = pmProContYield.getPmContFilterRequest().getMmPlate();
        if (pmProContYieldRespose.getLaim().compareTo(mmPlateVO.getMatActLen()) > 0
                || pmProContYieldRespose.getWaim().compareTo(mmPlateVO.getMatActWid()) > 0) {
            // 轧制目标长度【{0}】或宽度【{1}】大于钢板实际长度【{2}】或宽度【{3}】，请检查！,材料号【{4}】
            throw new CommonException("pm.pm_pd_secondary_design_003",pmProContYieldRespose.getLaim().toPlainString() , pmProContYieldRespose.getWaim().toPlainString() 
                    , mmPlateVO.getMatActLen().toPlainString() , mmPlateVO.getMatActWid().toPlainString() , mmPlateVO.getMatNo());
        }
        return pmProContYieldRespose;
    }

    @Override
    protected PmProContFilterRequestDTO buildPmProContFilterRequestDTO(
            PmProContYieldRequestDTO pmProContYieldRequestDTO, PmPdPlate salbInfo, PmCmdLargePlt pmCmdLargePlt) {
        PmProContFilterRequestDTO pmContFilterRequest = new PmProContFilterRequestDTO();
        MmPlateVO mmPlateVO = new MmPlateVO();
        mmPlateVO.setMatNo(salbInfo.getPlateNo());
        mmPlateVO.setTenantId(salbInfo.getTenantId());
        List<MmPlateVO> mmPlateVOList = mmPlateRepository.selectList(mmPlateVO);
        if (mmPlateVOList == null || mmPlateVOList.isEmpty()) {
            throw new CommonException("pm.pm_pd_plate_001");
        }
        PmPdPlateInfo pdPlateInfo = PM_PD_SLAB_INFO_LOCAL.get().get(salbInfo.getPlateNo());
        mmPlateVO = mmPlateVOList.get(0);
        pmContFilterRequest.setPlateId(mmPlateVOList.get(0).getPlateId());
        pmContFilterRequest.setMoldThick(mmPlateVO.getMatActThk());
        pmContFilterRequest.setMoldWidth(mmPlateVO.getMatActWid());
        pmContFilterRequest.setStNo(salbInfo.getStNo());
        pmContFilterRequest.setSgSign(salbInfo.getSgSign());
        pmContFilterRequest.setFurType(pdPlateInfo.getFurType());
        pmContFilterRequest.setCcType(CcType.TYPE_1);
        pmContFilterRequest.setMatDesignKind(salbInfo.getMatDesignKind());
        pmContFilterRequest.setSampleLenGice(BigDecimal.ZERO);
        pmContFilterRequest.setFixSlabLenFlag(0L);
        pmContFilterRequest.setFixSlabLen(BigDecimal.ZERO);
        pmContFilterRequest.setMscLineNo(pdPlateInfo.getMscLineNo());
        pmContFilterRequest.setFormPlateMode(salbInfo.getFormPlateMode());
        pmContFilterRequest.setRollDirectCode(salbInfo.getRollDirectCode());
        return pmContFilterRequest;
    }

    @Override
    protected PmPdPlate updateSlabInfo(PmPdPlate salbInfo, PmProContYieldResposeDTO pmProContYieldRespose,
            String pdStatusHp) {
        PmPdPlate pmPdPlate = new PmPdPlate();
        if (pmProContYieldRespose != null) {
            pmPdPlate.setRollAimThick(pmProContYieldRespose.getTaim());
            pmPdPlate.setRollAimWidth(pmProContYieldRespose.getWaim());
            pmPdPlate.setRollAimLen(pmProContYieldRespose.getLaim());
            pmPdPlate.setCommandYield(pmProContYieldRespose.getCommandYield());
            pmPdPlate.setHeadCut(pmProContYieldRespose.getHeadCut());
            pmPdPlate.setTailCut(pmProContYieldRespose.getTailCut());
        }
        pmPdPlate.setPdStatusHp(pdStatusHp);
        pmPdPlate.setObjectVersionNumber(salbInfo.getObjectVersionNumber());
        pmPdPlate.setPmPdPlateId(salbInfo.getPmPdPlateId());
        return pmPdPlate;
    }

    @Override
    protected void execUpdateSlabInfo(List<PmPdPlate> slabInfoList) {
        List<PmPdPlate> deduplicatedList = slabInfoList.stream()
                .collect(Collectors.toMap(PmPdPlate::getPmPdPlateId, Function.identity(), (k, v) -> k)).values().stream().collect(Collectors.toList());
        String pdStatusHp = null;
        for (PmPdPlate pmPdPlate : deduplicatedList) {
            pdStatusHp = pmPdPlate.getPdStatusHp();
        }
        if (PM_PD_SLAB_INFO_LOCAL.get() != null && PM_PD_SLAB_INFO_LOCAL.get().values() != null
                && !PM_PD_SLAB_INFO_LOCAL.get().values().isEmpty()) {
            List<PmPdPlateInfo> updatePmPdSlabInfos = new ArrayList<PmPdPlateInfo>();
            for (PmPdPlateInfo pmPdPlateInfo : PM_PD_SLAB_INFO_LOCAL.get().values()) {
                PmPdPlateInfo updatePmPdSlabInfo = new PmPdPlateInfo();
                updatePmPdSlabInfo.setPdStatusHp(pdStatusHp);
                updatePmPdSlabInfo.setPmPdPlateInfoId(pmPdPlateInfo.getPmPdPlateInfoId());
                updatePmPdSlabInfo.setObjectVersionNumber(pmPdPlateInfo.getObjectVersionNumber());
                updatePmPdSlabInfos.add(updatePmPdSlabInfo);
            }
            pmPdPlateInfoRepository.batchUpdateByPrimaryKeySelective(updatePmPdSlabInfos);
        }
        pmPdPlateRepository.batchUpdateByPrimaryKeySelective(deduplicatedList);
    }

    @Override
    protected void checkSlabInfo(PmPdPlate salbInfo, String virtualSlabNo) {
        if (salbInfo == null) {
            throw new CommonException("pm.pm_pd_plate_002", virtualSlabNo);
        }
    }

    @Override
    protected PdMatType getMatDesignKind() {
        return PdMatType.P;
    }

    @Override
    protected PdRuleSetType buildRuleParam() {
        return null;
    }
    
    @Override
    public void abandonContractEventCommand(List<String> virtualSlabNoList, Map<String, PmPdPlate> slabInfo,
            Map<String, List<PmPdContSum>> pmPdContSumListMap, Long tenantId) {
        List<PmSmallPltPos> pmSmallPltPosList = pmSmallPltPosRepository.selectByPdMatNoList(virtualSlabNoList, tenantId);
        Map<String, List<PmSmallPltPos>> pmSmallPltPosMap = pmSmallPltPosList.stream().collect(Collectors.groupingBy(PmSmallPltPos::getPdMatNo));
        List<PmSlabApplyMakeDTO> resList = new ArrayList<>();
        
        List<PmPdPlate> pmPdPlateList = pmPdPlateRepository.selectByPlateNoList(tenantId, virtualSlabNoList);
        List<Long> mmPlateIdList = pmPdPlateList.stream().map(PmPdPlate::getMmPlateId).distinct().collect(Collectors.toList());
        List<MmPlateVO> mmPlateVOList = mmPlateRepository.getMmPlateByPrimaryPlateId(mmPlateIdList, tenantId);
        Map<String, MmPlateVO> mmPlateMap = new HashMap<>(mmPlateVOList.size());
        if (CollectionUtils.isNotEmpty(mmPlateVOList)) {
            for (MmPlateVO mmPlateVO : mmPlateVOList) {
                mmPlateMap.put(mmPlateVO.getMatNo(), mmPlateVO);
            }
        }
        
        for (PmPdPlate pmPdPlate : slabInfo.values()) {
            MmPlateVO mmPlateVO = mmPlateMap.get(pmPdPlate.getPlateNo());
            if(mmPlateVO == null) {
                throw new CommonException("pm.pm_pd_plate_001");
            }
            PmSlabApplyMakeDTO applyMakeDTO = new PmSlabApplyMakeDTO();
            applyMakeDTO.setMatNo(pmPdPlate.getPlateNo());
            applyMakeDTO.setPpFlag(PdProdFlag.VIRTUAL_SLAB.getCode().intValue());
            applyMakeDTO.setOrdFlag(BaseConstants.Flag.YES.toString());
            applyMakeDTO.setPdMatStatus(Constants.PdStatusHp.PD_STATUS_HP_24);
            applyMakeDTO.setProcCd(mmPlateVO.getProcCd());
            applyMakeDTO.setWholeBacklogCode(mmPlateVO.getWholeBacklogCode());
            applyMakeDTO.setMoMatNo(pmPdPlate.getPlateNo());
            resList.add(applyMakeDTO);
            
            PmSlabApplyMakeDTO applyMakeDTO2 = new PmSlabApplyMakeDTO();
            applyMakeDTO2.setMatNo(pmPdPlate.getPlateNo());
            applyMakeDTO2.setPpFlag(PdProdFlag.VIRTUAL_COMPOSITE_SLAB.getCode().intValue());
            applyMakeDTO2.setOrdFlag(BaseConstants.Flag.YES.toString());
            applyMakeDTO2.setPdMatStatus(Constants.PdStatusHp.PD_STATUS_HP_24);
            applyMakeDTO2.setProcCd(mmPlateVO.getProcCd());
            applyMakeDTO2.setWholeBacklogCode(mmPlateVO.getWholeBacklogCode());
            applyMakeDTO2.setMoMatNo(pmPdPlate.getPlateNo());
            resList.add(applyMakeDTO2);
            String pdMatNo = Utils.removeLastThreeChars(pmPdPlate.getPlateNo());
            List<PmSmallPltPos> pmSmallPltPoss = pmSmallPltPosMap.get(pmPdPlate.getPlateNo());
            for (PmSmallPltPos pmSmallPltPos : pmSmallPltPoss) {
                PmSlabApplyMakeDTO applyMakeDTO3 = new PmSlabApplyMakeDTO();
                applyMakeDTO3.setMatNo(pdMatNo + pmSmallPltPos.getSmallPlateDivNo());
                applyMakeDTO3.setPpFlag(PdProdFlag.ACTUAL_SUB_SLAB.getCode().intValue());
                applyMakeDTO3.setOrdFlag(BaseConstants.Flag.YES.toString());
                applyMakeDTO3.setPdMatStatus(Constants.PdStatusHp.PD_STATUS_HP_24);
                applyMakeDTO3.setProcCd(mmPlateVO.getProcCd());
                applyMakeDTO3.setWholeBacklogCode(mmPlateVO.getWholeBacklogCode());
                applyMakeDTO3.setMoMatNo(pmPdPlate.getPlateNo());
                resList.add(applyMakeDTO3);
            }
        }
        log.info("<=====余材板坯调用命令作成事件，事件编码:{}, 入参为{}=====>", "PM_RESIDUE_COMMAND_MADE", JSON.toJSONString(resList));
        eventClient.sendEventWithOutResponse(tenantId, "PM_RESIDUE_COMMAND_MADE", resList);
    }

    @Override
    public void abandonContractEvent(List<String> virtualSlabNoList, Long tenantId, String pdMatStatus, String prodcCd, String wholeBacklogCode, String eventCode) {
        // 余材 试样添加，二次设计不抛
//        List<PmPdPlate> pmPdPlateList = pmPdPlateRepository.selectByPlateNoList(tenantId, virtualSlabNoList);
//        List<Long> mmPlateIdList = pmPdPlateList.stream().map(PmPdPlate::getMmPlateId).distinct().collect(Collectors.toList());
//        List<MmPlateVO> mmPlateVOList = mmPlateRepository.getMmPlateByPrimaryPlateId(mmPlateIdList, tenantId);
//        Map<String, MmPlateVO> mmPlateMap = new HashMap<>(mmPlateVOList.size());
//        if (CollectionUtils.isNotEmpty(mmPlateVOList)) {
//            for (MmPlateVO mmPlateVO : mmPlateVOList) {
//                mmPlateMap.put(mmPlateVO.getMatNo(), mmPlateVO);
//            }
//        }
//        List<PmSlabApplyMakeDTO> resList = new ArrayList<>();
//        for (String virtualSlabNo : virtualSlabNoList) {
//            PmSlabApplyMakeDTO applyMakeDTO = new PmSlabApplyMakeDTO();
//            MmPlateVO mmPlateVO = mmPlateMap.get(virtualSlabNo);
//            if(mmPlateVO == null) {
//                throw new CommonException("pm.pm_pd_plate_001");
//            }
//            applyMakeDTO.setMatNo(virtualSlabNo);
//            applyMakeDTO.setPpFlag(("BP".equals(mmPlateVO.getProdCd()) || "MP".equals(mmPlateVO.getProdCd())) ? 6 : 7);
//            applyMakeDTO.setOrdFlag("2");
//            applyMakeDTO.setPdMatStatus(pdMatStatus);
//            applyMakeDTO.setProcCd(Constants.ProdcCd.CD_13B);
//            applyMakeDTO.setWholeBacklogCode(Constants.WholeBacklogCode.CODE_13);
//            if(CtEventConstants.PM_COMMAND_MADE.equals(eventCode)) {
//                applyMakeDTO.setProcCd(mmPlateVO.getProcCd());
//                applyMakeDTO.setWholeBacklogCode(mmPlateVO.getWholeBacklogCode());
//            }
//            resList.add(applyMakeDTO);
//        }
//        if(CtEventConstants.PM_SAMPLE_ADDITION_CANCEL.equals(eventCode)) {
//            eventCode = "PM_RESIDUE_SAMPLE_ADDITION_CANCEL";
//        } else if(CtEventConstants.PM_SAMPLE_ADDITION.equals(eventCode)){
//            eventCode = "PM_RESIDUE_SAMPLE_ADDITION";
//        } else if(CtEventConstants.PM_SECONDARY_DESIGN_CANCEL.equals(eventCode)){
//            eventCode = "PM_RESIDUE_SECONDARY_DESIGN_CANCEL";
//        } else if(CtEventConstants.PM_SECONDARY_DESIGN.equals(eventCode)){
//            eventCode = "PM_RESIDUE_SECONDARY_DESIGN";
//        } else if(CtEventConstants.PM_COMMAND_MADE.equals(eventCode)) {
//            eventCode = "PM_RESIDUE_COMMAND_MADE";
//        }
//        log.info("<=====余材钢板调用试样添加事件，事件编码:{}, 入参为{}=====>", eventCode, JSON.toJSONString(resList));
//        eventClient.sendEventWithOutResponse(tenantId, eventCode, resList);
    }

    @Override
    protected List<String> filterPlateProcess(Map<String, List<String>> subBacklogCodeMap, List<String> list, PmCmdLargePlt pmCmdLargePltDb) {
        List<String> subBackLogCodeList = subBacklogCodeMap.get(pmCmdLargePltDb.getPlateNo());
        if (CollectionUtils.isEmpty(subBackLogCodeList)) {
            return list;
        }
        log.info("<=====要过滤的工序subBackLogCodeList:{}", JSON.toJSONString(subBackLogCodeList));
        List<String> resList = new ArrayList<>();
        for (String process : list) {
            // 是热处理工序，且生成过，则过滤掉
            if ((process.contains("A") || process.contains("N") || process.contains("Q") || process.contains("T")) &&
                    subBackLogCodeList.contains(process)) {
                continue;
            }
            resList.add(process);
        }
        return resList;
    }

    @Override
    protected Map<String, List<String>> getPassQualifiedPro(List<PmPdPlate> slabList, Long tenantId) {
        Map<String, List<String>> resMap = new HashMap<>(slabList.size());
        List<Long> plateIdList = slabList.stream().filter(item->item.getMmPlateId() != null)
                .map(PmPdPlate::getMmPlateId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(plateIdList)) {
            return resMap;
        }
        List<PassQualifiedProDTO> passQualifiedProList = mmEventClient.getPassQualifiedPro(tenantId, plateIdList);
        if (CollectionUtils.isEmpty(passQualifiedProList)) {
            return resMap;
        }
        Map<Long, List<String>> subCodeMap = passQualifiedProList.stream()
                .collect(Collectors.groupingBy(
                        PassQualifiedProDTO::getPlateId,
                        Collectors.mapping(PassQualifiedProDTO::getSubBacklogCode, Collectors.toList())
                ));
        for (PmPdPlate pmPdPlate : slabList) {
            resMap.put(pmPdPlate.getPlateNo(), subCodeMap.getOrDefault(pmPdPlate.getMmPlateId(), new ArrayList<>()));
        }
        return resMap;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void callMmMatStatusModify(Long tenantId, List<PmPdPlate> slabInfoList, List<String> tmpSlabNoList) {
        List<Long> mmPlateIdList = new ArrayList<>();
        List<String> plateNoList = new ArrayList<>();
        for (PmPdPlate pmPdPlate : slabInfoList) {
            mmPlateIdList.add(pmPdPlate.getMmPlateId());
            plateNoList.add(pmPdPlate.getPlateNo());
        }
        List<PmCmdCont> pmCmdContList = pmCmdContRepository.selectByCondition(Condition.builder(PmCmdCont.class)
                .andWhere(Sqls.custom().andIn(PmCmdCont.FIELD_PONO_SLAB_NO, tmpSlabNoList))
                .andWhere(Sqls.custom().andEqualTo(PmCmdCont.FIELD_TENANT_ID, tenantId))
                .andWhere(Sqls.custom().andEqualTo(PmCmdCont.FIELD_ORDER_SEQ_NO, 1L))
                .build());

        List<MmPlateVO> mmPlateList = mmPlateRepository.getMmPlateByPrimaryPlateId(mmPlateIdList, tenantId);
        Map<Long, MmPlateVO> mmPlateMap = new HashMap<>(mmPlateList.size());
        if (CollectionUtils.isNotEmpty(mmPlateList)) {
             mmPlateMap = mmPlateList.stream().collect(Collectors.toMap(MmPlateVO::getPlateId, Function.identity(), (k, v) -> k));
        }
        Map<String, PmCmdCont> pmCmdContMap = new HashMap<>(pmCmdContList.size());
        if (CollectionUtils.isNotEmpty(pmCmdContList)) {
            for (PmCmdCont pmCmdCont : pmCmdContList) {
                if(!pmCmdContMap.containsKey(pmCmdCont.getPlateNo())){
                    pmCmdContMap.put(pmCmdCont.getPlateNo(), pmCmdCont);
                }
            }
        }
        List<MatStatusModifyVO> matStatusModifyByTwoVOList = new ArrayList<>();
        Map<String , PmCmdVO> pmCmdVoMap = pmCmdLargePltService.queryCmdVo(tmpSlabNoList, tenantId, false);
        for (PmPdPlate pmPdPlate : slabInfoList) {
            MatStatusModifyVO matStatusModifyVO = new MatStatusModifyVO();
            matStatusModifyVO.setMatNo(pmPdPlate.getPlateNo());
            MmPlateVO mmPlate = mmPlateMap.get(pmPdPlate.getMmPlateId());
            if(mmPlate == null) {
                throw new CommonException("pm.pm_pd_plate_001");
            }
            matStatusModifyVO.setPpFlag(mmPlate.getProdCd());
            Map<String, Object> jsonMap = new HashMap<>(2);
            PmCmdCont pmCmdCont = pmCmdContMap.getOrDefault(pmPdPlate.getPlateNo(), new PmCmdCont());
            jsonMap.put("ponoSlab", pmCmdCont.getPonoSlabNo());
            jsonMap.put("contractCode", pmCmdCont.getOrderNo());
            jsonMap.put("relTime", new Date());
            jsonMap.put("relMaker", DetailsHelper.getUserDetails().getUserId());
            Map<String, Object> eventMap = new HashMap<>(16);
            List<EventFeignVO> ctEventList = new ArrayList<>();
            EventFeignVO eventFeignVO = new EventFeignVO();
            // 组装材料挂合同事件
            eventMap.put("contractNum", Utils.removeLastThreeChars(pmCmdCont.getOrderNo()));
            eventMap.put("orderNo", pmCmdCont.getOrderNo());
            eventMap.put("matNo", pmPdPlate.getPlateNo());
            eventMap.put("ppFlag", ("BP".equals(mmPlate.getProdCd()) || "MP".equals(mmPlate.getProdCd())) 
                    ? PdProdFlag.ACTUAL_COMPOSITE_SLAB.getCode() : PdProdFlag.ACTUAL_SUB_SLAB.getCode()); 
            eventMap.put("ordFlag", BaseConstants.Flag.YES.toString());
            eventMap.put("matStatus", Constants.PdStatusHp.PD_STATUS_HP_23);
            eventFeignVO.setEventContext(JSONObject.toJSONString(Collections.singletonList(eventMap)));
            eventFeignVO.setEventCode(Constants.EventCode.PM_MATERIAL_HANGING_CONTRACT);
            ctEventList.add(eventFeignVO);
            matStatusModifyVO.setCtEventList(ctEventList);
            
            matStatusModifyVO.setCmdVOList(new ArrayList<>());
            if(pmCmdVoMap.containsKey(pmCmdCont.getPonoSlabNo())) {
                matStatusModifyVO.getCmdVOList().add(pmCmdVoMap.get(pmCmdCont.getPonoSlabNo()));
            }
            matStatusModifyVO.setMatInformationString(JSONObject.toJSONString(jsonMap));
            matStatusModifyByTwoVOList.add(matStatusModifyVO);
            /*if ("31".equals(mmPlateMap.get(pmPdPlate.getPlateNo()))) {
                matStatusModifyByThreeVOList.add(matStatusModifyVO);
            }else {
                matStatusModifyByTwoVOList.add(matStatusModifyVO);
            }*/
        }

        log.info("余材钢板命令做成2F调用 异步调用存在事务性的问题：dtoList = {}", JSONObject.toJSONString(matStatusModifyByTwoVOList));
        mmEventClient.batchMatStatusModify(tenantId, Constants.EventCode.MM_RESIDUE_IN_PROCESS_BLOCKED_MATERIAL_BOARD_HANG_CONTRACT, matStatusModifyByTwoVOList);
        /*if (CollectionUtils.isNotEmpty(matStatusModifyByThreeVOList)) {
            log.info("余材钢板命令做成31调用：dtoList = {}", JSONObject.toJSONString(matStatusModifyByThreeVOList));
            mmEventClient.batchMatStatusModify(tenantId, Constants.EventCode.PM_HANG_ORDER_PROCESS_MATERIAL, matStatusModifyByThreeVOList);
        }

        if (CollectionUtils.isNotEmpty(matStatusModifyByTwoVOList)) {
            log.info("余材钢板命令做成21调用：dtoList = {}", JSONObject.toJSONString(matStatusModifyByTwoVOList));
            mmEventClient.batchMatStatusModify(tenantId, Constants.EventCode.MM_RESIDUE_IN_PROCESS_BLOCKED_MATERIAL_BOARD_HANG_CONTRACT, matStatusModifyByTwoVOList);
        }*/

    }

    @Override
    protected Map<String, String> queryProdYearByMatNo(Map<String, String> tmpSlabNoMap, List<String> matNoList, Long tenantId) {
        Map<String, String> resultMap = new HashMap<>(matNoList.size());
        List<PmPdPlate> pmPdPlateList = pmPdPlateRepository.selectByPlateNoList(tenantId, matNoList);
        List<Long> plateIdList = pmPdPlateList.stream().map(PmPdPlate::getMmPlateId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(plateIdList)) {
            throw new CommonException("pm.pd_plate_003");
        }
        List<MmPlateVO> mmPlateVOList = mmPlateRepository.getMmPlateByPrimaryPlateId(plateIdList, tenantId);
         if (CollectionUtils.isEmpty(mmPlateVOList)) {
             return resultMap;
         }
        Map<String, String> mmPlateVOMap = mmPlateVOList.stream().collect(Collectors.toMap(MmPlateVO::getMatNo, MmPlateVO::getProdYear));
        for (Map.Entry<String, String> entry : tmpSlabNoMap.entrySet()) {
            // 临时命令板坯号和prodYear的映射关系
            resultMap.put(entry.getKey(), mmPlateVOMap.getOrDefault(entry.getValue(), null));
         }
        return resultMap;
    }
}
