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


import com.xisc.pm.app.service.InterfaceReceiveExecuteService;
import com.xisc.pm.domain.entity.PmSteelCastHeatItf;
import com.xisc.pm.domain.repository.PmSteelCastHeatItfRepository;
import com.xisc.pm.domain.vo.InterfaceXBLG57VO;
import com.xisc.pm.infra.annotations.InterfaceCode;
import com.xisc.pm.infra.constant.Constants;
import org.apache.commons.collections.CollectionUtils;
import org.hzero.mybatis.helper.BatchSqlHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * 二级接口接收执行 XBLG57 服务实施
 *
 * @author 汉得-杨杰
 * @date 2024/10/15
 */
@InterfaceCode(code = "XBLG57")
@Service
public class InterfaceReceiveExecuteXBLG57ServiceImpl implements InterfaceReceiveExecuteService {

    @Autowired
    private PmSteelCastHeatItfRepository pmSteelCastHeatItfRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void execute(Long tenantId, String interfaceCode, String interfaceData) {
        // 解析二级电文数据格式（非接口组特殊说明，统一使用strToEntityList解析即可）
        List<InterfaceXBLG57VO> interfaceXBLG57VOList = strToEntityList(interfaceCode, interfaceData, InterfaceXBLG57VO.class);

        // 准备待插入的列表
        List<PmSteelCastHeatItf> insertList = new ArrayList<>(interfaceXBLG57VOList.size());

        // 遍历解析后的接口数据，映射到PmSteelCastHeat对象
        Set<String> ponoSet = new HashSet<>();
        long ItfBatchId = System.currentTimeMillis();
        for (InterfaceXBLG57VO interfaceVO : interfaceXBLG57VOList) {
            PmSteelCastHeatItf pmSteelCastHeatItf = getPmSteelCastHeatItf(interfaceVO);
            pmSteelCastHeatItf.setTenantId(tenantId);
            pmSteelCastHeatItf.setItfBatchId(ItfBatchId);
            pmSteelCastHeatItf.setMainBacklogCode(Constants.MainBacklogCode.CODE_C);
            if (ponoSet.contains(pmSteelCastHeatItf.getPono())) {
                pmSteelCastHeatItf.setItfStatus(Constants.ItfStatus.E);
                pmSteelCastHeatItf.setItfMessage(Constants.ItfMessage.REPEAT);
            } else {
                pmSteelCastHeatItf.setItfStatus(Constants.ItfStatus.N);
                ponoSet.add(pmSteelCastHeatItf.getPono());
            }
            pmSteelCastHeatItf.setItfRetryTimes(0L);
            // 添加到待插入列表
            insertList.add(pmSteelCastHeatItf);
        }

        // 查询是否数据已存在
        List<String> ponoList = insertList.stream().map(PmSteelCastHeatItf::getPono).distinct().collect(Collectors.toList());
        List<PmSteelCastHeatItf> pmSteelCastHeatItfList = pmSteelCastHeatItfRepository.selectByPonoList(ponoList, tenantId);
        if (CollectionUtils.isNotEmpty(pmSteelCastHeatItfList)) {
            List<String> existPonoList = pmSteelCastHeatItfList.stream().map(PmSteelCastHeatItf::getPono).distinct().collect(Collectors.toList());
            insertList.forEach(item->{
                if (Constants.ItfStatus.N.equals(item.getItfStatus()) && existPonoList.contains(item.getPono())) {
                    item.setItfStatus(Constants.ItfStatus.E);
                    item.setItfMessage(Constants.ItfMessage.EXIST);
                }
            });
        }

        // 批量插入数据
        BatchSqlHelper.open();
        BatchSqlHelper.size(500);
        pmSteelCastHeatItfRepository.batchInsert(insertList);
    }

