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


import com.alibaba.fastjson.JSONObject;
import com.cusc.nirvana.common.result.PageResult;
import com.cusc.nirvana.user.eiam.client.OrganizationClient;
import com.cusc.nirvana.user.eiam.client.UserClient;
import com.cusc.nirvana.user.eiam.dto.OrganizationDTO;
import com.cusc.nirvana.user.eiam.dto.UserDTO;
import com.cusc.nirvana.user.exception.CuscUserException;
import com.cusc.nirvana.user.rnr.fp.common.util.DateUtil;
import com.cusc.nirvana.user.rnr.fp.dto.RnrOrderDetailDTO;
import com.cusc.nirvana.user.rnr.fp.dto.RnrOrderListQueryDTO;
import com.cusc.nirvana.user.rnr.fp.dto.RnrOrderListResponseDTO;
import com.cusc.nirvana.user.rnr.fp.dto.VinCardDTO;
import com.cusc.nirvana.user.rnr.fp.service.IRnrOrderService;
import com.cusc.nirvana.user.rnr.mg.client.*;
import com.cusc.nirvana.user.rnr.mg.constants.*;
import com.cusc.nirvana.user.rnr.mg.dto.*;
import com.cusc.nirvana.user.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author stayAnd
 * @date 2022/7/28
 */
@Service
@Slf4j
public class RnrOrderServiceImpl implements IRnrOrderService {

	@Resource
	private RnrOrderClient orderClient;

	@Resource
	private MgRnrInfoClient rnrInfoClient;

	@Resource
	private MgRnrCardInfoClient cardInfoClient;

	@Resource
	private MgRnrLiaisonInfoClient liaisonInfoClient;

	@Resource
	private MgRnrFileClient fileClient;

	@Resource
	private MgRnrCompanyInfoClient companyInfoClient;

	@Resource
	private MgRnrAuthenticationResultClient authenticationResultClient;

	@Resource
	private UserClient userClient;

	@Value("${user.eiam.applicationId:4}")
	private String applicationId;

	@Resource
	private OrganizationClient organizationClient;


	/**
	 * 自然人关联文件类型集合
	 */
	private static Set<Integer> NPFileTypeSet = new HashSet<>();
	/**
	 * 企业关联文件类型
	 */
	private static Set<Integer> EntFileTypeSet = new HashSet<>();


	static {
		NPFileTypeSet.add(RnrFileType.IDENTITY_CARD_BACK.getCode());
		NPFileTypeSet.add(RnrFileType.IDENTITY_CARD_FRONT.getCode());
		NPFileTypeSet.add(RnrFileType.POLICE_CARD_FRONT.getCode());
		NPFileTypeSet.add(RnrFileType.POLICE_CARD_BACK.getCode());
		NPFileTypeSet.add(RnrFileType.HK_MACAO_PASSPORT_FRONT.getCode());
		NPFileTypeSet.add(RnrFileType.HK_MACAO_PASSPORT_BACK.getCode());
		NPFileTypeSet.add(RnrFileType.TAIWAN_CARD_FRONT.getCode());
		NPFileTypeSet.add(RnrFileType.TAIWAN_CARD_BACK.getCode());
		NPFileTypeSet.add(RnrFileType.FOREIGN_NATIONAL_PASSPORT_FRONT.getCode());
		NPFileTypeSet.add(RnrFileType.FOREIGN_NATIONAL_PASSPORT_BACK.getCode());
		NPFileTypeSet.add(RnrFileType.HK_MACAO_RESIDENCE_PERMIT_FRONT.getCode());
		NPFileTypeSet.add(RnrFileType.HK_MACAO_RESIDENCE_PERMIT_BACK.getCode());
		NPFileTypeSet.add(RnrFileType.TAIWAN_RESIDENCE_PERMIT_FRONT.getCode());
		NPFileTypeSet.add(RnrFileType.TAIWAN_RESIDENCE_PERMIT_BACK.getCode());
		NPFileTypeSet.add(RnrFileType.RESIDENCE_BOOKLET_FRONT.getCode());
		NPFileTypeSet.add(RnrFileType.RESIDENCE_BOOKLET_BACK.getCode());
		NPFileTypeSet.add(RnrFileType.OFFICIAL_CARD_FRONT.getCode());
		NPFileTypeSet.add(RnrFileType.OFFICIAL_CARD_BACK.getCode());
		NPFileTypeSet.add(RnrFileType.OTHER.getCode());


		EntFileTypeSet.add(RnrFileType.ENTERPRISE_PIC.getCode());

	}





