zn-cloud/yudao-module-system/yudao-module-system-biz/src/main/resources/mapper/worklog/LogInstanceMapper.xml
furongxin ef19973638 工作日志模块的查询性能优化和代码重构
- 工作日志查询逻辑重构,以提高查询效率和可读性。
- 引入MPJLambdaWrapperX以简化SQL查询构建。
-直接使用COALESCE函数替代多个if判断,简化SQL逻辑。- 移除不必要的注释和未使用的代码片段。
-调整数据库逻辑,引入逻辑删除字段以优化数据处理。
-确保单元测试覆盖更改,并调整以适应新的查询逻辑。
- 文档更新以反映代码更改,并添加缺失的数据类型说明。

解决了旧查询方法中存在的性能瓶颈,该方法由于复杂的嵌套SQL和不必要的临时表而导致效率低下。新的查询方法通过利用Mybatis-Plus的功能和更简洁的SQL逻辑,提高了查询速度和整体系统性能。此外,代码重构还简化了维护工作。
2024-09-18 18:57:20 +08:00

222 lines
8.4 KiB
XML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.yudao.module.system.dal.mysql.worklog.LogInstanceMapper">
<!--
一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。
无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。
代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。
文档可见https://www.iocoder.cn/MyBatis/x-plugins/
-->
<select id="selectRaedUser" resultType="cn.iocoder.yudao.module.system.service.worklog.dto.LogReadUserRespDTO">
SELECT
DISTINCT result.user_id, result.dept_id
FROM
(
SELECT
user.id,
user.dept_id,
user.nickname,
read_user.*,
CASE
read_user.data_scope
WHEN 1 THEN
'all' -- 全部数据权限
WHEN 2 THEN
read_user.data_scope_dept_ids -- 指定部门数据权限
WHEN 3 THEN
user.dept_id -- 本部门数据权限
WHEN 4 THEN
(
SELECT
GROUP_CONCAT( id SEPARATOR ', ' ) AS ids
FROM
(
SELECT
t1.id,
t1.NAME,
IF
( find_in_set( parent_id, @pids ) > 0, @pids := concat( @pids, ',', id ), 0 ) AS ischild
FROM
( SELECT id, parent_id, NAME FROM system_dept t ORDER BY parent_id, id ) t1,
( SELECT @pids := user.dept_id ) t2
) t3
WHERE
ischild != 0
OR id = user.dept_id
) -- read_user.data_scope_dept_ids -- 本部门及以下数据权限
END AS ids
FROM
system_users user
LEFT JOIN (
SELECT
u_role.user_id,
u_role.role_id,
role_id.data_scope,
role_id.data_scope_dept_ids
FROM
system_user_role u_role
LEFT JOIN (
SELECT
role.id, role.data_scope, role.data_scope_dept_ids
FROM
(
SELECT role_id
FROM system_role_menu a
LEFT JOIN system_menu role ON a.menu_id = role.id
WHERE
role.permission = 'system:view-log:query'
AND a.role_id != 1
AND a.role_id != 101
AND a.deleted = 0
) menu
LEFT JOIN system_role role on role.id = menu.role_id
) role_id ON u_role.role_id = role_id.id
WHERE u_role.deleted = 0
ORDER BY
u_role.role_id
) read_user ON user.id = read_user.user_id
WHERE
user.deleted = 0
AND user.status = 0
AND read_user.data_scope != 5
) result
WHERE
result.user_id != #{userId}
and
( ( LOCATE(#{deptId},result.ids) and result.data_scope = 4 )
or ( LOCATE(#{deptId},result.ids) and result.data_scope = 2 )
or ( result.dept_id = #{deptId} and result.data_scope = 3 )
or result.data_scope = 1 )
</select>
<select id="getNextOrUp"
resultType="cn.iocoder.yudao.module.system.controller.admin.worklog.vo.loginstance.LogInstanceRespVO">
SELECT
a.id,
a.name
FROM
work_log_instance_ext AS a
LEFT JOIN work_log_read AS e ON a.id = e.log_instance_id
AND e.read_user_id = #{userId}
<where>
a.deleted = 0
<if test="type == 0">
and a.id &lt; #{id}
</if>
<if test="type == 1">
and a.id &gt; #{id}
</if>
<if test="reqVO.type != null">
and a.type = #{reqVO.type}
</if>
<if test="reqVO.deptId != null">
and a.dept_id = #{reqVO.deptId}
</if>
<if test="reqVO.startUserId != null">
and a.start_user_id = #{reqVO.startUserId}
</if>
<if test="reqVO.createTime != null and reqVO.createTime.length > 0">
<if test="reqVO.createTime[0] != null">
and a.create_time &gt;= #{reqVO.createTime[0]}
</if>
<if test="reqVO.createTime[1] != null">
and a.create_time &lt;= #{reqVO.createTime[1]}
</if>
</if>
<if test="reqVO.readStatus != null">
and e.read_status = #{reqVO.readStatus}
</if>
<if test="pagingType == 0">
and a.start_user_id != #{userId}
</if>
<if test="pagingType == 1">
and a.start_user_id = #{userId}
</if>
<if test="reqVO.isBoss != null">
and a.start_user_id in
<foreach collection="userIds" item="userId" open="(" close=")" separator=",">
#{userId}
</foreach>
</if>
</where>
<if test="type == 0">
order by id desc
</if>
<if test="type == 1">
order by id asc
</if>
limit 1
</select>
<!-- <select id="selectPageResult" resultType="cn.iocoder.yudao.module.system.controller.admin.worklog.vo.loginstance.LogInstanceRespVO">-->
<!-- SELECT-->
<!-- a.*,-->
<!-- e.read_status as readStatus,-->
<!-- COALESCE(c.readCount, 0) as readCount,-->
<!-- COALESCE(d.unreadCount, 0) as unReadCount,-->
<!-- COALESCE(b.comment, 0) as comment-->
<!-- FROM-->
<!-- system_user_post userPost,-->
<!-- system_dept dept,-->
<!-- system_post post,-->
<!-- work_log_instance_ext as a-->
<!-- LEFT JOIN work_log_read as e ON a.id = e.log_instance_id and e.read_user_id = #{userId}-->
<!-- LEFT JOIN (SELECT log_instance_id, COUNT(log_instance_id) AS readCount FROM work_log_read where read_status = 1 AND deleted = 0 GROUP BY log_instance_id ) AS c ON a.id = c.log_instance_id-->
<!-- LEFT JOIN (SELECT log_instance_id, COUNT(log_instance_id) AS unReadCount FROM work_log_read where read_status = 0 AND deleted = 0 GROUP BY log_instance_id ) AS d ON a.id = d.log_instance_id-->
<!-- LEFT JOIN (SELECT work_log_id, COUNT(work_log_id) AS comment FROM work_log_comment where deleted = 0 GROUP BY work_log_id) AS b ON a.id = b.work_log_id-->
<!-- <where>-->
<!-- a.deleted = 0-->
<!-- AND a.start_user_id = userPost.user_id-->
<!-- AND userPost.post_id = post.id-->
<!-- AND a.dept_id = dept.id-->
<!-- <if test="reqVO.isProduce == 1">-->
<!-- AND dept.flag LIKE '%166%'-->
<!-- </if>-->
<!-- <if test="reqVO.isProduce == 2">-->
<!-- AND dept.flag NOT LIKE '%166%'-->
<!-- </if>-->
<!-- <if test="reqVO.formId != null">-->
<!-- and a.form_id = #{reqVO.formId}-->
<!-- </if>-->
<!-- <if test="reqVO.deptId != null">-->
<!-- and a.dept_id = #{reqVO.deptId}-->
<!-- </if>-->
<!-- <if test="reqVO.startUserId != null">-->
<!-- and a.start_user_id = #{reqVO.startUserId}-->
<!-- </if>-->
<!-- <if test="reqVO.createTime != null and reqVO.createTime.length > 0">-->
<!-- <if test="reqVO.createTime[0] != null">-->
<!-- and a.time &gt;= #{reqVO.createTime[0]}-->
<!-- </if>-->
<!-- <if test="reqVO.createTime[1] != null">-->
<!-- and a.time &lt;= #{reqVO.createTime[1]}-->
<!-- </if>-->
<!-- </if>-->
<!-- <if test="reqVO.readStatus != null">-->
<!-- and e.read_status = #{reqVO.readStatus}-->
<!-- </if>-->
<!-- <if test="pagingType == 0">-->
<!-- and a.start_user_id != #{userId}-->
<!-- </if>-->
<!-- <if test="pagingType == 1">-->
<!-- and a.start_user_id = #{userId}-->
<!-- </if>-->
<!-- <if test="reqVO.isBoss != null and reqVO.isProduce == null">-->
<!-- and a.start_user_id in-->
<!-- <foreach collection="userIds" item="userId" open="(" close=")" separator=",">-->
<!-- #{userId}-->
<!-- </foreach>-->
<!-- </if>-->
<!-- </where>-->
<!-- GROUP BY a.id-->
<!-- ORDER BY-->
<!-- a.time DESC,-->
<!-- post.sort,-->
<!-- a.create_time DESC-->
<!-- </select>-->
</mapper>