serv\web- 多租户修改

This commit is contained in:
2025-10-29 19:08:22 +08:00
parent c5c134fbb3
commit 82b6f14e64
86 changed files with 4446 additions and 2730 deletions

View File

@@ -5,7 +5,7 @@ import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.xyzh.common.core.page.PageParam;
import org.xyzh.common.dto.study.TbCourse;
import org.xyzh.common.vo.CourseItemVO;
import org.xyzh.common.vo.UserDeptRoleVO;
import java.util.List;
@@ -22,11 +22,12 @@ public interface CourseMapper extends BaseMapper<TbCourse> {
/**
* @description 查询课程列表
* @param filter 过滤条件
* @param userDeptRoles 用户部门角色列表
* @return List<TbCourse> 课程列表
* @author yslg
* @since 2025-10-15
*/
List<TbCourse> selectCourses(TbCourse filter);
List<TbCourse> selectCourses(@Param("filter") TbCourse filter, @Param("userDeptRoles") List<UserDeptRoleVO> userDeptRoles);
/**
* @description 根据课程ID查询课程信息
@@ -159,20 +160,22 @@ public interface CourseMapper extends BaseMapper<TbCourse> {
* @description 分页查询课程
* @param filter 过滤条件
* @param pageParam 分页参数
* @param userDeptRoles 用户部门角色列表
* @return List<TbCourse> 课程列表
* @author yslg
* @since 2025-10-15
*/
List<TbCourse> selectCoursesPage(@Param("filter") TbCourse filter, @Param("pageParam") PageParam pageParam);
List<TbCourse> selectCoursesPage(@Param("filter") TbCourse filter, @Param("pageParam") PageParam pageParam, @Param("userDeptRoles") List<UserDeptRoleVO> userDeptRoles);
/**
* @description 统计课程总数
* @param filter 过滤条件
* @param userDeptRoles 用户部门角色列表
* @return long 总数
* @author yslg
* @since 2025-10-15
*/
long countCourses(@Param("filter") TbCourse filter);
long countCourses(@Param("filter") TbCourse filter, @Param("userDeptRoles") List<UserDeptRoleVO> userDeptRoles);
/**
* @description 增加课程浏览次数

View File

@@ -5,6 +5,7 @@ import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.xyzh.common.core.page.PageParam;
import org.xyzh.common.dto.study.TbLearningTask;
import org.xyzh.common.vo.UserDeptRoleVO;
import org.xyzh.common.vo.TaskItemVO;
import java.util.List;
@@ -22,11 +23,12 @@ public interface LearningTaskMapper extends BaseMapper<TbLearningTask> {
/**
* @description 查询学习任务列表
* @param filter 过滤条件
* @param userDeptRoles 用户部门角色列表
* @return List<TbLearningTask> 学习任务列表
* @author yslg
* @since 2025-10-15
*/
List<TbLearningTask> selectLearningTasks(TbLearningTask filter);
List<TbLearningTask> selectLearningTasks(@Param("filter") TbLearningTask filter, @Param("userDeptRoles") List<UserDeptRoleVO> userDeptRoles);
/**
* @description 根据任务ID查询任务信息
@@ -150,20 +152,22 @@ public interface LearningTaskMapper extends BaseMapper<TbLearningTask> {
* @description 分页查询学习任务
* @param filter 过滤条件
* @param pageParam 分页参数
* @param userDeptRoles 用户部门角色列表
* @return List<TbLearningTask> 学习任务列表
* @author yslg
* @since 2025-10-15
*/
List<TbLearningTask> selectLearningTasksPage(@Param("filter") TbLearningTask filter, @Param("pageParam") PageParam pageParam);
List<TbLearningTask> selectLearningTasksPage(@Param("filter") TbLearningTask filter, @Param("pageParam") PageParam pageParam, @Param("userDeptRoles") List<UserDeptRoleVO> userDeptRoles);
List<TbLearningTask> selectUserLearningTasksPage(@Param("filter") TaskItemVO filter, @Param("pageParam") PageParam pageParam);
List<TbLearningTask> selectUserLearningTasksPage(@Param("filter") TaskItemVO filter, @Param("pageParam") PageParam pageParam, @Param("userDeptRoles") List<UserDeptRoleVO> userDeptRoles);
/**
* @description 统计学习任务总数
* @param filter 过滤条件
* @param userDeptRoles 用户部门角色列表
* @return long 总数
* @author yslg
* @since 2025-10-15
*/
long countLearningTasks(@Param("filter") TbLearningTask filter);
long countLearningTasks(@Param("filter") TbLearningTask filter, @Param("userDeptRoles") List<UserDeptRoleVO> userDeptRoles);
}