	@Override
	public PageResult<RnrOrderListResponseDTO> pageListQuery(RnrOrderListQueryDTO dto) {
		PageResult<RnrOrderListResponseDTO> result = new PageResult<>();

		PageResult<RnrOrderDTO> pageInfo = queryOrderPageOnCondition(dto,null,null);
		if (pageInfo.getTotalCount() == 0) {
			 return new PageResult<>(Collections.emptyList(),0,dto.getCurrPage(),dto.getPageSize());
		}

		List<RnrOrderDTO> orderList = pageInfo.getList();

		result.setTotalCount(pageInfo.getTotalCount());
		List<RnrOrderListResponseDTO> listResponseDTOList = buildListResponseDTO(orderList);
		result.setList(listResponseDTOList);
		return result;
	}


	private PageResult<RnrOrderDTO> queryOrderPageOnCondition(RnrOrderListQueryDTO dto,List<Integer> orderStatusList,List<Integer> orderTypeList){
		List<String> orderIdList = null;
		if (StringUtils.isNotBlank(dto.getVin()) || StringUtils.isNotBlank(dto.getIccid())) {
			MgRnrCardInfoDTO cardQuery = new MgRnrCardInfoDTO();
			cardQuery.setIotId(dto.getVin());
			cardQuery.setIccid(dto.getIccid());
			List<MgRnrCardInfoDTO> cardList = cardInfoClient.queryByList(cardQuery).getData();
			if (CollectionUtils.isEmpty(cardList)) {
				return new PageResult<>(Collections.emptyList(),0,dto.getCurrPage(),dto.getPageSize());
			}
			orderIdList = cardList.stream().map(MgRnrCardInfoDTO::getOrderId).distinct().collect(Collectors.toList());
		}

		List<String> rnrIdList = new ArrayList<>();

		if (StringUtils.isNotBlank(dto.getCompanyName())) {
			MgRnrCompanyInfoDTO companyQuery = new MgRnrCompanyInfoDTO();
			companyQuery.setCompanyName(dto.getCompanyName());
			List<MgRnrCompanyInfoDTO> companyList = companyInfoClient.queryByList(companyQuery).getData();
			if (CollectionUtils.isEmpty(companyList)) {
				return new PageResult<>(Collections.emptyList(),0,dto.getCurrPage(),dto.getPageSize());
			}
			List<String> companyRnrIdList = companyList.stream().map(MgRnrCompanyInfoDTO::getRnrId).distinct().collect(Collectors.toList());
			rnrIdList.addAll(companyRnrIdList);
		}

		if (StringUtils.isNotBlank(dto.getFullName()) || StringUtils.isNotBlank(dto.getPhone()) || StringUtils.isNotBlank(dto.getCertNumber())){
			MgRnrInfoDTO infoQuery = new MgRnrInfoDTO();
			infoQuery.setFullName(dto.getFullName());
			infoQuery.setPhone(dto.getPhone());
			infoQuery.setCertNumber(dto.getCertNumber());
			List<MgRnrInfoDTO> infoList = rnrInfoClient.queryByList(infoQuery).getData();
			if (CollectionUtils.isEmpty(infoList)) {
				return new PageResult<>(Collections.emptyList(),0,dto.getCurrPage(),dto.getPageSize());
			}
			List<String> infoRnrIdList = infoList.stream().map(MgRnrInfoDTO::getUuid).distinct().collect(Collectors.toList());
			if (CollectionUtils.isEmpty(rnrIdList)) {
				rnrIdList.addAll(infoRnrIdList);
			} else {
				rnrIdList.retainAll(infoRnrIdList);
			}
		}

		List<String> orgIdList = null;
		if (StringUtils.isNotBlank(dto.getOrganizationName())) {
			OrganizationDTO orgQury = new OrganizationDTO();
			orgQury.setOrganName(dto.getOrganizationName());
			orgIdList = organizationClient.queryByList(orgQury).getData().stream().map(OrganizationDTO::getUuid).collect(Collectors.toList());
		}

		RnrOrderDTO orderQuery = new RnrOrderDTO();
		BeanUtils.copyProperties(dto,orderQuery);
		if (!CollectionUtils.isEmpty(orderIdList)) {
			orderQuery.setUuidList(orderIdList);
		}

		if (!CollectionUtils.isEmpty(rnrIdList)) {
			orderQuery.setRnrIdList(rnrIdList);
		}

		if (!CollectionUtils.isEmpty(orgIdList)) {
			orderQuery.setOrgIdList(orgIdList);
		}

		if (!CollectionUtils.isEmpty(orderStatusList)) {
			orderQuery.setOrderStatusList(orderStatusList);
		}

		if (!CollectionUtils.isEmpty(orderTypeList)){
			orderQuery.setOrderTypeList(orderTypeList);
		}

		return orderClient.queryByPage(orderQuery).getData();
	}


