package com.leixi.common.operationlog.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.util.StringUtil;
import com.leixi.common.operationlog.dao.SysOperationLogMapper;
import com.leixi.common.operationlog.enums.OperationStatusEnum;
import com.leixi.common.operationlog.exception.OperationLogException;
import com.leixi.common.operationlog.pojo.entity.SysOperationLog;
import com.leixi.common.operationlog.pojo.req.SysOperationLogReq;
import com.leixi.common.operationlog.service.SysOperationLogService;
import com.leixi.common.operationlog.utils.LogConstant;
import com.leixi.common.operationlog.utils.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
import java.util.UUID;
/**
* <B>系统名称:雷袭月启的组件库</B><BR>
* <B>模块名称:日志管理模块</B><BR>
* <B>中文类名:</B><BR>
* <B>概要说明:</B><BR>
*
* @author leixiyueqi
* @since 2023/12/13 9:25
*/
@Service
public class SysOperationLogServiceImpl extends ServiceImpl<SysOperationLogMapper, SysOperationLog> implements SysOperationLogService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private ResourceLoader resourceLoader;
@PostConstruct
private void checkAndCreateTable() {
List<String> tableNameList = this.baseMapper.searchTableExists();
// 如果已经有表,就不用管理
if (!CollectionUtils.isEmpty(tableNameList)) {
return;
}
executeSqlScript("classpath:/sql/createTable.sql");
}
public void executeSqlScript(String scriptPath) {
Resource resource = resourceLoader.getResource(scriptPath);
try (InputStream inputStream = resource.getInputStream();
Scanner scanner = new Scanner(inputStream, StandardCharsets.UTF_8.name())) {
scanner.useDelimiter(";\\s*");
List<String> sqlStatements = new ArrayList<>();
while (scanner.hasNext()) {
String sqlStatement = scanner.next();
if (!sqlStatement.trim().isEmpty()) {
sqlStatements.add(sqlStatement);
}
}
jdbcTemplate.batchUpdate(sqlStatements.toArray(new String[0]));
} catch (IOException e) {
throw new OperationLogException(LogConstant.FAIL, "初始化系统日志表报错,请检查数据库配置是否正确,当前数据库是否为DM数据库");
}
}
/**
* 存储日志信息
*
* @param operateName 操作项名称
* @param dataId 数据Id
* @return 日志id
*/
@Override
public String saveOperationLog(String operateName, String dataId, Object params) {
SysOperationLog log = new SysOperationLog();
log.setId(UUID.randomUUID().toString());
log.setCreateTime(new Date());
log.setStatus(OperationStatusEnum.WORKING.value());
if (Objects.nonNull(params)) {
String jsonStr = JSON.toJSONString(params);
if (jsonStr.length() > LogConstant.PARAM_LENGTH) {
log.setParams(jsonStr.substring(0, LogConstant.PARAM_LENGTH));
} else {
log.setParams(jsonStr);
}
}
log.setOperationName(operateName);
log.setDataId(dataId);
this.baseMapper.insert(log);
return log.getId();
}
@Override
public String saveOperationLog(SysOperationLog log) {
log.setCreateTime(new Date());
log.setEndTime(new Date());
this.save(log);
return log.getId();
}
@Override
public void saveBatchOperationLogs(List<SysOperationLog> list) {
if (CollectionUtils.isEmpty(list)) {
return;
}
list.forEach(log -> {
log.setCreateTime(new Date());
log.setEndTime(new Date());
});
this.saveBatch(list);
}
@Override
public void updateOperationLog(String logId, Integer status, String operateNt) {
if (StringUtils.isEmpty(logId)) {
return;
}
this.baseMapper.updateOperationLog(logId, status, operateNt);
}
@Override
public List<SysOperationLog> getOperationLogList(SysOperationLogReq req) {
LambdaQueryWrapper<SysOperationLog> queryWrapper = new LambdaQueryWrapper<>();
if (StringUtil.isNotEmpty(req.getOperationCode())) {
queryWrapper.eq(SysOperationLog::getOperationCode, req.getOperationCode());
}
if (StringUtil.isNotEmpty(req.getDataId())) {
queryWrapper.eq(SysOperationLog::getDataId, req.getDataId());
}
if (!CollectionUtils.isEmpty(req.getDataIdList())) {
queryWrapper.in(SysOperationLog::getDataId, req.getDataIdList());
}
if (!StringUtil.isEmpty(req.getStartTime())) {
queryWrapper.ge(SysOperationLog::getCreateTime, LogUtils.toDate(req.getStartTime(), LogConstant.DATE_YYYYMMDD_STR));
}
if (!StringUtils.isEmpty(req.getEndTime())) {
queryWrapper.lt(SysOperationLog::getCreateTime,
LogUtils.addDate(LogUtils.toDate(req.getEndTime(),
LogConstant.DATE_YYYYMMDD_STR), 1));
}
if (!StringUtils.isEmpty(req.getOrgCode())) {
queryWrapper.eq(SysOperationLog::getOrgCode, req.getOrgCode());
}
if (StringUtil.isNotEmpty(req.getLogId())) {
queryWrapper.eq(SysOperationLog::getId, req.getLogId());
}
if (StringUtil.isNotEmpty(req.getOperatorName())) {
queryWrapper.like(SysOperationLog::getOperatorName, req.getOperatorName());
}
if (StringUtil.isNotEmpty(req.getOperatorId())) {
queryWrapper.eq(SysOperationLog::getOperatorId, req.getOperatorId());
}
if (StringUtil.isNotEmpty(req.getChangedItems())) {
queryWrapper.like(SysOperationLog::getChangedItems, req.getChangedItems());
}
if(StringUtil.isNotEmpty(req.getOperationType())){
queryWrapper.eq(SysOperationLog::getOperationType,req.getOperationType());
}
if (req.getStatus() != null) {
queryWrapper.eq(SysOperationLog::getStatus, req.getStatus());
}
queryWrapper.select(SysOperationLog::getId,
SysOperationLog::getParams,
SysOperationLog::getCreateTime,
SysOperationLog::getChangedItems,
SysOperationLog::getOperatorName,
SysOperationLog::getStatus,
SysOperationLog::getOperationName,
SysOperationLog::getOperatorId,
SysOperationLog::getDataId,
SysOperationLog::getOperationType,
SysOperationLog::getOperateNt,
SysOperationLog::getOrgCode
);
queryWrapper.orderByDesc(SysOperationLog::getCreateTime);
return this.list(queryWrapper);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
operation-log-dm.zip (20个子文件)
pom.xml 4KB
src
main
resources
sql
createTable.sql 3KB
mapper
SysOperationLogMapper.xml 2KB
config
application-dev.yml 1KB
application.yml 37B
java
com
leixi
common
operationlog
Application.java 680B
controller
SysOperationLogController.java 1KB
utils
LogConstant.java 834B
LogUtils.java 4KB
dao
SysOperationLogMapper.java 1001B
pojo
req
SysOperationLogReq.java 2KB
entity
SysOperationLog.java 3KB
service
BasicLogService.java 762B
impl
SysOperationLogServiceImpl.java 8KB
DefaultLogService.java 1KB
SysOperationLogService.java 2KB
enums
OperationStatusEnum.java 1KB
exception
OperationLogException.java 896B
annotations
OperationLogAspect.java 4KB
OperationLog.java 714B
共 20 条
- 1
资源评论
雷袭月启
- 粉丝: 232
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- copy2txt,一个连续拷贝并降拷贝结果保存至txt文本的小工具
- p1008打印机设置.rar
- 医学图像分割数据集:肺部癌症(Lung)切片分割【包含3个切面划分好的数据集(2类别)、标签文件、可视化代码】
- sitka-weather-2014.csv
- 基于python+CNN+opencv实现的驾驶员分心驾驶状态识别源码+数据+使用说明.zip
- xshell6安装包,包含xshell与xftp
- 基于python+CNN+opencv实现的驾驶员分心驾驶状态识别源码+数据+使用说明.zip
- 网站表白源码网站表白源码
- 行业大模型调研报告0513.pdf
- 植物大战僵尸杂交版v2.0安装程序.exe
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功