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.ShujubaoMobileNameParam;
import com.hr.param.ShujubaoMobileOwnerParam;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

    private static final List<String> MOBILE_TOP_THREE_OPERATOR = Arrays.asList("移动","联通","电信");
    @Override
    public ServiceTypeVO getType() {
        return new ServiceTypeVO("shujubaoMobileOwner","数据宝手机号归属地");
    }

    @Override
    public ShujubaoMobileOwnerVO handle(ShujubaoMobileOwnerParam shujubaoMobileNameParam, IpInterfacePO ipInterfacePO, ApiLogDto logDto) {

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

        // 封装调用参数
        Map<String, String> requestMap = new HashMap<>();
        requestMap.put("key", paramDto.getKey());
        requestMap.put("mobile", shujubaoMobileNameParam.getMobile());

        try {
            logDto.setUrl(paramDto.getUrl());
            logDto.setRequestParam(JSON.toJSONString(requestMap));
            // 第三方接口调用
            String checkResult = HttpClientUtils.post(paramDto.getUrl(), requestMap);
            log.info("第三方接口解析手机号归属地返回信息: {}", checkResult);

            // 解析数据
            JSONObject resultObj = JSON.parseObject(checkResult);
            logDto.setResponseParam(checkResult);
            logDto.setHttpStatus(resultObj.getString("code"));
            if (null != resultObj && "10000".equals(resultObj.getString("code")) && null != resultObj.getJSONObject("data")){
                JSONObject dataJson = resultObj.getJSONObject("data");
                ShujubaoMobileOwnerVO result = JSON.toJavaObject(dataJson, ShujubaoMobileOwnerVO.class);
                result.setIsVirtual(false);


                // 判断当前手机号是否是虚拟号(判断逻辑: 非三大运营商(移动,电信,联通)的统一标记为虚拟号)
                String isp = dataJson.getString("isp");
                if(MOBILE_TOP_THREE_OPERATOR.stream().noneMatch(isp::contains)){
                    result.setIsVirtual(true);
                }

                return result;
            }
            logDto.setErrorMessage(resultObj.getString("message"));
        } catch (Exception e){
            log.info("手机号归属地解析失败, 手机号: {}, exception:{}", shujubaoMobileNameParam.getMobile(), e.getMessage());
            return null;
        }
        return new ShujubaoMobileOwnerVO();
    }

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

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

}