	private List<RnrOrderListResponseDTO> buildListResponseDTO(List<RnrOrderDTO> orderList) {
		if (CollectionUtils.isEmpty(orderList)) {
			return Collections.emptyList();
		}

		List<String> orderIdList = orderList.stream().map(RnrOrderDTO::getUuid).collect(Collectors.toList());
		List<String> rnrIdList = orderList.stream().map(RnrOrderDTO::getRnrId).distinct().collect(Collectors.toList());

		//查询cardInfo
		MgRnrCardInfoDTO cardQuery = new MgRnrCardInfoDTO();
		cardQuery.setOrderIdList(orderIdList);
		List<MgRnrCardInfoDTO> cardList = cardInfoClient.queryByList(cardQuery).getData();
		Map<String, MgRnrCardInfoDTO> cardMap = cardList.stream().collect(Collectors.toMap(MgRnrCardInfoDTO::getOrderId, Function.identity(), (o1, o2) -> o2));


		//查询rnrInfo
		MgRnrInfoDTO infoQuery = new MgRnrInfoDTO();
		infoQuery.setUuidList(rnrIdList);
		List<MgRnrInfoDTO> infoList = rnrInfoClient.queryByList(infoQuery).getData();
		Map<String, MgRnrInfoDTO> infoMap = infoList.stream().collect(Collectors.toMap(MgRnrInfoDTO::getUuid, Function.identity(), (o1, o2) -> o2));

		//查询公司信息
		MgRnrCompanyInfoDTO companyQuery = new MgRnrCompanyInfoDTO();
		companyQuery.setRnrIdList(rnrIdList);
		List<MgRnrCompanyInfoDTO> companyList = companyInfoClient.queryByList(companyQuery).getData();
		Map<String, MgRnrCompanyInfoDTO> companyMap = companyList.stream().collect(Collectors.toMap(MgRnrCompanyInfoDTO::getRnrId, Function.identity(), (o1, o2) -> o2));


		return orderList.parallelStream().map(order->{
			RnrOrderListResponseDTO responseDTO = new RnrOrderListResponseDTO();
			BeanUtils.copyProperties(order,responseDTO);
			responseDTO.setOrderId(order.getUuid());

			Optional.ofNullable(cardMap.get(order.getUuid())).ifPresent(cardInfoDTO->{
				responseDTO.setVin(cardInfoDTO.getIotId());
				responseDTO.setIccid(cardInfoDTO.getIccid());
			});

			Optional.ofNullable(companyMap.get(order.getRnrId()))
					.ifPresent(companyInfoDTO -> responseDTO.setCompanyName(companyInfoDTO.getCompanyName()));

			Optional.ofNullable(infoMap.get(order.getRnrId())).ifPresent(infoDTO -> {
				responseDTO.setFullName(infoDTO.getFullName());
				responseDTO.setCertNumber(infoDTO.getCertNumber());
				responseDTO.setPhone(infoDTO.getPhone());
			});

			if (StringUtils.isNotBlank(order.getReviewUserId())) {
				responseDTO.setOperatorName(getAuditorFormWorkOrderSystem(order.getReviewUserId(),order.getTenantNo()));
			}

			return responseDTO;
		}).collect(Collectors.toList());
	}