View File

@@ -28,6 +28,9 @@ import org.xyzh.study.mapper.CourseChapterMapper;
import org.xyzh.study.mapper.CourseNodeMapper;
import org.xyzh.study.service.SCCourseService;
import org.xyzh.system.utils.LoginUtil;
import org.xyzh.api.system.permission.ResourcePermissionService;
import org.xyzh.common.vo.UserDeptRoleVO;
import org.xyzh.common.core.enums.ResourceType;
/**
* @description 课程服务实现类
@@ -50,10 +53,15 @@ public class SCCourseServiceImpl implements SCCourseService {
@Autowired
private CourseNodeMapper courseNodeMapper;
@Autowired
private ResourcePermissionService resourcePermissionService;
@Override
public ResultDomain<TbCourse> getCourseList(TbCourse filter) {
ResultDomain<TbCourse> resultDomain = new ResultDomain<>();
List<TbCourse> courses = courseMapper.selectCourses(filter);
// 获取当前用户的部门角色
List<UserDeptRoleVO> userDeptRoles = LoginUtil.getCurrentDeptRole();
List<TbCourse> courses = courseMapper.selectCourses(filter, userDeptRoles);
resultDomain.success("获取课程列表成功", courses);
return resultDomain;
}
@@ -63,8 +71,10 @@ public class SCCourseServiceImpl implements SCCourseService {
ResultDomain<TbCourse> resultDomain = new ResultDomain<>();
TbCourse filter = pageRequest.getFilter();
PageParam pageParam = pageRequest.getPageParam();
List<TbCourse> courses = courseMapper.selectCoursesPage(filter, pageParam);
int total = (int) courseMapper.countCourses(filter);
// 获取当前用户的部门角色
List<UserDeptRoleVO> userDeptRoles = LoginUtil.getCurrentDeptRole();
List<TbCourse> courses = courseMapper.selectCoursesPage(filter, pageParam, userDeptRoles);
int total = (int) courseMapper.countCourses(filter, userDeptRoles);
int totalPages = (int) Math.ceil((double) total / pageParam.getPageSize());
pageParam.setTotalPages(totalPages);
pageParam.setTotalElements(total);
@@ -194,6 +204,22 @@ public class SCCourseServiceImpl implements SCCourseService {
}
}
// 创建课程资源权限
try {
List<UserDeptRoleVO> userDeptRoles = LoginUtil.getCurrentDeptRole();
if (userDeptRoles != null && !userDeptRoles.isEmpty()) {
// 使用用户的第一个部门角色创建权限
resourcePermissionService.createResourcePermission(
ResourceType.COURSE.getCode(),
courseID,
userDeptRoles.get(0)
);
logger.info("创建课程权限成功: {}", courseID);
}
} catch (Exception e) {
logger.error("创建课程权限异常,但不影响课程创建: {}", e.getMessage(), e);
}
resultDomain.success("创建课程成功", courseItemVO);
return resultDomain;
}

View File

@@ -30,6 +30,9 @@ import org.xyzh.system.utils.LoginUtil;
import org.xyzh.study.mapper.TaskItemMapper;
import org.xyzh.api.study.task.LearningTaskService;
import org.xyzh.common.core.enums.TaskItemType;
import org.xyzh.api.system.permission.ResourcePermissionService;
import org.xyzh.common.vo.UserDeptRoleVO;
import org.xyzh.common.core.enums.ResourceType;
/**
* @description 学习任务服务实现类
@@ -55,6 +58,9 @@ public class SCLearningTaskServiceImpl implements LearningTaskService {
@Autowired
private TaskItemMapper taskItemMapper;
@Autowired
private ResourcePermissionService resourcePermissionService;
@Override
public ResultDomain<TbTaskItem> addTaskCourse(TbTaskItem taskItem) {
// TODO Auto-generated method stub
@@ -105,8 +111,10 @@ public class SCLearningTaskServiceImpl implements LearningTaskService {
@Override
public ResultDomain<TbLearningTask> getTaskPage(TbLearningTask filter, PageParam pageParam) {
ResultDomain<TbLearningTask> resultDomain = new ResultDomain<>();
List<TbLearningTask> taskList = learningTaskMapper.selectLearningTasksPage(filter, pageParam);
long total = learningTaskMapper.countLearningTasks(filter);
// 获取当前用户的部门角色
List<UserDeptRoleVO> userDeptRoles = LoginUtil.getCurrentDeptRole();
List<TbLearningTask> taskList = learningTaskMapper.selectLearningTasksPage(filter, pageParam, userDeptRoles);
long total = learningTaskMapper.countLearningTasks(filter, userDeptRoles);
pageParam.setTotalElements(total);
pageParam.setTotalPages((int) Math.ceil((double) total / pageParam.getPageSize()));
PageDomain<TbLearningTask> pageDomain = new PageDomain<>();
@@ -125,8 +133,10 @@ public class SCLearningTaskServiceImpl implements LearningTaskService {
return resultDomain;
}
filter.setUserID(user.getID());
List<TbLearningTask> taskList = learningTaskMapper.selectUserLearningTasksPage(filter, pageParam);
long total = learningTaskMapper.countLearningTasks(filter);
// 获取当前用户的部门角色
List<UserDeptRoleVO> userDeptRoles = LoginUtil.getCurrentDeptRole();
List<TbLearningTask> taskList = learningTaskMapper.selectUserLearningTasksPage(filter, pageParam, userDeptRoles);
long total = learningTaskMapper.countLearningTasks(filter, userDeptRoles);
pageParam.setTotalElements(total);
pageParam.setTotalPages((int) Math.ceil((double) total / pageParam.getPageSize()));
PageDomain<TbLearningTask> pageDomain = new PageDomain<>();
@@ -188,7 +198,22 @@ public class SCLearningTaskServiceImpl implements LearningTaskService {
taskUserMapper.batchInsertTaskUsers(taskUsers);
for(TbTaskItem item : taskCourses) {
int learnCount = courseMapper.incrementLearnCount(item.getItemID(), taskUsers.size());
}
// 创建任务资源权限
try {
List<UserDeptRoleVO> userDeptRoles = LoginUtil.getCurrentDeptRole();
if (userDeptRoles != null && !userDeptRoles.isEmpty()) {
// 使用用户的第一个部门角色创建权限
resourcePermissionService.createResourcePermission(
ResourceType.TASK.getCode(),
taskID,
userDeptRoles.get(0)
);
logger.info("创建任务权限成功: {}", taskID);
}
} catch (Exception e) {
logger.error("创建任务权限异常,但不影响任务创建: {}", e.getMessage(), e);
}
resultDomain.success("创建任务成功", taskVO);
@@ -515,7 +540,9 @@ public class SCLearningTaskServiceImpl implements LearningTaskService {
@Override
public ResultDomain<TbLearningTask> getTaskList(TbLearningTask filter) {
ResultDomain<TbLearningTask> resultDomain = new ResultDomain<>();
List<TbLearningTask> taskList = learningTaskMapper.selectLearningTasks(filter);
// 获取当前用户的部门角色
List<UserDeptRoleVO> userDeptRoles = LoginUtil.getCurrentDeptRole();
List<TbLearningTask> taskList = learningTaskMapper.selectLearningTasks(filter, userDeptRoles);
resultDomain.success("获取任务列表成功", taskList);
return resultDomain;
}

View File

@@ -78,13 +78,56 @@
</where>
</sql>
<!-- selectCourses -->
<!-- 权限过滤条件基于dept_path的高效继承 -->
<sql id="Permission_Filter">
INNER JOIN tb_resource_permission rp ON c.course_id = rp.resource_id
AND rp.resource_type = 2
AND rp.deleted = 0
AND rp.can_read = 1
AND (
-- 全局权限:所有用户可访问
(rp.dept_id IS NULL AND rp.role_id IS NULL)
<if test="userDeptRoles != null and userDeptRoles.size() > 0">
OR EXISTS (
SELECT 1
FROM (
<foreach collection="userDeptRoles" item="udr" separator=" UNION ALL ">
SELECT #{udr.deptID} AS dept_id, #{udr.deptPath} AS dept_path, #{udr.roleID} AS role_id
</foreach>
) user_roles
LEFT JOIN tb_sys_dept perm_dept ON perm_dept.dept_id = rp.dept_id AND perm_dept.deleted = 0
WHERE
-- 部门级权限当前部门或父部门通过dept_path判断继承关系
(rp.role_id IS NULL AND rp.dept_id IS NOT NULL
AND user_roles.dept_path LIKE CONCAT(perm_dept.dept_path, '%'))
-- 角色级权限:跨部门的角色权限
OR (rp.dept_id IS NULL AND rp.role_id = user_roles.role_id)
-- 精确权限:特定部门的特定角色
OR (rp.dept_id = user_roles.dept_id AND rp.role_id = user_roles.role_id)
)
</if>
)
</sql>
<!-- selectCourses - 添加权限过滤 -->
<select id="selectCourses" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
FROM tb_course
<include refid="Where_Clause"/>
ORDER BY order_num ASC, create_time DESC
SELECT DISTINCT c.*
FROM tb_course c
<include refid="Permission_Filter"/>
WHERE c.deleted = 0
<if test="filter.courseID != null and filter.courseID != ''">
AND c.course_id = #{filter.courseID}
</if>
<if test="filter.name != null and filter.name != ''">
AND c.name LIKE CONCAT('%', #{filter.name}, '%')
</if>
<if test="filter.teacher != null and filter.teacher != ''">
AND c.teacher LIKE CONCAT('%', #{filter.teacher}, '%')
</if>
<if test="filter.status != null">
AND c.status = #{filter.status}
</if>
ORDER BY c.order_num ASC, c.create_time DESC
</select>
<!-- 根据课程ID查询课程信息 -->
@@ -270,20 +313,62 @@
</delete>
<!-- 分页查询课程 -->
<!-- selectCoursesPage - 添加权限过滤 -->
<select id="selectCoursesPage" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
FROM tb_course
<include refid="Filter_Clause" />
ORDER BY order_num ASC, create_time DESC
SELECT DISTINCT c.*
FROM tb_course c
<include refid="Permission_Filter"/>
WHERE c.deleted = 0
<if test="filter != null">
<if test="filter.courseID != null and filter.courseID != ''">
AND c.course_id = #{filter.courseID}
</if>
<if test="filter.name != null and filter.name != ''">
AND c.name LIKE CONCAT('%', #{filter.name}, '%')
</if>
<if test="filter.teacher != null and filter.teacher != ''">
AND c.teacher LIKE CONCAT('%', #{filter.teacher}, '%')
</if>
<if test="filter.status != null">
AND c.status = #{filter.status}
</if>
<if test="filter.orderNum != null">
AND c.order_num = #{filter.orderNum}
</if>
<if test="filter.creator != null and filter.creator != ''">
AND c.creator = #{filter.creator}
</if>
</if>
ORDER BY c.order_num ASC, c.create_time DESC
LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
</select>
<!-- 统计课程总数 -->
<!-- 统计课程总数 - 添加权限过滤 -->
<select id="countCourses" resultType="long">
SELECT COUNT(1)
FROM tb_course
<include refid="Filter_Clause" />
SELECT COUNT(DISTINCT c.id)
FROM tb_course c
<include refid="Permission_Filter"/>
WHERE c.deleted = 0
<if test="filter != null">
<if test="filter.courseID != null and filter.courseID != ''">
AND c.course_id = #{filter.courseID}
</if>
<if test="filter.name != null and filter.name != ''">
AND c.name LIKE CONCAT('%', #{filter.name}, '%')
</if>
<if test="filter.teacher != null and filter.teacher != ''">
AND c.teacher LIKE CONCAT('%', #{filter.teacher}, '%')
</if>
<if test="filter.status != null">
AND c.status = #{filter.status}
</if>
<if test="filter.orderNum != null">
AND c.order_num = #{filter.orderNum}
</if>
<if test="filter.creator != null and filter.creator != ''">
AND c.creator = #{filter.creator}
</if>
</if>
</select>
<update id="incrementViewCount">

View File

@@ -56,13 +56,53 @@
</where>
</sql>
<!-- selectLearningTasks -->
<!-- 权限过滤条件基于dept_path的高效继承 -->
<sql id="Permission_Filter">
INNER JOIN tb_resource_permission rp ON t.task_id = rp.resource_id
AND rp.resource_type = 3
AND rp.deleted = 0
AND rp.can_read = 1
AND (
-- 全局权限:所有用户可访问
(rp.dept_id IS NULL AND rp.role_id IS NULL)
<if test="userDeptRoles != null and userDeptRoles.size() > 0">
OR EXISTS (
SELECT 1
FROM (
<foreach collection="userDeptRoles" item="udr" separator=" UNION ALL ">
SELECT #{udr.deptID} AS dept_id, #{udr.deptPath} AS dept_path, #{udr.roleID} AS role_id
</foreach>
) user_roles
LEFT JOIN tb_sys_dept perm_dept ON perm_dept.dept_id = rp.dept_id AND perm_dept.deleted = 0
WHERE
-- 部门级权限当前部门或父部门通过dept_path判断继承关系
(rp.role_id IS NULL AND rp.dept_id IS NOT NULL
AND user_roles.dept_path LIKE CONCAT(perm_dept.dept_path, '%'))
-- 角色级权限:跨部门的角色权限
OR (rp.dept_id IS NULL AND rp.role_id = user_roles.role_id)
-- 精确权限:特定部门的特定角色
OR (rp.dept_id = user_roles.dept_id AND rp.role_id = user_roles.role_id)
)
</if>
)
</sql>
<!-- selectLearningTasks - 添加权限过滤 -->
<select id="selectLearningTasks" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
FROM tb_learning_task
<include refid="Where_Clause"/>
ORDER BY create_time DESC
SELECT DISTINCT t.*
FROM tb_learning_task t
<include refid="Permission_Filter"/>
WHERE t.deleted = 0
<if test="filter.taskID != null and filter.taskID != ''">
AND t.task_id = #{filter.taskID}
</if>
<if test="filter.name != null and filter.name != ''">
AND t.name LIKE CONCAT('%', #{filter.name}, '%')
</if>
<if test="filter.status != null">
AND t.status = #{filter.status}
</if>
ORDER BY t.create_time DESC
</select>
<!-- 根据任务ID查询任务信息 -->
@@ -215,33 +255,78 @@
</delete>
<!-- 分页查询学习任务 -->
<!-- selectLearningTasksPage - 添加权限过滤 -->
<select id="selectLearningTasksPage" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
FROM tb_learning_task
<include refid="Filter_Clause" />
ORDER BY create_time DESC
SELECT DISTINCT t.*
FROM tb_learning_task t
<include refid="Permission_Filter"/>
WHERE t.deleted = 0
<if test="filter != null">
<if test="filter.taskID != null and filter.taskID != ''">
AND t.task_id = #{filter.taskID}
</if>
<if test="filter.name != null and filter.name != ''">
AND t.name LIKE CONCAT('%', #{filter.name}, '%')
</if>
<if test="filter.status != null">
AND t.status = #{filter.status}
</if>
</if>
ORDER BY t.create_time DESC
LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
</select>
<!-- selectUserLearningTasksPage - 添加权限过滤 -->
<select id="selectUserLearningTasksPage" resultMap="BaseResultMap">
SELECT
SELECT DISTINCT
tlt.id, tlt.task_id, tlt.name, tlt.description, tlt.start_time, tlt.end_time, ttu.status,
tlt.creator, tlt.updater, tlt.create_time, tlt.update_time
FROM tb_task_user ttu
INNER JOIN tb_learning_task tlt ON ttu.task_id = tlt.task_id
INNER JOIN tb_resource_permission rp ON tlt.task_id = rp.resource_id
AND rp.resource_type = 3
AND rp.deleted = 0
AND rp.can_read = 1
AND (
(rp.dept_id IS NULL AND rp.role_id IS NULL)
<if test="userDeptRoles != null and userDeptRoles.size() > 0">
OR EXISTS (
SELECT 1
FROM (
<foreach collection="userDeptRoles" item="udr" separator=" UNION ALL ">
SELECT #{udr.deptID} AS dept_id, #{udr.roleID} AS role_id
</foreach>
) user_roles
WHERE (rp.dept_id = user_roles.dept_id AND rp.role_id IS NULL)
OR (rp.role_id = user_roles.role_id AND rp.dept_id IS NULL)
OR (rp.dept_id = user_roles.dept_id AND rp.role_id = user_roles.role_id)
)
</if>
)
WHERE ttu.user_id = #{filter.userID}
AND tlt.deleted = 0
AND ttu.deleted = 0
ORDER BY create_time DESC
ORDER BY tlt.create_time DESC
LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
</select>
<!-- 统计学习任务总数 -->
<!-- 统计学习任务总数 - 添加权限过滤 -->
<select id="countLearningTasks" resultType="long">
SELECT COUNT(1)
FROM tb_learning_task
<include refid="Filter_Clause" />
SELECT COUNT(DISTINCT t.id)
FROM tb_learning_task t
<include refid="Permission_Filter"/>
WHERE t.deleted = 0
<if test="filter != null">
<if test="filter.taskID != null and filter.taskID != ''">
AND t.task_id = #{filter.taskID}
</if>
<if test="filter.name != null and filter.name != ''">
AND t.name LIKE CONCAT('%', #{filter.name}, '%')
</if>
<if test="filter.status != null">
AND t.status = #{filter.status}
</if>
</if>
</select>
</mapper>