package com.hr.externelSystem;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http;
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.yy.basedevelop.common.util.HttpClientUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 数据宝空号检测
 */
@Service
@Slf4j
public class ShujubaoMobileStateService implements BizHandler<ShujubaoMobileOwnerParam,Boolean>{
    @Override
    public ServiceTypeVO getType() {
        return new ServiceTypeVO("shujubaoMobileState","数据宝手机号空号检测");
    }

    @Override
    public Boolean 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());
        Boolean result = true;
        try {
            logDto.setUrl(paramDto.getUrl());
            logDto.setRequestParam(JSON.toJSONString(requestMap));
            String checkResult = HttpClientUtils.post(paramDto.getUrl(), requestMap);
            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")){
                String state = resultObj.getJSONObject("data").getString("state");
                if (StringUtils.isNotBlank(state) && ("2".equals(state) || "4".equals(state) || "5".equals(state))) {
                    return false;
                }
            }
            logDto.setErrorMessage(resultObj.getString("message"));
        } catch (Exception e){
            log.info("phone state check error data:{}  exception:{}", shujubaoMobileNameParam.getMobile(), e.getMessage());
        }
        return  result;
    }

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

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