	private String getAuditorFormWorkOrderSystem(String reviewUserId,String tenantNo) {
		UserDTO userQuery = new UserDTO();
		userQuery.setUuid(reviewUserId);
		userQuery.setTenantNo(tenantNo);
		userQuery.setApplicationId(applicationId);
		UserDTO data = userClient.getByUuid(userQuery).getData();
		return null != data ? data.getFullName() : "";
	}


	@Override
	public PageResult<VinCardDTO> queryCardPageByOrderId(MgRnrCardInfoDTO dto) {

		MgRnrCardInfoDTO cardQuery = new MgRnrCardInfoDTO();
		cardQuery.setCurrPage(dto.getCurrPage());
		cardQuery.setPageSize(dto.getPageSize());
		cardQuery.setOrderId(dto.getOrderId());
		PageResult<MgRnrCardInfoDTO> pageInfo = cardInfoClient.queryByPage(cardQuery).getData();

		PageResult<VinCardDTO> result = new PageResult<>();
		result.setTotalCount(pageInfo.getTotalCount());
		result.setList(pageInfo.getList().stream().map(cardInfoDto->{
			VinCardDTO vinCardDTO = new VinCardDTO();
			vinCardDTO.setVin(cardInfoDto.getIotId());
			vinCardDTO.setIccid(cardInfoDto.getIccid());
			return vinCardDTO;
		}).collect(Collectors.toList()));
		return result;
	}

	@Override
	public RnrOrderDetailDTO queryOrderDetail(String orderId) {

		RnrOrderDTO orderQuery = new RnrOrderDTO();
		orderQuery.setUuid(orderId);
		RnrOrderDTO order = orderClient.getByUuid(orderQuery).getData();
		if (null == order) {
			throw new CuscUserException("","未查询到订单信息");
		}

		MgRnrInfoDTO infoQuery = new MgRnrInfoDTO();
		infoQuery.setUuid(order.getRnrId());
		MgRnrInfoDTO rnrInfo = rnrInfoClient.getByUuid(infoQuery).getData();
		if (null == rnrInfo) {
			throw new CuscUserException("","未查询到实名信息");
		}

		MgRnrLiaisonInfoDTO liaisonQuery = new MgRnrLiaisonInfoDTO();
		liaisonQuery.setRnrId(order.getRnrId());
		MgRnrLiaisonInfoDTO liaisonInfo = liaisonInfoClient.getByRnrid(liaisonQuery).getData();

		MgRnrCompanyInfoDTO companyQuery = new MgRnrCompanyInfoDTO();
		companyQuery.setRnrId(order.getRnrId());
		MgRnrCompanyInfoDTO companyInfo = companyInfoClient.getByRnrid(companyQuery).getData();


		MgRnrAuthenticationResultDTO authQuery = new MgRnrAuthenticationResultDTO();
		authQuery.setOrderId(orderId);
		List<MgRnrAuthenticationResultDTO> authenticationList = authenticationResultClient.queryByList(authQuery).getData();


		MgRnrFileDTO fileQuery = new MgRnrFileDTO();
		fileQuery.setRnrId(rnrInfo.getUuid());
		fileQuery.setTenantNo(order.getTenantNo());
		List<MgRnrFileDTO> fileList = fileClient.getByRnrid(fileQuery).getData();

		return buildRnrOrderDetailDTO(order,rnrInfo,liaisonInfo,companyInfo,authenticationList,fileList);
	}

