package com.kmall.api.service.quotalInquiry;

import com.google.common.collect.ImmutableBiMap;
import com.kmall.api.dao.*;
import com.kmall.api.entity.*;
import com.kmall.common.constant.Dict;
import com.kmall.common.utils.*;
import com.kmall.manager.dto.CrossBoundaryQuotalDto;
import com.kmall.manager.dto.CrossQuotalInquiryResponseDto;
import com.kmall.manager.manager.quotalInquiry.CrossBoundaryQuotalInquiryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * @author huangyq
 * @version 1.0
 * 2019-12-17 15:52
 */
@Service
public class ApiQuotalInquiryService {
    @Autowired
    private ApiThirdMerchantBizMapper apiThirdMerchantBizMapper;
    @Autowired
    private ApiMerchMapper apiMerchMapper;
    @Autowired
    private ApiUserMapper apiUserMapper;

    /**
     * 跨境额度查询
     * @param crossBoundaryQuotalDto
     * @param storeId
     * @param userId
     * @return
     */
    @Transactional
    public CrossQuotalInquiryResponseDto getCrossBoundaryQuotaQuery(CrossBoundaryQuotalDto crossBoundaryQuotalDto, Long storeId, Long userId) {
        Map<String, Object> valideDate = MapBeanUtil.fromObject(crossBoundaryQuotalDto);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("merchId", "商户编号");
        builder.put("idCard", "身份证号");
        builder.put("name", "姓名");
        R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
        if (Integer.valueOf(r.get("code").toString()) != 0) {
            throw new RRException(r.get("msg").toString());
        }
        /*UserVo userVo = apiUserMapper.queryObjectByIdNoAndName(crossBoundaryQuotalDto.getIdCard(),crossBoundaryQuotalDto.getName());
        if(userVo == null){
            throw new RRException("身份证信息未绑定");
        }else{
            if(userVo.getId() != userId){
                throw new RRException("只能查询当前微信用户绑定的身份证信息");
            }
        }*/
        String merchSn = crossBoundaryQuotalDto.getMerchId();
        MerchVo merchVo = apiMerchMapper.queryObjectByMerchSn(merchSn);
        if(merchVo == null){
            throw new RRException("商户不存在");
        }
        ThirdMerchantBizVo thirdMerchantBizVo = apiThirdMerchantBizMapper.queryDataByStoreId(storeId);
        if(thirdMerchantBizVo == null){
            throw new RRException("该门店的第三方商户不存在");
        }
        crossBoundaryQuotalDto.setMerchName(merchVo.getMerchName());
        crossBoundaryQuotalDto.setThirdPartyMerchCode(thirdMerchantBizVo.getThirdPartyMerchCode());
        crossBoundaryQuotalDto.setThirdPartyMerchName(thirdMerchantBizVo.getThirdPartyMerchName());
        crossBoundaryQuotalDto.setIsCheckIdCard("0");//是否身份证认证：0：否，1：是

        ResponseData responseData = CrossBoundaryQuotalInquiryUtil.getCrossBoundaryQuotaQuery(crossBoundaryQuotalDto);
        if(!responseData.getCode().equalsIgnoreCase("0")){
            throw new RRException(responseData.getMsg());
        }else{
            //跨境额度查询成功，保存返回数据
            Map quotalInquiryResponseDto = (Map)responseData.getData().getRows().get(0);
            CrossQuotalInquiryResponseDto crossQuotalInquiryResponseDto = new CrossQuotalInquiryResponseDto();
            String serviceTime = (String)quotalInquiryResponseDto.get("serviceTime");
            Map resultMap = (Map)quotalInquiryResponseDto.get("result");
            if(resultMap != null){
                Double totalAmount = (Double)resultMap.get("totalAmount");
                Double innerbalance = (Double)resultMap.get("innerbalance");
                crossQuotalInquiryResponseDto.setInnerbalance(innerbalance);
                crossQuotalInquiryResponseDto.setTotalAmount(totalAmount);
            }
            crossQuotalInquiryResponseDto.setServiceTime(serviceTime);

            return crossQuotalInquiryResponseDto;
        }
    }
}
