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


import com.xisc.pm.app.service.InterfaceReceiveExecuteService;
import com.xisc.pm.domain.entity.PmSteelCastSlabItf;
import com.xisc.pm.domain.repository.PmSteelCastSlabItfRepository;
import com.xisc.pm.domain.vo.InterfaceXBLG58VO;
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;


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

    @Autowired
    private PmSteelCastSlabItfRepository pmSteelCastSlabItfRepository;

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

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

        // 遍历解析后的接口数据，映射到PmSteelCastHeat对象
        long itfBatchId = System.currentTimeMillis();
        Set<String> slabNoSet = new HashSet<>();
        for (InterfaceXBLG58VO interfaceVO : interfaceXBLG58VOList) {
            PmSteelCastSlabItf pmSteelCastSlabItf = getPmSteelCastSlabItf(interfaceVO);
            pmSteelCastSlabItf.setTenantId(tenantId);
            pmSteelCastSlabItf.setItfBatchId(itfBatchId);
            pmSteelCastSlabItf.setMainBacklogCode(Constants.MainBacklogCode.CODE_C);
            if (slabNoSet.contains(pmSteelCastSlabItf.getSlabNo())) {
                pmSteelCastSlabItf.setItfStatus(Constants.ItfStatus.E);
                pmSteelCastSlabItf.setItfMessage(Constants.ItfMessage.REPEAT);
            } else {
                pmSteelCastSlabItf.setItfStatus(Constants.ItfStatus.N);
                slabNoSet.add(pmSteelCastSlabItf.getSlabNo());
            }
            pmSteelCastSlabItf.setItfRetryTimes(0L);
            insertList.add(pmSteelCastSlabItf);
        }

        // 查询是否数据已存在
        List<String> slabNoList = insertList.stream().map(PmSteelCastSlabItf::getSlabNo).distinct().collect(Collectors.toList());
        List<PmSteelCastSlabItf> pmSteelCastSlabItfList = pmSteelCastSlabItfRepository.selectBySlabNoList(slabNoList, tenantId);
        if (CollectionUtils.isNotEmpty(pmSteelCastSlabItfList)) {
            List<String> existSlabNoList = pmSteelCastSlabItfList.stream().map(PmSteelCastSlabItf::getSlabNo).distinct().collect(Collectors.toList());
            insertList.forEach(item->{
                if (Constants.ItfStatus.N.equals(item.getItfStatus()) && existSlabNoList.contains(item.getSlabNo())) {
                    item.setItfStatus(Constants.ItfStatus.E);
                    item.setItfMessage(Constants.ItfMessage.EXIST);
                }
            });
        }

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

    private static PmSteelCastSlabItf getPmSteelCastSlabItf(InterfaceXBLG58VO interfaceVO) {
        PmSteelCastSlabItf pmSteelCastSlabItf = new PmSteelCastSlabItf();

        // 赋值其他字段
        pmSteelCastSlabItf.setMainBacklogCode(interfaceVO.getMainBacklogCode());
        pmSteelCastSlabItf.setSlabNo(interfaceVO.getSlabNo());
        pmSteelCastSlabItf.setCastLotNo(interfaceVO.getCastLotNo());
        pmSteelCastSlabItf.setPono(interfaceVO.getPono());
        pmSteelCastSlabItf.setStrandNo(interfaceVO.getStrandNo());
        pmSteelCastSlabItf.setBilletType(interfaceVO.getBilletType());
        pmSteelCastSlabItf.setSlabGroupNo(interfaceVO.getSlabGroupNo());
        pmSteelCastSlabItf.setSlabNum(interfaceVO.getSlabNum());
        pmSteelCastSlabItf.setVarietyCode(interfaceVO.getVarietyCode());
        pmSteelCastSlabItf.setSteelCode(interfaceVO.getSteelCode());
        pmSteelCastSlabItf.setSlabThick(interfaceVO.getSlabThick());
        pmSteelCastSlabItf.setSlabWidth(interfaceVO.getSlabWidth());
        pmSteelCastSlabItf.setSlabLen(interfaceVO.getSlabLen());
        pmSteelCastSlabItf.setSlabMaxLen(interfaceVO.getSlabMaxLen());
        pmSteelCastSlabItf.setSlabMinLen(interfaceVO.getSlabMinLen());
        pmSteelCastSlabItf.setSlabWt(interfaceVO.getSlabWt());
        pmSteelCastSlabItf.setSlabMaxWt(interfaceVO.getSlabMaxWt());
        pmSteelCastSlabItf.setSlabMinWt(interfaceVO.getSlabMinWt());
        pmSteelCastSlabItf.setOrderLenMax(interfaceVO.getOrderLenMax());
        pmSteelCastSlabItf.setOrderLenMin(interfaceVO.getOrderLenMin());
        pmSteelCastSlabItf.setOrderShortMax(interfaceVO.getOrderShortMax());
        pmSteelCastSlabItf.setOrderShortMin(interfaceVO.getOrderShortMin());
        pmSteelCastSlabItf.setOrderShortRate(interfaceVO.getOrderShortRate());
        pmSteelCastSlabItf.setInfurSlabWid(interfaceVO.getInfurSlabWid());
        pmSteelCastSlabItf.setInfurSlabLen(interfaceVO.getInfurSlabLen());
        pmSteelCastSlabItf.setInfurSlabMaxLen(interfaceVO.getInfurSlabMaxLen());
        pmSteelCastSlabItf.setInfurSlabMinLen(interfaceVO.getInfurSlabMinLen());
        pmSteelCastSlabItf.setInfurSlabWt(interfaceVO.getInfurSlabWt());
        pmSteelCastSlabItf.setInfurSlabMaxWt(interfaceVO.getInfurSlabMaxWt());
        pmSteelCastSlabItf.setInfurSlabMinWt(interfaceVO.getInfurSlabMinWt());
        pmSteelCastSlabItf.setOrderNo(interfaceVO.getOrderNo());
        pmSteelCastSlabItf.setPlanType(interfaceVO.getPlanType());
        pmSteelCastSlabItf.setSlabDest(interfaceVO.getSlabDest());
        pmSteelCastSlabItf.setHotSendFlag(interfaceVO.getHotSendFlag());
        pmSteelCastSlabItf.setHotChargeFlag(interfaceVO.getHotChargeFlag());
        pmSteelCastSlabItf.setMendFlag(interfaceVO.getMendFlag());
        pmSteelCastSlabItf.setSlabFinishCode(interfaceVO.getSlabFinishCode());
        pmSteelCastSlabItf.setFlameCleanDiv(interfaceVO.getFlameCleanDiv());
        pmSteelCastSlabItf.setNewTestNo(interfaceVO.getNewTestNo());
        pmSteelCastSlabItf.setShortCutFlag(interfaceVO.getShortCutFlag());
        pmSteelCastSlabItf.setPrecRollPlanNo(interfaceVO.getPrecRollPlanNo());
        pmSteelCastSlabItf.setPrecRollSeqNo(interfaceVO.getPrecRollSeqNo());
        pmSteelCastSlabItf.setSlabCoolCode(interfaceVO.getSlabCoolCode());
        pmSteelCastSlabItf.setOrderRemainDiv(interfaceVO.getOrderRemainDiv());
        pmSteelCastSlabItf.setSteelLatestDate(interfaceVO.getSteelLatestDate());
        return pmSteelCastSlabItf;
    }

}
