package com.mosty.wzzx.remote;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mosty.base.model.dto.base.GetSsbmDto;
import com.mosty.base.model.dto.base.SysDeptDTO;
import com.mosty.base.feign.service.MostyBaseFeignService;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.common.base.domain.ResponseResult;
import com.mosty.common.base.exception.BusinessException;
import com.mosty.common.core.business.entity.SysUser;
import com.mosty.common.core.business.entity.vo.SysUserVO;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 调用部门信息远程适配层
 *
 * @author kevin
 * @date 2022/7/6 10:37 上午
 * @since 1.0.0
 */
@Slf4j
@Service
@AllArgsConstructor
public class TbBaseAdaptRemoteService {

    private final MostyBaseFeignService mostyBaseFeignService;

    // 根据部门编码查询部门信息
    public DeptInfoVo getOrgByDeptId(String deptId) {
        if (StringUtils.isBlank(deptId)) {
            return null;
        }
        ResponseResult<DeptInfoVo> responseResult = mostyBaseFeignService.getOrgByDeptId(deptId);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("调用部门编码查询部门信息异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("调用部门编码查询部门信息异常");
        }
        return responseResult.getData();
    }

    // 根据经纬度获取地址信息
    public JSONObject getAddress(String jd,String wd) {
        if (StringUtils.isBlank(jd) || StringUtils.isBlank(wd)) {
            return null;
        }
        ResponseResult<JSONObject> responseResult = mostyBaseFeignService.getAddress(jd,wd);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("调用部门编码查询部门信息异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("调用部门编码查询部门信息异常");
        }
        return responseResult.getData();
    }

    // 根据经纬度获取地址信息
    public JSONObject getLzAddress(String jd,String wd) {
        if (StringUtils.isBlank(jd) || StringUtils.isBlank(wd)) {
            return null;
        }
        ResponseResult<JSONObject> responseResult = mostyBaseFeignService.getLzAddress(jd,wd);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("根据经纬度获取地址信息异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("根据经纬度获取地址信息异常");
        }
        return responseResult.getData();
    }

    // 获取用户部门信息
    public DeptInfoVo getDeptInfoByUserId(String userId) {
        if (StringUtils.isBlank(userId)) {
            return null;
        }
        ResponseResult<DeptInfoVo> responseResult = mostyBaseFeignService.getDeptInfoByUserId(userId);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("获取用户部门信息异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("获取用户部门信息异常");
        }
        return responseResult.getData();
    }
    // 获取权限查询条件
    public String getSsbm(String ssbmdm, String isChild) {
        GetSsbmDto dto = new GetSsbmDto(ssbmdm, isChild);
        ResponseResult<String> responseResult = mostyBaseFeignService.getSsbm(dto);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("获取权限查询条件异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("获取权限查询条件异常");
        }
        return responseResult.getData();
    }
    // 获取用户信息
    public SysUser getUserInfoBySfzh(String sfzh) {
        ResponseResult<SysUser> responseResult = mostyBaseFeignService.getUserInfoBySfzh(sfzh);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("获取用户信息失败， responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("获取用户信息失败");
        }
        return responseResult.getData();
    }
}
