package com.mosty.qwzx.remote;

import com.alibaba.fastjson.JSON;
import com.mosty.base.model.dto.base.GetDeptListDTO;
import com.mosty.base.model.dto.base.GetSsbmDto;
import com.mosty.base.model.dto.base.SysDeptDTO;
import com.mosty.base.model.dto.base.SysMessageInfoInsertDto;
import com.mosty.base.feign.service.MostyBaseFeignService;
import com.mosty.base.model.query.base.GetSysDeptListQuery;
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.SysDept;
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;

    // 根据部门deptid获取所属分县局、所属地市州
    public DeptInfoVo getOrgByDeptId(String deptid) {
        if (StringUtils.isBlank(deptid)) {
            return null;
        }
        ResponseResult<DeptInfoVo> responseResult = mostyBaseFeignService.getOrgByDeptId(deptid);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("调用部门id查询部门详情信息异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("调用部门id查询部门详情信息异常");
        }
        return responseResult.getData();
    }


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

    // 获取部门下的用户列表，不包含子部门
    public List<SysUserDeptVO> getUserList(String deptid) {
        ResponseResult<List<SysUserDeptVO>> responseResult = mostyBaseFeignService.getUserList(deptid);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("获取部门下的用户列表异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("获取部门下的用户列表异常");
        }
        return responseResult.getData();
    }
    // 获取所有的用户数据
    public List<SysUserDeptVO> getUserAll(String deptId) {
        ResponseResult<List<SysUserDeptVO>> responseResult = mostyBaseFeignService.getUserAll(deptId);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("调用部门id查询部门详情信息异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("调用部门id查询部门详情信息异常");
        }
        return responseResult.getData();
    }

    // 获取所有的部门信息集合
    public List<DeptInfoVo> getDeptAll() {
        ResponseResult<List<DeptInfoVo>> responseResult = mostyBaseFeignService.getDeptAll();
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("调用部门id查询部门详情信息异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("调用部门id查询部门详情信息异常");
        }
        return responseResult.getData();
    }

    // 给人员发送消息
    public Integer sendMsg(SysMessageInfoInsertDto dto) {
        ResponseResult<Integer> responseResult = mostyBaseFeignService.sendMsg(dto);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("给人员发送消息失败， responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("给人员发送消息失败");
        }
        return responseResult.getData();
    }

    // 获取人员的部门所属
    public DeptInfoVo getDeptInfoByUserId(String userId) {
        ResponseResult<DeptInfoVo> responseResult = mostyBaseFeignService.getDeptInfoByUserId(userId);
        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();
    }

    // 获取用户信息
    public List<String> getChildDept(String deptId) {
        ResponseResult<List<String>> responseResult = mostyBaseFeignService.getChildDept(deptId);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("获取用户信息失败， responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("获取用户信息失败");
        }
        return responseResult.getData();
    }

    // 查询部门下的所有的子部门信息列表
    public List<DeptInfoVo> getChildDept(GetDeptListDTO dto) {
        ResponseResult<List<DeptInfoVo>> responseResult = mostyBaseFeignService.getChildDept(dto);
        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();
    }

    /**
     * 获取下级部门列表
     *
     * @param query 组织机构部门查询对象
     * @return 组织机构列表
     */
    public List<SysDept> getChildOrgList(GetSysDeptListQuery query) {
        if (null == query) {
            return new ArrayList<>();
        }
        ResponseResult<List<SysDept>> responseResult = mostyBaseFeignService.getOrganizationList(query);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("获取查询条件异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("获取查询条件异常");
        }
        return responseResult.getData();
    }


    // 查询部门详情
    public SysDept getSysDeptBySsbmdm(String ssbmdm) {
        ResponseResult<SysDept> responseResult = mostyBaseFeignService.getSysDeptBySsbmdm(ssbmdm);
        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();
    }

}
