From 515e2b239c106b754b8014d7c0d840e53ba6c826 Mon Sep 17 00:00:00 2001 From: memorylkf <312904636@qq.com> Date: Tue, 3 Feb 2026 20:49:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20[=E6=93=8D=E4=BD=9C=E6=97=A5=E5=BF=97]?= =?UTF-8?q?=20=E8=A7=92=E8=89=B2=E6=93=8D=E4=BD=9C=E6=97=A5=E5=BF=97?= =?UTF-8?q?=EF=BC=8C=E5=BE=85=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/hxhq/system/api/domain/SysRole.java | 18 +++ .../java/com/hxhq/system/mapper/SysMenuMapper.java | 7 ++ .../system/service/impl/SysMenuServiceImpl.java | 6 + .../system/service/impl/SysRoleServiceImpl.java | 132 +++++++++++++++++++-- .../main/resources/mapper/system/SysMenuMapper.xml | 6 + 5 files changed, 161 insertions(+), 8 deletions(-) diff --git a/hxhq-api/hxhq-api-system/src/main/java/com/hxhq/system/api/domain/SysRole.java b/hxhq-api/hxhq-api-system/src/main/java/com/hxhq/system/api/domain/SysRole.java index 53a25c0..0ae342c 100644 --- a/hxhq-api/hxhq-api-system/src/main/java/com/hxhq/system/api/domain/SysRole.java +++ b/hxhq-api/hxhq-api-system/src/main/java/com/hxhq/system/api/domain/SysRole.java @@ -4,6 +4,8 @@ import java.util.Set; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; + +import com.hxhq.common.core.annotation.Compare; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import com.hxhq.common.core.annotation.Excel; @@ -25,10 +27,12 @@ public class SysRole extends BaseEntity /** 角色名称 */ @Excel(name = "角色名称") + @Compare(name = "角色名称",nameEn = "Role Name") private String roleName; /** 角色权限 */ @Excel(name = "角色权限") + @Compare(name = "角色编码",nameEn = "Role ID") private String roleKey; /** 角色排序 */ @@ -64,6 +68,10 @@ public class SysRole extends BaseEntity /** 角色菜单权限 */ private Set permissions; + /** 备注 */ + @Compare(name = "备注",nameEn = "Role Description") + private String remark; + public SysRole() { @@ -238,4 +246,14 @@ public class SysRole extends BaseEntity .append("remark", getRemark()) .toString(); } + + @Override + public String getRemark() { + return remark; + } + + @Override + public void setRemark(String remark) { + this.remark = remark; + } } diff --git a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/mapper/SysMenuMapper.java b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/mapper/SysMenuMapper.java index e2bc1fa..eca5da4 100644 --- a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/mapper/SysMenuMapper.java +++ b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/mapper/SysMenuMapper.java @@ -122,4 +122,11 @@ public interface SysMenuMapper * @return 结果 */ public SysMenu checkMenuNameUnique(@Param("menuName") String menuName, @Param("parentId") Long parentId); + + /** + * 获取角色下的所有菜单 + * @param roleId + * @return + */ + List selectMenuByRoleId(@Param("roleId") Long roleId); } diff --git a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/service/impl/SysMenuServiceImpl.java b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/service/impl/SysMenuServiceImpl.java index 208b26b..cfdffcb 100644 --- a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/service/impl/SysMenuServiceImpl.java +++ b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/service/impl/SysMenuServiceImpl.java @@ -560,6 +560,12 @@ public class SysMenuServiceImpl implements ISysMenuService new String[] { "", "", "", "/", "/" }); } + /** + * 获取修改日志 + * @param info + * @param old + * @return + */ private List getModifyLogList(SysMenu info,SysMenu old){ List list = new ArrayList<>(); diff --git a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/service/impl/SysRoleServiceImpl.java b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/service/impl/SysRoleServiceImpl.java index d86601e..9fe922c 100644 --- a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/service/impl/SysRoleServiceImpl.java +++ b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/service/impl/SysRoleServiceImpl.java @@ -5,14 +5,16 @@ import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; +import com.hxhq.business.domain.SystemLog; +import com.hxhq.business.service.ISystemLogService; +import com.hxhq.business.utils.ObjectCompareUtil; +import com.hxhq.system.domain.SysMenu; import com.hxhq.system.domain.SysRoleDept; import com.hxhq.system.domain.SysRoleMenu; import com.hxhq.system.domain.SysUserRole; -import com.hxhq.system.mapper.SysRoleDeptMapper; -import com.hxhq.system.mapper.SysRoleMapper; -import com.hxhq.system.mapper.SysRoleMenuMapper; -import com.hxhq.system.mapper.SysUserRoleMapper; +import com.hxhq.system.mapper.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -46,6 +48,12 @@ public class SysRoleServiceImpl implements ISysRoleService @Autowired private SysRoleDeptMapper roleDeptMapper; + @Autowired + private SysMenuMapper menuMapper; + + @Autowired + private ISystemLogService systemLogService; + /** * 根据条件分页查询角色数据 * @@ -237,7 +245,9 @@ public class SysRoleServiceImpl implements ISysRoleService { // 新增角色信息 roleMapper.insertRole(role); - return insertRoleMenu(role); + int row = insertRoleMenu(role); + systemLogService.saveInfo("新增角色","Add Role",role.getRoleName(),role.getRoleName(),SecurityUtils.getUserId(),SecurityUtils.getNickName(),SecurityUtils.getUsername(),null); + return row; } /** @@ -250,11 +260,21 @@ public class SysRoleServiceImpl implements ISysRoleService @Transactional(rollbackFor = Exception.class) public int updateRole(SysRole role) { + SysRole old = selectRoleById(role.getRoleId()); + List oldMenuList = menuMapper.selectMenuByRoleId(role.getRoleId()); + // 修改角色信息 roleMapper.updateRole(role); // 删除角色与菜单关联 roleMenuMapper.deleteRoleMenuByRoleId(role.getRoleId()); - return insertRoleMenu(role); + int row = insertRoleMenu(role); + + List logList = getModifyLogList(role,old,oldMenuList); + if(logList.size()>0){ + systemLogService.saveBatch(logList); + } + + return row; } /** @@ -266,7 +286,10 @@ public class SysRoleServiceImpl implements ISysRoleService @Override public int updateRoleStatus(SysRole role) { - return roleMapper.updateRole(role); + SysRole info = selectRoleById(role.getRoleId()); + int row = roleMapper.updateRole(role); + systemLogService.saveInfo(role.getStatus().equals("0")?"启用角色":"禁用角色",role.getStatus().equals("0")?"Enable Role":"Disable Role",info.getRoleName(),info.getRoleName(),SecurityUtils.getUserId(),SecurityUtils.getNickName(),SecurityUtils.getUsername(),null); + return row; } /** @@ -345,11 +368,17 @@ public class SysRoleServiceImpl implements ISysRoleService @Transactional(rollbackFor = Exception.class) public int deleteRoleById(Long roleId) { + SysRole sysRole = selectRoleById(roleId); + // 删除角色与菜单关联 roleMenuMapper.deleteRoleMenuByRoleId(roleId); // 删除角色与部门关联 roleDeptMapper.deleteRoleDeptByRoleId(roleId); - return roleMapper.deleteRoleById(roleId); + + int row = roleMapper.deleteRoleById(roleId); + + systemLogService.saveInfo("删除角色","Remove Role",sysRole.getRoleName(),sysRole.getRoleName(),SecurityUtils.getUserId(),SecurityUtils.getNickName(),SecurityUtils.getUsername(),null); + return row; } /** @@ -425,4 +454,91 @@ public class SysRoleServiceImpl implements ISysRoleService } return userRoleMapper.batchUserRole(list); } + + /** + * 获取修改日志 + * @param info + * @param old + * @param oldMenuList + * @return + */ + private List getModifyLogList(SysRole info, SysRole old,List oldMenuList){ + List list = new ArrayList<>(); + + List fieldChanges = ObjectCompareUtil.compareObjects(old, info); + for (ObjectCompareUtil.FieldChange fieldChange : fieldChanges) { + SystemLog log = getLogInfo(); + log.setJcnr(fieldChange.toString()); + log.setJcnrEn(fieldChange.toEnString()); + list.add(log); + } + if(!info.getStatus().equals(old.getStatus())){ + SystemLog log = getLogInfo(); + log.setJcnr("[{\"name\":\"字段名\",\"value\":\"状态\"},{\"name\":\"原值\",\"value\":\""+("0".equals(old.getStatus())?"启用":"禁用")+"\"},{\"name\":\"新值\",\"value\":\""+("0".equals(info.getStatus())?"启用":"禁用")+"\"}]"); + log.setJcnrEn("[{\"name\":\"Field\",\"value\":\"Status\"},{\"name\":\"Old Value\",\"value\":\""+("0".equals(old.getStatus())?"Enabled":"Disabled")+"\"},{\"name\":\"New Value\",\"value\":\""+("0".equals(info.getStatus())?"Enabled":"Disabled")+"\"}]"); + list.add(log); + } + + //判断菜单修改 + Boolean updateMenu = false; + if(info.getMenuIds().length!=oldMenuList.size()){ + updateMenu = true; + }else{ + List oldMenuIdList = oldMenuList.stream().map(o->o.getMenuId()).collect(Collectors.toList()); + for(Long newMenuId : info.getMenuIds()){ + if(!oldMenuIdList.contains(newMenuId)){ + updateMenu = true; + break; + } + } + } + if(updateMenu){ + List newMenuList = new ArrayList<>(); + if(info.getMenuIds()!=null && info.getMenuIds().length>0){ + List allMenuList = menuMapper.selectMenuList(new SysMenu()); + for(Long newMenuId : info.getMenuIds()){ + newMenuList.add(allMenuList.stream().filter(o->o.getMenuId().equals(newMenuId)).collect(Collectors.toList()).get(0)); + } + } + + List addNameList = new ArrayList<>(); + List deleteNameList = new ArrayList<>(); + List oldMenuIdList = oldMenuList.stream().map(o->o.getMenuId()).collect(Collectors.toList()); + List newMenuIdList = newMenuList.stream().map(o->o.getMenuId()).collect(Collectors.toList()); + for(SysMenu newMenu : newMenuList){ + if(!oldMenuIdList.contains(newMenu.getMenuId())){ + addNameList.add(newMenu.getMenuName()); + } + } + + for(SysMenu oldMenu : oldMenuList){ + if(!newMenuIdList.contains(oldMenu.getMenuId())){ + deleteNameList.add(oldMenu.getMenuName()); + } + } + + if(addNameList.size()>0 || deleteNameList.size()>0){ + SystemLog log = getLogInfo(); + log.setJcnr((addNameList.size()>0?("新增权限:"+String.join(",",addNameList)):"")+(deleteNameList.size()>0?(";删除权限:"+String.join(",",deleteNameList)):"")); + log.setJcnrEn((addNameList.size()>0?("Add Permit:"+String.join(",",addNameList)):"")+(deleteNameList.size()>0?(";Remove Permit:"+String.join(",",deleteNameList)):"")); + list.add(log); + } + } + + return list; + } + + /** + * 获取日志基础信息 + * @return + */ + private SystemLog getLogInfo(){ + SystemLog log = new SystemLog(); + log.setJcmc("修改角色"); + log.setJcmcEn("Modify Role"); + log.setQmrId(SecurityUtils.getUserId()); + log.setQmrMc(SecurityUtils.getNickName()); + log.setQmrMcEn(SecurityUtils.getUsername()); + return log; + } } diff --git a/hxhq-modules/hxhq-system/src/main/resources/mapper/system/SysMenuMapper.xml b/hxhq-modules/hxhq-system/src/main/resources/mapper/system/SysMenuMapper.xml index a6c4800..5167bf5 100644 --- a/hxhq-modules/hxhq-system/src/main/resources/mapper/system/SysMenuMapper.xml +++ b/hxhq-modules/hxhq-system/src/main/resources/mapper/system/SysMenuMapper.xml @@ -203,4 +203,10 @@ delete from sys_menu where menu_id = #{menuId} + + \ No newline at end of file