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


import com.xisc.pm.app.service.InterfaceReceiveExecuteService;
import com.xisc.pm.domain.entity.PmSteelCastFlowItf;
import com.xisc.pm.domain.repository.PmSteelCastFlowItfRepository;
import com.xisc.pm.domain.vo.InterfaceXBLG59VO;
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;


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

    @Autowired
    private PmSteelCastFlowItfRepository pmSteelCastFlowItfRepository;

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

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

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

        // 查询是否数据已存在
        List<String> ponoList = insertList.stream().map(PmSteelCastFlowItf::getPono).distinct().collect(Collectors.toList());
        List<PmSteelCastFlowItf> pmSteelCastFlowItfList = pmSteelCastFlowItfRepository.selectByPonoList(ponoList, tenantId);
        if (CollectionUtils.isNotEmpty(pmSteelCastFlowItfList)) {
            List<String> existPonoList = pmSteelCastFlowItfList.stream().map(PmSteelCastFlowItf::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);
        pmSteelCastFlowItfRepository.batchInsert(insertList);
    }

    private static PmSteelCastFlowItf getPmSteelCastFlowItf(InterfaceXBLG59VO interfaceVO) {
        PmSteelCastFlowItf pmSteelCastFlowItf = new PmSteelCastFlowItf();
        // 映射 InterfaceXBHE08VO 的字段到 PmSteelCastFlowItf 对象
        pmSteelCastFlowItf.setMainBacklogCode(interfaceVO.getMainBacklogCode());
        pmSteelCastFlowItf.setPono(interfaceVO.getPono());
        pmSteelCastFlowItf.setStrandNo(interfaceVO.getStrandNo());
        pmSteelCastFlowItf.setHeatSlabNum(interfaceVO.getFlowSlabNum());
        pmSteelCastFlowItf.setSlabThick(interfaceVO.getSlabThick());
        pmSteelCastFlowItf.setSlabWidth(interfaceVO.getSlabWidth());
        pmSteelCastFlowItf.setAdjustWidthNum(interfaceVO.getAdjustWidthNum());
        pmSteelCastFlowItf.setUnappSlabLen(interfaceVO.getUnappSlabLen());
        pmSteelCastFlowItf.setUnappSlabLenMax(interfaceVO.getUnappSlabLenMax());
        pmSteelCastFlowItf.setUnappSlabLenMin(interfaceVO.getUnappSlabLenMin());
        return pmSteelCastFlowItf;
    }

}
