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

import com.alibaba.fastjson.JSON;
import com.cusc.nirvana.common.result.PageResult;
import com.cusc.nirvana.common.result.Response;
import com.cusc.nirvana.user.eiam.client.OrganizationClient;
import com.cusc.nirvana.user.eiam.dto.OrganizationDTO;
import com.cusc.nirvana.user.rnr.fp.service.IFpSearchCardAuthService;
import com.cusc.nirvana.user.rnr.mg.client.*;
import com.cusc.nirvana.user.rnr.mg.constants.CommonYesOrNoEnum;
import com.cusc.nirvana.user.rnr.mg.dto.*;
import com.cusc.nirvana.user.util.crypt.CryptKeyUtil;
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.util.ArrayList;
import java.util.List;

/**
 * @modify hxin 2022-05-18
 *
 */
@Service
@Slf4j
public class FpSearchCardAuthServiceImpl implements IFpSearchCardAuthService {

    @Resource
    private MgRnrCardInfoClient cardInfoClient;

    @Resource
    private MgRnrInfoClient infoClient;

    @Resource
    private MgRnrCompanyInfoClient companyInfoClient;

    @Resource
    private RnrOrderClient orderClient;

    @Resource
    private MgRnrLiaisonInfoClient liaisonInfoClient;
    
    @Resource
    private OrganizationClient organizationClient;

    @Resource
    private MgCheckProgressClient mgCheckProgressClient;

    /**
     * 单车进度查询 重构返回对象
     * @param dto
     * @return
     */
    @Override
    public Response<List<MgSearchCardAuthDTO>> getMgSearchCardAuthDTO(MgRnrCardInfoDTO dto){
        log.info("fp获取单车审核进度入参cardInfo: {}", JSON.toJSONString(dto));
        List<MgRnrCardInfoDTO> cards = getCards(dto);
        log.info("fp获取单车审核进度出参cardInfo: {}", JSON.toJSONString(cards));
        if(CollectionUtils.isEmpty(cards)){
            return Response.createError("该vin查不到任何卡信息");
        }
        List<MgSearchCardAuthDTO> searchCardAuthDTOS = new ArrayList<>();
        for(MgRnrCardInfoDTO cardInfoDTO : cards){
            MgSearchCardAuthDTO mgSearchCardAuthDTO = new MgSearchCardAuthDTO();
            //MgRnrCardInfoDTO cardInfoDTO = cards.get(i);
            mgSearchCardAuthDTO.setCardInfoDTO(cardInfoDTO);
            //查order信息
            RnrOrderDTO orderDTO = new RnrOrderDTO();
            orderDTO.setUuid(cardInfoDTO.getOrderId());
            RnrOrderDTO order = getOrder(orderDTO);
            log.info("fp获取单车审核进度order出参: {}", JSON.toJSONString(order));
            mgSearchCardAuthDTO.setRnrOrderDTO(order);
            //查rnrinfo 实名信息
            MgRnrInfoDTO rnrInfoDTO = new MgRnrInfoDTO();
            rnrInfoDTO.setUuid(cardInfoDTO.getRnrId());
            MgRnrInfoDTO rnrInfo = getRnrInfo(rnrInfoDTO);
            log.info("fp获取单车审核进度rnrinfo出参: {}", JSON.toJSONString(rnrInfo));
            mgSearchCardAuthDTO.setRnrInfoDTO(rnrInfo);
            if(rnrInfo != null){
                if(rnrInfo.getIsCompany().equals(CommonYesOrNoEnum.YES.getCode())){ //若是企业实名查企业信息 魔法值重构？

                    MgRnrCompanyInfoDTO companyInfoDTO = new MgRnrCompanyInfoDTO();
                    companyInfoDTO.setRnrId(rnrInfo.getUuid());
                    MgRnrCompanyInfoDTO company = getCompany(companyInfoDTO);
                    log.info("fp获取单车审核进度companyInfo出参: {}", JSON.toJSONString(company));
                    mgSearchCardAuthDTO.setCompanyInfoDTO(company);
                }
                //和委托实名暂无关系
//                if(rnrInfo.getIsTrust().equals("1")){
//                    MgRnrLiaisonInfoDTO liaisonInfoDTO = new MgRnrLiaisonInfoDTO();
//                    liaisonInfoDTO.setRnrId(rnrInfo.getUuid());
//                    MgRnrLiaisonInfoDTO liaison = getLiaison(liaisonInfoDTO);
//                    mgSearchCardAuthDTO.setLiaisonInfoDTO(liaison);
//                }
                //查询经销商信息
                if(StringUtils.isNotEmpty(rnrInfo.getOrgId())){
                    OrganizationDTO organizationDTO = getOrganizationDTO(rnrInfo.getOrgId());
                    mgSearchCardAuthDTO.setOrgName(organizationDTO.getOrganName());
                }
            }
            searchCardAuthDTOS.add(mgSearchCardAuthDTO);

        }
        return Response.createSuccess(searchCardAuthDTOS);
    }


