package com.cusc.nirvana.user.rnr.fp.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSONObject;
import com.cusc.nirvana.common.result.Response;
import com.cusc.nirvana.user.exception.CuscUserException;
import com.cusc.nirvana.user.rnr.fp.common.ResponseCode;
import com.cusc.nirvana.user.rnr.fp.common.constant.VinCardCheckBussinessDetailType;
import com.cusc.nirvana.user.rnr.fp.dto.*;
import com.cusc.nirvana.user.rnr.fp.handler.VinCardCheckMessageHandler;
import com.cusc.nirvana.user.rnr.fp.service.IFileService;
import com.cusc.nirvana.user.rnr.fp.service.INewVinCardService;
import com.cusc.nirvana.user.rnr.fp.service.ISimVehicleRelService;
import com.cusc.nirvana.user.rnr.mg.client.MgRnrCardInfoClient;
import com.cusc.nirvana.user.rnr.mg.client.MgRnrInfoClient;
import com.cusc.nirvana.user.rnr.mg.client.RnrOrderClient;
import com.cusc.nirvana.user.rnr.mg.constants.RnrOrderStatusEnum;
import com.cusc.nirvana.user.rnr.mg.constants.RnrOrderType;
import com.cusc.nirvana.user.rnr.mg.constants.RnrStatus;
import com.cusc.nirvana.user.rnr.mg.dto.MgRnrCardInfoDTO;
import com.cusc.nirvana.user.rnr.mg.dto.MgRnrInfoDTO;
import com.cusc.nirvana.user.rnr.mg.dto.RnrOrderDTO;
import com.cusc.nirvana.user.util.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author stayAnd
 * @date 2022/5/17
 */
@Service
@Slf4j
public class NewVinCardServiceImpl implements INewVinCardService {

	@Resource
	private MgRnrCardInfoClient cardInfoClient;

	@Resource
	private ISimVehicleRelService simVehicleRelService;

	@Resource
	private MgRnrInfoClient rnrInfoClient;

	@Resource
	private IFileService fileService;

	@Resource
	private RnrOrderClient orderClient;

	private static final Integer BATCH_CHECK_SIZE = 500;

	/**
	 * 新改造的方法
	 */
	@Override
	public  Response<VinCardResultDTO> queryUnBindCardByVin(VinCardQueryDTO queryDTO) {
		VinCardResultDTO resultDTO = new VinCardResultDTO();
		resultDTO.setVin(queryDTO.getVin());
		//根据vin获取iccid卡列表
		Response<List<VinIccidDTO>> iccidResponse = null;
		try {
			iccidResponse = simVehicleRelService.getIccidByVin(queryDTO.getVin());
			log.info("根据vin查询iccid列表,vin = {} ,response = {}",queryDTO.getVin(),JSONObject.toJSONString(iccidResponse));
		} catch (Exception e) {
			log.info("根据vin查询iccid列表出错,vin = {}",queryDTO.getVin(),e);
			resultDTO.setIccidList(Collections.emptyList());
			//return Response.createError("根据vin查询iccid列表出错");
			return Response.createSuccess(resultDTO);
		}

		if (null == iccidResponse || !iccidResponse.isSuccess() || CollectionUtils.isEmpty(iccidResponse.getData())) {
			resultDTO.setIccidList(Collections.emptyList());
			return Response.createSuccess(resultDTO);
		}
		List<VinIccidDTO> iccidDTOList = iccidResponse.getData();
		List<String> iccidList = iccidDTOList.stream().map(VinIccidDTO::getIccid).collect(Collectors.toList());
		MgRnrCardInfoDTO cardQuery = new MgRnrCardInfoDTO();
		cardQuery.setIccidList(iccidList);
		List<MgRnrCardInfoDTO> cardInfoList = cardInfoClient.queryByList(cardQuery).getData();

		//非一车多卡时 过滤掉车企业实名的卡
		if (!Boolean.TRUE.equals(queryDTO.getFilter())) {
			cardInfoList = filterManufacturerRnr(cardInfoList);
		}

		List<String> rnrCardList = cardInfoList.stream().filter(card -> RnrStatus.INIT.getCode() == card.getRnrStatus() || RnrStatus.RNR.getCode() == card.getRnrStatus())
				.map(MgRnrCardInfoDTO::getIccid).collect(Collectors.toList());


		// 过滤掉已实名/初始化状态的iccid
		List<String> filterIccidList = iccidList.stream().filter(iccId -> !rnrCardList.contains(iccId)).collect(Collectors.toList());
		resultDTO.setIccidList(filterIccidList);
		log.info("查询返回的iccid是,{}",iccidList);
		log.info("已实名/初始化状态iccid是,{}",rnrCardList);
		log.info("通过vin查询车卡关系表,当前未实名的iccid是,{}",filterIccidList);
		return Response.createSuccess(resultDTO);
	}


