医院废弃物信息化管理平台
“mitao”
2026-04-21 2f18b3a28054c3917010f95ba84f7425280a2622
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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, "无权操作此医院的数据");
        }
    }
}