package com.hr.externelSystem;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hr.dto.ApiLogDto;
import com.hr.dto.ShujubaoMobileNameDto;
import com.hr.param.ShujubaoMobileOwnerParam;
import com.hr.param.ShujubaoTEVParam;
import com.hr.repository.domain.IpInterfacePO;
import com.hr.repository.domain.IpOrderPO;
import com.hr.repository.domain.IpTokenInterfacePO;
import com.hr.vo.CallbackResponseVO;
import com.hr.vo.ServiceTypeVO;
import com.hr.vo.ShujubaoMobileOwnerVO;
import com.yy.basedevelop.common.util.HttpClientUtils;
import com.yy.basedevelop.common.util.tiangua.aliyunoss.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 数据宝手机号归属地服务
 */
@Service
@Slf4j
public class ShujubaoThreeElementVerificationService implements BizHandler<ShujubaoTEVParam,Boolean>{


    @Override
    public ServiceTypeVO getType() {
        return new ServiceTypeVO("shujubaoTEV","数据宝三要素校验");
    }

    @Override
    public Boolean handle(ShujubaoTEVParam shujubaoTEVParam, IpInterfacePO ipInterfacePO, ApiLogDto logDto) {

        String interfaceConfig = ipInterfacePO.getInterfaceConfig();
        ShujubaoMobileNameDto paramDto = JSON.parseObject(interfaceConfig, ShujubaoMobileNameDto.class);


        try {
            Map<String, String> requestMap = new HashMap<>();
            requestMap.put("key", paramDto.getKey());
            requestMap.put("name", shujubaoTEVParam.getName());
            requestMap.put("idcard", shujubaoTEVParam.getIdCardNo());
            requestMap.put("mobile", shujubaoTEVParam.getMobile());
            logDto.setUrl(paramDto.getUrl());
            logDto.setRequestParam(JSON.toJSONString(requestMap));
            // 调用数据宝接口验证用户三要素是否一致
            String response = HttpClientUtils.post(paramDto.getUrl(), requestMap);
            log.info("调用【数据宝】三要素校验接口的结果为: {}", response);

            JSONObject respJson = JSONObject.parseObject(response);
            logDto.setResponseParam(response);
            logDto.setHttpStatus(respJson.getString("code"));
            if(null != respJson && Objects.equals(respJson.getString("code"), "10000")){
                JSONObject dataJson = JSONObject.parseObject(respJson.getString("data"));
                if(null != dataJson && dataJson.getInteger("state") != 3){
                    switch (dataJson.getInteger("state")){
                        case 1:
                           return true;
                        case 2:
                            return false;
                        default:
                            return false;
                    }
                }
            }
            logDto.setErrorMessage(respJson.getString("message"));
        } catch (Exception e) {
            log.info("三要素校验接口调用失败, 失败原因: {},", e.getMessage(), e);
        }
        return false;
    }

    @Override
    public Class<ShujubaoTEVParam> getRequestClass() {
        return ShujubaoTEVParam.class;
    }

    @Override
    public CallbackResponseVO doCallBack(IpOrderPO ipOrderPO, JSONObject callbackParam) {
        return null;
    }
}
