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

import com.google.common.collect.Maps;
import com.xisc.pm.app.service.PmCmdSlabService;
import com.xisc.pm.app.service.PmRuleCodeService;
import com.xisc.pm.domain.entity.PmCmdCont;
import com.xisc.pm.domain.entity.PmCmdLargePlt;
import com.xisc.pm.domain.entity.PmCmdSlab;
import com.xisc.pm.domain.entity.PmCmdSmallPlt;
import com.xisc.pm.domain.entity.PmContMscTech;
import com.xisc.pm.domain.entity.PmPdContSum;
import com.xisc.pm.domain.entity.PmPdPlate;
import com.xisc.pm.domain.entity.PmPdSlab;
import com.xisc.pm.domain.entity.PmPdSlabInfo;
import com.xisc.pm.domain.entity.PmProContProdTtr;
import com.xisc.pm.domain.entity.PmSmallPltPos;
import com.xisc.pm.domain.repository.PmCmdContRepository;
import com.xisc.pm.domain.repository.PmCmdLargePltRepository;
import com.xisc.pm.domain.repository.PmCmdSlabRepository;
import com.xisc.pm.domain.repository.PmCmdSmallPltRepository;
import com.xisc.pm.domain.repository.PmContMscTechRepository;
import com.xisc.pm.domain.repository.PmPdContSumRepository;
import com.xisc.pm.domain.repository.PmProContProdTtrRepository;
import com.xisc.pm.domain.repository.PmSmallPltPosRepository;
import com.xisc.pm.infra.constant.Constants;
import com.xisc.pm.infra.constant.Constants.PdStatusHp;
import com.xisc.pm.infra.constant.PmPlateConstants;
import com.xisc.pm.infra.util.BeanUtils;
import com.xisc.pm.infra.util.Utils;
import io.choerodon.core.domain.Page;
import io.choerodon.core.domain.PageInfo;
import io.choerodon.core.exception.CommonException;
import io.choerodon.mybatis.pagehelper.PageHelper;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.hzero.core.base.AopProxy;
import org.hzero.core.base.BaseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 命令板坯表(PmCmdSlab)应用服务
 *
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @since 2024-08-01 10:24:07
 */
@Service
public class PmCmdSlabServiceImpl implements PmCmdSlabService , AopProxy<PmCmdSlabService>{
    
    @Autowired
    private PmCmdLargePltRepository pmCmdLargePltRepository;
    
    @Autowired
    private PmCmdSmallPltRepository pmCmdSmallPltRepository;
    
    @Autowired
    private PmProContProdTtrRepository pmProContProdTtrRepository;
    
    @Autowired
    private PmContMscTechRepository pmContMscTechRepository;
    
    @Autowired
    private PmPdContSumRepository pmPdContSumRepository;
    
    @Autowired
    private PmSmallPltPosRepository pmSmallPltPosRepository;
    
    @Autowired
    private PmCmdContRepository pmCmdContRepository;
    
    @Autowired
    private PmRuleCodeService pmRuleCodeService;

    private PmCmdSlabRepository pmCmdSlabRepository;

    public PmCmdSlabServiceImpl(PmCmdSlabRepository pmCmdSlabRepository) {
        this.pmCmdSlabRepository = pmCmdSlabRepository;
    }

    @Override
    public Page<PmCmdSlab> selectList(PageRequest pageRequest, PmCmdSlab pmCmdSlab) {
        Page<PmCmdSlab> page = PageHelper.doPage(pageRequest, () -> pmCmdSlabRepository.selectList(pmCmdSlab));
        if (CollectionUtils.isEmpty(page.getContent())) {
            return page;
        }
        // 将合并板坯的两块短坯排序到合并板坯后面
        List<String> ponoSlabNoList = new ArrayList<>();
        Map<String, List<PmCmdSlab>> cmdSlabMap = new HashMap<>(page.getContent().size());
        for (PmCmdSlab cmdSlab : page.getContent()) {
            if (StringUtils.isNotEmpty(cmdSlab.getPonoSlabNo())) {
                ponoSlabNoList.add(cmdSlab.getPonoSlabNo());
            }
        }
        List<String> mergePonoSlabNoList = new ArrayList<>();
        for (PmCmdSlab cmdSlab : page.getContent()) {
            if (StringUtils.isNotEmpty(cmdSlab.getSlabNo()) && ponoSlabNoList.contains(cmdSlab.getSlabNo())) {
                if (!cmdSlabMap.containsKey(cmdSlab.getSlabNo())) {
                    cmdSlabMap.put(cmdSlab.getSlabNo(), new ArrayList<>());
                }
                mergePonoSlabNoList.add(cmdSlab.getPonoSlabNo());
                cmdSlabMap.get(cmdSlab.getSlabNo()).add(cmdSlab);
            }
        }
        List<PmCmdSlab> resultList = new ArrayList<>();
        for (PmCmdSlab cmdSlab : page.getContent()) {
            if (CollectionUtils.isNotEmpty(mergePonoSlabNoList) && mergePonoSlabNoList.contains(cmdSlab.getPonoSlabNo())) {
                continue;
            }
            resultList.add(cmdSlab);
            if (cmdSlabMap.containsKey(cmdSlab.getPonoSlabNo())) {
                resultList.addAll(cmdSlabMap.get(cmdSlab.getPonoSlabNo()));
            }
        }
        return new Page<>(resultList, new PageInfo(pageRequest.getPage(), pageRequest.getSize()), page.getTotalElements());
    }

