From c5b9d7129cf66cb2427772b13d82324bfa8892bf Mon Sep 17 00:00:00 2001 From: caihaohan Date: Wed, 17 Apr 2024 11:30:08 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=B8=B4=E6=97=B6=E6=9D=83=E9=99=90?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E6=80=A7=E8=83=BD=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EnterpriseStandardAuthDeptMapper.java | 6 ++ .../EnterpriseStandardAuthUserMapper.java | 7 +- .../xml/EnterpriseStandardAuthDeptMapper.xml | 62 +++++++++++++++ .../xml/EnterpriseStandardAuthUserMapper.xml | 78 ++++++++++--------- ...EnterpriseStandardAuthUserServiceImpl.java | 67 +++++++++++++++- .../controller/LawsEnterpriseController.java | 3 + .../com/jero/JeroSystemSingleApplication.java | 2 + 7 files changed, 184 insertions(+), 41 deletions(-) diff --git a/laws-modules/src/main/java/com/jero/modules/laws/enterprise/mapper/EnterpriseStandardAuthDeptMapper.java b/laws-modules/src/main/java/com/jero/modules/laws/enterprise/mapper/EnterpriseStandardAuthDeptMapper.java index ab65481a..49962c9b 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/enterprise/mapper/EnterpriseStandardAuthDeptMapper.java +++ b/laws-modules/src/main/java/com/jero/modules/laws/enterprise/mapper/EnterpriseStandardAuthDeptMapper.java @@ -2,6 +2,8 @@ package com.jero.modules.laws.enterprise.mapper; import com.jero.modules.laws.enterprise.entity.EnterpriseStandardAuthDept; import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.jero.modules.laws.standard.controller.TempAuthRecordQueryDto; +import org.apache.ibatis.annotations.Param; import java.util.List; @@ -15,6 +17,10 @@ public interface EnterpriseStandardAuthDeptMapper extends BaseMapper list); + int countDeptAuths(@Param("param") TempAuthRecordQueryDto tempAuthRecordQueryDto); + + List queryDeptAuths(@Param("param") TempAuthRecordQueryDto tempAuthRecordQueryDto, + int pageSize, int offset); } diff --git a/laws-modules/src/main/java/com/jero/modules/laws/enterprise/mapper/EnterpriseStandardAuthUserMapper.java b/laws-modules/src/main/java/com/jero/modules/laws/enterprise/mapper/EnterpriseStandardAuthUserMapper.java index 7608195e..0805b70c 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/enterprise/mapper/EnterpriseStandardAuthUserMapper.java +++ b/laws-modules/src/main/java/com/jero/modules/laws/enterprise/mapper/EnterpriseStandardAuthUserMapper.java @@ -6,6 +6,8 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.jero.modules.laws.standard.controller.TempAuthRecordQueryDto; import org.apache.ibatis.annotations.Param; +import java.util.List; + /** * @author ThinkBook * @description 针对表【laws_enterprise_standard_temp_permission(企业标准)】的数据库操作Mapper @@ -14,7 +16,10 @@ import org.apache.ibatis.annotations.Param; */ public interface EnterpriseStandardAuthUserMapper extends BaseMapper { - IPage tempAuthRecord(IPage page, @Param("param") TempAuthRecordQueryDto tempAuthRecordQueryDto); + int countUserAuths(@Param("param") TempAuthRecordQueryDto tempAuthRecordQueryDto); + + List queryUserAuths(@Param("param") TempAuthRecordQueryDto tempAuthRecordQueryDto, + int pageSize, int offset); } diff --git a/laws-modules/src/main/java/com/jero/modules/laws/enterprise/mapper/xml/EnterpriseStandardAuthDeptMapper.xml b/laws-modules/src/main/java/com/jero/modules/laws/enterprise/mapper/xml/EnterpriseStandardAuthDeptMapper.xml index 5b25cd61..ce4ecfed 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/enterprise/mapper/xml/EnterpriseStandardAuthDeptMapper.xml +++ b/laws-modules/src/main/java/com/jero/modules/laws/enterprise/mapper/xml/EnterpriseStandardAuthDeptMapper.xml @@ -29,4 +29,66 @@ (#{item.id}, #{item.standardId}, #{item.authDept}) + + + + + + diff --git a/laws-modules/src/main/java/com/jero/modules/laws/enterprise/mapper/xml/EnterpriseStandardAuthUserMapper.xml b/laws-modules/src/main/java/com/jero/modules/laws/enterprise/mapper/xml/EnterpriseStandardAuthUserMapper.xml index 16e6b774..b49fc148 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/enterprise/mapper/xml/EnterpriseStandardAuthUserMapper.xml +++ b/laws-modules/src/main/java/com/jero/modules/laws/enterprise/mapper/xml/EnterpriseStandardAuthUserMapper.xml @@ -22,48 +22,28 @@ standard_id,auth_user,auth_expire_date - SELECT auth.id AS Id, auth.auth_user AS authUser, - auth.auth_dept AS authDept, auth.create_time AS createTime, auth.standard_id AS standardId, - es.standard_number AS standardNumber, auth.auth_expire_date AS authExpireDate - FROM - ( - SELECT - id, - NULL AS auth_user, - auth_dept, - standard_id, - auth_expire_date, - create_time - FROM - laws_enterprise_standard_auth_dept - WHERE - auth_expire_date IS NOT NULL - UNION - SELECT - id, - auth_user, - NULL AS auth_dept, - standard_id, - auth_expire_date, - create_time - FROM - laws_enterprise_standard_auth_user - ) AS auth - LEFT JOIN laws_enterprise_standard AS es ON auth.standard_id = es.id - LEFT JOIN sys_user AS u ON u.id = auth.auth_user + FROM (select * from laws_enterprise_standard_auth_user WHERE auth_expire_date IS NOT NULL) auth + + LEFT JOIN laws_enterprise_standard AS es ON auth.standard_id = es.id + + + + LEFT JOIN sys_user AS u ON u.id = auth.auth_user + WHERE - es.del_flag = 0 + auth.auth_expire_date IS NOT NULL AND es.standard_number LIKE CONCAT('%',#{param.standardNumber},'%') - + AND CONCAT(u.realname, '(', u.username, ')') LIKE CONCAT('%',#{param.authUser},'%') @@ -72,16 +52,38 @@ AND auth.auth_expire_date >= #{param.startAuthExpireDate} AND auth.auth_expire_date <= #{param.endAuthExpireDate} - - - AND auth.auth_dept IN - - #{item} - - ORDER BY auth.create_time DESC, auth.auth_expire_date DESC + LIMIT #{pageSize} OFFSET #{offset} + + + + diff --git a/laws-modules/src/main/java/com/jero/modules/laws/enterprise/service/impl/EnterpriseStandardAuthUserServiceImpl.java b/laws-modules/src/main/java/com/jero/modules/laws/enterprise/service/impl/EnterpriseStandardAuthUserServiceImpl.java index a0c41ab0..c3c13106 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/enterprise/service/impl/EnterpriseStandardAuthUserServiceImpl.java +++ b/laws-modules/src/main/java/com/jero/modules/laws/enterprise/service/impl/EnterpriseStandardAuthUserServiceImpl.java @@ -14,6 +14,7 @@ import com.jero.modules.laws.common.constant.FieldCommon; import com.jero.modules.laws.common.vo.ManyStandardSelectionBox; import com.jero.modules.laws.enterprise.entity.EnterpriseStandardAuthDept; import com.jero.modules.laws.enterprise.entity.EnterpriseStandardAuthUser; +import com.jero.modules.laws.enterprise.mapper.EnterpriseStandardAuthDeptMapper; import com.jero.modules.laws.enterprise.mapper.EnterpriseStandardAuthUserMapper; import com.jero.modules.laws.enterprise.service.EnterpriseStandardAuthDeptService; import com.jero.modules.laws.enterprise.service.EnterpriseStandardAuthUserService; @@ -37,6 +38,7 @@ import com.jero.modules.system.service.ISysUserService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -84,6 +86,9 @@ public class EnterpriseStandardAuthUserServiceImpl extends ServiceImpl adminRoleCodeList; @@ -788,9 +793,67 @@ public class EnterpriseStandardAuthUserServiceImpl extends ServiceImpl tempAuthRecord(TempAuthRecordQueryDto tempAuthRecordQueryDto, Integer pageNo, Integer pageSize) { - IPage page = new Page<>(pageNo, pageSize); - return authUserMapper.tempAuthRecord(page, tempAuthRecordQueryDto); + List authDeptList = tempAuthRecordQueryDto.getAuthDeptList(); + String authUser = tempAuthRecordQueryDto.getAuthUser(); + int totalDeptAuths = 0; + int totalUserAuths = 0; + // 没有授权人员筛选的时候 + if (StrUtil.isBlank(authUser)) { + totalDeptAuths = authDeptMapper.countDeptAuths(tempAuthRecordQueryDto); + } + // 授权部门和授权人员都没有查询条件的时候 + if (authDeptList == null || authDeptList.isEmpty()) { + totalUserAuths = authUserMapper.countUserAuths(tempAuthRecordQueryDto); + } + int totalRecords = totalDeptAuths + totalUserAuths; + + // 分页查询 + List results = new ArrayList<>(); + if (totalRecords > 0) { + // 计算offset和实际pageSize + int offset = (pageNo - 1) * pageSize; + + // 首先查询auth_dept数据 + List deptAuths = new ArrayList<>(); + if (totalDeptAuths > 0) { + deptAuths = authDeptMapper.queryDeptAuths(tempAuthRecordQueryDto, pageSize, offset); + } + // 如果deptAuths的数量少于pageSize,则查询auth_user填充剩余空间 + int remainingSize = pageSize - deptAuths.size(); + List userAuths = new ArrayList<>(); + if (remainingSize > 0 && deptAuths.size() <= totalDeptAuths && totalUserAuths > 0) { + // 计算auth_user的offset + int userOffset = Math.max(0, offset - totalDeptAuths); + userAuths = authUserMapper.queryUserAuths(tempAuthRecordQueryDto, remainingSize, userOffset); + } + + results.addAll(deptAuths); + results.addAll(userAuths); + } + + // 手动翻译标准编号 + if (!results.isEmpty()) { + Set standardIdSet = results.stream().map(TempAuthRecordQueryDto::getStandardId).collect(Collectors.toSet()); + LambdaQueryWrapper esWrapper = new LambdaQueryWrapper<>(); + esWrapper.in(LawsEnterpriseStandard::getId, standardIdSet); + List esList = esService.list(esWrapper); + Map esStandardMap = esList.stream().collect( + Collectors.toMap(LawsEnterpriseStandard::getId, LawsEnterpriseStandard::getStandardNumber)); + results.forEach(r -> { + r.setStandardNumber(esStandardMap.get(r.getStandardId())); + }); + } + + // 构建分页结果 + Page page = new Page<>(); + page.setRecords(results); + page.setCurrent(pageNo); + page.setSize(pageSize); + page.setTotal(totalRecords); + + return page; } @Override diff --git a/laws-modules/src/main/java/com/jero/modules/laws/standard/controller/LawsEnterpriseController.java b/laws-modules/src/main/java/com/jero/modules/laws/standard/controller/LawsEnterpriseController.java index 28f2c957..d17d3893 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/standard/controller/LawsEnterpriseController.java +++ b/laws-modules/src/main/java/com/jero/modules/laws/standard/controller/LawsEnterpriseController.java @@ -33,6 +33,7 @@ import com.jero.modules.tag.enums.TableNameEnum; import io.swagger.annotations.*; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.CacheEvict; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @@ -259,6 +260,7 @@ public class LawsEnterpriseController { @PostMapping(value = "/tempPermission") @ApiOperationSupport(order = 13) @RequiresPermissions("enterpriseStandard:temporaryPermissSet") + @CacheEvict(value = "tempAuthRecord", allEntries = true) public Result tempPermission(@RequestBody LawsStandardTempPermission lawsStandardTempPermission) { String userIds = lawsStandardTempPermission.getUserIds(); String deptIds = lawsStandardTempPermission.getDeptIds(); @@ -385,6 +387,7 @@ public class LawsEnterpriseController { @GetMapping(value = "/revokeAuth") @ApiOperationSupport(order = 22) @RequiresPermissions("enterpriseStandard:temporaryPermissRecord") + @CacheEvict(value = "tempAuthRecord", allEntries = true) public Result revokeAuth(@RequestParam(name="id", required = false) @ApiParam("Id") String authId) { esAuthUserService.revokeAuth(authId); return Result.OK(); diff --git a/laws-single-startup/src/main/java/com/jero/JeroSystemSingleApplication.java b/laws-single-startup/src/main/java/com/jero/JeroSystemSingleApplication.java index a76291be..35e15064 100644 --- a/laws-single-startup/src/main/java/com/jero/JeroSystemSingleApplication.java +++ b/laws-single-startup/src/main/java/com/jero/JeroSystemSingleApplication.java @@ -9,6 +9,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.Environment; import org.springframework.scheduling.annotation.EnableScheduling; @@ -23,6 +24,7 @@ import java.net.UnknownHostException; **/ @Slf4j @EnableScheduling +@EnableCaching @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class, SecurityAutoConfiguration.class,