package com.mosty.yjzl.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.query.base.SysDeptQuery;
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.config.entity.vo.SysDictVO;
import com.mosty.common.core.business.entity.SysDept;
import com.mosty.common.core.business.entity.SysUser;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

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

    private final MostyBaseFeignService mostyBaseFeignService;

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

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

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

    // 根据用户ID，获取用户部门信息
    public DeptInfoVo getDeptInfoByUserId(String userId) {
        if (StringUtils.isBlank(userId)) {
            throw new BusinessException("获取用户部门信息异常");
        }
        ResponseResult<DeptInfoVo> responseResult = mostyBaseFeignService.getDeptInfoByUserId(userId);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("获取用户部门信息异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("获取用户部门信息异常");
        }
        return responseResult.getData();
    }

    // 查询部门下的总人数（包含子部门）
    public Integer getUserCount(String deptId) {
        if (StringUtils.isBlank(deptId)) {
            throw new BusinessException("查询部门下的总人数异常");
        }
        ResponseResult<Integer> responseResult = mostyBaseFeignService.getUserCount(deptId);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("查询部门下的总人数异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("查询部门下的总人数异常");
        }
        return responseResult.getData();
    }

    // 通过key获取配置信息
    public String getValue(String pzj) {
        if (StringUtils.isBlank(pzj)) {
            return null;
        }
        ResponseResult<String> responseResult = mostyBaseFeignService.getValue(pzj);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("查询系统配置异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("查询系统配置异常");
        }
        return responseResult.getData();
    }

    // 通过字典名称获取词条列表
    public SysDictVO getSysDictByCode(String dictCode) {
        if (StringUtils.isBlank(dictCode)) {
            return null;
        }
        ResponseResult<SysDictVO> responseResult = mostyBaseFeignService.getSysDictByCode(dictCode);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("获取字典信息异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("获取字典信息异常");
        }
        return responseResult.getData();
    }

    // 通过身份证号获取用户信息
    public SysUser getUserInfoBySfzh(String sfzh) {
        if (StringUtils.isBlank(sfzh)) {
            return null;
        }
        ResponseResult<SysUser> responseResult = mostyBaseFeignService.getUserInfoBySfzh(sfzh);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("获取用户信息异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("获取用户信息异常");
        }
        return responseResult.getData();
    }

    // 获取部门信息
    public List<SysDept> getOrg(SysDeptQuery deptQuery) {
        ResponseResult<List<SysDept>> responseResult = mostyBaseFeignService.getOrg(deptQuery);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("获取用户信息异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("获取用户信息异常");
        }
        return responseResult.getData();
    }

    // 获取地址中文信息
    public JSONObject getAddress(String jd, String wd) {
        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 List<String> getChildDept(String ssbmid) {
        ResponseResult<List<String>> responseResult = mostyBaseFeignService.getChildDept(ssbmid);
        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();
    }


    // 根据用户ID，获取用户部门信息
    public DeptInfoVo getDeptInfoBySfzh(String sfzh) {
        if (StringUtils.isBlank(sfzh)) {
            throw new BusinessException("获取用户部门信息异常");
        }
        ResponseResult<DeptInfoVo> responseResult = mostyBaseFeignService.getDeptInfoBySfzh(sfzh);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("获取用户部门信息异常 responseResult = {}", JSON.toJSONString(responseResult));
//            throw new BusinessException("获取用户部门信息异常");
            return null;
        }
        return responseResult.getData();
    }
}
