package cn.hanghui.outapi.platform.tenant.service.impl;

import cn.hanghui.outapi.platform.common.entity.rest.RestResponse;
import cn.hanghui.outapi.platform.common.entity.rest.RestResult;
import cn.hanghui.outapi.platform.common.utils.Preconditions;
import cn.hanghui.outapi.platform.tenant.entity.dto.open.AllTypePersonVO;
import cn.hanghui.outapi.platform.tenant.entity.dto.open.PdKeyPersonVO;
import cn.hanghui.outapi.platform.tenant.entity.dto.open.PdPersonBasicVO;
import cn.hanghui.outapi.platform.tenant.entity.dto.open.PdProvincePersonVO;
import cn.hanghui.outapi.platform.tenant.entity.dto.open.UserInfoRequestDto;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shisl
 * @package cn.hanghui.outapi.platform.tenant.service.impl
 * @class DragonServer
 * @date 2024/3/1 上午9:39
 * @description
 */
@Slf4j
@Service
public class DragonServer {

    //巨龙接口查询用户基本信息
    @Value("${dragon.domain}")
    public String dragonDomain;
    @Value("${dragon.queryUserInfoUrl}")
    public String dragonQueryUserInfoUrl;

    public RestResult<PdPersonBasicVO> handleProvinceUser(UserInfoRequestDto reqDto) {
        try {
            reqDto.setType(2);
            String respStr = HttpUtil.post(dragonDomain + dragonQueryUserInfoUrl, JSONObject.toJSONString(reqDto), 60000);
            log.info("--->查询省常口用户基本信息响应参数：{}", respStr);
            RestResult result = JSONObject.parseObject(respStr, RestResult.class);
            if (Preconditions.isBlank(result) || Preconditions.isBlank(result.getData())) {
                return RestResponse.ok(null);
            }
            AllTypePersonVO all = JSONObject.parseObject(result.getData().toString(), AllTypePersonVO.class);
            if (Preconditions.isBlank(all.getBasic())) {
                return RestResponse.ok(null);
            }
            List<PdProvincePersonVO> province = all.getProvince();
            PdProvincePersonVO provinceVO = province.get(0);
            PdPersonBasicVO pdPersonBasicVO = BeanUtil.copyProperties(provinceVO, PdPersonBasicVO.class);
            return RestResponse.ok(pdPersonBasicVO);
        } catch (Exception e) {
            log.error("--->查询省常口用户基本信息异常.", e);
        }
        return RestResponse.ok(null);
    }

    public RestResult<PdPersonBasicVO> handleGongAnUser(UserInfoRequestDto reqDto) {
        reqDto.setType(1);
        try {
            String respStr = HttpUtil.post(dragonDomain + dragonQueryUserInfoUrl, JSONObject.toJSONString(reqDto), 60000);
            log.info("--->查询公安部用户基本信息响应参数：{}", respStr);
            RestResult result = JSONObject.parseObject(respStr, RestResult.class);
            if (Preconditions.isBlank(result) || Preconditions.isBlank(result.getData())) {
                return RestResponse.ok(null);
            }
            AllTypePersonVO all = JSONObject.parseObject(result.getData().toString(), AllTypePersonVO.class);
            if (Preconditions.isBlank(all.getBasic())) {
                return RestResponse.ok(null);
            }
            List<PdPersonBasicVO> basic = all.getBasic();
            PdPersonBasicVO pdPersonBasicVO = basic.get(0);
            return RestResponse.ok(pdPersonBasicVO);
        } catch (Exception e) {
            log.error("--->查询公安部用户基本信息异常.", e);
        }
        return RestResponse.ok(null);
    }

    public RestResult<PdKeyPersonVO> handleBkUser(UserInfoRequestDto reqDto) {
        try {
            reqDto.setType(5);
            String respStr = HttpUtil.post(dragonDomain + dragonQueryUserInfoUrl, JSONObject.toJSONString(reqDto), 60000);
            log.info("--->查询用户布控信息响应参数：{}", respStr);
            RestResult result = JSONObject.parseObject(respStr, RestResult.class);
            if (Preconditions.isBlank(result) || Preconditions.isBlank(result.getData())) {
                return RestResponse.ok(null);
            }
            AllTypePersonVO all = JSONObject.parseObject(result.getData().toString(), AllTypePersonVO.class);
            if (Preconditions.isBlank(all.getBasic())) {
                return RestResponse.ok(null);
            }
            List<PdKeyPersonVO> bk = all.getKey();
            PdKeyPersonVO bkVo = bk.get(0);
            return RestResponse.ok(bkVo);
        } catch (Exception e) {
            log.error("--->查询用户布控信息异常.", e);
        }
        return RestResponse.ok(null);
    }




}
