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.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.springframework.stereotype.Service;

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

/**
 * 数据宝二要素校验
 */
@Service
@Slf4j
public class ShujubaoMobileNameService implements BizHandler<ShujubaoMobileNameParam,Boolean>{
    @Override
    public ServiceTypeVO getType() {
        return new ServiceTypeVO("shujubaoMobileName","数据宝手机号姓名二要素");
    }

    @Override
    public Boolean handle(ShujubaoMobileNameParam 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("name", shujubaoMobileNameParam.getName());
        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") && "2".equals(resultObj.getJSONObject("data").getString("state"))){
                return false;
            }
            logDto.setErrorMessage(resultObj.getString("message"));
        } catch (Exception e){
            log.info("phone name check error data:{}  exception:{}", shujubaoMobileNameParam, e.getMessage());
        }
        return  result;
    }

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

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