    @Override
    public Response<PageResult<MgCheckProgressDTO>> getMgCheckProgress(MgCheckProgressDTO param) {

        Response<PageResult<MgCheckProgressDTO>> pageResultResponse = mgCheckProgressClient.queryByPage(param);

        //可能需要调别的服务查询经销商信息、审核人信息
        List<MgCheckProgressDTO> list = pageResultResponse.getData().getList();
        list.forEach( mgCheckProgressDTO -> {

//            if(StringUtils.isNotBlank(mgCheckProgressDTO.getStartCheckDate())){
//                mgCheckProgressDTO.setStartCheckDate(mgCheckProgressDTO.getStartCheckDate().substring(0,10));
//            }
//            if(StringUtils.isNotBlank(mgCheckProgressDTO.getEndCheckDate())){
//                mgCheckProgressDTO.setEndCheckDate(mgCheckProgressDTO.getEndCheckDate().substring(0,10));
//            }

            //敏感信息解密
            mgCheckProgressDTO.setFullName(CryptKeyUtil.decryptByBase64(mgCheckProgressDTO.getFullName()));
            mgCheckProgressDTO.setPhone(CryptKeyUtil.decryptByBase64(mgCheckProgressDTO.getPhone()));
            mgCheckProgressDTO.setCompanyName(CryptKeyUtil.decryptByBase64(mgCheckProgressDTO.getCompanyName()));
            //经销商信息
            if(StringUtils.isNotBlank(mgCheckProgressDTO.getOrgId())){
                OrganizationDTO organizationDTO = getOrganizationDTO(mgCheckProgressDTO.getOrgId());
                mgCheckProgressDTO.setOrgName(organizationDTO.getOrganName());
            }
        });

        return pageResultResponse;
    }


    /**********************************私有方法区********************************************/

    /**
     * 获取cardlist
     */
    public List<MgRnrCardInfoDTO> getCards(MgRnrCardInfoDTO dto){
        Response<List<MgRnrCardInfoDTO>> listResponse = cardInfoClient.queryByList(dto);
        if(!listResponse.isSuccess()){
            return null;
        }
        return listResponse.getData();
    }

    /**
     * 获取rnrinfo
     * @param dto
     * @return
     */
    public MgRnrInfoDTO getRnrInfo(MgRnrInfoDTO dto){
        Response<MgRnrInfoDTO> byUuid = infoClient.getByUuid(dto);
        if(!byUuid.isSuccess()){
            return new MgRnrInfoDTO();
        }
        return byUuid.getData();
    }

    /**
     * 获取rnrinfo
     * @param dto
     * @return
     */
    public RnrOrderDTO getOrder(RnrOrderDTO dto){
        Response<RnrOrderDTO> byUuid = orderClient.getByUuid(dto);
        if(!byUuid.isSuccess()){
            return new RnrOrderDTO();
        }
        return byUuid.getData();
    }

    /**
     * 获取公司信息
     * @param dto
     * @return
     */
    public MgRnrCompanyInfoDTO getCompany(MgRnrCompanyInfoDTO dto){
        Response<MgRnrCompanyInfoDTO> byRnrid = companyInfoClient.getByRnrid(dto);
        if(!byRnrid.isSuccess()){
            return new MgRnrCompanyInfoDTO();
        }
        return byRnrid.getData();
    }

    /**
     * 获取代办人信息
     * @param dto
     * @return
     */
    public MgRnrLiaisonInfoDTO getLiaison(MgRnrLiaisonInfoDTO dto){
        Response<MgRnrLiaisonInfoDTO> byRnrid = liaisonInfoClient.getByRnrid(dto);
        if(!byRnrid.isSuccess()){
            return null;
        }
        return byRnrid.getData();
    }

    /**
     * 获取组织信息
     * @param orgId
     * @return
     */
    private OrganizationDTO getOrganizationDTO(String orgId) {
        OrganizationDTO organizationDTO = new OrganizationDTO();
        organizationDTO.setUuid(orgId);
        Response<OrganizationDTO> byUuid = organizationClient.getByUuid(organizationDTO);
        if (byUuid.isSuccess()){
            return byUuid.getData();
        }else{
            return organizationDTO;
        }

    }

}
