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

import com.xisc.pm.api.dto.InterfaceReceiveDTO;
import com.xisc.pm.api.dto.PmSteelCastHeatReqParamDTO;
import com.xisc.pm.app.service.PmMeterJobService;
import com.xisc.pm.app.service.PmSteelCastHeatService;
import com.xisc.pm.domain.entity.PmSteelCastFlow;
import com.xisc.pm.domain.entity.PmSteelCastFlowItf;
import com.xisc.pm.domain.entity.PmSteelCastHeat;
import com.xisc.pm.domain.entity.PmSteelCastHeatItf;
import com.xisc.pm.domain.entity.PmSteelCastLot;
import com.xisc.pm.domain.entity.PmSteelCastLotItf;
import com.xisc.pm.domain.entity.PmSteelCastSlab;
import com.xisc.pm.domain.entity.PmSteelCastSlabItf;
import com.xisc.pm.domain.repository.PmSteelCastFlowItfRepository;
import com.xisc.pm.domain.repository.PmSteelCastFlowRepository;
import com.xisc.pm.domain.repository.PmSteelCastHeatItfRepository;
import com.xisc.pm.domain.repository.PmSteelCastHeatRepository;
import com.xisc.pm.domain.repository.PmSteelCastLotItfRepository;
import com.xisc.pm.domain.repository.PmSteelCastLotRepository;
import com.xisc.pm.domain.repository.PmSteelCastSlabItfRepository;
import com.xisc.pm.domain.repository.PmSteelCastSlabRepository;
import com.xisc.pm.infra.client.HeApiClient;
import com.xisc.pm.infra.constant.Constants;
import com.xisc.pm.infra.util.DateUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.hzero.core.base.BaseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @date 2024/12/5 17:52
 * @description
 */
@Service
public class PmMeterJobServiceImpl implements PmMeterJobService {

    @Autowired
    private PmSteelCastHeatItfRepository pmSteelCastHeatItfRepository;

    @Autowired
    private PmSteelCastLotItfRepository pmSteelCastLotItfRepository;

    @Autowired
    private PmSteelCastFlowItfRepository pmSteelCastFlowItfRepository;

    @Autowired
    private PmSteelCastSlabItfRepository pmSteelCastSlabItfRepository;

    @Autowired
    private PmSteelCastHeatRepository pmSteelCastHeatRepository;

    @Autowired
    private PmSteelCastLotRepository pmSteelCastLotRepository;

    @Autowired
    private PmSteelCastFlowRepository pmSteelCastFlowRepository;

    @Autowired
    private PmSteelCastSlabRepository pmSteelCastSlabRepository;

    @Autowired
    private PmSteelCastHeatService pmSteelCastHeatService;

