package cn.besteam.common.util;
|
|
import cn.besteam.common.constant.ErrorCode;
|
import cn.besteam.common.exception.BusinessException;
|
import cn.besteam.modules.system.entity.SysDepartment;
|
import cn.besteam.modules.system.entity.SysUser;
|
import cn.besteam.modules.system.entity.SysUserDepartment;
|
import cn.besteam.modules.system.service.ISysDepartmentService;
|
import cn.besteam.modules.system.service.ISysUserDepartmentService;
|
import cn.besteam.modules.shiro.authc.util.EnvUtils;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
/**
|
* 用户上下文工具类
|
* 用于获取当前用户信息和权限检查
|
*/
|
@Component
|
@Slf4j
|
public class UserContextUtil {
|
|
@Autowired
|
private ISysDepartmentService sysDepartmentService;
|
|
@Autowired
|
private ISysUserDepartmentService sysUserDepartmentService;
|
|
/**
|
* 机构类型:区域
|
*/
|
private static final Integer ORG_TYPE_REGION = 1;
|
|
/**
|
* 机构类型:单位(医院)
|
*/
|
private static final Integer ORG_TYPE_HOSPITAL = 2;
|
|
/**
|
* 机构类型:部门
|
*/
|
private static final Integer ORG_TYPE_DEPARTMENT = 3;
|
|
/**
|
* 机构类型:科室
|
*/
|
private static final Integer ORG_TYPE_OFFICE = 4;
|
|
/**
|
* 获取当前用户信息
|
* @return 当前用户信息
|
*/
|
public SysUser getCurrentUser() {
|
SysUser currentUser = EnvUtils.getUser();
|
if (currentUser == null) {
|
throw new BusinessException(ErrorCode.ERR_500, "用户未登录");
|
}
|
return currentUser;
|
}
|
|
/**
|
* 获取当前用户的部门信息
|
* @return 当前用户部门信息
|
*/
|
public SysDepartment getCurrentUserDepartment() {
|
SysUser currentUser = getCurrentUser();
|
|
// 查询用户部门关联
|
SysUserDepartment userDepartment = sysUserDepartmentService.getOne(
|
new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<SysUserDepartment>()
|
.eq("user_id", currentUser.getId())
|
);
|
|
if (userDepartment == null || userDepartment.getDepartmentId() == null) {
|
throw new BusinessException(ErrorCode.ERR_500, "用户部门信息不存在");
|
}
|
|
return sysDepartmentService.getById(userDepartment.getDepartmentId());
|
}
|
|
/**
|
* 检查用户是否有操作权限
|
* 区域用户不能进行操作
|
*/
|
public void checkUserPermission() {
|
SysDepartment department = getCurrentUserDepartment();
|
if (department.getOrgType().equals(ORG_TYPE_REGION)) {
|
throw new BusinessException(ErrorCode.ERR_500, "区域用户不能进行此操作");
|
}
|
}
|
|
/**
|
* 获取当前用户所属医院的ID
|
* @return 医院ID
|
*/
|
public Integer getCurrentUserHospitalId() {
|
SysDepartment department = getCurrentUserDepartment();
|
return calculateHospitalId(department);
|
}
|
|
/**
|
* 根据部门信息计算医院ID
|
* @param department 部门信息
|
* @return 医院ID
|
*/
|
private Integer calculateHospitalId(SysDepartment department) {
|
if (department == null) {
|
throw new BusinessException(ErrorCode.ERR_500, "部门信息不存在");
|
}
|
|
Integer orgType = department.getOrgType();
|
|
if (orgType.equals(ORG_TYPE_HOSPITAL)) {
|
// 当前部门就是医院
|
return department.getId();
|
} else if (orgType.equals(ORG_TYPE_DEPARTMENT)) {
|
// 部门用户,医院是父级部门
|
return department.getParentId();
|
} else if (orgType.equals(ORG_TYPE_OFFICE)) {
|
// 科室用户,医院是父级部门的父级部门
|
SysDepartment parentDepartment = sysDepartmentService.getById(department.getParentId());
|
if (parentDepartment == null) {
|
throw new BusinessException(ErrorCode.ERR_500, "父级部门信息不存在");
|
}
|
return parentDepartment.getParentId();
|
} else {
|
throw new BusinessException(ErrorCode.ERR_500, "不支持的用户类型");
|
}
|
}
|
|
/**
|
* 验证医院ID是否有效
|
* @param hospitalId 医院ID
|
*/
|
public void validateHospitalId(Integer hospitalId) {
|
Integer currentUserHospitalId = getCurrentUserHospitalId();
|
if (!hospitalId.equals(currentUserHospitalId)) {
|
throw new BusinessException(ErrorCode.ERR_500, "无权操作此医院的数据");
|
}
|
}
|
}
|