package com.xisc.pm.app.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.xisc.pm.api.dto.*;
import com.xisc.pm.api.dto.ctevent.ProContBoardEndDTO;
import com.xisc.pm.api.dto.ctevent.ProContOverDTO;
import com.xisc.pm.api.dto.model.*;
import com.xisc.pm.app.service.PmPdRecService;
import com.xisc.pm.app.service.strategy.PdMatTypeStrategyFactory;
import com.xisc.pm.app.service.strategy.pdmattype.PdMatTypeStrategy;
import com.xisc.pm.domain.entity.*;
import com.xisc.pm.domain.repository.*;
import com.xisc.pm.domain.vo.MatStatusModifyVO;
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.CtEventConstants;
import com.xisc.pm.infra.constant.PmPlateConstants;
import com.xisc.pm.infra.enums.PdEventType;
import com.xisc.pm.infra.enums.PdProdFlag;
import com.xisc.pm.infra.mapper.SubMapper;
import com.xisc.pm.app.service.SubService;
import com.xisc.pm.infra.util.BeanUtils;
import io.choerodon.core.exception.CommonException;
import io.choerodon.core.oauth.CustomUserDetails;
import io.choerodon.core.oauth.DetailsHelper;
import org.hzero.core.base.BaseConstants;
import org.hzero.core.redis.RedisHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class SubServiceImpl implements SubService {

    @Autowired
    private SubMapper subMapper;

    @Autowired
    private PmPdSlabRepository pmPdSlabRepository;

    @Autowired
    private PmCmdSlabRepository pmCmdSlabRepository;

    @Autowired
    private PmCmdLargePltRepository pmCmdLargePltRepository;

    @Autowired
    private PmPdContSumRepository pmPdContSumRepository;

    @Autowired
    private PmPdBlockPosRepository pmPdBlockPosRepository;

    @Autowired
    private PmPdPlateCutRepository pmPdPlateCutRepository;

    @Autowired
    private PmSmallPltPosRepository pmSmallPltPosRepository;

    @Autowired
    private PmCmdSmallPltRepository pmCmdSmallPltRepository;

    @Autowired
    private PmCmdContRepository pmCmdContRepository;

    @Autowired
    private PmSubHisRepository pmSubHisRepository;

    @Autowired
    private PmSubOrdRepository pmSubOrdRepository;

    @Autowired
    private PmPdSlabInfoRepository pmPdSlabInfoRepository;

    @Autowired
    private MmEventClient mmEventClient;

    @Autowired
    private RedisHelper redisHelper;

    @Autowired
    private PmPdRecService pmPdRecService;

    @Autowired
    private PmProContRepository pmProContRepository;

    @Autowired
    protected EventClient eventClient;

    //    获取板坯信息
    @Override
    public List<SlabInfoDO> getSlabInfo(SubParams subParams) {

        return subMapper.getSlabInfo(subParams);

    }

    //    获取订单信息
    @Override
    public List<PmProCont> getOrderInfo() {

        return subMapper.getOrderInfo();

    }

    //    生产合同产品属性表
    @Override
    public List<PmProContProdTtr> getPmProCountProdAttr() {

        return subMapper.getPmProCountProdAttr();

    }

    //    合同产线工艺参数表
    @Override
    public List<PmContMscTech> getPmContMscTech() {

        return subMapper.getPmContMscTech();

    }

    //    组板设计规则集
    @Override
    public List<PmPdRuleSet> getPmPdRuleSet() {

        return subMapper.getPmPdRuleSet();

    }

    //    组板设计规则集详细
    @Override
    public List<PmPdRuleItem> getPmPdRuleItem() {

        return subMapper.getPmPdRuleItem();

    }

    //    最小轧制厚度公差范围基准表
    @Override
    public List<PmMinThickTolBt> getPmMinThickTolBt() {

        return subMapper.getPmMinThickTolBt();

    }

    //    钢板轧制规格对应收得率基准表
    @Override
    public List<PmPlateSpecYieldBt> getPmPlateSpecYieldBt() {

        return subMapper.getPmPlateSpecYieldBt();

    }

    //    强度级别对照基准表
    @Override
    public List<PmStCodeBt> getPmStCodeBt() {

        return subMapper.getPmStCodeBt();

    }

    //    粗切位置决定基准表
    @Override
    public List<PmCutPlaceBt> getPmCutPlaceBt() {

        return subMapper.getPmCutPlaceBt();

    }

    //    组板常数基准表
    @Override
    public List<PmPdConstBt> getPmPdConstBt() {

        return subMapper.getPmPdConstBt();

    }

    //    钢板切缝切头尾范围基准表
    @Override
    public List<PmPlateCutLenBt> getPmPlateCutLenBt() {

        return subMapper.getPmPlateCutLenBt();

    }

    //    组板临时收得率基准表
    @Override
    public List<PmPdTmpYieldBt> getPmPdTmpYieldBt() {

        return subMapper.getPmPdTmpYieldBt();

    }

    //    钢板切边量基准表
    @Override
    public List<PmPlateTrimBt> getPmPlateTrimBt() {

        return subMapper.getPmPlateTrimBt();

    }

    //    余材组板内部钢种以优充次对照表
    @Override
    public List<PmSlabReplBt> getPmSlabReplBt() {

        return subMapper.getPmSlabReplBt();

    }

    //    热处理炉基准规格表
    @Override
    public List<PmHeatTreatFurnaceBt> getPmHeatTreatFurnaceBt() {

        return subMapper.getPmHeatTreatFurnaceBt();

    }

    //    特殊钢种余量增加表
    @Override
    public List<PmSpeStAddvBt> getPmSpeStAddvBt() {

        return subMapper.getPmSpeStAddvBt();

    }

    //合同处理成分结果数据
    @Override
    public List<QdToLineupResultCheDO> getQdToLineupResultChe() {
        return subMapper.getQdToLineupResultChe();
    }

    //合同处理坯料性能要求
    @Override
    public List<QdToLineupResultSlabphyDO> getQdToLineupResultSlabphy() {
        return subMapper.getQdToLineupResultSlabphy();
    }

    //炉次工序成分代表实绩表
    @Override
    public List<QeTqHeatcheStandResultDO> getQeTqHeatcheStandResult() {
        return subMapper.getQeTqHeatcheStandResult();
    }

    //炉次待委托表
    @Override
    public List<QeTqPonoWaitTestDO> getQeTqPonoWaitTest() {
        return subMapper.getQeTqPonoWaitTest();
    }

    //坯料低倍委托实绩表
    @Override
    public List<QeTqSlabMacroResultDO> getQeTqSlabMacroResult() {
        return subMapper.getQeTqSlabMacroResult();
    }

    //连铸板坯生产规格基准表
    @Override
    public List<PmSlabSpecBt> getPmSlabSpecBt() {
        return subMapper.getPmSlabSpecBt();
    }

    @Override
    public void batchInsert(List<PmSubOrd> pmSubOrdDOList) {
        subMapper.batchInsert(pmSubOrdDOList);
    }

    @Override
    public void batchInsertPmSubHisDO(List<PmSubHis> pmSubHisDOList) {
        subMapper.batchInsertPmSubHisDO(pmSubHisDOList);
    }

    @Override
    public void InsertPmSubHisDO(PmSubHis p) {
        subMapper.InsertPmSubHisDO(p);
    }

    @Override
    public void InsertPmSubOrdDO(PmSubOrd p) {
        subMapper.InsertPmSubOrdDO(p);
    }

    @Override
    public List<PmIrsubableCont> getPmIrsubableCont() {
        return subMapper.getPmIrsubableCont();
    }

    @Override
    public List<PmIrsubableSlab> getPmIrsubableSlab() {
        return subMapper.getPmIrsubableSlab();
    }

    @Override
    public List<PmAutoSubSetting> getPmAutoSubSetting() {
        return subMapper.getPmAutoSubSetting();
    }

    @Override
    public List<PmRetainSt> getPmRetainSt() {
        return subMapper.getPmRetainSt();
    }

    @Override
    public List<PmRetainStack> getPmRetainStack() {
        return subMapper.getPmRetainStack();
    }

    @Override
    public List<PmPdSlabInfo> getPmPdSlabInfo(SubParams subParams) {
        return subMapper.getPmPdSlabInfo(subParams);
    }

    @Override
    public List<QdToPoOrder> getQdToPoOrder() {
        return subMapper.getQdToPoOrder();
    }


//    @Override
//    public void batchInsertPmPdSlab(List<PmPdSlab> pmPdSlabDOList) {
////        BigDecimal id = subMapper.getMaxPmPdSlabId();
//        for (int i = 0 ; i < pmPdSlabDOList.size() ; i++){
////            pmPdSlabDOList.get(i).setPmPdSlabId(id.add(BigDecimal.valueOf(i+1)));
//            pmPdSlabDOList.get(i).setTenantId(1L);
//            pmPdSlabDOList.get(i).setObjectVersionNumber(1L);
//            subMapper.insertPmPdSlab(pmPdSlabDOList.get(i));
//        }
////        subMapper.batchInsertPmPdSlab(pmPdSlabDOList);
//    }
//
//    @Override
//    public void batchInsertPmCmdSlab(List<PmCmdSlab> pmCmdSlabDOList) {
////        BigDecimal id = subMapper.getMaxPmCmdSlabId();
//        for (int i = 0 ; i < pmCmdSlabDOList.size() ; i++){
////            pmCmdSlabDOList.get(i).setPmCmdSlabId(id.add(BigDecimal.valueOf(i+1)));
//            pmCmdSlabDOList.get(i).setTenantId(1L);
//            pmCmdSlabDOList.get(i).setObjectVersionNumber(1L);
//            subMapper.insertPmCmdSlab(pmCmdSlabDOList.get(i));
//        }
////        subMapper.batchInsertPmCmdSlab(pmCmdSlabDOList);
//    }
//
//    @Override
//    public void insertPmPdSlab(PmPdSlab p) {
//        subMapper.insertPmPdSlab(p);
//    }
//
//    @Override
//    public void batchInsertPmCmdLargePlt(List<PmCmdLargePlt> pmCmdLargePltDOList) {
////        BigDecimal id = subMapper.getMaxPmCmdLargePltId();
//        for (int i = 0 ; i < pmCmdLargePltDOList.size() ; i++){
////            pmCmdLargePltDOList.get(i).setPmCmdLargePltId(id.add(BigDecimal.valueOf(i+1)));
//            pmCmdLargePltDOList.get(i).setTenantId(1L);
//            pmCmdLargePltDOList.get(i).setObjectVersionNumber(1L);
//            subMapper.insertPmCmdLargePlt(pmCmdLargePltDOList.get(i));
//        }
//    }
//
//    @Override
//    public void batchInsertPmPdPlateCut(List<PmPdPlateCut> pmPdPlateCutDOList) {
////        BigDecimal id = subMapper.getMaxPmPdPlateCutId();
//        for (int i = 0 ; i < pmPdPlateCutDOList.size() ; i++){
////            pmPdPlateCutDOList.get(i).setCutId(id.add(BigDecimal.valueOf(i+1)));
//            pmPdPlateCutDOList.get(i).setTenantId(1L);
//            pmPdPlateCutDOList.get(i).setObjectVersionNumber(1L);
//            subMapper.insertPmPdPlateCut(pmPdPlateCutDOList.get(i));
//        }
//    }
//
//    @Override
//    public void batchInsertPmPdBlockPos(List<PmPdBlockPos> pmPdBlockPosDOList) {
////        BigDecimal id = subMapper.getMaxPmPdBlockPosId();
//        for (int i = 0 ; i < pmPdBlockPosDOList.size() ; i++){
////            pmPdBlockPosDOList.get(i).setPdBlockPosId(id.add(BigDecimal.valueOf(i+1)));
//            pmPdBlockPosDOList.get(i).setTenantId(1L);
//            pmPdBlockPosDOList.get(i).setObjectVersionNumber(1L);
//            subMapper.insertPmPdBlockPos(pmPdBlockPosDOList.get(i));
//        }
//    }
//
//    @Override
//    public void batchInsertPmPdContSum(List<PmPdContSum> pmPdContSumDOList) {
////        BigDecimal id = subMapper.getMaxPmPdContSumId();
//        for (int i = 0 ; i < pmPdContSumDOList.size() ; i++){
////            pmPdContSumDOList.get(i).setPpcsId(id.add(BigDecimal.valueOf(i+1)));
//            pmPdContSumDOList.get(i).setTenantId(1L);
//            pmPdContSumDOList.get(i).setObjectVersionNumber(1L);
//            subMapper.insertPmPdContSum(pmPdContSumDOList.get(i));
//        }
//    }
//
//    @Override
//    public void batchInsertPmSmallPltPos(List<PmSmallPltPos> pmSmallPltPosDOList) {
////        BigDecimal id = subMapper.getMaxPmSmallPltPosId();
//        for (int i = 0 ; i < pmSmallPltPosDOList.size() ; i++){
////            pmSmallPltPosDOList.get(i).setPsppId(id.add(BigDecimal.valueOf(i+1)));
//            pmSmallPltPosDOList.get(i).setTenantId(1L);
//            pmSmallPltPosDOList.get(i).setObjectVersionNumber(1L);
//            subMapper.insertPmSmallPltPos(pmSmallPltPosDOList.get(i));
//        }
//    }
//
//    @Override
//    public void batchInsertPmCmdSmallPlt(List<PmCmdSmallPlt> pmCmdSmallPltDOList) {
////        BigDecimal id = subMapper.getMaxPmCmdSmallPltId();
//        for (int i = 0 ; i < pmCmdSmallPltDOList.size() ; i++){
////            pmCmdSmallPltDOList.get(i).setPmCmdSmallPltId(id.add(BigDecimal.valueOf(i+1)));
//            pmCmdSmallPltDOList.get(i).setTenantId(1L);
//            pmCmdSmallPltDOList.get(i).setObjectVersionNumber(1L);
//            subMapper.insertPmCmdSmallPlt(pmCmdSmallPltDOList.get(i));
//        }
//    }
//
//    @Override
//    public void batchInsertPmCmdCont(List<PmCmdCont> pmCmdContDOList) {
////        BigDecimal id = subMapper.getMaxPmCmdContId();
//        for (int i = 0 ; i < pmCmdContDOList.size() ; i++){
////            pmCmdContDOList.get(i).setPmCmdContId(id.add(BigDecimal.valueOf(i+1)));
//            pmCmdContDOList.get(i).setTenantId(1L);
//            pmCmdContDOList.get(i).setObjectVersionNumber(1L);
//            subMapper.insertPmCmdCont(pmCmdContDOList.get(i));
//        }
//    }

    @Override
    public String GetMaxSeq(String pono) {
        return subMapper.GetMaxSeq(pono);
    }

    @Override
    public List<PmThickAddvBt> getPmThickAddvBt() {
        return subMapper.getPmThickAddvBt();
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void SaveData(SlabInfoDO p, List<PmPdSlab> pmPdSlabDOList, List<PmPdPlateCut> pmPdPlateCutDOList, List<PmPdBlockPos> pmPdBlockPosDOList, List<PmPdContSum> pmPdContSumDOList, List<PmSmallPltPos> pmSmallPltPosDOList, List<PmCmdSlab> pmCmdSlabDOList, List<PmCmdLargePlt> pmCmdLargePltDOList, List<PmCmdCont> pmCmdContDOList, List<PmCmdSmallPlt> pmCmdSmallPltDOList, List<PmSubHis> plistpmSubHisDO, List<PmSubOrd> plistpmSubOrdDO, List<PmPdRecDTO> pmPdRecDTOList, Long organizationId) {


        //抛合同事件
        String bizId = pmPdRecDTOList.get(0).getKeyvalue1();
        String key = String.format(PmPlateConstants.YIELD_EXEC_LOCK_KEY, organizationId.toString(), bizId);
        if (redisHelper.hasKey(key)) {
            throw new CommonException("pm.pm_pro_cont_043", bizId);
        }

        PdMatTypeStrategy pdMatTypeStrategy = PdMatTypeStrategyFactory.matchStrategy("S");
        try {
            // 抛合同组板事件
            pmPdRecService.createEvent(organizationId, pmPdRecDTOList
                    , PdEventType.HP04, Constants.SYSTEM_PD_CODE, pdMatTypeStrategy.pdProgram());
            redisHelper.strSet(key, "1", 30L, TimeUnit.SECONDS);
            // 抛组板履历事件
            List<PmPdRecDTO> pmPdRecs = new ArrayList<>();
            CustomUserDetails userDetails = DetailsHelper.getUserDetails();
            for (PmPdContSum contSum : pmPdContSumDOList) {
                PmPdRecDTO pmPdRecDTO = new PmPdRecDTO();
                pmPdRecDTO.setOrderNo(contSum.getOrderNo());
                pmPdRecDTO.setEventMaker(userDetails == null ? "" : userDetails.getUserId().toString());
                pmPdRecDTO.setMscLineNo(pmPdRecDTOList.get(0).getMscLineNo());
                pmPdRecDTO.setKeyvalue1(bizId);
                pmPdRecDTO.setKeyvalue1Desc(PmPdRecDTO.DESC_VIRTUAL_SLAB_NO);
                pmPdRecDTO.setMatNo(bizId);
                pmPdRecDTO.setQty(contSum.getOrderPlateNum());
                pmPdRecs.add(pmPdRecDTO);
            }
            pmPdRecService.createEvent(organizationId, pmPdRecs, PdEventType.HP10,
                    Constants.SYSTEM_PD_CODE, "余材板坯组板");
            System.out.println("抛合同组板事件 执行成功");
            // 抛合同


        } finally {
            // 抛合同组板释放事件
            pmPdRecService.createEvent(organizationId, pmPdRecDTOList
                    , PdEventType.HP05, Constants.SYSTEM_PD_CODE, pdMatTypeStrategy.pdProgram());
            redisHelper.delKey(key);
        }


        if (plistpmSubHisDO.size() > 0) {
            pmSubHisRepository.batchInsertSelective(plistpmSubHisDO);
            System.out.println("pmSubHis表 保存成功");
        }
        if (plistpmSubOrdDO.size() > 0) {
            pmSubOrdRepository.batchInsertSelective(plistpmSubOrdDO);
            System.out.println("pmSubOrd表 保存成功");
        }
//
        if (pmPdSlabDOList.size() > 0) {
            pmPdSlabRepository.batchInsertSelective(pmPdSlabDOList);
            System.out.println("PmPdSlab表 保存成功");
        }
        if (pmPdPlateCutDOList.size() > 0) {
            pmPdPlateCutRepository.batchInsertSelective(pmPdPlateCutDOList);
            System.out.println("PmPdPlateCut 大板粗切位置 保存成功");
        }
        if (pmPdBlockPosDOList.size() > 0) {
            pmPdBlockPosRepository.batchInsertSelective(pmPdBlockPosDOList);
            System.out.println("PmPdBlockPos 组合子板位置表 保存成功");
        }
        if (pmPdContSumDOList.size() > 0) {
            pmPdContSumRepository.batchInsertSelective(pmPdContSumDOList);
            System.out.println("PmPdContSum 合同总量表 保存成功");
        }
        if (pmSmallPltPosDOList.size() > 0) {
            pmSmallPltPosRepository.batchInsertSelective(pmSmallPltPosDOList);
            System.out.println("PmSmallPltPos 命令小板位置表  保存成功");
        }
        if (pmCmdSlabDOList.size() > 0) {
            pmCmdSlabRepository.batchInsertSelective(pmCmdSlabDOList);
            System.out.println("PmCmdSlab 保存成功");
        }
        if (pmCmdLargePltDOList.size() > 0) {
            pmCmdLargePltRepository.batchInsertSelective(pmCmdLargePltDOList);
            System.out.println("pmCmdLargePltDO 命令大板 保存成功");
        }
        if (pmCmdContDOList.size() > 0) {
            pmCmdContRepository.batchInsertSelective(pmCmdContDOList);
            System.out.println("PmCmdCont 命令参数 保存成功");
        }
        if (pmCmdSmallPltDOList.size() > 0) {
            pmCmdSmallPltRepository.batchInsertSelective(pmCmdSmallPltDOList);
            System.out.println("PmCmdSmallPlt 命令小板表 保存成功");
        }

        // 调用mm接口
        // 查询MM_SLAB 信息获取板坯信息
        PmPdSlabSplitDTO pmPdSlabSplitDTO = pmPdSlabRepository.selectMmSlabBySlabId(p.getSlabId(), p.getTenantId());
        // 查询板坯信息表。第一次查询可能无值
        PmPdSlabInfo pmPdSlabInfo = pmPdSlabInfoRepository.selectBySlabNo(p.getSlabNo(), p.getTenantId());

        pmPdSlabInfo = buildPmPdSlabInfo(pmPdSlabSplitDTO, p.getSlabNo(), p.getMscLineNo(), p.getTenantId());
        pmPdSlabInfoRepository.insertSelective(pmPdSlabInfo);
        System.out.println("pmPdSlabInfo表 保存成功");

        //抛合同
        zbCtEventRec(organizationId, pmPdSlabDOList, pmCmdLargePltDOList, pmCmdSlabDOList, pmSmallPltPosDOList);
        System.out.println("抛合同事件完成");

        // 轧钢模块“在制品材料管理封锁事件接口"
        callMatStatusModify(pmPdSlabInfo);
        System.out.println("mm模块接口 调用成功");
        System.out.println("程序执行完成！");

    }

    public void zbCtEventRec(Long organizationId, List<PmPdSlab> pmPdSlabDOList, List<PmCmdLargePlt> pmCmdLargePltDOList, List<PmCmdSlab> pmCmdSlabDOList, List<PmSmallPltPos> pmSmallPltPosDOList) {
        for (PmPdSlab pmPdSlab : pmPdSlabDOList) {
            List<PmSmallPltPos> smallPltPosList = pmSmallPltPosDOList.stream()
                    .filter(p -> p.getPdMatNo().equals(pmPdSlab.getVirtualSlabNo()))
                    .collect(Collectors.toList());
            List<PmCmdLargePlt> largePltList = pmCmdLargePltDOList.stream()
                    .filter(p -> p.getPonoSlabNo().equals(pmPdSlab.getVirtualSlabNo()))
                    .collect(Collectors.toList());
            List<PmCmdSlab> slabList = pmCmdSlabDOList.stream()
                    .filter(p -> p.getVirtualSlabNo().equals(pmPdSlab.getVirtualSlabNo()))
                    .collect(Collectors.toList());
            List<String> orderNoList = smallPltPosList.stream().map(p -> p.getOrderNo()).collect(Collectors.toList());
            Map<String, PmProContFilterResposeDTO> filterOrderMap = pmProContRepository.orderFilterMap(orderNoList, organizationId, pmPdSlab.getMscLineNo());
            List<ProContBoardEndDTO> boardEndEventList = new ArrayList<>();
            PmProContFilterResposeDTO axisPmProCont = filterOrderMap.get(orderNoList.get(0));

            // 虚拟板坯维度
            boardEndEventList.add(new ProContBoardEndDTO(axisPmProCont.getOrderNo()).setMatNo(pmPdSlab.getVirtualSlabNo())
                    .setLen(pmPdSlab.getPreCleanSlabLen()).setMoMatNo(null)
                    .setMscLineNo(pmPdSlab.getMscLineNo())
                    .setPdMatStatus(Constants.PdStatusHp.PD_STATUS_HP_12).setPpFlag(PdProdFlag.VIRTUAL_SLAB.getCode())
                    .setThk(slabList.get(0).getMoldThick())
                    .setWid(slabList.get(0).getMoldWid())
                    .setWgt(pmPdSlab.getPreCleanSlabWt())
                    .setWholeBacklogCode(axisPmProCont.getWholeBacklogCode())
                    .setStNo(axisPmProCont.getStNo())
                    .setSgSign(axisPmProCont.getSgSign())
                    .setProcCd(Constants.ProdcCd.CD_13B));

            BigDecimal weight = largePltList.get(0).getRollAimLen().multiply(largePltList.get(0).getRollAimThick()).multiply(largePltList.get(0).getRollAimWidth()).multiply(new BigDecimal(7.85)).divide(new BigDecimal(1000000000), PmPlateConstants.WT_SCALE, RoundingMode.HALF_UP);
            // 大板维度
            boardEndEventList.add(new ProContBoardEndDTO(axisPmProCont.getOrderNo()).setMatNo(pmPdSlab.getVirtualSlabNo())
                    .setLen(largePltList.get(0).getRollAimLen()).setMoMatNo(null)
                    .setMscLineNo(pmPdSlab.getMscLineNo())
                    .setPdMatStatus(Constants.PdStatusHp.PD_STATUS_HP_12).setPpFlag(PdProdFlag.VIRTUAL_COMPOSITE_SLAB.getCode())
                    .setThk(largePltList.get(0).getRollAimThick())
                    .setWid(largePltList.get(0).getRollAimWidth())
                    .setWgt(weight)
                    .setWholeBacklogCode(axisPmProCont.getWholeBacklogCode())
                    .setStNo(axisPmProCont.getStNo())
                    .setSgSign(axisPmProCont.getSgSign())
                    .setProcCd(Constants.ProdcCd.CD_13B));
            for (PmSmallPltPos pmSmallPltPos : pmSmallPltPosDOList) {
                PmProContFilterResposeDTO pmProContFilterResposeDTO = filterOrderMap.get(pmSmallPltPos.getOrderNo());
                // 小板维度
                boardEndEventList
                        .add(new ProContBoardEndDTO(pmSmallPltPos.getOrderNo()).setMatNo(pmPdSlab.getVirtualSlabNo() + pmSmallPltPos.getSmallPlateDivNo())
                                .setLen(pmSmallPltPos.getOrderLen()).setMoMatNo(pmSmallPltPos.getPdMatNo())
                                .setMscLineNo(pmPdSlab.getMscLineNo())
                                .setPdMatStatus(Constants.PdStatusHp.PD_STATUS_HP_12).setPpFlag(PdProdFlag.SMALL_POS_VIRTUAL_SLAB.getCode())
                                .setThk(pmSmallPltPos.getOrderThick()).setWid(pmSmallPltPos.getOrderWidth())
                                .setWgt(pmProContFilterResposeDTO.getOrderUnitAimWt())
                                .setWholeBacklogCode(axisPmProCont.getWholeBacklogCode())
                                .setStNo(pmProContFilterResposeDTO.getStNo())
                                .setSgSign(pmProContFilterResposeDTO.getSgSign())
                                .setProcCd(Constants.ProdcCd.CD_13B));
            }
            eventClient.addEvent(CtEventConstants.PM_CONTRACT_RESIDUE_BOARD_END, boardEndEventList);
            eventClient.sendEvent(organizationId);
            System.out.println("抛合同接口 执行成功");
        }
    }

    /**
     * 组装板坯信息表数据
     *
     * @param pmPdSlabSplitDTO
     * @return
     */
    private PmPdSlabInfo buildPmPdSlabInfo(PmPdSlabSplitDTO pmPdSlabSplitDTO, String slabNo, String mscLineNo, Long tenantId) {
        PmPdSlabInfo pmPdSlabInfo = BeanUtils.copy(pmPdSlabSplitDTO, PmPdSlabInfo.class);
        pmPdSlabInfo.setOuBacklogCode("3");
        pmPdSlabInfo.setOuBacklogSeq(13L);
        pmPdSlabInfo.setMatStatus(pmPdSlabSplitDTO.getSlabStatus());
//        pmPdSlabInfo.setSlabFlag(pmPdSlabSplitDTO.getSlabFlag());
        pmPdSlabInfo.setSlabFlag("S");
        pmPdSlabInfo.setPdStatusHp(Constants.PdStatusHp.PD_STATUS_HP_11);
        pmPdSlabInfo.setAutoManualFlag(Constants.AutoManualFlag.AUTO_MANUA_FLAG_M);
        pmPdSlabInfo.setPrecStNo(pmPdSlabSplitDTO.getPreStNo());
//        pmPdSlabInfo.setFinStNo(finStNo);
        pmPdSlabInfo.setAutoManualFlag(Constants.AutoManualFlag.AUTO_MANUA_FLAG_M);
        pmPdSlabInfo.setCanAssignFlag(BaseConstants.Flag.YES.toString());
        pmPdSlabInfo.setMatActThick(pmPdSlabSplitDTO.getSlabActThk());
        pmPdSlabInfo.setMatActLen(pmPdSlabSplitDTO.getSlabActLen());
        pmPdSlabInfo.setMatActWidth(pmPdSlabSplitDTO.getSlabActWid());
        pmPdSlabInfo.setMatActWt(pmPdSlabSplitDTO.getSlabActWgt());
        pmPdSlabInfo.setIngotType(pmPdSlabSplitDTO.getSlabType());
        pmPdSlabInfo.setOldMscLine(pmPdSlabSplitDTO.getMscLineNo());
        pmPdSlabInfo.setSlabNo(slabNo);
        pmPdSlabInfo.setTwoRollInterimFlag(BaseConstants.Flag.NO.longValue());
        pmPdSlabInfo.setTenantId(tenantId);
        // 若MAT_NO首位为‘G’或第三位为‘5’，则为‘A’，否则为‘1’
        String matNo = pmPdSlabSplitDTO.getSlabNo();
        if (matNo.charAt(0) == 'G' || matNo.charAt(2) == '5') {
            pmPdSlabInfo.setCcType("A");
        } else {
            pmPdSlabInfo.setCcType(Constants.CcType.TYPE_1);
        }
        pmPdSlabInfo.setMscLineNo(mscLineNo);
        pmPdSlabInfo.setPono(pmPdSlabSplitDTO.getPono());
        pmPdSlabInfo.setPsc(pmPdSlabSplitDTO.getPsc());
        pmPdSlabInfo.setSgSign(pmPdSlabSplitDTO.getSgSign());
        pmPdSlabInfo.setStNo(pmPdSlabSplitDTO.getStNo());
        pmPdSlabInfo.setApn(pmPdSlabSplitDTO.getApn());
        pmPdSlabInfo.setMsc(pmPdSlabSplitDTO.getMsc());
        return pmPdSlabInfo;
    }

    private void callMatStatusModify(PmPdSlabInfo pmPdSlabInfo) {
        MatStatusModifyVO matStatusModifyVO = new MatStatusModifyVO();
        matStatusModifyVO.setMatNo(pmPdSlabInfo.getSlabNo());
        matStatusModifyVO.setPpFlag(Constants.PpFlag.SM);
        Map<String, Object> informationMap = new HashMap<>(16);
        informationMap.put("holdCauseCode", Constants.HoldCauseCode.PM01);
        informationMap.put("holdCauseCodeMeaning", "余材板坯智能组板");
        informationMap.put("holdTime", new Date());
        informationMap.put("holdMaker", DetailsHelper.getUserDetails().getUserId());
        informationMap.put("plateLockRegion", "Q77");
        matStatusModifyVO.setMatInformationString(JSONObject.toJSONString(informationMap));
        mmEventClient.matStatusModify(pmPdSlabInfo.getTenantId(), Constants.EventCode.MM_PM_PROD_MANAGEMENT_BLOCKED_IN_PROCESS_MATERIAL, matStatusModifyVO);
    }


}