    @Autowired
    private HeApiClient heApiClient;

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public void updateItfToProcess(List<PmSteelCastSlabItf> pmSteelCastSlabItfList, List<PmSteelCastLotItf> pmSteelCastLotItfList,
                                   List<PmSteelCastHeatItf> pmSteelCastHeatItfList, List<PmSteelCastFlowItf> pmSteelCastFlowItfList,
                                   String itfStatus,  String iftMessage) {
        if (CollectionUtils.isNotEmpty(pmSteelCastSlabItfList)) {
            pmSteelCastSlabItfList.forEach(item -> item.setItfStatus(itfStatus));
            pmSteelCastSlabItfRepository.batchUpdateByPrimaryKeySelective(pmSteelCastSlabItfList);
        }

        if (CollectionUtils.isNotEmpty(pmSteelCastLotItfList)) {
            pmSteelCastLotItfList.forEach(item -> item.setItfStatus(itfStatus));
            pmSteelCastLotItfRepository.batchUpdateByPrimaryKeySelective(pmSteelCastLotItfList);
        }

        if (CollectionUtils.isNotEmpty(pmSteelCastHeatItfList)) {
            pmSteelCastHeatItfList.forEach(item -> item.setItfStatus(itfStatus));
            pmSteelCastHeatItfRepository.batchUpdateByPrimaryKeySelective(pmSteelCastHeatItfList);
        }

        if (CollectionUtils.isNotEmpty(pmSteelCastFlowItfList)) {
            pmSteelCastFlowItfList.forEach(item -> item.setItfStatus(itfStatus));
            pmSteelCastFlowItfRepository.batchUpdateByPrimaryKeySelective(pmSteelCastFlowItfList);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public void syncToSteelCast(List<PmSteelCastSlabItf> pmSteelCastSlabItfList, List<PmSteelCastLotItf> pmSteelCastLotItfList,
                                List<PmSteelCastHeatItf> pmSteelCastHeatItfList, List<PmSteelCastFlowItf> pmSteelCastFlowItfList,
                                Long tenantId) {
        // 区分下达和回收的
        List<PmSteelCastHeatItf> assignSteelCastHeatItfList = pmSteelCastHeatItfList.stream().filter(item -> "1".equals(item.getDisFlag())).collect(Collectors.toList());
        List<PmSteelCastHeatItf> recycleSteelCastHeatItfList = pmSteelCastHeatItfList.stream().filter(item -> "3".equals(item.getDisFlag())).collect(Collectors.toList());

        // 下达
        if (CollectionUtils.isNotEmpty(assignSteelCastHeatItfList)) {
//            Map<String, Long> castLotDivNoMap = new HashMap<>(assignSteelCastHeatItfList.size());
            // 遍历每一浇次
            Map<String, List<PmSteelCastHeatItf>> assignSteelCastHeatItfMap = assignSteelCastHeatItfList.stream().collect(Collectors.groupingBy(PmSteelCastHeatItf::getCastLotNo));
            for (Map.Entry<String, List<PmSteelCastHeatItf>> entry : assignSteelCastHeatItfMap.entrySet()) {
                boolean flag = false;
                StringBuilder message = new StringBuilder();

//                Optional<Long> maxCastLotNoOptional = entry.getValue().stream().max(Comparator.comparing(PmSteelCastHeatItf::getCastLotDivNo)).map(PmSteelCastHeatItf::getCastLotDivNo);
//                if (maxCastLotNoOptional.isPresent()) {
//                    castLotDivNoMap.put(entry.getKey(), maxCastLotNoOptional.get());
//                }
                // 获取制造命令号集合
                List<String> ponoList = entry.getValue().stream().map(PmSteelCastHeatItf::getPono).distinct().collect(Collectors.toList());

                List<PmSteelCastLotItf> steelCastLotItfList = pmSteelCastLotItfList.stream().filter(item -> entry.getKey().equals(item.getCastLotNo())).collect(Collectors.toList());
                if (CollectionUtils.isEmpty(steelCastLotItfList)) {
                    flag = true;
                    message.append("根据浇次号【" + entry.getKey() + "】未从炼钢连铸制造命令LOT-接口表获取到对应数据");
                }

                List<PmSteelCastFlowItf> steelCastFlowItfList = pmSteelCastFlowItfList.stream().filter(item -> ponoList.contains(item.getPono())).collect(Collectors.toList());
                if (CollectionUtils.isEmpty(steelCastFlowItfList)) {
                    flag = true;
                    message.append("根据制造命令号【" + Strings.join(ponoList, ',') + "】未从炼钢连铸制造命令铸机流-接口表获取到对应数据");
                }

                List<PmSteelCastSlabItf> steelCastSlabItfList = pmSteelCastSlabItfList.stream().filter(item -> ponoList.contains(item.getPono())).collect(Collectors.toList());
                if (CollectionUtils.isEmpty(steelCastSlabItfList)) {
                    flag = true;
                    message.append("根据制造命令号【" + Strings.join(ponoList, ',') + "】未从炼钢连铸制造命令板坯-接口表获取到对应数据");
                }

                for (String pono : ponoList) {
                    List<PmSteelCastSlabItf> filterSlabList = steelCastSlabItfList.stream().filter(item -> item.getPono().equals(pono)).filter(item -> item.getSlabThick() != null && item.getSlabWidth() != null).collect(Collectors.toList());
                    if (CollectionUtils.isEmpty(filterSlabList)) {
                        flag = true;
                        message.append("制造命令号【" + Strings.join(ponoList, ',') + "】对应的炼钢连铸制造命令板坯表接口表的板坯厚和宽都为空");
                    }
                }

                // 校验失败
                if (flag) {
                    steelCastLotItfList.forEach(item -> {
                        item.setItfStatus(Constants.ItfStatus.E);
                        item.setItfMessage(message.toString());
                    });

                    entry.getValue().forEach(item -> {
                        item.setItfStatus(Constants.ItfStatus.E);
                        item.setItfMessage(message.toString());
                    });

                    steelCastFlowItfList.forEach(item -> {
                        item.setItfStatus(Constants.ItfStatus.E);
                        item.setItfMessage(message.toString());
                    });

                    steelCastSlabItfList.forEach(item -> {
                        item.setItfStatus(Constants.ItfStatus.E);
                        item.setItfMessage(message.toString());
                    });
                } else {
                    // 校验成功，存入业务表
                    steelCastLotItfList.forEach(item -> {
                        item.setItfStatus(Constants.ItfStatus.S);
                    });

                    entry.getValue().forEach(item -> {
                        item.setItfStatus(Constants.ItfStatus.S);
                    });

                    steelCastFlowItfList.forEach(item -> {
                        item.setItfStatus(Constants.ItfStatus.S);
                    });

                    steelCastSlabItfList.forEach(item -> {
                        item.setItfStatus(Constants.ItfStatus.S);
                    });
                }
            }


            // 将接口表数据导入对应的业务表
            List<PmSteelCastSlab> insertPmSteelCastSlabList = new ArrayList<>(pmSteelCastSlabItfList.size());
            pmSteelCastSlabItfList.stream().filter(item -> Constants.ItfStatus.S.equals(item.getItfStatus())).forEach(item -> {
                PmSteelCastSlab pmSteelCastSlab = new PmSteelCastSlab();
                transToPmSteelCastSlab(pmSteelCastSlab, item);
                pmSteelCastSlab.setTenantId(tenantId);
                insertPmSteelCastSlabList.add(pmSteelCastSlab);
            });

            Map<String, PmSteelCastSlab> insertPmSteelCastSlabMap = insertPmSteelCastSlabList.stream().filter(item -> item.getSlabThick() != null && item.getSlabWidth() != null).collect(Collectors.toMap(PmSteelCastSlab::getPono, Function.identity(), (k, v) -> k));

            List<PmSteelCastLot> insertPmSteelCastLotList = new ArrayList<>(pmSteelCastSlabItfList.size());
            pmSteelCastLotItfList.stream().filter(item -> Constants.ItfStatus.S.equals(item.getItfStatus())).forEach(item -> {
                PmSteelCastLot pmSteelCastLot = new PmSteelCastLot();
                transToPmSteelCastLot(pmSteelCastLot, item);
                pmSteelCastLot.setTenantId(tenantId);
                insertPmSteelCastLotList.add(pmSteelCastLot);
            });

            List<PmSteelCastHeat> insertPmSteelCastHeatList = new ArrayList<>(pmSteelCastHeatItfList.size());
            pmSteelCastHeatItfList.stream().filter(item -> Constants.ItfStatus.S.equals(item.getItfStatus())).forEach(item -> {
                PmSteelCastHeat pmSteelCastHeat = new PmSteelCastHeat();
                transToPmSteelCastHeat(pmSteelCastHeat, item);
                pmSteelCastHeat.setTenantId(tenantId);
                pmSteelCastHeat.setMoldWidth(insertPmSteelCastSlabMap.get(item.getPono()).getSlabWidth());
                pmSteelCastHeat.setMoldThick(insertPmSteelCastSlabMap.get(item.getPono()).getSlabThick());
                // 尾炉标记
//                if (castLotDivNoMap.containsKey(item.getCastLotNo()) && castLotDivNoMap.get(item.getCastLotNo()).equals(item.getCastLotDivNo())) {
//                    pmSteelCastHeat.setRestrandFlg("W");
//                }
                insertPmSteelCastHeatList.add(pmSteelCastHeat);
            });

            List<PmSteelCastFlow> insertPmSteelCastFlowList = new ArrayList<>(pmSteelCastFlowItfList.size());
            pmSteelCastFlowItfList.stream().filter(item -> Constants.ItfStatus.S.equals(item.getItfStatus())).forEach(item -> {
                PmSteelCastFlow pmSteelCastFlow = new PmSteelCastFlow();
                transToPmSteelCastFlow(pmSteelCastFlow, item);
                pmSteelCastFlow.setTenantId(tenantId);
                insertPmSteelCastFlowList.add(pmSteelCastFlow);
            });

            // 批量插入
            pmSteelCastSlabRepository.batchInsertSelective(insertPmSteelCastSlabList);
            pmSteelCastLotRepository.batchInsertSelective(insertPmSteelCastLotList);
            pmSteelCastHeatRepository.batchInsertSelective(insertPmSteelCastHeatList);
            pmSteelCastFlowRepository.batchInsertSelective(insertPmSteelCastFlowList);

            // 更新接口表状态
            pmSteelCastSlabItfRepository.batchUpdateByPrimaryKeySelective(pmSteelCastSlabItfList);
            pmSteelCastLotItfRepository.batchUpdateByPrimaryKeySelective(pmSteelCastLotItfList);
            pmSteelCastHeatItfRepository.batchUpdateByPrimaryKeySelective(pmSteelCastHeatItfList);
            pmSteelCastFlowItfRepository.batchUpdateByPrimaryKeySelective(pmSteelCastFlowItfList);

            // d)	同时调用【计划下达】逻辑，将数据下发给炼钢模块，
            if (CollectionUtils.isNotEmpty(insertPmSteelCastHeatList)) {
                pmSteelCastHeatService.squarePlanAssign(tenantId, insertPmSteelCastHeatList);

                // 若下发给炼钢成功，则调用线板材MES接口：计划下达（HEXB02），返回如下信息（下发失败不返回）：
                heApiClient.callSecondPlanAssign(insertPmSteelCastHeatList, tenantId, "HEXB02", false);
            }
        }

        if (CollectionUtils.isNotEmpty(recycleSteelCastHeatItfList)) {
            StringBuilder message = new StringBuilder();
            Boolean flag = false;
            List<String> ponoList = recycleSteelCastHeatItfList.stream().map(PmSteelCastHeatItf::getPono).distinct().collect(Collectors.toList());
            List<String> castLotNoList = recycleSteelCastHeatItfList.stream().map(PmSteelCastHeatItf::getCastLotNo).collect(Collectors.toList());

            List<PmSteelCastSlabItf> recycleSteelCastSlabItfList = pmSteelCastSlabItfList.stream().filter(item -> ponoList.contains(item.getPono())).collect(Collectors.toList());
            List<PmSteelCastLotItf> recycleSteelCastLotItfListList = pmSteelCastLotItfList.stream().filter(item -> castLotNoList.contains(item.getCastLotNo())).collect(Collectors.toList());
            List<PmSteelCastFlowItf> recycleSteelCastFlowItfListList = pmSteelCastFlowItfList.stream().filter(item -> ponoList.contains(item.getPono())).collect(Collectors.toList());
            // 判断是否删除Lot表
            List<PmSteelCastHeat> deleteSteelCastHeatList = pmSteelCastHeatRepository.selectByPonoList(ponoList, tenantId);
            List<PmSteelCastHeat> allSteelCastHeatList = pmSteelCastHeatRepository.selectByCastLotNoList(castLotNoList, tenantId);
            if (CollectionUtils.isEmpty(deleteSteelCastHeatList)) {
                flag = true;
                message.append("根据制造命令号【" + Strings.join(ponoList, ',') + "】未在炼钢连铸制造命令炉次表获取到数据");
            }
            if (CollectionUtils.isEmpty(allSteelCastHeatList)) {
                flag = true;
                message.append("根据浇次号【" + Strings.join(castLotNoList, ',') + "】未在炼钢连铸制造命令炉次表获取到数据");
            }
            if (flag) {
                for (PmSteelCastHeatItf castHeatItf : recycleSteelCastHeatItfList) {
                    castHeatItf.setItfStatus(Constants.ItfStatus.E);
                    castHeatItf.setItfMessage(message.toString());
                }
                recycleSteelCastSlabItfList.forEach(item -> item.setItfStatus(Constants.ItfStatus.E));
                recycleSteelCastLotItfListList.forEach(item -> item.setItfStatus(Constants.ItfStatus.E));
                recycleSteelCastFlowItfListList.forEach(item -> item.setItfStatus(Constants.ItfStatus.E));
            } else {
                recycleSteelCastHeatItfList.forEach(item -> {
                    item.setItfStatus(Constants.ItfStatus.S);
                });
                recycleSteelCastSlabItfList.forEach(item -> item.setItfStatus(Constants.ItfStatus.S));
                recycleSteelCastLotItfListList.forEach(item -> item.setItfStatus(Constants.ItfStatus.S));
                recycleSteelCastFlowItfListList.forEach(item -> item.setItfStatus(Constants.ItfStatus.S));
            }
            // 更新接口表
            pmSteelCastHeatItfRepository.batchUpdateByPrimaryKeySelective(recycleSteelCastHeatItfList);
            pmSteelCastSlabItfRepository.batchUpdateByPrimaryKeySelective(recycleSteelCastSlabItfList);
            pmSteelCastLotItfRepository.batchUpdateByPrimaryKeySelective(recycleSteelCastLotItfListList);
            pmSteelCastFlowItfRepository.batchUpdateByPrimaryKeySelective(recycleSteelCastFlowItfListList);

            Map<String, Long> deleteSteelCastHeatMap = deleteSteelCastHeatList.stream().collect(Collectors.groupingBy(PmSteelCastHeat::getCastLotNo, Collectors.counting()));
            Map<String, Long> allSteelCastHeatMap = allSteelCastHeatList.stream().collect(Collectors.groupingBy(PmSteelCastHeat::getCastLotNo, Collectors.counting()));
            // 每一个浇次
            List<String> deleteCastLotNoList = new ArrayList<>();
            Map<String, List<PmSteelCastHeatItf>> recycleSteelCastHeatItfMap = recycleSteelCastHeatItfList.stream().collect(Collectors.groupingBy(PmSteelCastHeatItf::getCastLotNo));
            for (Map.Entry<String, List<PmSteelCastHeatItf>> entry : recycleSteelCastHeatItfMap.entrySet()) {
                // 判断是否删除LOT表
                if (deleteSteelCastHeatMap.getOrDefault(entry.getKey(), 0L).equals(allSteelCastHeatMap.getOrDefault(entry.getKey(), 0L))) {
                    deleteCastLotNoList.add(entry.getKey());
                }
            }

            boolean recycleFlag = false;
            String recycleMessage = "";
            try {
                // 1.	调用【炉次回收】逻辑，将已下发的炉次信息重新收回；
                PmSteelCastHeatReqParamDTO dto = new PmSteelCastHeatReqParamDTO();
                dto.setIsInterface(BaseConstants.FlagChar.YES);
                dto.setPmSteelCastHeatList(deleteSteelCastHeatList);
                pmSteelCastHeatService.furnaceRecycle(tenantId, dto);
            } catch (Exception e) {
                recycleFlag = false;
                recycleMessage = e.getMessage();
            }

            // 删除炼钢四张表
            List<PmSteelCastHeat> newDeleteSteelCastHeatList = pmSteelCastHeatRepository.selectByPonoList(ponoList, tenantId);
            List<PmSteelCastSlab> deletePmSteelCastSlabList = pmSteelCastSlabRepository.selectByPonoList(ponoList, tenantId);
            List<PmSteelCastFlow> deletePmSteelCastFlowList = pmSteelCastFlowRepository.selectByPonoList(ponoList, tenantId);
            if (CollectionUtils.isNotEmpty(deleteCastLotNoList)) {
                List<PmSteelCastLot> pmSteelCastLotList = pmSteelCastLotRepository.selectByCastLotNoList(deleteCastLotNoList, tenantId);
                pmSteelCastLotRepository.batchDeleteByPrimaryKey(pmSteelCastLotList);
            }
            pmSteelCastHeatRepository.batchDeleteByPrimaryKey(newDeleteSteelCastHeatList);
            pmSteelCastSlabRepository.batchDeleteByPrimaryKey(deletePmSteelCastSlabList);
            pmSteelCastFlowRepository.batchDeleteByPrimaryKey(deletePmSteelCastFlowList);


            //2.	调用线板材MES接口：删除炉次应答（HEXB01），将炉次回收的结果以及【炼钢连铸制造命令炉次表（PM_STEEL_CAST_HEAT）】中的信息返回给线板材MES系统；
            InterfaceReceiveDTO interfaceDto = new InterfaceReceiveDTO();
            String interfaceCode = "";
            List<Map<String, String>> context = new ArrayList<>();
            for (PmSteelCastHeatItf item : recycleSteelCastHeatItfList) {
                Map<String, String> lineMap = new HashMap<>(47);
                getInterfaceMap(lineMap, item);
                if (recycleFlag) {
                    // 回收炉次成功
                    lineMap.put("FLAG", "1");
                    lineMap.put("LOG", null);
                } else {
                    // 回收炉次失败
                    lineMap.put("FLAG", "2");
                    lineMap.put("LOG", recycleMessage);
                }
                context.add(lineMap);

                if (StringUtils.isNotEmpty(interfaceCode)) {
                    continue;
                }
                if ("A".equals(item.getMainBacklogCode())) {
                    interfaceCode = "HEXB01";
                }else {
                    interfaceCode = "LGXB02";
                }
            }

            interfaceDto.setInterfaceCode(interfaceCode);
            interfaceDto.setInterfaceData(context);
            heApiClient.secondPlanAssign(tenantId, interfaceDto);
        }
    }

    private void getInterfaceMap(Map<String, String> lineMap, PmSteelCastHeatItf item) {
        lineMap.put("DIS_FLAG", item.getDisFlag());
        lineMap.put("MAIN_BACKLOG_CODE", item.getMainBacklogCode());
        lineMap.put("PONO", item.getPono());
        lineMap.put("PONO_STATUS", item.getPonoStatus() != null ? item.getPonoStatus().toString() : null);
        lineMap.put("CC_SEQ", item.getCcSeq() != null ? item.getCcSeq().toString() : null);
        lineMap.put("ST_NO", item.getStNo());
        lineMap.put("SG_SIGN", item.getSgSign());
        lineMap.put("CAST_LOT_NO", item.getCastLotNo());
        lineMap.put("CAST_LOT_DIV_NO", item.getCastLotDivNo() != null ? item.getCastLotDivNo().toString() : null);
        lineMap.put("CAST_LOT_SUM", item.getCastLotSum() != null ? item.getCastLotSum().toString() : null);
        lineMap.put("CC_MACH_NO", item.getCcMachNo());
        lineMap.put("CC_TYPE", item.getCcType());
        lineMap.put("RESTRAND_FLG", item.getRestrandFlg());
        lineMap.put("BACKLOG_EA", item.getBacklogEa());
        lineMap.put("REFINE_ROUTE_CODE", item.getRefineDiv());
        lineMap.put("SMELT_MODE", item.getSmeltMode() != null ? item.getSmeltMode().toString() : null);
        lineMap.put("PLAN_TAP_WT", item.getPlanTapWt() != null ? item.getPlanTapWt().toString() : null);
        lineMap.put("APN", item.getApn());
        lineMap.put("DEST", item.getDest());
        lineMap.put("NEW_TEST_NO", item.getNewTestNo());
        lineMap.put("STEEL_PLAN_DATE", item.getSteelPlanDate());
        lineMap.put("STEEL_APP_DATE", item.getSteelAppDate());
        lineMap.put("PLAN_DATE", item.getPlanDate());
        lineMap.put("CC_REQ_TIME", item.getCcReqTime());
        lineMap.put("CC_REQ_TIME_FLAG", item.getCcReqTimeFlag());
        lineMap.put("HEAT_CONFM_TIME", item.getHeatConfmTime());
        lineMap.put("HOT_REQ_TIME", item.getHotReqTime());
        lineMap.put("HOT_SEND_FLAG", item.getHotSendFlag());
        lineMap.put("HOT_CHARGE_FLAG", item.getHotChargeFlag());
        lineMap.put("TD_CHG_FLG", item.getTdChgFlg() != null ? item.getTdChgFlg().toString() : null);
        lineMap.put("PONO_OLD", item.getPonoOld());
        lineMap.put("SLAB_REQ", item.getSlabReq());
        lineMap.put("FLAME_CLEAN_DIV", item.getFlameCleanDiv());
        lineMap.put("SHIFT_POINT", item.getShiftPoint());
        lineMap.put("SHIFT_NO", item.getShiftNo());
        lineMap.put("SHIFT_GROUP", item.getShiftGroup());
        lineMap.put("HOLD_PLANNER", item.getHoldPlanner());
        lineMap.put("HOLD_TIME", item.getHoldTime());
        lineMap.put("PLAN_RELEASER", item.getPlanReleaser());
        lineMap.put("REL_TIME", item.getRelTime());
        lineMap.put("SEND_PLANNER", item.getSendPlanner());
        lineMap.put("SEND_TIME", item.getSendTime());
        lineMap.put("CC_BASE", item.getCcBase());
        lineMap.put("POUR_TIME", item.getPourTime() != null ? item.getPourTime().toString() : null);
        lineMap.put("ADJUST_FLAG", item.getAdjustFlag());
    }

    private void transToPmSteelCastFlow(PmSteelCastFlow pmSteelCastFlow, PmSteelCastFlowItf item) {
        pmSteelCastFlow.setMainBacklogCode(item.getMainBacklogCode());
        pmSteelCastFlow.setPono(item.getPono());
        pmSteelCastFlow.setHeatSlabNum(item.getHeatSlabNum() != null ? Long.parseLong(item.getHeatSlabNum()) : null);
        pmSteelCastFlow.setSlabThick(item.getSlabThick());
        pmSteelCastFlow.setSlabWidth(item.getSlabWidth());
        pmSteelCastFlow.setAdjustWidthNum(item.getAdjustWidthNum());
        pmSteelCastFlow.setUnappSlabLen(item.getUnappSlabLen() != null ? BigDecimal.valueOf(item.getUnappSlabLen()) : null);
        pmSteelCastFlow.setUnappSlabLenMax(item.getUnappSlabLenMax() != null ? BigDecimal.valueOf(item.getUnappSlabLenMax()) : null);
        pmSteelCastFlow.setUnappSlabLenMin(item.getUnappSlabLenMin() != null ? BigDecimal.valueOf(item.getUnappSlabLenMin()) : null);
        pmSteelCastFlow.setStrandNo(item.getStrandNo());
    }

    private void transToPmSteelCastHeat(PmSteelCastHeat pmSteelCastHeat, PmSteelCastHeatItf item) {
        int year = LocalDate.now().getYear();
        pmSteelCastHeat.setArchiveFlag(null);
        pmSteelCastHeat.setMainBacklogCode(item.getMainBacklogCode());
        pmSteelCastHeat.setPono(item.getPono());
        pmSteelCastHeat.setPonoStatus(item.getPonoStatus() != null ? item.getPonoStatus().toString() : null);
        pmSteelCastHeat.setCcSeq(item.getCcSeq());
        pmSteelCastHeat.setStNo(item.getStNo());
        pmSteelCastHeat.setSgSign(item.getSgSign());
        pmSteelCastHeat.setMoldThick(null);
        pmSteelCastHeat.setMoldWidth(null);
        pmSteelCastHeat.setCastLotNo(item.getCastLotNo());
        pmSteelCastHeat.setYearId((long) year);
        pmSteelCastHeat.setCastLotDivNo(item.getCastLotDivNo());
        pmSteelCastHeat.setCastLotSum(item.getCastLotSum());
        pmSteelCastHeat.setCcMachNo("2".equals(item.getCcMachNo()) ? "6" : item.getCcMachNo());
        pmSteelCastHeat.setCcType(item.getCcType());
        pmSteelCastHeat.setRestrandFlg("1".equals(item.getRestrandFlg()) ? "T" : null);
        pmSteelCastHeat.setBacklogEa(item.getBacklogEa());
        pmSteelCastHeat.setRefineRouteCode(item.getRefineDiv());
        pmSteelCastHeat.setSmeltMode(item.getSmeltMode());
        pmSteelCastHeat.setPlanTapWt(item.getPlanTapWt());
        pmSteelCastHeat.setApn(item.getApn());
        pmSteelCastHeat.setDest(item.getDest());
        pmSteelCastHeat.setNewTestNo(item.getNewTestNo());
        pmSteelCastHeat.setSteelPlanDate(item.getSteelPlanDate() != null ? DateUtils.parseDate(item.getSteelPlanDate(), DateUtils.YMD) : null);
        pmSteelCastHeat.setSteelAppDate(item.getSteelAppDate() != null ? DateUtils.parseDate(item.getSteelAppDate(), DateUtils.YMD) : null);
        pmSteelCastHeat.setPlanDate(item.getPlanDate() != null ? DateUtils.parseDate(item.getPlanDate(), DateUtils.YMD) : null);
        pmSteelCastHeat.setCcReqTime(item.getCcReqTime() != null ? DateUtils.parseDate(item.getCcReqTime()) : null);
        pmSteelCastHeat.setCcReqTimeFlag(item.getCcReqTimeFlag());
        pmSteelCastHeat.setHeatConfmTime(item.getHeatConfmTime() != null ? DateUtils.parseDate(item.getHeatConfmTime()) : null);
        pmSteelCastHeat.setHotReqTime(item.getHotReqTime() != null ? DateUtils.parseDate(item.getHotReqTime()) : null);
        pmSteelCastHeat.setHotSendFlag(item.getHotSendFlag());
        pmSteelCastHeat.setHotChargeFlag(item.getHotChargeFlag());
        pmSteelCastHeat.setTdChgFlg(item.getTdChgFlg());
        pmSteelCastHeat.setPrevPono(item.getPonoOld());
        pmSteelCastHeat.setSlabReq(item.getSlabReq());
        pmSteelCastHeat.setFlameCleanDiv(item.getFlameCleanDiv());
        pmSteelCastHeat.setShiftPoint(item.getShiftPoint());
        pmSteelCastHeat.setShiftNo(item.getShiftNo());
        pmSteelCastHeat.setShiftGroup(item.getShiftGroup());
        pmSteelCastHeat.setHoldPlanner(item.getHoldPlanner());
        pmSteelCastHeat.setHoldTime(item.getHoldTime());
        pmSteelCastHeat.setPlanReleaser(item.getPlanReleaser());
        pmSteelCastHeat.setRelTime(item.getRelTime());
        pmSteelCastHeat.setSendPlanner(item.getSendPlanner());
        pmSteelCastHeat.setSendTime(item.getSendTime());
        pmSteelCastHeat.setCcBase(item.getCcBase());
        pmSteelCastHeat.setPourTime(item.getPourTime());
        pmSteelCastHeat.setAdjustFlag(item.getAdjustFlag());
        pmSteelCastHeat.setIsBloom(1L);
        pmSteelCastHeat.setCcType(Constants.CcType.TYPE_3);
        pmSteelCastHeat.setWitnessPointStatus(null);
    }

    private void transToPmSteelCastLot(PmSteelCastLot pmSteelCastLot, PmSteelCastLotItf item) {
        pmSteelCastLot.setMainBacklogCode(item.getMainBacklogCode());
        pmSteelCastLot.setCastLotNo(item.getCastLotNo());
        pmSteelCastLot.setStNo(item.getStNo());
        pmSteelCastLot.setSgSign(item.getSgSign());
        pmSteelCastLot.setLotStatus(item.getLotStatus());
        pmSteelCastLot.setCcType(item.getCcType());
        pmSteelCastLot.setOrderTypeCode(null);
        pmSteelCastLot.setCcPlanMode(item.getCcPlanMode());
        pmSteelCastLot.setUrgencyOrderMark(item.getUrgencyOrderMark());
        pmSteelCastLot.setAppType(item.getAppType());
        pmSteelCastLot.setBilletType(item.getBilletType());
        pmSteelCastLot.setSlabGroupSum(item.getSlabGroupSum());
        pmSteelCastLot.setSlabThick(item.getSlabThick());
        pmSteelCastLot.setSlabNum(item.getSlabNum());
    }

    private void transToPmSteelCastSlab(PmSteelCastSlab pmSteelCastSlab, PmSteelCastSlabItf item) {
        int year = LocalDate.now().getYear();
        pmSteelCastSlab.setMainBacklogCode(item.getMainBacklogCode());
        pmSteelCastSlab.setSlabNo(item.getSlabNo());
        pmSteelCastSlab.setCastLotNo(item.getCastLotNo());
        pmSteelCastSlab.setPono(item.getPono());
        pmSteelCastSlab.setBilletType(item.getBilletType());
        pmSteelCastSlab.setSlabGroupNo(item.getSlabGroupNo());
        pmSteelCastSlab.setYearId((long) year);
        pmSteelCastSlab.setSlabNum(item.getSlabNum() != null ? Long.parseLong(item.getSlabNum()) : null);
        pmSteelCastSlab.setSlabPlaceCode(null);
        pmSteelCastSlab.setSlabSeqHeat(null);
        pmSteelCastSlab.setVarietyCode(item.getVarietyCode());
        pmSteelCastSlab.setSteelCode(item.getSteelCode());
        pmSteelCastSlab.setSlabThick(item.getSlabThick());
        pmSteelCastSlab.setSlabWidth(item.getSlabWidth());
        pmSteelCastSlab.setSlabLen(item.getSlabLen() != null ? new BigDecimal(item.getSlabLen()) : null);
        pmSteelCastSlab.setSlabMaxLen(item.getSlabMaxLen() != null ? new BigDecimal(item.getSlabMaxLen()) : null);
        pmSteelCastSlab.setSlabMinLen(item.getSlabMinLen() != null ? new BigDecimal(item.getSlabMinLen()) : null);
        pmSteelCastSlab.setSlabWt(item.getSlabWt());
        pmSteelCastSlab.setSlabMaxWt(item.getSlabMaxWt());
        pmSteelCastSlab.setSlabMinWt(item.getSlabMinWt());
        pmSteelCastSlab.setOrderLenMax(item.getOrderLenMax() != null ? new BigDecimal(item.getOrderLenMax()) : null);
        pmSteelCastSlab.setOrderLenMin(item.getOrderLenMin() != null ? new BigDecimal(item.getOrderLenMin()) : null);
        pmSteelCastSlab.setOrderShortMax(item.getOrderShortMax() != null ? new BigDecimal(item.getOrderShortMax()) : null);
        pmSteelCastSlab.setOrderShortMin(item.getOrderShortMin() != null ? new BigDecimal(item.getOrderShortMin()) : null);
        pmSteelCastSlab.setOrderShortRate(item.getOrderShortRate());
        pmSteelCastSlab.setInfurSlabWid(item.getInfurSlabWid());
        pmSteelCastSlab.setInfurSlabLen(item.getInfurSlabLen() != null ? new BigDecimal(item.getInfurSlabLen()) : null);
        pmSteelCastSlab.setInfurSlabMaxLen(item.getInfurSlabMaxLen() != null ? new BigDecimal(item.getInfurSlabMaxLen()) : null);
        pmSteelCastSlab.setInfurSlabMinLen(item.getInfurSlabMinLen() != null ? new BigDecimal(item.getInfurSlabMinLen()) : null);
        pmSteelCastSlab.setInfurSlabWt(item.getInfurSlabWt());
        pmSteelCastSlab.setInfurSlabMaxWt(item.getSlabMaxWt());
        pmSteelCastSlab.setInfurSlabMinWt(item.getInfurSlabMinWt());
        pmSteelCastSlab.setOrderNo(item.getOrderNo());
        pmSteelCastSlab.setPlanType(item.getPlanType());
        pmSteelCastSlab.setSlabDest(item.getSlabDest());
        pmSteelCastSlab.setHotSendFlag(item.getHotSendFlag());
        pmSteelCastSlab.setHotChargeFlag(item.getHotChargeFlag());
        pmSteelCastSlab.setMendFlag(item.getMendFlag() != null ? Long.parseLong(item.getMendFlag()) : null);
        pmSteelCastSlab.setSlabFinishCode(item.getSlabFinishCode());
        pmSteelCastSlab.setFlameCleanDiv(item.getFlameCleanDiv());
        pmSteelCastSlab.setNewTestNo(item.getNewTestNo());
        pmSteelCastSlab.setShortCutFlag(item.getShortCutFlag());
        pmSteelCastSlab.setPrecRollPlanNo(item.getPrecRollPlanNo());
        pmSteelCastSlab.setPrecRollSeqNo(item.getPrecRollSeqNo() != null ? Long.parseLong(item.getPrecRollSeqNo()) : null);
        pmSteelCastSlab.setSlabCoolCode(item.getSlabCoolCode());
        pmSteelCastSlab.setOrderRemainDiv(item.getOrderRemainDiv());
        pmSteelCastSlab.setSteelLatestDate(item.getSteelLatestDate() != null ? DateUtils.parseDate(item.getSteelLatestDate(), DateUtils.YMD) : null);
        pmSteelCastSlab.setMscLineNo(null);
        pmSteelCastSlab.setRepMatFlag(null);
        pmSteelCastSlab.setSgSign(null);
        pmSteelCastSlab.setStrandNo(item.getStrandNo());
    }
}