    private static PmSteelCastHeatItf getPmSteelCastHeatItf(InterfaceXBLG57VO interfaceVO) {
        PmSteelCastHeatItf pmSteelCastHeatItf = new PmSteelCastHeatItf();
        // 映射 InterfaceXBHE07VO 的字段到 PmSteelCastHeat 对象
        pmSteelCastHeatItf.setDisFlag(interfaceVO.getDisFlag());
        pmSteelCastHeatItf.setMainBacklogCode(interfaceVO.getMainBacklogCode());
        pmSteelCastHeatItf.setPono(interfaceVO.getPono());
        pmSteelCastHeatItf.setPonoStatus(interfaceVO.getPonoStatus());
        pmSteelCastHeatItf.setCcSeq(interfaceVO.getCcSeq());
        pmSteelCastHeatItf.setStNo(interfaceVO.getStNo());
        pmSteelCastHeatItf.setSgSign(interfaceVO.getSgSign());
        pmSteelCastHeatItf.setCastLotNo(interfaceVO.getCastLotNo());
        pmSteelCastHeatItf.setCastLotDivNo(interfaceVO.getCastLotDivNo());
        pmSteelCastHeatItf.setCastLotSum(interfaceVO.getCastLotSum());
        pmSteelCastHeatItf.setCcMachNo(interfaceVO.getCcMachNo());
        pmSteelCastHeatItf.setCcType(interfaceVO.getCcType());
        pmSteelCastHeatItf.setRestrandFlg(interfaceVO.getRestrandFlg());
        pmSteelCastHeatItf.setBacklogEa(interfaceVO.getBacklogEa());
        pmSteelCastHeatItf.setRefineDiv(interfaceVO.getRefineDiv());
        pmSteelCastHeatItf.setSmeltMode(interfaceVO.getSmeltMode());
        pmSteelCastHeatItf.setPlanTapWt(interfaceVO.getPlanTapWt());
        pmSteelCastHeatItf.setApn(interfaceVO.getApn());
        pmSteelCastHeatItf.setDest(interfaceVO.getDest());
        pmSteelCastHeatItf.setNewTestNo(interfaceVO.getNewTestNo());
        pmSteelCastHeatItf.setSteelPlanDate(interfaceVO.getSteelPlanDate());
        pmSteelCastHeatItf.setSteelAppDate(interfaceVO.getSteelAppDate());
        pmSteelCastHeatItf.setPlanDate(interfaceVO.getPlanDate());
        pmSteelCastHeatItf.setCcReqTime(interfaceVO.getCcReqTime());
        pmSteelCastHeatItf.setCcReqTimeFlag(interfaceVO.getCcReqTimeFlag());
        pmSteelCastHeatItf.setHeatConfmTime(interfaceVO.getHeatConfmTime());
        pmSteelCastHeatItf.setHotReqTime(interfaceVO.getHotReqTime());
        pmSteelCastHeatItf.setHotSendFlag(interfaceVO.getHotSendFlag());
        pmSteelCastHeatItf.setHotChargeFlag(interfaceVO.getHotChargeFlag());
        pmSteelCastHeatItf.setTdChgFlg(interfaceVO.getTdChgFlg());
        pmSteelCastHeatItf.setPonoOld(interfaceVO.getPonoOld());
        pmSteelCastHeatItf.setSlabReq(interfaceVO.getSlabReq());
        pmSteelCastHeatItf.setFlameCleanDiv(interfaceVO.getFlameCleanDiv());
        pmSteelCastHeatItf.setShiftPoint(interfaceVO.getShiftPoint());
        pmSteelCastHeatItf.setShiftNo(interfaceVO.getShiftNo());
        pmSteelCastHeatItf.setShiftGroup(interfaceVO.getShiftGroup());
        pmSteelCastHeatItf.setHoldPlanner(interfaceVO.getHoldPlanner());
        pmSteelCastHeatItf.setHoldTime(interfaceVO.getHoldTime());
        pmSteelCastHeatItf.setPlanReleaser(interfaceVO.getPlanReleaser());
        pmSteelCastHeatItf.setRelTime(interfaceVO.getRelTime());
        pmSteelCastHeatItf.setSendPlanner(interfaceVO.getSendPlanner());
        pmSteelCastHeatItf.setSendTime(interfaceVO.getSendTime());
        pmSteelCastHeatItf.setCcBase(interfaceVO.getCcBase());
        pmSteelCastHeatItf.setPourTime(interfaceVO.getPourTime());
        pmSteelCastHeatItf.setAdjustFlag(interfaceVO.getAdjustFlag());
        return pmSteelCastHeatItf;
    }

}
