package com.mosty.lzother.remote;

import com.alibaba.fastjson.JSON;
import com.mosty.base.feign.service.MostyBaseFeignService;
import com.mosty.base.model.dto.base.SysDeptDTO;
import com.mosty.base.model.dto.base.SysMessageInfoInsertDto;
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.vo.SysUserDeptVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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;

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

    /**
     * 根据部门deptid获取所属分县局、所属地市州
     *
     * @param 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));
            return null;
        }
        return responseResult.getData();
    }

    /**
     * 根据部门deptid获取所属分县局、所属地市州
     *
     * @param orgcode 部门编码
     */
    public DeptInfoVo getOrgByOrgcode(String orgcode) {
        if (StringUtils.isBlank(orgcode)) {
            return null;
        }
        ResponseResult<DeptInfoVo> responseResult = mostyBaseFeignService.getOrgByOrgcode(orgcode);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("调用部门code查询部门详情信息异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("调用部门code查询部门详情信息异常");
        }
        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));
            return new ArrayList<>();
        }
        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));
            return new ArrayList<>();
        }
        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));
            return 0;
        }
        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));
            return null;
        }
        return responseResult.getData();
    }

}