    @Override
    public void saveData(List<PmCmdSlab> pmCmdSlabs) {
        List<PmCmdSlab> insertList = pmCmdSlabs.stream().filter(line -> line.getPmCmdSlabId() == null)
                .collect(Collectors.toList());
        List<PmCmdSlab> updateList = pmCmdSlabs.stream().filter(line -> line.getPmCmdSlabId() != null)
                .collect(Collectors.toList());
        pmCmdSlabRepository.batchInsertSelective(insertList);
        pmCmdSlabRepository.batchUpdateByPrimaryKeySelective(updateList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createPmCmdSlab(PmPdSlabInfo pmPdSlabInfo, List<PmPdSlab> pmPdSlabs, String mscLineNo) {
        int size = pmPdSlabs.size();
        
        List<String> virtualSlabNoList = new ArrayList<String>();
        
        List<PmCmdSlab> pmCmdSlabs = new ArrayList<>(pmPdSlabs.size());
        List<PmCmdLargePlt> pmCmdLargePlts = new ArrayList<>(pmPdSlabs.size());
        
        Map<String, PmPdSlab> pmPdSlabMap = new HashMap<>(pmPdSlabs.size());
        List<String> orderNoList = new ArrayList<String>();
        for (PmPdSlab pmPdSlab : pmPdSlabs) {
            orderNoList.add(pmPdSlab.getOrderNoAxis());
            virtualSlabNoList.add(pmPdSlab.getVirtualSlabNo());
            pmPdSlabMap.put(pmPdSlab.getVirtualSlabNo(), pmPdSlab);
        }
        
        List<PmPdContSum> contSums = pmPdContSumRepository.selectByPdMatNoList(pmPdSlabInfo.getTenantId(), virtualSlabNoList);
        
        for (PmPdContSum pmPdContSum : contSums) {
            orderNoList.add(pmPdContSum.getOrderNo());
        }
        List<PmCmdCont> pmCmdConts = new ArrayList<>(contSums.size());
        List<PmCmdSmallPlt> pmCmdSmallPlts = new ArrayList<>();
        
        List<PmProContProdTtr> pmProContProdTtrs = pmProContProdTtrRepository.selectByOrderNoList(orderNoList, pmPdSlabInfo.getTenantId());
        List<PmContMscTech> pmContMscTechs = pmContMscTechRepository.selectByOrderNoAndMscLineNo(orderNoList, pmPdSlabInfo.getMscLineNo(), pmPdSlabInfo.getTenantId());
        
        Map<String, PmProContProdTtr> prodTtrMap = Maps.uniqueIndex(pmProContProdTtrs, PmProContProdTtr::getOrderNo);
        Map<String, PmContMscTech> mscTechMap = Maps.uniqueIndex(pmContMscTechs, PmContMscTech::getOrderNo);
        
        List<PmSmallPltPos> pmSmallPltPoss = pmSmallPltPosRepository.selectPmSmallPltPos(pmPdSlabInfo.getTenantId(), virtualSlabNoList);
        Map<String , String> divNoMap = new HashMap<>(virtualSlabNoList.size());
        for (PmSmallPltPos pmSmallPltPos : pmSmallPltPoss) {
            if(!divNoMap.containsKey(pmSmallPltPos.getPdMatNo())) {
                divNoMap.put(pmSmallPltPos.getPdMatNo(), pmSmallPltPos.getSmallPlateDivNo());
            }
            String divNo = divNoMap.get(pmSmallPltPos.getPdMatNo());
            if(pmSmallPltPos.getSmallPlateDivNo().compareTo(divNo) > 0) {
                divNoMap.put(pmSmallPltPos.getPdMatNo(), pmSmallPltPos.getSmallPlateDivNo());
            }
            PmPdSlab pmPdSlab = pmPdSlabMap.get(pmSmallPltPos.getPdMatNo()) ;
            PmCmdSmallPlt pmCmdSmallPlt = buildPmCmdSmallPlt(pmPdSlab, pmSmallPltPos);
            pmCmdSmallPlts.add(pmCmdSmallPlt);
        }
        
        for (PmPdSlab pmPdSlab : pmPdSlabs) {
            PmProContProdTtr axisPmProContProdTtr = prodTtrMap.get(pmPdSlab.getOrderNoAxis());
            PmContMscTech axisPmContMscTech = mscTechMap.get(pmPdSlab.getOrderNoAxis());
            PmCmdSlab pmCmdSlab = buildPmCmdSlab(pmPdSlabInfo, pmPdSlab,axisPmProContProdTtr,axisPmContMscTech);
            PmPdSlab lastPmPdSlab = pmPdSlabs.get(size - 1);
            pmCmdSlab.setShortSlabNumInLong(size + 0L);
            // 判断最后一个是不是无合同钢板，如果不是，那全部都是有合同的。pmPdSlabs按顺序号排序
            if(BaseConstants.Flag.NO.toString().equals(lastPmPdSlab.getOrderRemainDiv())) {
                pmCmdSlab.setBigPlateNum(size - 1L);
            } else {
                pmCmdSlab.setBigPlateNum(size + 0L);
            }
            pmCmdSlabs.add(pmCmdSlab);
            if(!BaseConstants.Flag.NO.toString().equals(pmPdSlab.getOrderRemainDiv())) {
                PmCmdLargePlt pmCmdLargePlt = buildPmCmdLargePlt(pmPdSlabInfo, pmPdSlab, divNoMap , axisPmProContProdTtr , axisPmContMscTech);
                pmCmdLargePlts.add(pmCmdLargePlt);
            } else {
                // 剩余的那块余坯生成火切工序，并直接修改状态
                switch (Utils.mscLineNoToRoolLineNo(pmCmdSlab.getMscLineNo())) {
                    case "10":
                        pmCmdSlab.setSlabPreProc1("1B2");
                        break;
                    case "20":
                        pmCmdSlab.setSlabPreProc1("2B2");
                        break;
                    case "30":
                        pmCmdSlab.setSlabPreProc1("5B2");
                        break;
                    default:
                }
            }
        }
        
        for (PmPdContSum pmPdContSum : contSums) {
            PmCmdCont pmCmdCont = buildPmCmdCont(pmPdSlabMap.get(pmPdContSum.getPdMatNo()) , pmPdContSum , prodTtrMap.get(pmPdContSum.getOrderNo()) , mscTechMap.get(pmPdContSum.getOrderNo()));
            pmCmdCont.setMscLineNo(mscLineNo);
            pmCmdConts.add(pmCmdCont);
        }
        if(pmCmdLargePlts != null && !pmCmdLargePlts.isEmpty()) {
            pmCmdLargePltRepository.batchInsertSelective(pmCmdLargePlts);
        }
        if(pmCmdConts != null && !pmCmdConts.isEmpty()) {
            pmCmdContRepository.batchInsertSelective(pmCmdConts);
        }
        if(pmCmdSmallPlts != null && !pmCmdSmallPlts.isEmpty()) {
            pmCmdSmallPltRepository.batchInsertSelective(pmCmdSmallPlts);
        }
        if(pmCmdSlabs != null && !pmCmdSlabs.isEmpty()) {
            pmCmdSlabRepository.batchInsertSelective(pmCmdSlabs);
        }
        
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createPmCmdByPlate(PmPdPlate pmPdPlate, List<PmPdContSum> pmPdContSums,
            List<PmSmallPltPos> pmSmallPltPoss , String mscLineNo) {
        List<String> orderNoList = new ArrayList<String>();
        String axisOrderNo = null;
        for (PmPdContSum pmPdContSum : pmPdContSums) {
            orderNoList.add(pmPdContSum.getOrderNo());
            if(pmPdContSum.getAxisFlag() != null && BaseConstants.Flag.YES.longValue() == pmPdContSum.getAxisFlag().longValue()) {
                axisOrderNo = pmPdContSum.getOrderNo();
            }
        }
        if(StringUtils.isBlank(axisOrderNo)) {
            throw new CommonException("pm.pm_pro_cont_048");
        }
        List<PmCmdCont> pmCmdConts = new ArrayList<>(pmPdContSums.size());
        List<PmCmdSmallPlt> pmCmdSmallPlts = new ArrayList<>();
        List<PmProContProdTtr> pmProContProdTtrs = pmProContProdTtrRepository.selectByOrderNoList(orderNoList, pmPdPlate.getTenantId());
        List<PmContMscTech> pmContMscTechs = pmContMscTechRepository.selectByOrderNoAndMscLineNo(orderNoList, mscLineNo, pmPdPlate.getTenantId());
        Map<String, PmProContProdTtr> prodTtrMap = Maps.uniqueIndex(pmProContProdTtrs, PmProContProdTtr::getOrderNo);
        Map<String, PmContMscTech> mscTechMap = Maps.uniqueIndex(pmContMscTechs, PmContMscTech::getOrderNo);
        String tmpSlabNo = pmRuleCodeService.generateCode(pmPdPlate.getTenantId(), Constants.CodeRule.PM_PD_PLATE_TMP_SLAB_NO, Utils.removeLastSecondChars(pmPdPlate.getPlateNo()), 3);
        PmProContProdTtr axisPmProContProdTtr = prodTtrMap.get(axisOrderNo);
        PmContMscTech axisPmContMscTech = mscTechMap.get(axisOrderNo);
        
        String maxDivNo = null;
        for (PmSmallPltPos pmSmallPltPos : pmSmallPltPoss) {
            if(StringUtils.isBlank(maxDivNo) || pmSmallPltPos.getSmallPlateDivNo().compareTo(maxDivNo) > 0) {
                maxDivNo = pmSmallPltPos.getSmallPlateDivNo();
            }
            PmCmdSmallPlt pmCmdSmallPlt = buildPmCmdSmallPlt(tmpSlabNo ,pmPdPlate , pmSmallPltPos);
            pmCmdSmallPlts.add(pmCmdSmallPlt);
        }
        
        PmCmdLargePlt pmCmdLargePlt = buildPmCmdLargePlt(tmpSlabNo , pmPdPlate, maxDivNo, axisPmProContProdTtr, axisPmContMscTech, mscLineNo);
        
        for (PmPdContSum pmPdContSum : pmPdContSums) {
            PmCmdCont pmCmdCont = buildPmCmdCont(tmpSlabNo ,pmPdPlate , pmPdContSum , prodTtrMap.get(pmPdContSum.getOrderNo()) , mscTechMap.get(pmPdContSum.getOrderNo()));
            pmCmdConts.add(pmCmdCont);
        }
        
        pmCmdLargePltRepository.insertSelective(pmCmdLargePlt);
        pmCmdContRepository.batchInsertSelective(pmCmdConts);
        pmCmdSmallPltRepository.batchInsertSelective(pmCmdSmallPlts);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removePmCmdByPlateNoList(Long tenantId, String plateNo) {
        PmCmdLargePlt pmCmdLargePlt = new PmCmdLargePlt();
        pmCmdLargePlt.setTenantId(tenantId);
        pmCmdLargePlt.setPlateNo(plateNo);
        pmCmdLargePltRepository.delete(pmCmdLargePlt);
        
        PmCmdCont pmCmdCont = new PmCmdCont();
        pmCmdCont.setTenantId(tenantId);
        pmCmdCont.setPlateNo(plateNo);
        pmCmdContRepository.delete(pmCmdCont);
        
        PmCmdSmallPlt pmCmdSmallPlt = new PmCmdSmallPlt();
        pmCmdSmallPlt.setTenantId(tenantId);
        pmCmdSmallPlt.setPlateNo(plateNo);
        pmCmdSmallPltRepository.delete(pmCmdSmallPlt);
    }
    
    /**
     * 小板位置表，转换为命令小板表
     * @param pmPdSlab
     * @param pmSmallPltPos
     * @return
     */
    private PmCmdSmallPlt buildPmCmdSmallPlt(PmPdSlab pmPdSlab, PmSmallPltPos pmSmallPltPos) {
        PmCmdSmallPlt pmCmdSmallPlt = BeanUtils.copy(pmSmallPltPos, PmCmdSmallPlt.class);
        pmCmdSmallPlt.setTenantId(pmPdSlab.getTenantId());
        pmCmdSmallPlt.setTmpSlabNo(pmPdSlab.getVirtualSlabNo());
        pmCmdSmallPlt.setPonoSlabNo(pmPdSlab.getVirtualSlabNo());
        pmCmdSmallPlt.setSlabNo(pmPdSlab.getSlabNo());
        pmCmdSmallPlt.setMscLineNo(pmPdSlab.getMscLineNo());
        return pmCmdSmallPlt;
    }
    
    /**
     * 小板位置表，转换为命令小板表
     * @param tmpSlabNo
     * @param pmSmallPltPos
     * @return
     */
    private PmCmdSmallPlt buildPmCmdSmallPlt(String tmpSlabNo, PmPdPlate pmPdPlate, PmSmallPltPos pmSmallPltPos) {
        PmCmdSmallPlt pmCmdSmallPlt = BeanUtils.copy(pmSmallPltPos, PmCmdSmallPlt.class);
        pmCmdSmallPlt.setTenantId(pmPdPlate.getTenantId());
        pmCmdSmallPlt.setTmpSlabNo(tmpSlabNo);  
        pmCmdSmallPlt.setPonoSlabNo(tmpSlabNo); 
        pmCmdSmallPlt.setSlabNo(pmSmallPltPos.getSlabNo());
        pmCmdSmallPlt.setPlateNo(pmSmallPltPos.getPlateNo());
        return pmCmdSmallPlt;
    }

    /**
     * 合同表转换
     * @param tmpSlabNo
     * @param pmPdContSum
     * @param pmProContProdTtr
     * @param pmContMscTech
     * @return
     */
    private PmCmdCont buildPmCmdCont(String tmpSlabNo, PmPdPlate pmPdPlate, PmPdContSum pmPdContSum, PmProContProdTtr pmProContProdTtr, PmContMscTech pmContMscTech) {
        PmCmdCont pmCmdCont = BeanUtils.copy(pmPdPlate, PmCmdCont.class);
        BeanUtils.copy(pmContMscTech, pmCmdCont);
        BeanUtils.copy(pmProContProdTtr, pmCmdCont);
        pmCmdCont.setTenantId(pmPdPlate.getTenantId());
        pmCmdCont.setTmpSlabNo(tmpSlabNo);
        pmCmdCont.setPonoSlabNo(tmpSlabNo);
        pmCmdCont.setOrderNo(pmPdContSum.getOrderNo());
        pmCmdCont.setDsFlag(pmContMscTech.getDsFlag() == null ? null : pmContMscTech.getDsFlag().toString());
        pmCmdCont.setSsFlag(pmContMscTech.getSsFlag() == null ? null : pmContMscTech.getSsFlag().toString());
        pmCmdCont.setDssFlag(pmContMscTech.getDssFlag() == null ? null : pmContMscTech.getDssFlag().toString());
        pmCmdCont.setGFlag(pmContMscTech.getGFlag() == null ? null : pmContMscTech.getGFlag().toString());
        pmCmdCont.setCsFlag(pmContMscTech.getCsFlag() == null ? null : pmContMscTech.getCsFlag().toString());
        pmCmdCont.setSlabNo(pmPdContSum.getSlabNo());
        pmCmdCont.setPlateNo(pmPdContSum.getPlateNo());
        pmCmdCont.setExportFlag(pmProContProdTtr.getExportFlag() == null ? null : pmProContProdTtr.getExportFlag().toString());
        pmCmdCont.setUnitWtAim(pmProContProdTtr.getOrderUnitAimWt());
        pmCmdCont.setUnitWtMax(pmProContProdTtr.getOrderUnitMaxWt());
        pmCmdCont.setUnitWtMin(pmProContProdTtr.getOrderUnitMinWt());
        pmCmdCont.setWidthDiv(pmProContProdTtr.getWidthDiv() == null ? null : pmProContProdTtr.getWidthDiv().toString());
        pmCmdCont.setLenDiv(pmProContProdTtr.getLenDiv() == null ? null : pmProContProdTtr.getLenDiv().toString());
        pmCmdCont.setOrderLenMin(pmProContProdTtr.getOrderMinLen());
        pmCmdCont.setOrderLenMax(pmProContProdTtr.getOrderMaxLen());
        pmCmdCont.setSmallPlateLen(pmPdContSum.getSmallPlateLen());
        pmCmdCont.setSmallPlateThick(pmPdContSum.getSmallPlateThick());
        pmCmdCont.setSmallPlateWidth(pmPdContSum.getSmallPlateWidth());
        pmCmdCont.setHotRectifyFlag(pmContMscTech.getHotRectifyFlag() == null ? null : pmContMscTech.getHotRectifyFlag().toString());
        pmCmdCont.setHeadCut(pmPdPlate.getHeadCut());
        pmCmdCont.setTailCut(pmPdPlate.getTailCut());
        pmCmdCont.setAxisFlag(pmPdContSum.getAxisFlag());
        return pmCmdCont;
    }
    
    /**
     * 合同表转换
     * @param pmPdSlab
     * @param pmPdContSum
     * @param pmProContProdTtr
     * @param pmContMscTech
     * @return
     */
    private PmCmdCont buildPmCmdCont(PmPdSlab pmPdSlab, PmPdContSum pmPdContSum, PmProContProdTtr pmProContProdTtr, PmContMscTech pmContMscTech) {
        PmCmdCont pmCmdCont = BeanUtils.copy(pmPdSlab, PmCmdCont.class);
        BeanUtils.copy(pmContMscTech, pmCmdCont);
        BeanUtils.copy(pmProContProdTtr, pmCmdCont);
        pmCmdCont.setTenantId(pmPdSlab.getTenantId());
        pmCmdCont.setTmpSlabNo(pmPdSlab.getVirtualSlabNo());
        pmCmdCont.setPonoSlabNo(pmPdSlab.getVirtualSlabNo());
        pmCmdCont.setOrderNo(pmPdContSum.getOrderNo());
        pmCmdCont.setDsFlag(pmContMscTech.getDsFlag() == null ? null : pmContMscTech.getDsFlag().toString());
        pmCmdCont.setSsFlag(pmContMscTech.getSsFlag() == null ? null : pmContMscTech.getSsFlag().toString());
        pmCmdCont.setDssFlag(pmContMscTech.getDssFlag() == null ? null : pmContMscTech.getDssFlag().toString());
        pmCmdCont.setGFlag(pmContMscTech.getGFlag() == null ? null : pmContMscTech.getGFlag().toString());
        pmCmdCont.setCsFlag(pmContMscTech.getCsFlag() == null ? null : pmContMscTech.getCsFlag().toString());
        pmCmdCont.setSlabNo(pmPdContSum.getSlabNo());
        pmCmdCont.setPlateNo(pmPdContSum.getPlateNo());
        pmCmdCont.setExportFlag(pmProContProdTtr.getExportFlag() == null ? null : pmProContProdTtr.getExportFlag().toString());
        pmCmdCont.setUnitWtAim(pmProContProdTtr.getOrderUnitAimWt());
        pmCmdCont.setUnitWtMax(pmProContProdTtr.getOrderUnitMaxWt());
        pmCmdCont.setUnitWtMin(pmProContProdTtr.getOrderUnitMinWt());
        pmCmdCont.setWidthDiv(pmProContProdTtr.getWidthDiv() == null ? null : pmProContProdTtr.getWidthDiv().toString());
        pmCmdCont.setLenDiv(pmProContProdTtr.getLenDiv() == null ? null : pmProContProdTtr.getLenDiv().toString());
        pmCmdCont.setOrderLenMin(pmProContProdTtr.getOrderMinLen());
        pmCmdCont.setOrderLenMax(pmProContProdTtr.getOrderMaxLen());
        pmCmdCont.setSmallPlateLen(pmPdContSum.getSmallPlateLen());
        pmCmdCont.setSmallPlateThick(pmPdContSum.getSmallPlateThick());
        pmCmdCont.setSmallPlateWidth(pmPdContSum.getSmallPlateWidth());
        pmCmdCont.setHotRectifyFlag(pmContMscTech.getHotRectifyFlag() == null ? null : pmContMscTech.getHotRectifyFlag().toString());
        pmCmdCont.setHeadCut(pmPdSlab.getHeadCut());
        pmCmdCont.setTailCut(pmPdSlab.getTailCut());
        pmCmdCont.setAxisFlag(pmPdContSum.getAxisFlag());
        return pmCmdCont;
    }
    
    /**
     * 构造命令板坯表
     * @param pmPdSlabInfo
     * @param pmPdSlab
     * @return
     */
    private PmCmdSlab buildPmCmdSlab(PmPdSlabInfo pmPdSlabInfo, PmPdSlab pmPdSlab, PmProContProdTtr axisPmProContProdTtr, PmContMscTech axisPmContMscTech) {
        PmCmdSlab pmCmdSlab = BeanUtils.copy(pmPdSlab, PmCmdSlab.class);
        pmCmdSlab.setFixSlabLenFlag(pmPdSlab.getFixSlabLenFlag() == null ? "0" : pmPdSlab.getFixSlabLenFlag().toString());
        pmCmdSlab.setTenantId(pmPdSlab.getTenantId());
        pmCmdSlab.setMoldWid(pmPdSlabInfo.getMatActWidth());
        pmCmdSlab.setMoldThick(pmPdSlabInfo.getMatActThick());
        pmCmdSlab.setPonoSlabNo(pmPdSlab.getVirtualSlabNo());
        pmCmdSlab.setTmpSlabNo(pmPdSlab.getVirtualSlabNo());
        pmCmdSlab.setSlabNo(pmPdSlab.getSlabNo());
        pmCmdSlab.setVirtualSlabNo(pmPdSlab.getVirtualSlabNo());
        pmCmdSlab.setPdStatusHp(PdStatusHp.PD_STATUS_HP_18);
        pmCmdSlab.setLastSlabFlag(pmPdSlab.getLastSlabFlag().toString());
        pmCmdSlab.setHotSendDiv("H");
        pmCmdSlab.setHotChargeFlag(BaseConstants.Flag.NO.toString());
        pmCmdSlab.setFormPlateTime(pmPdSlab.getCreationDate());
        pmCmdSlab.setProdCodeHp(axisPmProContProdTtr.getProdCodeHp());
//        pmCmdSlab.setSlabPreProc1(pmPdSlab.GETSLABPRE);
        
        return pmCmdSlab;
    }
    
    /**
     * 根据钢板信息-> 生成命令大板表
     * @param tmpSlabNo 
     * @param pmPdPlate
     * @param maxDivNo
     * @param axisPmProContProdTtr
     * @param axisPmContMscTech
     * @return
     */
    private PmCmdLargePlt buildPmCmdLargePlt(String tmpSlabNo, PmPdPlate pmPdPlate, String maxDivNo, PmProContProdTtr axisPmProContProdTtr, PmContMscTech axisPmContMscTech,
                                             String mscLineNo) {
        PmCmdLargePlt pmCmdLargePlt = BeanUtils.copy(pmPdPlate, PmCmdLargePlt.class);
        pmCmdLargePlt.setTenantId(pmPdPlate.getTenantId());
        pmCmdLargePlt.setPonoSlabNo(tmpSlabNo); 
        pmCmdLargePlt.setTmpSlabNo(tmpSlabNo); 
        pmCmdLargePlt.setPlateNo(pmPdPlate.getPlateNo());
        pmCmdLargePlt.setPdStatusHp(Constants.PdStatusHp.PD_STATUS_HP_18);
        pmCmdLargePlt.setTwoRollInterimFlag(pmPdPlate.getTwoRollInterimFlag() == null ? "":pmPdPlate.getTwoRollInterimFlag().toString());
        pmCmdLargePlt.setProdCodeHp(axisPmProContProdTtr.getProdCodeHp());
        pmCmdLargePlt.setMaxRollThick(axisPmProContProdTtr.getOrderThick().add(pmPdPlate.getRollThickTolMax() == null ? BigDecimal.ZERO : pmPdPlate.getRollThickTolMax()));
        pmCmdLargePlt.setMinRollThick(axisPmProContProdTtr.getOrderThick().add(pmPdPlate.getRollThickTolMax() == null ? BigDecimal.ZERO : pmPdPlate.getRollThickTolMin()));
        pmCmdLargePlt.setDsFlag(axisPmContMscTech.getDsFlag() == null ? null : axisPmContMscTech.getDsFlag().toString());
        pmCmdLargePlt.setSsFlag(axisPmContMscTech.getSsFlag() == null ? null : axisPmContMscTech.getSsFlag().toString());
        pmCmdLargePlt.setDssFlag(axisPmContMscTech.getDssFlag() == null ? null : axisPmContMscTech.getDssFlag().toString());
        pmCmdLargePlt.setGFlag(axisPmContMscTech.getGFlag() == null ? null : axisPmContMscTech.getGFlag().toString());
        pmCmdLargePlt.setCsFlag(axisPmContMscTech.getCsFlag() == null ? null : axisPmContMscTech.getCsFlag().toString());
        pmCmdLargePlt.setProdDensity(axisPmProContProdTtr.getProdDensity());
        pmCmdLargePlt.setFormPlateTime(pmPdPlate.getCreationDate());
        pmCmdLargePlt.setPlateDtCode(axisPmContMscTech.getPlateDtCode());
        pmCmdLargePlt.setSampleLenGive(axisPmProContProdTtr.getSampleLenGive());
        pmCmdLargePlt.setMscLineNo(mscLineNo);
        pmCmdLargePlt.setUltCode(pmPdPlate.getUltCode());
        pmCmdLargePlt.setRollMode(axisPmContMscTech.getRollMode());
        
//        String[] arr = getNextDivNo(maxDivNo);
        String[] arr = getNextDivNoByPlateNo(pmPdPlate.getPlateNo(), maxDivNo);
        pmCmdLargePlt.setOtherPlateNo1(arr[0]);
        pmCmdLargePlt.setOtherPlateNo2(arr[1]);
        pmCmdLargePlt.setOtherPlateNo3(arr[2]);
        pmCmdLargePlt.setOtherPlateNo4(arr[3]);
        
        return pmCmdLargePlt;
    }

    private String[] getNextDivNoByPlateNo(String plateNo, String maxDivNo) {
        String []arr = new String[4];
        StringBuilder plateNoBuilder = new StringBuilder(plateNo.substring(plateNo.length() - 3));
        // 找出为0的下标
        int index = Utils.findPlatNoIndexZero(plateNo);
        for (int i = 0; i < PmPlateConstants.PLATE_SMALL_SPLIT_NO.length - 4; i++) {
            if (maxDivNo.charAt(index) == PmPlateConstants.PLATE_SMALL_SPLIT_NO[i]) {
                plateNoBuilder.setCharAt(index, PmPlateConstants.PLATE_SMALL_SPLIT_NO[++i]);
                arr[0] =  plateNoBuilder.toString();
                plateNoBuilder.setCharAt(index, PmPlateConstants.PLATE_SMALL_SPLIT_NO[++i]);
                arr[1] =  plateNoBuilder.toString();
                plateNoBuilder.setCharAt(index, PmPlateConstants.PLATE_SMALL_SPLIT_NO[++i]);
                arr[2] =  plateNoBuilder.toString();
                plateNoBuilder.setCharAt(index, PmPlateConstants.PLATE_SMALL_SPLIT_NO[++i]);
                arr[3] =  plateNoBuilder.toString();
                return arr ;
            }
        }
        return arr;
    }

    /**
     * 根据板坯表生产命令大板表
     * @param pmPdSlabInfo
     * @param pmPdSlab
     * @param divNoMap
     * @param axisPmProContProdTtr
     * @param axisPmContMscTech
     * @return
     */
    private PmCmdLargePlt buildPmCmdLargePlt(PmPdSlabInfo pmPdSlabInfo, PmPdSlab pmPdSlab, Map<String , String> divNoMap,
                                             PmProContProdTtr axisPmProContProdTtr, PmContMscTech axisPmContMscTech) {
        PmCmdLargePlt pmCmdLargePlt = BeanUtils.copy(pmPdSlab, PmCmdLargePlt.class);
        pmCmdLargePlt.setTenantId(pmPdSlabInfo.getTenantId());
        pmCmdLargePlt.setPonoSlabNo(pmPdSlab.getVirtualSlabNo());
        pmCmdLargePlt.setTmpSlabNo(pmPdSlab.getVirtualSlabNo());
        pmCmdLargePlt.setPdStatusHp(Constants.PdStatusHp.PD_STATUS_HP_18);
        pmCmdLargePlt.setTwoRollInterimFlag(pmPdSlab.getTwoRollInterimFlag() == null ? "":pmPdSlab.getTwoRollInterimFlag().toString());
        pmCmdLargePlt.setProdCodeHp(axisPmProContProdTtr.getProdCodeHp());
        pmCmdLargePlt.setMaxRollThick(axisPmProContProdTtr.getOrderThick().add(pmPdSlab.getRollThickTolMax() == null ? BigDecimal.ZERO : pmPdSlab.getRollThickTolMax()));
        pmCmdLargePlt.setMinRollThick(axisPmProContProdTtr.getOrderThick().add(pmPdSlab.getRollThickTolMax() == null ? BigDecimal.ZERO : pmPdSlab.getRollThickTolMin()));
        pmCmdLargePlt.setDsFlag(axisPmContMscTech.getDsFlag() == null ? null : axisPmContMscTech.getDsFlag().toString());
        pmCmdLargePlt.setSsFlag(axisPmContMscTech.getSsFlag() == null ? null : axisPmContMscTech.getSsFlag().toString());
        pmCmdLargePlt.setDssFlag(axisPmContMscTech.getDssFlag() == null ? null : axisPmContMscTech.getDssFlag().toString());
        pmCmdLargePlt.setGFlag(axisPmContMscTech.getGFlag() == null ? null : axisPmContMscTech.getGFlag().toString());
        pmCmdLargePlt.setCsFlag(axisPmContMscTech.getCsFlag() == null ? null : axisPmContMscTech.getCsFlag().toString());
        pmCmdLargePlt.setProdDensity(axisPmProContProdTtr.getProdDensity());
        pmCmdLargePlt.setFormPlateTime(pmPdSlab.getCreationDate());
        pmCmdLargePlt.setPlateDtCode(axisPmContMscTech.getPlateDtCode());
        pmCmdLargePlt.setSampleLenGive(axisPmProContProdTtr.getSampleLenGive());
        pmCmdLargePlt.setMscLineNo(pmPdSlab.getMscLineNo());
        pmCmdLargePlt.setRollMode(axisPmContMscTech.getRollMode());
        pmCmdLargePlt.setOrder1Thick(pmPdSlab.getOrderThickAxis());
        String[] arr = getNextDivNo(divNoMap.get(pmPdSlab.getVirtualSlabNo()));
        pmCmdLargePlt.setOtherPlateNo1(arr[0]);
        pmCmdLargePlt.setOtherPlateNo2(arr[1]);
        pmCmdLargePlt.setOtherPlateNo3(arr[2]);
        pmCmdLargePlt.setOtherPlateNo4(arr[3]);
        
        return pmCmdLargePlt;
    }
    
    private String[] getNextDivNo(String deviNo) {
        String []arr = new String[4];
        for (int i = 0 ; i < PmPlateConstants.SMALL_PLATE_SPLIT_NO.length - 4 ; i++) {
            if(PmPlateConstants.SMALL_PLATE_SPLIT_NO[i].equals(deviNo)) {
                arr[0] = PmPlateConstants.SMALL_PLATE_SPLIT_NO[++i];
                arr[1] = PmPlateConstants.SMALL_PLATE_SPLIT_NO[++i];
                arr[2] = PmPlateConstants.SMALL_PLATE_SPLIT_NO[++i];
                arr[3] = PmPlateConstants.SMALL_PLATE_SPLIT_NO[++i];
                return arr ;
            }
        }
        return arr;
    }
    
}
