package com.cusc.nirvana.user.rnr.fp.dto;

import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author stayAnd
 * @date 2022/6/1
 * 车卡关系校验
 */

@Data
public class VinCardCheckMessageResultDTO {

	/**
	 * 检查结果
	 */
	private Boolean checkResult;

	private List<CardErrorMessageDetailDTO> errorMessageList;



	@Data
	public static class CardErrorMessageDetailDTO{

		/**
		 * iccid
		 */
		private String iccid;

		/**
		 * 检查错误信息
		 */
		private List<String> errorMsg;
	}

	public void build(List<VinCardCheckMessageFailDTO> failDTOList) {
		if (CollectionUtils.isEmpty(failDTOList)) {
			this.setCheckResult(true);
			this.setErrorMessageList(Collections.emptyList());
		} else {
			this.setCheckResult(false);
			Map<String, List<VinCardCheckMessageFailDTO>> iccidMap = failDTOList.stream().collect(Collectors.groupingBy(VinCardCheckMessageFailDTO::getIccid));
			List<VinCardCheckMessageResultDTO.CardErrorMessageDetailDTO> detailDTOList = iccidMap.entrySet().stream().map(entry -> {
				VinCardCheckMessageResultDTO.CardErrorMessageDetailDTO detailDTO = new VinCardCheckMessageResultDTO.CardErrorMessageDetailDTO();
				detailDTO.setIccid(entry.getKey());
				List<String> errorMsgList = entry.getValue().stream().map(VinCardCheckMessageFailDTO::getErrorMsg).collect(Collectors.toList());
				detailDTO.setErrorMsg(errorMsgList);
				return detailDTO;
			}).collect(Collectors.toList());
			this.setErrorMessageList(detailDTOList);
		}
	}

	public void merge(VinCardCheckMessageResultDTO merge) {
		this.setCheckResult(this.getCheckResult() &&  merge.getCheckResult());
		this.getErrorMessageList().addAll(merge.getErrorMessageList());
	}

}
