工作日志模块的查询性能优化和代码重构
- 工作日志查询逻辑重构,以提高查询效率和可读性。 - 引入MPJLambdaWrapperX以简化SQL查询构建。 -直接使用COALESCE函数替代多个if判断,简化SQL逻辑。- 移除不必要的注释和未使用的代码片段。 -调整数据库逻辑,引入逻辑删除字段以优化数据处理。 -确保单元测试覆盖更改,并调整以适应新的查询逻辑。 - 文档更新以反映代码更改,并添加缺失的数据类型说明。 解决了旧查询方法中存在的性能瓶颈,该方法由于复杂的嵌套SQL和不必要的临时表而导致效率低下。新的查询方法通过利用Mybatis-Plus的功能和更简洁的SQL逻辑,提高了查询速度和整体系统性能。此外,代码重构还简化了维护工作。
This commit is contained in:
parent
1708943c9d
commit
ef19973638
@ -51,4 +51,9 @@ public class LogReadDo extends BaseDO {
|
|||||||
* 1:已读
|
* 1:已读
|
||||||
*/
|
*/
|
||||||
private Integer readStatus;
|
private Integer readStatus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否删除
|
||||||
|
*/
|
||||||
|
private Boolean deleted;
|
||||||
}
|
}
|
||||||
|
@ -5,14 +5,18 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|||||||
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
|
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
|
||||||
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
||||||
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||||
|
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
|
||||||
import cn.iocoder.yudao.module.system.controller.admin.worklog.vo.loginstance.LogInstancePageReqVO;
|
import cn.iocoder.yudao.module.system.controller.admin.worklog.vo.loginstance.LogInstancePageReqVO;
|
||||||
import cn.iocoder.yudao.module.system.controller.admin.worklog.vo.loginstance.LogInstanceRespVO;
|
import cn.iocoder.yudao.module.system.controller.admin.worklog.vo.loginstance.LogInstanceRespVO;
|
||||||
|
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
|
||||||
|
import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO;
|
||||||
|
import cn.iocoder.yudao.module.system.dal.dataobject.dept.UserPostDO;
|
||||||
import cn.iocoder.yudao.module.system.dal.dataobject.worklog.LogInstanceDO;
|
import cn.iocoder.yudao.module.system.dal.dataobject.worklog.LogInstanceDO;
|
||||||
|
import cn.iocoder.yudao.module.system.dal.dataobject.worklog.LogReadDo;
|
||||||
import cn.iocoder.yudao.module.system.service.worklog.dto.LogReadUserRespDTO;
|
import cn.iocoder.yudao.module.system.service.worklog.dto.LogReadUserRespDTO;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
import org.apache.ibatis.annotations.Param;
|
import org.apache.ibatis.annotations.Param;
|
||||||
import org.apache.ibatis.annotations.Select;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -41,15 +45,56 @@ public interface LogInstanceMapper extends BaseMapperX<LogInstanceDO> {
|
|||||||
.in(LogInstanceDO::getTime, dateList));
|
.in(LogInstanceDO::getTime, dateList));
|
||||||
}
|
}
|
||||||
|
|
||||||
// List<LogInstanceRespVO> selectPageResult( @Param("reqVO") LogInstancePageReqVO reqVO,
|
default IPage<LogInstanceRespVO> selectPageResult(@Param("page") IPage<LogInstanceRespVO> page,
|
||||||
// @Param("userId") Long userId,
|
@Param("reqVO") LogInstancePageReqVO reqVO,
|
||||||
// @Param("ids") List<Long> ids);
|
@Param("userId") Long userId,
|
||||||
|
@Param("pagingType") Integer pagingType,
|
||||||
|
@Param("userIds") List<Long> userIds) {
|
||||||
|
|
||||||
IPage<LogInstanceRespVO> selectPageResult(@Param("page") IPage<LogInstanceRespVO> page,
|
MPJLambdaWrapperX<LogInstanceDO> queryWrapper = new MPJLambdaWrapperX<>();
|
||||||
@Param("reqVO") LogInstancePageReqVO reqVO,
|
queryWrapper.selectAll(LogInstanceDO.class);
|
||||||
@Param("userId") Long userId,
|
queryWrapper.selectAs("e.read_status", LogInstanceRespVO::getReadStatus);
|
||||||
@Param("pagingType") Integer pagingType,
|
queryWrapper.selectAs("COALESCE(c.readCount, 0)", LogInstanceRespVO::getReadCount);
|
||||||
@Param("userIds") List<Long> userIds);
|
queryWrapper.selectAs("COALESCE(d.unreadCount, 0)", LogInstanceRespVO::getUnreadCount);
|
||||||
|
queryWrapper.selectAs("COALESCE(b.comment, 0)", LogInstanceRespVO::getComment);
|
||||||
|
queryWrapper.innerJoin(UserPostDO.class, "userPost", UserPostDO::getUserId, LogInstanceDO::getStartUserId);
|
||||||
|
queryWrapper.innerJoin(DeptDO.class, "dept", DeptDO::getId, LogInstanceDO::getDeptId);
|
||||||
|
queryWrapper.innerJoin(PostDO.class, "post", PostDO::getId, UserPostDO::getPostId);
|
||||||
|
queryWrapper.leftJoin(LogReadDo.class, "e", on -> on
|
||||||
|
.eq(LogReadDo::getLogInstanceId, LogInstanceDO::getId)
|
||||||
|
.eq(LogReadDo::getReadUserId, userId)
|
||||||
|
.eq(LogReadDo::getDeleted, 0));
|
||||||
|
queryWrapper.leftJoin("(SELECT log_instance_id, COUNT(log_instance_id) AS readCount FROM work_log_read where read_status = 1 GROUP BY log_instance_id) c on t.id = c.log_instance_id");
|
||||||
|
queryWrapper.leftJoin("(SELECT log_instance_id, COUNT(log_instance_id) AS unReadCount FROM work_log_read where read_status = 0 GROUP BY log_instance_id) AS d ON t.id = d.log_instance_id");
|
||||||
|
queryWrapper.leftJoin("(SELECT work_log_id, COUNT(work_log_id) AS comment FROM work_log_comment GROUP BY work_log_id) AS b ON t.id = b.work_log_id");
|
||||||
|
queryWrapper.eqIfPresent(LogInstanceDO::getDeptId, reqVO.getDeptId());
|
||||||
|
queryWrapper.eqIfPresent(LogInstanceDO::getFormId, reqVO.getFormId());
|
||||||
|
queryWrapper.eqIfPresent(LogInstanceDO::getStartUserId, reqVO.getStartUserId());
|
||||||
|
queryWrapper.betweenIfPresent(LogInstanceDO::getTime, reqVO.getCreateTime());
|
||||||
|
queryWrapper.inIfPresent(LogInstanceDO::getStartUserId, userIds);
|
||||||
|
|
||||||
|
if (reqVO.getReadStatus() != null) {
|
||||||
|
queryWrapper.eq("e.read_status", reqVO.getReadStatus());
|
||||||
|
}
|
||||||
|
if (reqVO.getIsProduce() != null && reqVO.getIsProduce() == 1) {
|
||||||
|
queryWrapper.like(DeptDO::getFlag, "166");
|
||||||
|
}
|
||||||
|
if (reqVO.getIsProduce() != null && reqVO.getIsProduce() == 2) {
|
||||||
|
queryWrapper.notLike(DeptDO::getFlag, "166");
|
||||||
|
}
|
||||||
|
if (pagingType == 0) {
|
||||||
|
queryWrapper.ne(LogInstanceDO::getStartUserId, userId);
|
||||||
|
}
|
||||||
|
if (pagingType == 1) {
|
||||||
|
queryWrapper.eq(LogInstanceDO::getStartUserId, userId);
|
||||||
|
}
|
||||||
|
queryWrapper.groupBy(LogInstanceDO::getId);
|
||||||
|
queryWrapper.orderByDesc(LogInstanceDO::getTime);
|
||||||
|
queryWrapper.orderByAsc(PostDO::getSort);
|
||||||
|
queryWrapper.orderByDesc(LogInstanceDO::getCreateTime);
|
||||||
|
|
||||||
|
return selectJoinPage(page, LogInstanceRespVO.class, queryWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
default PageResult<LogInstanceDO> selectPage(LogInstancePageReqVO reqVO, Long userId,
|
default PageResult<LogInstanceDO> selectPage(LogInstancePageReqVO reqVO, Long userId,
|
||||||
Integer pagingType, List<Long> userIds) {
|
Integer pagingType, List<Long> userIds) {
|
||||||
|
@ -244,9 +244,6 @@ public class LogInstanceServiceImpl implements LogInstanceService {
|
|||||||
leaderUserIds = adminUserService.getUserByBoss();
|
leaderUserIds = adminUserService.getUserByBoss();
|
||||||
}
|
}
|
||||||
|
|
||||||
// PageResult<LogInstanceDO> pageList = logInstanceMapper.selectPage(pageReqVO, getLoginUserId(), pagingType, leaderUserIds);
|
|
||||||
// List<LogInstanceRespVO> records = logInstanceMapper.selectPageResult(pageReqVO, getLoginUserId(), convertList(pageList.getList(), LogInstanceDO::getId));
|
|
||||||
|
|
||||||
Page<LogInstanceRespVO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
|
Page<LogInstanceRespVO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
|
||||||
IPage<LogInstanceRespVO> pageList = logInstanceMapper.selectPageResult(page, pageReqVO, getLoginUserId(), pagingType, leaderUserIds);
|
IPage<LogInstanceRespVO> pageList = logInstanceMapper.selectPageResult(page, pageReqVO, getLoginUserId(), pagingType, leaderUserIds);
|
||||||
List<LogInstanceRespVO> records = pageList.getRecords();
|
List<LogInstanceRespVO> records = pageList.getRecords();
|
||||||
|
@ -94,54 +94,6 @@
|
|||||||
or result.data_scope = 1 )
|
or result.data_scope = 1 )
|
||||||
</select>
|
</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-->
|
|
||||||
<!-- <if test="ids != null and ids.size > 0">-->
|
|
||||||
<!-- and a.id in-->
|
|
||||||
<!-- <foreach collection="ids" item="ids" open="(" close=")" separator=",">-->
|
|
||||||
<!-- #{ids}-->
|
|
||||||
<!-- </foreach>-->
|
|
||||||
<!-- </if>-->
|
|
||||||
<!-- <if test="ids == null or ids.size == 0">-->
|
|
||||||
<!-- and a.id = null-->
|
|
||||||
<!-- </if>-->
|
|
||||||
<!-- 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.readStatus != null">-->
|
|
||||||
<!-- and e.read_status = #{reqVO.readStatus}-->
|
|
||||||
<!-- </if>-->
|
|
||||||
<!-- </where>-->
|
|
||||||
<!-- GROUP BY a.id-->
|
|
||||||
<!-- ORDER BY-->
|
|
||||||
<!-- a.time DESC,-->
|
|
||||||
<!-- post.sort,-->
|
|
||||||
<!-- a.start_user_id-->
|
|
||||||
<!-- </select>-->
|
|
||||||
|
|
||||||
|
|
||||||
<select id="getNextOrUp"
|
<select id="getNextOrUp"
|
||||||
resultType="cn.iocoder.yudao.module.system.controller.admin.worklog.vo.loginstance.LogInstanceRespVO">
|
resultType="cn.iocoder.yudao.module.system.controller.admin.worklog.vo.loginstance.LogInstanceRespVO">
|
||||||
SELECT
|
SELECT
|
||||||
@ -201,70 +153,70 @@
|
|||||||
limit 1
|
limit 1
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="selectPageResult" resultType="cn.iocoder.yudao.module.system.controller.admin.worklog.vo.loginstance.LogInstanceRespVO">
|
<!-- <select id="selectPageResult" resultType="cn.iocoder.yudao.module.system.controller.admin.worklog.vo.loginstance.LogInstanceRespVO">-->
|
||||||
SELECT
|
<!-- SELECT-->
|
||||||
a.*,
|
<!-- a.*,-->
|
||||||
e.read_status as readStatus,
|
<!-- e.read_status as readStatus,-->
|
||||||
COALESCE(c.readCount, 0) as readCount,
|
<!-- COALESCE(c.readCount, 0) as readCount,-->
|
||||||
COALESCE(d.unreadCount, 0) as unReadCount,
|
<!-- COALESCE(d.unreadCount, 0) as unReadCount,-->
|
||||||
COALESCE(b.comment, 0) as comment
|
<!-- COALESCE(b.comment, 0) as comment-->
|
||||||
FROM
|
<!-- FROM-->
|
||||||
system_user_post userPost,
|
<!-- system_user_post userPost,-->
|
||||||
system_dept dept,
|
<!-- system_dept dept,-->
|
||||||
system_post post,
|
<!-- system_post post,-->
|
||||||
work_log_instance_ext as a
|
<!-- 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 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 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 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
|
<!-- 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>
|
<!-- <where>-->
|
||||||
a.deleted = 0
|
<!-- a.deleted = 0-->
|
||||||
AND a.start_user_id = userPost.user_id
|
<!-- AND a.start_user_id = userPost.user_id-->
|
||||||
AND userPost.post_id = post.id
|
<!-- AND userPost.post_id = post.id-->
|
||||||
AND a.dept_id = dept.id
|
<!-- AND a.dept_id = dept.id-->
|
||||||
<if test="reqVO.isProduce == 1">
|
<!-- <if test="reqVO.isProduce == 1">-->
|
||||||
AND dept.flag LIKE '%166%'
|
<!-- AND dept.flag LIKE '%166%'-->
|
||||||
</if>
|
<!-- </if>-->
|
||||||
<if test="reqVO.isProduce == 2">
|
<!-- <if test="reqVO.isProduce == 2">-->
|
||||||
AND dept.flag NOT LIKE '%166%'
|
<!-- AND dept.flag NOT LIKE '%166%'-->
|
||||||
</if>
|
<!-- </if>-->
|
||||||
<if test="reqVO.formId != null">
|
<!-- <if test="reqVO.formId != null">-->
|
||||||
and a.form_id = #{reqVO.formId}
|
<!-- and a.form_id = #{reqVO.formId}-->
|
||||||
</if>
|
<!-- </if>-->
|
||||||
<if test="reqVO.deptId != null">
|
<!-- <if test="reqVO.deptId != null">-->
|
||||||
and a.dept_id = #{reqVO.deptId}
|
<!-- and a.dept_id = #{reqVO.deptId}-->
|
||||||
</if>
|
<!-- </if>-->
|
||||||
<if test="reqVO.startUserId != null">
|
<!-- <if test="reqVO.startUserId != null">-->
|
||||||
and a.start_user_id = #{reqVO.startUserId}
|
<!-- and a.start_user_id = #{reqVO.startUserId}-->
|
||||||
</if>
|
<!-- </if>-->
|
||||||
<if test="reqVO.createTime != null and reqVO.createTime.length > 0">
|
<!-- <if test="reqVO.createTime != null and reqVO.createTime.length > 0">-->
|
||||||
<if test="reqVO.createTime[0] != null">
|
<!-- <if test="reqVO.createTime[0] != null">-->
|
||||||
and a.time >= #{reqVO.createTime[0]}
|
<!-- and a.time >= #{reqVO.createTime[0]}-->
|
||||||
</if>
|
<!-- </if>-->
|
||||||
<if test="reqVO.createTime[1] != null">
|
<!-- <if test="reqVO.createTime[1] != null">-->
|
||||||
and a.time <= #{reqVO.createTime[1]}
|
<!-- and a.time <= #{reqVO.createTime[1]}-->
|
||||||
</if>
|
<!-- </if>-->
|
||||||
</if>
|
<!-- </if>-->
|
||||||
<if test="reqVO.readStatus != null">
|
<!-- <if test="reqVO.readStatus != null">-->
|
||||||
and e.read_status = #{reqVO.readStatus}
|
<!-- and e.read_status = #{reqVO.readStatus}-->
|
||||||
</if>
|
<!-- </if>-->
|
||||||
<if test="pagingType == 0">
|
<!-- <if test="pagingType == 0">-->
|
||||||
and a.start_user_id != #{userId}
|
<!-- and a.start_user_id != #{userId}-->
|
||||||
</if>
|
<!-- </if>-->
|
||||||
<if test="pagingType == 1">
|
<!-- <if test="pagingType == 1">-->
|
||||||
and a.start_user_id = #{userId}
|
<!-- and a.start_user_id = #{userId}-->
|
||||||
</if>
|
<!-- </if>-->
|
||||||
<if test="reqVO.isBoss != null and reqVO.isProduce == null">
|
<!-- <if test="reqVO.isBoss != null and reqVO.isProduce == null">-->
|
||||||
and a.start_user_id in
|
<!-- and a.start_user_id in-->
|
||||||
<foreach collection="userIds" item="userId" open="(" close=")" separator=",">
|
<!-- <foreach collection="userIds" item="userId" open="(" close=")" separator=",">-->
|
||||||
#{userId}
|
<!-- #{userId}-->
|
||||||
</foreach>
|
<!-- </foreach>-->
|
||||||
</if>
|
<!-- </if>-->
|
||||||
</where>
|
<!-- </where>-->
|
||||||
GROUP BY a.id
|
<!-- GROUP BY a.id-->
|
||||||
ORDER BY
|
<!-- ORDER BY-->
|
||||||
a.time DESC,
|
<!-- a.time DESC,-->
|
||||||
post.sort,
|
<!-- post.sort,-->
|
||||||
a.create_time DESC
|
<!-- a.create_time DESC-->
|
||||||
</select>
|
<!-- </select>-->
|
||||||
</mapper>
|
</mapper>
|
Loading…
Reference in New Issue
Block a user