	@Override
	public VinCardResultDTO queryBindCardByVin(VinCardQueryDTO queryDTO) {
		MgRnrCardInfoDTO query = new MgRnrCardInfoDTO();
		query.setIotId(queryDTO.getVin());
		Response<List<MgRnrCardInfoDTO>> response = cardInfoClient.queryByList(query);

		VinCardResultDTO resultDTO = new VinCardResultDTO();
		resultDTO.setVin(queryDTO.getVin());
		if (null == response || !response.isSuccess() || CollectionUtils.isEmpty(response.getData())) {
			resultDTO.setIccidList(Collections.emptyList());
			return resultDTO;
		}

		List<MgRnrCardInfoDTO> cardList = response.getData();
		String rnrId = cardList.get(0).getRnrId();
		resultDTO.setRnrId(rnrId);
		List<String> iccidList = cardList.stream().filter(
				card->card.getRnrStatus() == RnrStatus.INIT.getCode()
				|| card.getRnrStatus() == RnrStatus.RNR.getCode()
		).map(MgRnrCardInfoDTO::getIccid).collect(Collectors.toList());
		//添加去重
		iccidList = iccidList.stream().distinct().collect(Collectors.toList());
		log.info("查询实名卡信息表,有实名记录的是,{}", iccidList);
		resultDTO.setIccidList(iccidList);

		return resultDTO;
	}

	@Override
	public VinCardCheckResultDTO checkVinCard(VinCardCheckDTO vinCardCheckDTO) {
		log.info("checkVinCard 检查卡信息:{}",JSONObject.toJSONString(vinCardCheckDTO));
		Integer businessType = vinCardCheckDTO.getBusinessType();
		if (VinCardCheckBussinessDetailType.NATURALPERSON_NEW.getType().equals(businessType) || VinCardCheckBussinessDetailType.ONECAR_MORECARD.getType().equals(businessType)) {
			//自然人实名-新车主 ||  一车多卡绑定
			return checkNewNeedBindCard(vinCardCheckDTO.getVin(),vinCardCheckDTO.getIccidList(),VinCardCheckBussinessDetailType.getBytype(businessType));
		} else if (VinCardCheckBussinessDetailType.NATURALPERSON_SECOND.getType().equals(businessType)) {
			//自然人实名-二手车新车主
			checkNeedUnBindCard(vinCardCheckDTO.getVin(),vinCardCheckDTO.getIccidList(),vinCardCheckDTO.getRnrId(),VinCardCheckBussinessDetailType.NATURALPERSON_SECOND);
		} else if (VinCardCheckBussinessDetailType.ENTERPRISE.getType().equals(businessType) || VinCardCheckBussinessDetailType.VEHICLE.getType().equals(businessType)){
			//企业实名 || 车企实名
			processExcel(vinCardCheckDTO,VinCardCheckBussinessDetailType.getBytype(businessType));
		} else if (VinCardCheckBussinessDetailType.UNBIND_ORIGINAL.getType().equals(businessType) || VinCardCheckBussinessDetailType.UNBIND_SECOND.getType().equals(businessType)){
			//卡解绑 - 原车主 ||  卡解绑 - 二手车解绑
			return checkNeedUnBindCard(vinCardCheckDTO.getVin(),vinCardCheckDTO.getIccidList(),vinCardCheckDTO.getRnrId(),VinCardCheckBussinessDetailType.getBytype(businessType));
		} else if (VinCardCheckBussinessDetailType.CHANGE_BOX.getType().equals(businessType)){
			//换件
			if (!CollectionUtils.isEmpty(vinCardCheckDTO.getIccidList())) {
				//老卡
				checkNeedUnBindCard(vinCardCheckDTO.getVin(),vinCardCheckDTO.getIccidList(),vinCardCheckDTO.getRnrId(),VinCardCheckBussinessDetailType.CHANGE_BOX);
			}
			if (!CollectionUtils.isEmpty(vinCardCheckDTO.getNewIccidList())) {
				//新卡
				checkNewNeedBindCard(vinCardCheckDTO.getVin(),vinCardCheckDTO.getNewIccidList(),VinCardCheckBussinessDetailType.CHANGE_BOX);
			}
		} else if (VinCardCheckBussinessDetailType.UNBIND_VEHICLE.getType().equals(businessType)){
			//车企实名解绑
			processExcel(vinCardCheckDTO,VinCardCheckBussinessDetailType.UNBIND_VEHICLE);
		}

		return null;
	}


