package com.mmall.service;
import com.google.common.base.Preconditions;
import com.mmall.beans.LogType;
import com.mmall.beans.PageQuery;
import com.mmall.beans.PageResult;
import com.mmall.common.RequestHolder;
import com.mmall.dao.SysAclMapper;
import com.mmall.dao.SysAclModuleMapper;
import com.mmall.dao.SysDeptMapper;
import com.mmall.dao.SysLogMapper;
import com.mmall.dao.SysRoleMapper;
import com.mmall.dao.SysUserMapper;
import com.mmall.dto.SearchLogDto;
import com.mmall.exception.ParamException;
import com.mmall.model.SysAcl;
import com.mmall.model.SysAclModule;
import com.mmall.model.SysDept;
import com.mmall.model.SysLogWithBLOBs;
import com.mmall.model.SysRole;
import com.mmall.model.SysUser;
import com.mmall.param.SearchLogParam;
import com.mmall.util.BeanValidator;
import com.mmall.util.IpUtil;
import com.mmall.util.JsonMapper;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
public class SysLogService {
@Resource
private SysLogMapper sysLogMapper;
@Resource
private SysDeptMapper sysDeptMapper;
@Resource
private SysUserMapper sysUserMapper;
@Resource
private SysAclModuleMapper sysAclModuleMapper;
@Resource
private SysAclMapper sysAclMapper;
@Resource
private SysRoleMapper sysRoleMapper;
@Resource
private SysRoleAclService sysRoleAclService;
@Resource
private SysRoleUserService sysRoleUserService;
public void recover(int id) {
SysLogWithBLOBs sysLog = sysLogMapper.selectByPrimaryKey(id);
Preconditions.checkNotNull(sysLog, "待还原的记录不存在");
switch (sysLog.getType()){
case LogType.TYPE_DEPT:
SysDept beforeDept = sysDeptMapper.selectByPrimaryKey(sysLog.getTargetId());
Preconditions.checkNotNull(beforeDept, "待还原的部门已经不存在了");
if (StringUtils.isBlank(sysLog.getNewValue()) || StringUtils.isBlank(sysLog.getOldValue())) {
throw new ParamException("新增和删除操作不做还原");
}
SysDept afterDept = JsonMapper.string2Obj(sysLog.getOldValue(), new TypeReference<SysDept>() {
});
afterDept.setOperator(RequestHolder.getCurrentUser().getUsername());
afterDept.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
afterDept.setOperateTime(new Date());
sysDeptMapper.updateByPrimaryKeySelective(afterDept);
saveDeptLog(beforeDept, afterDept);
break;
case LogType.TYPE_USER:
SysUser beforeUser = sysUserMapper.selectByPrimaryKey(sysLog.getTargetId());
Preconditions.checkNotNull(beforeUser, "待还原的用户已经不存在了");
if (StringUtils.isBlank(sysLog.getNewValue()) || StringUtils.isBlank(sysLog.getOldValue())) {
throw new ParamException("新增和删除操作不做还原");
}
SysUser afterUser = JsonMapper.string2Obj(sysLog.getOldValue(), new TypeReference<SysUser>() {
});
afterUser.setOperator(RequestHolder.getCurrentUser().getUsername());
afterUser.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
afterUser.setOperateTime(new Date());
sysUserMapper.updateByPrimaryKeySelective(afterUser);
saveUserLog(beforeUser, afterUser);
break;
case LogType.TYPE_ACL_MODULE:
SysAclModule beforeAclModule = sysAclModuleMapper.selectByPrimaryKey(sysLog.getTargetId());
Preconditions.checkNotNull(beforeAclModule, "待还原的权限模块已经不存在了");
if (StringUtils.isBlank(sysLog.getNewValue()) || StringUtils.isBlank(sysLog.getOldValue())) {
throw new ParamException("新增和删除操作不做还原");
}
SysAclModule afterAclModule = JsonMapper.string2Obj(sysLog.getOldValue(), new TypeReference<SysAclModule>() {
});
afterAclModule.setOperator(RequestHolder.getCurrentUser().getUsername());
afterAclModule.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
afterAclModule.setOperateTime(new Date());
sysAclModuleMapper.updateByPrimaryKeySelective(afterAclModule);
saveAclModuleLog(beforeAclModule, afterAclModule);
break;
case LogType.TYPE_ACL:
SysAcl beforeAcl = sysAclMapper.selectByPrimaryKey(sysLog.getTargetId());
Preconditions.checkNotNull(beforeAcl, "待还原的权限点已经不存在了");
if (StringUtils.isBlank(sysLog.getNewValue()) || StringUtils.isBlank(sysLog.getOldValue())) {
throw new ParamException("新增和删除操作不做还原");
}
SysAcl afterAcl = JsonMapper.string2Obj(sysLog.getOldValue(), new TypeReference<SysAcl>() {
});
afterAcl.setOperator(RequestHolder.getCurrentUser().getUsername());
afterAcl.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
afterAcl.setOperateTime(new Date());
sysAclMapper.updateByPrimaryKeySelective(afterAcl);
saveAclLog(beforeAcl, afterAcl);
break;
case LogType.TYPE_ROLE:
SysRole beforeRole = sysRoleMapper.selectByPrimaryKey(sysLog.getTargetId());
Preconditions.checkNotNull(beforeRole, "待还原的角色已经不存在了");
if (StringUtils.isBlank(sysLog.getNewValue()) || StringUtils.isBlank(sysLog.getOldValue())) {
throw new ParamException("新增和删除操作不做还原");
}
SysRole afterRole = JsonMapper.string2Obj(sysLog.getOldValue(), new TypeReference<SysRole>() {
});
afterRole.setOperator(RequestHolder.getCurrentUser().getUsername());
afterRole.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
afterRole.setOperateTime(new Date());
sysRoleMapper.updateByPrimaryKeySelective(afterRole);
saveRoleLog(beforeRole, afterRole);
break;
case LogType.TYPE_ROLE_ACL:
SysRole aclRole = sysRoleMapper.selectByPrimaryKey(sysLog.getTargetId());
Preconditions.checkNotNull(aclRole, "角色已经不存在了");
sysRoleAclService.changeRoleAcls(sysLog.getTargetId(), JsonMapper.string2Obj(sysLog.getOldValue(), new TypeReference<List<Integer>>() {
}));
break;
case LogType.TYPE_ROLE_USER:
SysRole userRole = sysRoleMapper.selectByPrimaryKey(sysLog.getTargetId());
Preconditions.checkNotNull(userRole, "角色已经不存在了");
sysRoleUserService.changeRoleUsers(sysLog.getTargetId(), JsonMapper.string2Obj(sysLog.getOldValue(), new TypeReference<List<Integer>>() {
}));
break;
default:;
}
}
public PageResult<SysLogWithBLOBs> searchPageList(SearchLogParam param, PageQuery page) {
BeanValidator.check(page);
SearchLogDto dto = new SearchLogDto();
dto.setType(param.getType());
if (StringUtils.isNotBlank(param.getBeforeSeg())) {
dto.setBeforeSeg("%" + param.getBeforeSeg() + "%");
}
if (StringUtils.isNotBlank(param.getAfterSeg())) {
dto.setAfterSeg("%" + param.getAfterSeg() + "%");
}
if (StringUtils.is
猰貐的新时代
- 粉丝: 1w+
- 资源: 2883
最新资源
- 基于 SSM 和 JSP 的网月科技公司门户网站:整合企业资源优势
- com.contrarywind:Android-PickerView:4.1.9
- 基于CNN-LSTM的降水反演源代码+项目说明,特征因子包括FY3D-MWHS、MWTS、FY4A cn1-14
- abcdefrghjklmnzxcasdqwe
- KEBA(科巴)工业机器人Kestudio快速入门
- 基于 SSM 和 JSP 开发学生就业管理系统:实现精细化就业管理
- 大豆叶片病害图像分类数据集【已标注,约3,600张数据】
- 基于ConvLSTM2D、CNN3D等模型架构对Sentinel-1的多时序雷达数据进行农作物分类源代码+使用说明
- 前端课程设计-后台管理系统前端页面(源码+文档)
- 废料垃圾数据集,yolov7标注,9562张图片,可识别瓶子,纸板,金属,其他的,纸,宠物,塑料,聚丙烯,塑料,皮带
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