From 9d8b9fabbea813e980d7f8e67ee633a55d205955 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子Li <15040126243@163.com>
Date: 星期四, 16 一月 2025 11:50:19 +0800
Subject: [PATCH] update 优化 ws模块替换session的时候关闭session连接
---
ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysDeptServiceImpl.java | 96 ++++++++++++++++++++++++++++++++++++-----------
1 files changed, 73 insertions(+), 23 deletions(-)
diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysDeptServiceImpl.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysDeptServiceImpl.java
index 86edf59..ca062be 100644
--- a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysDeptServiceImpl.java
+++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysDeptServiceImpl.java
@@ -1,5 +1,6 @@
package org.dromara.system.service.impl;
+import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.tree.Tree;
@@ -9,13 +10,11 @@
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.constant.CacheNames;
-import org.dromara.common.core.constant.UserConstants;
+import org.dromara.common.core.constant.SystemConstants;
+import org.dromara.common.core.domain.dto.DeptDTO;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.service.DeptService;
-import org.dromara.common.core.utils.MapstructUtils;
-import org.dromara.common.core.utils.SpringUtils;
-import org.dromara.common.core.utils.StringUtils;
-import org.dromara.common.core.utils.TreeBuildUtils;
+import org.dromara.common.core.utils.*;
import org.dromara.common.mybatis.helper.DataBaseHelper;
import org.dromara.common.redis.utils.CacheUtils;
import org.dromara.common.satoken.utils.LoginHelper;
@@ -30,7 +29,9 @@
import org.dromara.system.service.ISysDeptService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
+import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
@@ -69,8 +70,6 @@
*/
@Override
public List<Tree<Long>> selectDeptTreeList(SysDeptBo bo) {
- // 鍙煡璇㈡湭绂佺敤閮ㄩ棬
- bo.setStatus(UserConstants.DEPT_NORMAL);
LambdaQueryWrapper<SysDept> lqw = buildQueryWrapper(bo);
List<SysDeptVo> depts = baseMapper.selectDeptList(lqw);
return buildDeptTreeSelect(depts);
@@ -78,10 +77,11 @@
private LambdaQueryWrapper<SysDept> buildQueryWrapper(SysDeptBo bo) {
LambdaQueryWrapper<SysDept> lqw = Wrappers.lambdaQuery();
- lqw.eq(SysDept::getDelFlag, "0");
+ lqw.eq(SysDept::getDelFlag, SystemConstants.NORMAL);
lqw.eq(ObjectUtil.isNotNull(bo.getDeptId()), SysDept::getDeptId, bo.getDeptId());
lqw.eq(ObjectUtil.isNotNull(bo.getParentId()), SysDept::getParentId, bo.getParentId());
lqw.like(StringUtils.isNotBlank(bo.getDeptName()), SysDept::getDeptName, bo.getDeptName());
+ lqw.like(StringUtils.isNotBlank(bo.getDeptCategory()), SysDept::getDeptCategory, bo.getDeptCategory());
lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysDept::getStatus, bo.getStatus());
lqw.orderByAsc(SysDept::getAncestors);
lqw.orderByAsc(SysDept::getParentId);
@@ -101,11 +101,23 @@
if (CollUtil.isEmpty(depts)) {
return CollUtil.newArrayList();
}
- return TreeBuildUtils.build(depts, (dept, tree) ->
- tree.setId(dept.getDeptId())
- .setParentId(dept.getParentId())
- .setName(dept.getDeptName())
- .setWeight(dept.getOrderNum()));
+ // 鑾峰彇褰撳墠鍒楄〃涓瘡涓�涓妭鐐圭殑parentId锛岀劧鍚庡湪鍒楄〃涓煡鎵炬槸鍚︽湁id涓庡叾parentId瀵瑰簲锛岃嫢鏃犲搴旓紝鍒欒〃鏄庢鏃惰妭鐐瑰垪琛ㄤ腑锛岃鑺傜偣鍦ㄥ綋鍓嶅垪琛ㄤ腑灞炰簬椤剁骇鑺傜偣
+ List<Tree<Long>> treeList = CollUtil.newArrayList();
+ for (SysDeptVo d : depts) {
+ Long parentId = d.getParentId();
+ SysDeptVo sysDeptVo = StreamUtils.findFirst(depts, it -> it.getDeptId().longValue() == parentId);
+ if (ObjectUtil.isNull(sysDeptVo)) {
+ List<Tree<Long>> trees = TreeBuildUtils.build(depts, parentId, (dept, tree) ->
+ tree.setId(dept.getDeptId())
+ .setParentId(dept.getParentId())
+ .setName(dept.getDeptName())
+ .setWeight(dept.getOrderNum())
+ .putExtra("disabled", SystemConstants.DISABLE.equals(dept.getStatus())));
+ Tree<Long> tree = StreamUtils.findFirst(trees, it -> it.getId().longValue() == d.getDeptId());
+ treeList.add(tree);
+ }
+ }
+ return treeList;
}
/**
@@ -135,7 +147,7 @@
}
SysDeptVo parentDept = baseMapper.selectVoOne(new LambdaQueryWrapper<SysDept>()
.select(SysDept::getDeptName).eq(SysDept::getDeptId, dept.getParentId()));
- dept.setParentName(ObjectUtil.isNotNull(parentDept) ? parentDept.getDeptName() : null);
+ dept.setParentName(ObjectUtils.notNullGetter(parentDept, SysDeptVo::getDeptName));
return dept;
}
@@ -143,7 +155,7 @@
public List<SysDeptVo> selectDeptByIds(List<Long> deptIds) {
return baseMapper.selectDeptList(new LambdaQueryWrapper<SysDept>()
.select(SysDept::getDeptId, SysDept::getDeptName, SysDept::getLeader)
- .eq(SysDept::getStatus, UserConstants.DEPT_NORMAL)
+ .eq(SysDept::getStatus, SystemConstants.NORMAL)
.in(CollUtil.isNotEmpty(deptIds), SysDept::getDeptId, deptIds));
}
@@ -166,6 +178,31 @@
}
/**
+ * 鏍规嵁閮ㄩ棬ID鏌ヨ閮ㄩ棬璐熻矗浜�
+ *
+ * @param deptId 閮ㄩ棬ID锛岀敤浜庢寚瀹氶渶瑕佹煡璇㈢殑閮ㄩ棬
+ * @return 杩斿洖璇ラ儴闂ㄧ殑璐熻矗浜篒D
+ */
+ @Override
+ public Long selectDeptLeaderById(Long deptId) {
+ SysDeptVo vo = SpringUtils.getAopProxy(this).selectDeptById(deptId);
+ return vo.getLeader();
+ }
+
+ /**
+ * 鏌ヨ閮ㄩ棬
+ *
+ * @return 閮ㄩ棬鍒楄〃
+ */
+ @Override
+ public List<DeptDTO> selectDeptsByList() {
+ List<SysDeptVo> list = baseMapper.selectDeptList(new LambdaQueryWrapper<SysDept>()
+ .select(SysDept::getDeptId, SysDept::getDeptName, SysDept::getParentId)
+ .eq(SysDept::getStatus, SystemConstants.NORMAL));
+ return BeanUtil.copyToList(list, DeptDTO.class);
+ }
+
+ /**
* 鏍规嵁ID鏌ヨ鎵�鏈夊瓙閮ㄩ棬鏁帮紙姝e父鐘舵�侊級
*
* @param deptId 閮ㄩ棬ID
@@ -174,7 +211,7 @@
@Override
public long selectNormalChildrenDeptById(Long deptId) {
return baseMapper.selectCount(new LambdaQueryWrapper<SysDept>()
- .eq(SysDept::getStatus, UserConstants.DEPT_NORMAL)
+ .eq(SysDept::getStatus, SystemConstants.NORMAL)
.apply(DataBaseHelper.findInSet(deptId, "ancestors")));
}
@@ -241,11 +278,12 @@
* @param bo 閮ㄩ棬淇℃伅
* @return 缁撴灉
*/
+ @CacheEvict(cacheNames = CacheNames.SYS_DEPT_AND_CHILD, allEntries = true)
@Override
public int insertDept(SysDeptBo bo) {
SysDept info = baseMapper.selectById(bo.getParentId());
// 濡傛灉鐖惰妭鐐逛笉涓烘甯哥姸鎬�,鍒欎笉鍏佽鏂板瀛愯妭鐐�
- if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) {
+ if (!SystemConstants.NORMAL.equals(info.getStatus())) {
throw new ServiceException("閮ㄩ棬鍋滅敤锛屼笉鍏佽鏂板");
}
SysDept dept = MapstructUtils.convert(bo, SysDept.class);
@@ -259,25 +297,34 @@
* @param bo 閮ㄩ棬淇℃伅
* @return 缁撴灉
*/
- @CacheEvict(cacheNames = CacheNames.SYS_DEPT, key = "#bo.deptId")
+ @Caching(evict = {
+ @CacheEvict(cacheNames = CacheNames.SYS_DEPT, key = "#bo.deptId"),
+ @CacheEvict(cacheNames = CacheNames.SYS_DEPT_AND_CHILD, allEntries = true)
+ })
@Override
+ @Transactional(rollbackFor = Exception.class)
public int updateDept(SysDeptBo bo) {
SysDept dept = MapstructUtils.convert(bo, SysDept.class);
SysDept oldDept = baseMapper.selectById(dept.getDeptId());
+ if (ObjectUtil.isNull(oldDept)) {
+ throw new ServiceException("閮ㄩ棬涓嶅瓨鍦紝鏃犳硶淇敼");
+ }
if (!oldDept.getParentId().equals(dept.getParentId())) {
// 濡傛灉鏄柊鐖堕儴闂� 鍒欐牎楠屾槸鍚﹀叿鏈夋柊鐖堕儴闂ㄦ潈闄� 閬垮厤瓒婃潈
this.checkDeptDataScope(dept.getParentId());
SysDept newParentDept = baseMapper.selectById(dept.getParentId());
- if (ObjectUtil.isNotNull(newParentDept) && ObjectUtil.isNotNull(oldDept)) {
+ if (ObjectUtil.isNotNull(newParentDept)) {
String newAncestors = newParentDept.getAncestors() + StringUtils.SEPARATOR + newParentDept.getDeptId();
String oldAncestors = oldDept.getAncestors();
dept.setAncestors(newAncestors);
updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
}
+ } else {
+ dept.setAncestors(oldDept.getAncestors());
}
int result = baseMapper.updateById(dept);
- if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors())
- && !StringUtils.equals(UserConstants.DEPT_NORMAL, dept.getAncestors())) {
+ if (SystemConstants.NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors())
+ && !StringUtils.equals(SystemConstants.NORMAL, dept.getAncestors())) {
// 濡傛灉璇ラ儴闂ㄦ槸鍚敤鐘舵�侊紝鍒欏惎鐢ㄨ閮ㄩ棬鐨勬墍鏈変笂绾ч儴闂�
updateParentDeptStatusNormal(dept);
}
@@ -293,7 +340,7 @@
String ancestors = dept.getAncestors();
Long[] deptIds = Convert.toLongArray(ancestors);
baseMapper.update(null, new LambdaUpdateWrapper<SysDept>()
- .set(SysDept::getStatus, UserConstants.DEPT_NORMAL)
+ .set(SysDept::getStatus, SystemConstants.NORMAL)
.in(SysDept::getDeptId, Arrays.asList(deptIds)));
}
@@ -327,7 +374,10 @@
* @param deptId 閮ㄩ棬ID
* @return 缁撴灉
*/
- @CacheEvict(cacheNames = CacheNames.SYS_DEPT, key = "#deptId")
+ @Caching(evict = {
+ @CacheEvict(cacheNames = CacheNames.SYS_DEPT, key = "#deptId"),
+ @CacheEvict(cacheNames = CacheNames.SYS_DEPT_AND_CHILD, key = "#deptId")
+ })
@Override
public int deleteDeptById(Long deptId) {
return baseMapper.deleteById(deptId);
--
Gitblit v1.9.3