	private void processExcel(VinCardCheckDTO vinCardCheckDTO, VinCardCheckBussinessDetailType bussinessDetailType) {
		String fileId = vinCardCheckDTO.getFileId();
		if (StringUtils.isBlank(fileId)){
			throw new CuscUserException("","文件信息为空");
		}
		byte[] bytes = fileService.downLoadBytes(fileId);
		ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
		List<VinCardExcelData> excelList = new ArrayList<>();
		EasyExcel.read(inputStream, VinCardExcelData.class, new AnalysisEventListener<VinCardExcelData>() {
			@Override
			public void invoke(VinCardExcelData vinCardExcelData, AnalysisContext analysisContext) {
				excelList.add(vinCardExcelData);
				if (excelList.size() >= BATCH_CHECK_SIZE) {
					processExcelData(excelList,bussinessDetailType);
					excelList.clear();
				}
			}

			@Override
			public void doAfterAllAnalysed(AnalysisContext analysisContext) {
				if (!CollectionUtils.isEmpty(excelList)) {
					processExcelData(excelList,bussinessDetailType);
				}
			}
		}).sheet().doRead();

		try {
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void processExcelData(List<VinCardExcelData> excelList, VinCardCheckBussinessDetailType detailType) {
		log.info("processExcelData excelList = {}",JSONObject.toJSONString(excelList));
		Map<String, List<VinCardExcelData>> map = excelList.stream().collect(Collectors.groupingBy(VinCardExcelData::getVin));

		map.entrySet().parallelStream().forEach(entry->{
			String vin = entry.getKey();
			List<VinCardExcelData> excelDataList = entry.getValue();
			List<String> iccidList = excelDataList.stream().map(VinCardExcelData::getIccid).collect(Collectors.toList());
			if (detailType.equals(VinCardCheckBussinessDetailType.UNBIND_VEHICLE)) {
				//车企实名解绑
				checkNeedUnBindCard(vin,iccidList,null,VinCardCheckBussinessDetailType.UNBIND_VEHICLE);
			} else {
				//企业实名 车企实名
				checkNewNeedBindCard(vin,iccidList,detailType);
			}
		});
	}




	/**
	 * 校验需要新解绑的卡
	 * @param vin
	 * @param iccidList
	 * @param rnrId
	 * @param detailType
	 * @return
	 */
	private VinCardCheckResultDTO checkNeedUnBindCard(String vin,List<String> iccidList,String rnrId,VinCardCheckBussinessDetailType detailType) {
		//输入的卡必须是 已经实名的记录
		List<MgRnrCardInfoDTO> cardInfoDTOList = queryCardListByIccidList(iccidList);
		List<String> hasRnrIccidList = cardInfoDTOList.stream()
				.filter(card -> RnrStatus.RNR.getCode() == card.getRnrStatus()).map(MgRnrCardInfoDTO::getIccid)
				.collect(Collectors.toList());
		if (!hasRnrIccidList.containsAll(iccidList)) {
			log.error("或者存在实名过,但是实名状态是初始化的情况");
			throw new CuscUserException("", "输入的卡信息存在非实名的卡");
		}

		if (cardInfoDTOList.stream().filter(card->RnrStatus.RNR.getCode() == card.getRnrStatus()).anyMatch(card->StringUtils.isNotBlank(card.getIotId()) && !card.getIotId().equals(vin))){
			throw new CuscUserException("", "输入的卡信息存在vin和输入的vin不一致");
		}

		//检查 要解绑的卡是否存在未完成的工单信息
		checkUnFinishOrder(cardInfoDTOList);

		if (detailType.getType().equals(VinCardCheckBussinessDetailType.UNBIND_ORIGINAL.getType()) || detailType.getType().equals(VinCardCheckBussinessDetailType.CHANGE_BOX.getType())) {
			rnrId = StringUtils.isBlank(rnrId) ? cardInfoDTOList.get(0).getRnrId():rnrId;
			MgRnrInfoDTO query = new MgRnrInfoDTO();
			query.setUuid(rnrId);
			MgRnrInfoDTO rnrInfoDTO = rnrInfoClient.getByUuid(query).getData();
			if (null == rnrInfoDTO) {
				throw new CuscUserException("","未查询到实名信息,rnrId:"+rnrId);
			}
			return new VinCardCheckResultDTO().setName(rnrInfoDTO.getFullName()).setPhone(rnrInfoDTO.getPhone());
		}

		return null;
	}

	private void checkUnFinishOrder(List<MgRnrCardInfoDTO> iccidList) {
		List<String> orderIdList = iccidList.stream().map(MgRnrCardInfoDTO::getOrderId).distinct().collect(Collectors.toList());

		List<String> notFinishOrderIdList = orderIdList.parallelStream().filter(orderId -> {
			RnrOrderDTO query = new RnrOrderDTO();
			query.setUuid(orderId);
			RnrOrderDTO orderDTO = orderClient.getByUuid(query).getData();
			return RnrOrderStatusEnum.notFinished(orderDTO.getOrderStatus());
		}).collect(Collectors.toList());

		if (!CollectionUtils.isEmpty(notFinishOrderIdList)) {
			throw new CuscUserException("", "存在未完成的工单信息");
		}
	}

	/**
	 * 校验需要新绑定的卡
	 * @param vin
	 * @param iccidList
	 * @param detailType
	 */
	private VinCardCheckResultDTO checkNewNeedBindCard(String vin,List<String> iccidList,VinCardCheckBussinessDetailType detailType) {
		//查询卡是否存在
		checkCardExsit(iccidList);
		//查询车卡关系
		checkVinCardRelation(iccidList,vin);

		//查询vin是否存在已实名/初始化(认证中)的记录
		List<MgRnrCardInfoDTO> mgRnrCardInfoList = queryCardListByVin(vin);
		//输入的卡是否存在 已实名/初始化(认证中)的记录
		List<MgRnrCardInfoDTO> cardInfoDTOList = queryCardListByIccidList(iccidList);

		//新车 企业实名需要过滤掉车企实名的卡
		if(detailType.getType().equals(VinCardCheckBussinessDetailType.NATURALPERSON_NEW.getType())
				|| detailType.getType().equals(VinCardCheckBussinessDetailType.ENTERPRISE.getType())) {
			mgRnrCardInfoList = filterManufacturerRnr(mgRnrCardInfoList);
			cardInfoDTOList = filterManufacturerRnr(cardInfoDTOList);
		}

		if (cardInfoDTOList.stream().anyMatch(
				card -> RnrStatus.INIT.getCode() == card.getRnrStatus() || RnrStatus.RNR.getCode() == card.getRnrStatus())) {
			throw new CuscUserException("", "输入的卡信息已存在实名记录");
		}
		if (detailType.getType().equals(VinCardCheckBussinessDetailType.NATURALPERSON_NEW.getType())
				|| detailType.getType().equals(VinCardCheckBussinessDetailType.ENTERPRISE.getType())
				|| detailType.getType().equals(VinCardCheckBussinessDetailType.VEHICLE.getType())) {
			//自然人 - 新车 || 企业实名 || 车企实名
			if (mgRnrCardInfoList.stream().anyMatch(
					card -> RnrStatus.INIT.getCode() == card.getRnrStatus() || RnrStatus.RNR.getCode() == card.getRnrStatus())) {
//				throw new CuscUserException("", "vin已经存在实名信息,请走一车多卡流程");
				throw new CuscUserException("", "您提交的VIN号已实名，请勿重复提交实名！");
			}
		} else if(detailType.getType().equals(VinCardCheckBussinessDetailType.ONECAR_MORECARD.getType())){
			//一车多卡
			List<MgRnrCardInfoDTO> rnrCardList = mgRnrCardInfoList.stream()
					.filter(card -> card.getRnrStatus() == RnrStatus.RNR.getCode()).collect(Collectors.toList());
			if (CollectionUtils.isEmpty(rnrCardList)) {
				throw new CuscUserException("", "该vin号未查询到实名的卡信息");
			}
			String rnrId = rnrCardList.get(0).getRnrId();
			MgRnrInfoDTO rnrInfoQuery = new MgRnrInfoDTO();
			rnrInfoQuery.setUuid(rnrId);
			Response<MgRnrInfoDTO> rnrInfoDTOResponse = rnrInfoClient.getByUuid(rnrInfoQuery);
			if (null != rnrInfoDTOResponse && rnrInfoDTOResponse.isSuccess() && null != rnrInfoDTOResponse.getData()) {
				MgRnrInfoDTO data = rnrInfoDTOResponse.getData();
				return new VinCardCheckResultDTO().setPhone(data.getPhone()).setName(data.getFullName());
			}
		}


		return null;
	}


	private List<MgRnrCardInfoDTO> queryCardListByIccidList(List<String> iccidList){
		MgRnrCardInfoDTO query = new MgRnrCardInfoDTO();
		query.setIccidList(iccidList);
		Response<List<MgRnrCardInfoDTO>> response = cardInfoClient.queryByList(query);
		if (null == response || !response.isSuccess()) {
			throw new CuscUserException("","查询卡信息失败");
		}
		return response.getData();
	}

	/*private List<MgRnrCardInfoDTO> queryCardListByIccidListAndVin(List<String> iccidList,String vin){
		MgRnrCardInfoDTO query = new MgRnrCardInfoDTO();
		query.setIccidList(iccidList);
		//query.setIotId(vin);
		Response<List<MgRnrCardInfoDTO>> response = cardInfoClient.queryByList(query);
		if (null == response || !response.isSuccess()) {
			throw new CuscUserException("","查询卡信息失败");
		}
		return response.getData();
	}*/

	private List<MgRnrCardInfoDTO> queryCardListByVin(String vin){
		MgRnrCardInfoDTO query = new MgRnrCardInfoDTO();
		query.setIotId(vin);
		Response<List<MgRnrCardInfoDTO>> response = cardInfoClient.queryByList(query);
		if (null == response || !response.isSuccess()) {
			throw new CuscUserException("","查询卡信息失败");
		}
		return response.getData();
	}


	private void checkCardExsit(List<String> cardList){
		boolean allMatch = cardList.parallelStream().allMatch(iccid -> {
			Response<IccidDetailDTO> response = simVehicleRelService.getDetailByIccid(iccid);
			log.info("查询卡是否存在,iccid = {},response = {}",iccid,JSONObject.toJSONString(response));
			return response != null && response.isSuccess() && null != response.getData();
		});
		if (!allMatch) {
			throw new CuscUserException("","提交的卡信息未查询到");
		}
	}

	private void checkVinCardRelation(List<String> iccidList, String vin) {
		boolean allMatch = iccidList.stream().allMatch(iccid -> {
			Response<VinIccidDTO> response = simVehicleRelService.getVinByIccid(iccid);
			log.info("查询车卡匹配,iccid = {},输入vin = {},response = {}", iccid, vin, JSONObject.toJSONString(response));
			if (null == response || !response.isSuccess()) {
				throw new CuscUserException("", "查询车卡匹配关系失败");
			}
			VinIccidDTO data = response.getData();
			if (null == data || StringUtils.isBlank(data.getVin())) {
				return true;
			}
			return vin.equals(data.getVin());
		});
		if (!allMatch) {
			throw new CuscUserException("", "卡信息的vin码和输入的vin不一致");
		}
	}


	@Override
	public VinCardCheckMessageResultDTO checkVinCardMessage(VinCardCheckMessageRequestDTO requestDTO) {
		VinCardCheckBussinessDetailType detailType = VinCardCheckBussinessDetailType.getBytype(requestDTO.getBusinessType());

		//绑定卡的 校验
		String bindVinCardCheckHandlerName = detailType.getBindVinCardCheckHandlerName();

		VinCardCheckMessageResultDTO bindResultDto = null;
		if (StringUtils.isNotBlank(bindVinCardCheckHandlerName)){
			VinCardCheckMessageHandler bindHandler = SpringUtil.getBean(bindVinCardCheckHandlerName, VinCardCheckMessageHandler.class);
			bindResultDto = bindHandler.checkVinCard(requestDTO.getVin(), requestDTO.getBindIccidList(), detailType);
		}

		//解绑的卡的 校验
		String unBindVinCardCheckHandlerName = detailType.getUnBindVinCardCheckHandlerName();

		VinCardCheckMessageResultDTO unBindResultDto = null;
		if (StringUtils.isNotBlank(unBindVinCardCheckHandlerName)) {
			VinCardCheckMessageHandler unBindHandler = SpringUtil.getBean(unBindVinCardCheckHandlerName, VinCardCheckMessageHandler.class);
			unBindResultDto = unBindHandler.checkVinCard(requestDTO.getVin(), requestDTO.getUnBindIccidList(), detailType);
		}

		if (bindResultDto != null && unBindResultDto != null) {
			bindResultDto.merge(unBindResultDto);
			return bindResultDto;
		}

		if (null != bindResultDto) {
			return bindResultDto;
		} else if (null != unBindResultDto){
			return unBindResultDto;
		} else {
			throw new CuscUserException(ResponseCode.SYSTEM_ERROR.getCode(),"校验结果为空");
		}
	}

	@Override
	public VinCheckResultDTO checkVin(VinCheckRequestDTO requestDTO) {
		VinCheckResultDTO resultDTO = new VinCheckResultDTO();
		List<VinCheckResultDTO.VinCheckDetailDTO> checkList = requestDTO.getVinList().parallelStream().map(vin -> {
			VinCheckResultDTO.VinCheckDetailDTO detailDTO = new VinCheckResultDTO.VinCheckDetailDTO();
			detailDTO.setVin(vin);
			List<VinIccidDTO> iccidDTOList = null;
			try {
				Response<List<VinIccidDTO>> response = simVehicleRelService.getIccidByVin(vin);
				log.info("checkVin getIccidByVin vin = {},response = {}", vin, JSONObject.toJSONString(response));
				iccidDTOList = response.getData();
			} catch (Exception e) {
				log.error("checkVin getIccidByVin has error vin = {}", vin, e);
			}


			if (CollectionUtils.isEmpty(iccidDTOList)) {
				detailDTO.setErrorMsg("缺乏车卡关系");
			} else {
				List<MgRnrCardInfoDTO> mgRnrCardInfoList = queryCardListByVin(vin);
				mgRnrCardInfoList = filterManufacturerRnr(mgRnrCardInfoList);
				if (mgRnrCardInfoList.stream().anyMatch(
						card -> RnrStatus.INIT.getCode() == card.getRnrStatus() || RnrStatus.RNR.getCode() == card.getRnrStatus())) {
					detailDTO.setErrorMsg("已绑定");
				}
			}
			detailDTO.setCheckResult(StringUtils.isBlank(detailDTO.getErrorMsg()));
			if (!CollectionUtils.isEmpty(iccidDTOList)){
				detailDTO.setIccidList(iccidDTOList.stream().map(VinIccidDTO::getIccid).distinct().collect(Collectors.toList()));
			}
			return detailDTO;
		}).collect(Collectors.toList());

		resultDTO.setCheckList(checkList);
		return resultDTO;
	}

	@Override
	public List<MgRnrCardInfoDTO> filterManufacturerRnr(List<MgRnrCardInfoDTO> cardInfoList) {
		if (CollectionUtils.isEmpty(cardInfoList)) {
			return Collections.emptyList();
		}
		List<String> orderIdList = cardInfoList.stream().filter(card->RnrStatus.RNR.getCode() == card.getRnrStatus())
				.map(MgRnrCardInfoDTO::getOrderId).distinct().collect(Collectors.toList());

		if (CollectionUtils.isEmpty(orderIdList)) {
			//没有实名的卡 返回cardInfoList
			return cardInfoList;
		}

		RnrOrderDTO orderQuery = new RnrOrderDTO();
		orderQuery.setUuidList(orderIdList);
		orderQuery.setOrderType(RnrOrderType.CARMAKER_NEW_VEHICLE.getCode());
		List<RnrOrderDTO> orderList = orderClient.queryByList(orderQuery).getData();

		List<String> manufacturerRnrOrderIdList = orderList.stream().map(RnrOrderDTO::getUuid).collect(Collectors.toList());

		return cardInfoList.stream().filter(card->!manufacturerRnrOrderIdList.contains(card.getOrderId())).collect(Collectors.toList());
	}


}
