package com.mosty.rwzx.remote;

import com.alibaba.fastjson.JSON;
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.SysUserDeptVO;
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;
import java.util.ArrayList;
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;

    // 根据用户ID，获取用户部门信息
    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();
    }

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

    // 查询所有的用户数据（包含对应的部门信息）
    public List<SysUserDeptVO> getUserAll(String deptid) {
        if (StringUtils.isBlank(deptid)) {
            return new ArrayList<>();
        }
        ResponseResult<List<SysUserDeptVO>> responseResult = mostyBaseFeignService.getUserAll(deptid);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("查询所有的用户数据异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("查询所有的用户数据异常");
        }
        return responseResult.getData();
    }

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

    // 查询用户详情信息
    public SysUser getUserInfo(String userId) {
        if (StringUtils.isBlank(userId)) {
            return null;
        }
        ResponseResult<SysUser> responseResult = mostyBaseFeignService.getUserInfo(userId);
        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 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();
    }
}