	@Override
	public PageResult<LocalVerifyListDTO> localverifyList(RnrOrderListQueryDTO dto) {
		log.info("localverifyList start ,dto = {}",JSONObject.toJSONString(dto));
		PageResult<LocalVerifyListDTO> result = new PageResult<>();
		List<Integer> orderStatusList = Arrays.asList(RnrOrderStatusEnum.ASSIGNMENT.getCode(), RnrOrderStatusEnum.ASSIGNMENT.getCode());
		List<Integer> orderTypeList = Arrays.asList(RnrOrderType.NEW_VEHICLE.getCode(), RnrOrderType.SEC_VEHICLE.getCode(), RnrOrderType.COMPANY_NEW_VEHICLE.getCode(), RnrOrderType.CARMAKER_NEW_VEHICLE.getCode(),
				RnrOrderType.COMPANY_CORPORATION_CHANGE.getCode(), RnrOrderType.SEC_UNBIND.getCode());
		PageResult<RnrOrderDTO> pageInfo = queryOrderPageOnCondition(dto,orderStatusList,orderTypeList);

		if (pageInfo.getTotalCount() == 0) {
			return new PageResult<>(Collections.emptyList(),0,dto.getCurrPage(),dto.getPageSize());
		}
		List<RnrOrderDTO> orderList = pageInfo.getList();

		result.setTotalCount(pageInfo.getTotalCount());
		List<LocalVerifyListDTO> list = buildVerifyListDTO(orderList);
		result.setList(list);
		return result;
	}

	/**
	 * 构建待审核列表
	 * @param orderList
	 * @return
	 */
	private List<LocalVerifyListDTO> buildVerifyListDTO(List<RnrOrderDTO> orderList) {
		if (CollectionUtils.isEmpty(orderList)) {
			return Collections.emptyList();
		}

		List<String> orderIdList = orderList.stream().map(RnrOrderDTO::getUuid).collect(Collectors.toList());
		List<String> rnrIdList = orderList.stream().map(RnrOrderDTO::getRnrId).distinct().collect(Collectors.toList());
		List<String> orgIdList = orderList.stream().map(RnrOrderDTO::getOrgId).distinct().collect(Collectors.toList());

		//查询cardInfo
		MgRnrCardInfoDTO cardQuery = new MgRnrCardInfoDTO();
		cardQuery.setOrderIdList(orderIdList);
		List<MgRnrCardInfoDTO> cardList = cardInfoClient.queryByList(cardQuery).getData();
		Map<String, List<MgRnrCardInfoDTO>> cardMap = cardList.stream().collect(Collectors.groupingBy(MgRnrCardInfoDTO::getOrderId));

		//查询rnrInfo
		MgRnrInfoDTO infoQuery = new MgRnrInfoDTO();
		infoQuery.setUuidList(rnrIdList);
		List<MgRnrInfoDTO> infoList = rnrInfoClient.queryByList(infoQuery).getData();
		Map<String, MgRnrInfoDTO> infoMap = infoList.stream().collect(Collectors.toMap(MgRnrInfoDTO::getUuid, Function.identity(), (o1, o2) -> o2));

		//查询公司信息
		MgRnrCompanyInfoDTO companyQuery = new MgRnrCompanyInfoDTO();
		companyQuery.setRnrIdList(rnrIdList);
		List<MgRnrCompanyInfoDTO> companyList = companyInfoClient.queryByList(companyQuery).getData();
		Map<String, MgRnrCompanyInfoDTO> companyMap = companyList.stream().collect(Collectors.toMap(MgRnrCompanyInfoDTO::getRnrId, Function.identity(), (o1, o2) -> o2));

		//组织信息
		Map<String,String> orgNameMap;
		if (!CollectionUtils.isEmpty(orderIdList)) {
			OrganizationDTO orgQuery = new OrganizationDTO();
			orgQuery.setUuidList(orgIdList);
			orgNameMap = organizationClient.queryByList(orgQuery).getData().stream().collect(Collectors.toMap(OrganizationDTO::getUuid,OrganizationDTO::getOrganName,(o1,o2)->o2));
		} else {
			orgNameMap = Collections.EMPTY_MAP;
		}



		return orderList.stream().map(order->{
			LocalVerifyListDTO verifyDto = new LocalVerifyListDTO();
			verifyDto.setRnrOrderInfoId(order.getUuid());
			List<MgRnrCardInfoDTO> cardInfoDTOList = cardMap.get(order.getUuid());
			if (!CollectionUtils.isEmpty(cardInfoDTOList)){
				List<String> iccidList = cardInfoDTOList.stream().map(MgRnrCardInfoDTO::getIccid).distinct().collect(Collectors.toList());
				List<String> vinList = cardInfoDTOList.stream().map(MgRnrCardInfoDTO::getIotId).distinct().collect(Collectors.toList());

				String iccidStr = StringUtils.join(iccidList, ",");
				verifyDto.setIccid(iccidStr.length() < 100 ? iccidStr : iccidStr.substring(0, 100));

				String vinStr = StringUtils.join(vinList, ",");
				verifyDto.setVin(vinStr.length() < 100 ? vinStr : vinStr.substring(0, 100));
			}
			verifyDto.setOrderStatus(order.getOrderStatus());
			verifyDto.setOrderType(String.valueOf(order.getOrderType()));
			verifyDto.setOrderTypeName(RnrOrderType.getTypeByCode(order.getOrderType()).getComment());
			verifyDto.setOrgName(orgNameMap.get(order.getOrgId()));
			verifyDto.setRnrDate(DateUtil.formate(order.getCreateTime(),DateUtil.FORMAT_YYYYMMDDHHMMSS));

			Optional.ofNullable(companyMap.get(order.getRnrId()))
					.ifPresent(companyInfoDTO -> verifyDto.setVinCompanyName(companyInfoDTO.getCompanyName()));

			Optional.ofNullable(infoMap.get(order.getRnrId())).ifPresent(infoDTO -> {
				verifyDto.setVinOwnerName(infoDTO.getFullName());
				verifyDto.setVinOwnerPhone(infoDTO.getPhone());
			});

			return verifyDto;
		}).collect(Collectors.toList());

	}

