Browse Source

feat: [操作日志] 角色操作日志,待完善

master
memorylkf 2 months ago
parent
commit
515e2b239c
5 changed files with 161 additions and 8 deletions
  1. +18
    -0
      hxhq-api/hxhq-api-system/src/main/java/com/hxhq/system/api/domain/SysRole.java
  2. +7
    -0
      hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/mapper/SysMenuMapper.java
  3. +6
    -0
      hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/service/impl/SysMenuServiceImpl.java
  4. +124
    -8
      hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/service/impl/SysRoleServiceImpl.java
  5. +6
    -0
      hxhq-modules/hxhq-system/src/main/resources/mapper/system/SysMenuMapper.xml

+ 18
- 0
hxhq-api/hxhq-api-system/src/main/java/com/hxhq/system/api/domain/SysRole.java View File

@ -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<String> 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;
}
}

+ 7
- 0
hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/mapper/SysMenuMapper.java View File

@ -122,4 +122,11 @@ public interface SysMenuMapper
* @return 结果
*/
public SysMenu checkMenuNameUnique(@Param("menuName") String menuName, @Param("parentId") Long parentId);
/**
* 获取角色下的所有菜单
* @param roleId
* @return
*/
List<SysMenu> selectMenuByRoleId(@Param("roleId") Long roleId);
}

+ 6
- 0
hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/service/impl/SysMenuServiceImpl.java View File

@ -560,6 +560,12 @@ public class SysMenuServiceImpl implements ISysMenuService
new String[] { "", "", "", "/", "/" });
}
/**
* 获取修改日志
* @param info
* @param old
* @return
*/
private List<SystemLog> getModifyLogList(SysMenu info,SysMenu old){
List<SystemLog> list = new ArrayList<>();

+ 124
- 8
hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/service/impl/SysRoleServiceImpl.java View File

@ -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<SysMenu> oldMenuList = menuMapper.selectMenuByRoleId(role.getRoleId());
// 修改角色信息
roleMapper.updateRole(role);
// 删除角色与菜单关联
roleMenuMapper.deleteRoleMenuByRoleId(role.getRoleId());
return insertRoleMenu(role);
int row = insertRoleMenu(role);
List<SystemLog> 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<SystemLog> getModifyLogList(SysRole info, SysRole old,List<SysMenu> oldMenuList){
List<SystemLog> list = new ArrayList<>();
List<ObjectCompareUtil.FieldChange> 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<Long> 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<SysMenu> newMenuList = new ArrayList<>();
if(info.getMenuIds()!=null && info.getMenuIds().length>0){
List<SysMenu> 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<String> addNameList = new ArrayList<>();
List<String> deleteNameList = new ArrayList<>();
List<Long> oldMenuIdList = oldMenuList.stream().map(o->o.getMenuId()).collect(Collectors.toList());
List<Long> 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;
}
}

+ 6
- 0
hxhq-modules/hxhq-system/src/main/resources/mapper/system/SysMenuMapper.xml View File

@ -203,4 +203,10 @@
delete from sys_menu where menu_id = #{menuId}
</delete>
<select id="selectMenuByRoleId" resultType="com.hxhq.system.domain.SysMenu">
SELECT DISTINCT m.`menu_id`,m.`menu_name` FROM `sys_role_menu` rm
JOIN `sys_menu` m ON rm.`menu_id`=m.`menu_id`
WHERE rm.`role_id`=#{roleId}
</select>
</mapper>

Loading…
Cancel
Save