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


import com.xisc.pm.app.service.InterfaceReceiveExecuteService;
import com.xisc.pm.domain.entity.PmSteelCastLotItf;
import com.xisc.pm.domain.repository.PmSteelCastLotItfRepository;
import com.xisc.pm.domain.vo.InterfaceXBHE11VO;
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;


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

    @Autowired
    private PmSteelCastLotItfRepository pmSteelCastLotItfRepository;

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

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

        // 遍历解析后的接口数据，映射到PmSteelCastHeat对象
        long itfBatchId = System.currentTimeMillis();
        Set<String> castLotNoSet = new HashSet<>();
        for (InterfaceXBHE11VO interfaceVO : interfaceXBHE11VOList) {
            PmSteelCastLotItf pmSteelCastLotItf = getPmSteelCastLotItf(interfaceVO);
            pmSteelCastLotItf.setTenantId(tenantId);
            pmSteelCastLotItf.setItfBatchId(itfBatchId);
            if (castLotNoSet.contains(pmSteelCastLotItf.getCastLotNo())) {
                pmSteelCastLotItf.setItfStatus(Constants.ItfStatus.E);
                pmSteelCastLotItf.setItfMessage(Constants.ItfMessage.REPEAT);
            } else {
                pmSteelCastLotItf.setItfStatus(Constants.ItfStatus.N);
                castLotNoSet.add(pmSteelCastLotItf.getCastLotNo());
            }
            pmSteelCastLotItf.setItfRetryTimes(0L);
            insertList.add(pmSteelCastLotItf);
        }

        // 查询是否数据已存在
        List<String> castLotNoList = insertList.stream().map(PmSteelCastLotItf::getCastLotNo).distinct().collect(Collectors.toList());
        List<PmSteelCastLotItf> pmSteelCastLotItfList = pmSteelCastLotItfRepository.selectByCastLotNoList(castLotNoList, tenantId);
        if (CollectionUtils.isNotEmpty(pmSteelCastLotItfList)) {
            List<String> existCastLotNoList = pmSteelCastLotItfList.stream().map(PmSteelCastLotItf::getCastLotNo).distinct().collect(Collectors.toList());
            insertList.forEach(item->{
                if (Constants.ItfStatus.N.equals(item.getItfStatus()) && existCastLotNoList.contains(item.getCastLotNo())) {
                    item.setItfStatus(Constants.ItfStatus.E);
                    item.setItfMessage(Constants.ItfMessage.EXIST);
                }
            });
        }

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

    private static PmSteelCastLotItf getPmSteelCastLotItf(InterfaceXBHE11VO interfaceVO) {
        PmSteelCastLotItf pmSteelCastLotItf = new PmSteelCastLotItf();
        // 赋值其他字段
        pmSteelCastLotItf.setMainBacklogCode(interfaceVO.getMainBacklogCode());
        pmSteelCastLotItf.setCastLotNo(interfaceVO.getCastLotNo());
        pmSteelCastLotItf.setStNo(interfaceVO.getStNo());
        pmSteelCastLotItf.setSgSign(interfaceVO.getSgSign());
        pmSteelCastLotItf.setLotStatus(interfaceVO.getLotStatus());
        pmSteelCastLotItf.setCcType(interfaceVO.getCcType());
        pmSteelCastLotItf.setCcPlanMode(interfaceVO.getCcPlanMode());
        pmSteelCastLotItf.setUrgencyOrderMark(interfaceVO.getUrgencyOrderMark());
        pmSteelCastLotItf.setAppType(interfaceVO.getAppType());
        pmSteelCastLotItf.setBilletType(interfaceVO.getBilletType());
        pmSteelCastLotItf.setSlabGroupSum(interfaceVO.getSlabGroupSum());
        pmSteelCastLotItf.setSlabThick(interfaceVO.getSlabThick());
        pmSteelCastLotItf.setSlabNum(interfaceVO.getSlabNum());
        return pmSteelCastLotItf;
    }


}