	private RnrOrderDetailDTO buildRnrOrderDetailDTO(RnrOrderDTO order, MgRnrInfoDTO rnrInfo,
													 MgRnrLiaisonInfoDTO liaisonInfo, MgRnrCompanyInfoDTO companyInfo,
													 List<MgRnrAuthenticationResultDTO> authenticationList,List<MgRnrFileDTO> fileList) {
		RnrOrderDetailDTO result = new RnrOrderDetailDTO();

		RnrOrderDetailDTO.BizDetailDTO bizDetailDTO = new RnrOrderDetailDTO.BizDetailDTO();
		BeanUtils.copyProperties(order,bizDetailDTO);
		result.setBizDetail(bizDetailDTO);

		RnrOrderDetailDTO.PersonDetailDTO rnrDetail = new RnrOrderDetailDTO.PersonDetailDTO();
		BeanUtils.copyProperties(rnrInfo,rnrDetail);
		Optional.ofNullable(CertTypeEnum.getEnumByCode(rnrDetail.getCertType())).ifPresent(certTypeEnum -> rnrDetail.setCertTypeName(certTypeEnum.getName()));
		result.setRnrDetail(rnrDetail);

		if (null != liaisonInfo) {
			RnrOrderDetailDTO.PersonDetailDTO liaisonDetail = new RnrOrderDetailDTO.PersonDetailDTO();
			liaisonDetail.setFullName(liaisonInfo.getLiaisonName());
			liaisonDetail.setGender(liaisonInfo.getLiaisonGender());
			liaisonDetail.setCertType(liaisonInfo.getLiaisonCertType());
			liaisonDetail.setCertNumber(liaisonInfo.getLiaisonCertNumber());
			liaisonDetail.setPhone(liaisonInfo.getLiaisonPhone());
			liaisonDetail.setExpiredDate(liaisonInfo.getLiaisonExpiredDate());
			Optional.ofNullable(CertTypeEnum.getEnumByCode(liaisonDetail.getCertType())).ifPresent(certTypeEnum -> liaisonDetail.setCertTypeName(certTypeEnum.getName()));
			result.setLiaisonDetail(liaisonDetail);
		}

		if (null != companyInfo) {
			RnrOrderDetailDTO.CompanyDetailDTO companyDetail = new RnrOrderDetailDTO.CompanyDetailDTO();
			BeanUtils.copyProperties(companyInfo,companyDetail);
			Optional.ofNullable(IndustryTypeEnum.getEnumByCode(companyDetail.getIndustryType())).ifPresent(industryTypeEnum -> companyDetail.setIndustryTypeName(industryTypeEnum.getValue()));
			Optional.ofNullable(CompanyTypeEnum.getEnumByCode(companyDetail.getCompanyType())).ifPresent(companyTypeEnum -> companyDetail.setCompanyTypeName(companyTypeEnum.getValue()));
			Optional.ofNullable(CertTypeEnum.getEnumByCode(companyDetail.getCompanyCertType())).ifPresent(certTypeEnum -> companyDetail.setCompanyCertTypeName(certTypeEnum.getName()));
			result.setCompanyDetail(companyDetail);
		}

		if (!CollectionUtils.isEmpty(authenticationList)) {
			List<RnrOrderDetailDTO.AuthenticationDetailDTO> authDetailList = authenticationList.stream().map(auth -> {
				RnrOrderDetailDTO.AuthenticationDetailDTO authDetailDTO = new RnrOrderDetailDTO.AuthenticationDetailDTO();
				BeanUtils.copyProperties(auth, authDetailDTO);
				return authDetailDTO;
			}).collect(Collectors.toList());
			result.setAuthenticationList(authDetailList);
		}

		RnrOrderDetailDTO.AuditDetailDTO auditDetailDTO = new RnrOrderDetailDTO.AuditDetailDTO();
		BeanUtils.copyProperties(order,auditDetailDTO);

		if (StringUtils.isNotBlank(order.getComment())) {
			auditDetailDTO.setContent(order.getComment());
		} else if (StringUtils.isNotBlank(order.getVerifyComments())) {
			auditDetailDTO.setContent(order.getVerifyComments());
		}

		if (StringUtils.isNotBlank(order.getReviewUserId())) {
			auditDetailDTO.setOperatorName(getAuditorFormWorkOrderSystem(order.getReviewUserId(),order.getTenantNo()));
		}
		result.setAuditDetail(auditDetailDTO);

		List<RnrOrderDetailDTO.FileDTO> rnrFileList = new ArrayList<>();

		List<RnrOrderDetailDTO.FileDTO> liaisonRnrFileList = new ArrayList<>();

		List<RnrOrderDetailDTO.FileDTO> companyFileList = new ArrayList<>();

		List<RnrOrderDetailDTO.FileDTO> ortherFileList = new ArrayList<>();

		if (!CollectionUtils.isEmpty(fileList)) {
			for (MgRnrFileDTO file : fileList) {

				RnrOrderDetailDTO.FileDTO fileDTO = new RnrOrderDetailDTO.FileDTO();
				fileDTO.setFileType(file.getFileType());
				fileDTO.setFileId(file.getFileSystemId());

				if (NPFileTypeSet.contains(file.getFileType())) {
					if ("0".equals(file.getLiaisonId())) {
						rnrFileList.add(fileDTO);
					} else {
						liaisonRnrFileList.add(fileDTO);
					}
				} else if (EntFileTypeSet.contains(file.getFileType())) {
					companyFileList.add(fileDTO);
				} else {
					ortherFileList.add(fileDTO);
				}

			}
		}
		rnrDetail.setRnrInfoFile(rnrFileList);

		if (null != result.getCompanyDetail()) {
			result.getCompanyDetail().setCompanyFile(companyFileList);
		}

		if (null != result.getLiaisonDetail()) {
			result.getLiaisonDetail().setRnrInfoFile(liaisonRnrFileList);
		}

		result.setFileList(ortherFileList);

		return result;
	}



}
