diff --git a/pom.xml b/pom.xml index c4e918a11..faff48e62 100644 --- a/pom.xml +++ b/pom.xml @@ -48,8 +48,14 @@ ${middle.server.url}:18848 - - fd74182b-1fce-4dba-afa7-2623b0376205 + + + + + + + + ${middle.server.url}:8080 @@ -97,7 +103,7 @@ 1.15 1.8.1 2.12.4 - 2.2.6 + 3.0.5 2.18 3.0.0.6 2.8.9 diff --git a/pqs-harmonic/harmonic-boot/src/main/java/com/njcn/harmonic/service/impl/HistoryResultServiceImpl.java b/pqs-harmonic/harmonic-boot/src/main/java/com/njcn/harmonic/service/impl/HistoryResultServiceImpl.java index b862a3d63..ee437d55f 100644 --- a/pqs-harmonic/harmonic-boot/src/main/java/com/njcn/harmonic/service/impl/HistoryResultServiceImpl.java +++ b/pqs-harmonic/harmonic-boot/src/main/java/com/njcn/harmonic/service/impl/HistoryResultServiceImpl.java @@ -143,11 +143,19 @@ public class HistoryResultServiceImpl implements HistoryResultService { Date d = formatter.parse(objectList.get(0).toString()); time.add(d); fValue.add(BigDecimal.valueOf(Float.parseFloat(objectList.get(1).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue()); + if (Integer.parseInt(contion) == 14) { + bValue.add(BigDecimal.valueOf(Float.parseFloat(objectList.get(2).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue()); + cValue.add(BigDecimal.valueOf(Float.parseFloat(objectList.get(3).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue()); + } } for (int i = 0; i < time.size(); i++) { List objects = new ArrayList<>(); objects.add(time.get(i)); objects.add(fValue.get(i)); + if (Integer.parseInt(contion) == 14) { + objects.add(bValue.get(i)); + objects.add(cValue.get(i)); + } objectListData.add(objects); } historyDataResultVO.setTopLimit(queryResultLimitVO.getTopLimit()); @@ -291,7 +299,7 @@ public class HistoryResultServiceImpl implements HistoryResultService { break; case 14: //电压不平衡 - sql = "SELECT time as time, v_zero as aValue FROM data_v WHERE " + stringBuilder.toString() + + sql = "SELECT time as time, v_zero as aValue, v_pos as bValue, v_neg as cValue FROM data_v WHERE " + stringBuilder.toString() + " and (phasic_type ='T') group by phasic_type order by time asc tz('Asia/Shanghai');"; phasicType.add("零序电压"); phasicType.add("正序电压"); diff --git a/pqs-job/job-executor/src/main/java/com/njcn/executor/handler/AuditJob.java b/pqs-job/job-executor/src/main/java/com/njcn/executor/handler/AuditJob.java new file mode 100644 index 000000000..296bd5094 --- /dev/null +++ b/pqs-job/job-executor/src/main/java/com/njcn/executor/handler/AuditJob.java @@ -0,0 +1,34 @@ +package com.njcn.executor.handler; + +import com.njcn.system.api.AuditFeignClient; +import com.xxl.job.core.handler.annotation.XxlJob; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +/** + * @author wr + * @description + * @date 2023/5/17 19:48 + */ +@Slf4j +@Component +@AllArgsConstructor +public class AuditJob { + private final AuditFeignClient auditFeignClient; + + @XxlJob("clearHistoryLogJob") + public void clearHistoryLogJob() { + try { + String time = LocalDateTime.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); + log.info("执行日期deviceAbnormalStatisticsJob===============>"+time); + auditFeignClient.clearHistoryLog(); + }catch (Exception e){ + e.printStackTrace(); + } + + } +} diff --git a/pqs-system/system-api/src/main/java/com/njcn/system/api/AuditFeignClient.java b/pqs-system/system-api/src/main/java/com/njcn/system/api/AuditFeignClient.java new file mode 100644 index 000000000..d7b42ab8e --- /dev/null +++ b/pqs-system/system-api/src/main/java/com/njcn/system/api/AuditFeignClient.java @@ -0,0 +1,21 @@ +package com.njcn.system.api; + +import com.njcn.common.pojo.constant.ServerInfo; +import com.njcn.common.pojo.response.HttpResult; +import com.njcn.system.api.fallback.AuditFeignClientFallbackFactory; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.PostMapping; + +/** + * @author denghuajun + * @version 1.0.0 + * @date 2022年01月05日 15:11 + */ +@FeignClient(value = ServerInfo.SYSTEM, path = "/audit", fallbackFactory = AuditFeignClientFallbackFactory.class, contextId = "area") +public interface AuditFeignClient { + + + @PostMapping("/clearHistoryLog") + HttpResult clearHistoryLog(); + +} diff --git a/pqs-system/system-api/src/main/java/com/njcn/system/api/fallback/AuditFeignClientFallbackFactory.java b/pqs-system/system-api/src/main/java/com/njcn/system/api/fallback/AuditFeignClientFallbackFactory.java new file mode 100644 index 000000000..ed12d13b2 --- /dev/null +++ b/pqs-system/system-api/src/main/java/com/njcn/system/api/fallback/AuditFeignClientFallbackFactory.java @@ -0,0 +1,41 @@ +package com.njcn.system.api.fallback; + +import com.njcn.common.pojo.enums.response.CommonResponseEnum; +import com.njcn.common.pojo.exception.BusinessException; +import com.njcn.common.pojo.response.HttpResult; +import com.njcn.system.api.AuditFeignClient; +import com.njcn.system.utils.SystemEnumUtil; +import feign.hystrix.FallbackFactory; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + + +/** + * @author denghuajun + * @version 1.0.0 + * @date 2022年01月05日 15:08 + */ +@Slf4j +@Component +public class AuditFeignClientFallbackFactory implements FallbackFactory { + + + @Override + public AuditFeignClient create(Throwable cause) { + //判断抛出异常是否为解码器抛出的业务异常 + Enum exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK; + if(cause.getCause() instanceof BusinessException){ + BusinessException businessException = (BusinessException) cause.getCause(); + exceptionEnum = SystemEnumUtil.getExceptionEnum(businessException.getResult()); + } + Enum finalExceptionEnum = exceptionEnum; + return new AuditFeignClient() { + + @Override + public HttpResult clearHistoryLog() { + log.error("{}异常,降级处理,异常为:{}","清空历史日志",cause.toString()); + throw new BusinessException(finalExceptionEnum); + } + }; + } +} diff --git a/pqs-system/system-api/src/main/java/com/njcn/system/enums/AuditLogEnum.java b/pqs-system/system-api/src/main/java/com/njcn/system/enums/AuditLogEnum.java index 1e0b030fd..7c996d1e6 100644 --- a/pqs-system/system-api/src/main/java/com/njcn/system/enums/AuditLogEnum.java +++ b/pqs-system/system-api/src/main/java/com/njcn/system/enums/AuditLogEnum.java @@ -11,7 +11,9 @@ public enum AuditLogEnum { * 审计日志模块异常响应码 */ NOT_FIND_FILE("A0300", "文件未备份,请先备份文件"), - LOG_EXCEPTION("A0301", "导入旧日志文件异常") + LOG_EXCEPTION("A0301", "导入旧日志文件异常"), + LOG_EXCEPTIONTIME("A0302", "导入旧日志文件异常:缺少时间范围"), + DELETE_DATA("A0303", "导入旧日志文件异常:删除数据失败") ; diff --git a/pqs-system/system-boot/pom.xml b/pqs-system/system-boot/pom.xml index 9d1c1ed2a..6aff32ecc 100644 --- a/pqs-system/system-boot/pom.xml +++ b/pqs-system/system-boot/pom.xml @@ -58,6 +58,11 @@ 1.0.0 compile + + com.alibaba + easyexcel + ${easyexcel} + diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/controller/AuditController.java b/pqs-system/system-boot/src/main/java/com/njcn/system/controller/AuditController.java index 7d9970080..9c18c0c47 100644 --- a/pqs-system/system-boot/src/main/java/com/njcn/system/controller/AuditController.java +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/controller/AuditController.java @@ -51,7 +51,7 @@ public class AuditController extends BaseController { @OperateInfo(info = LogEnum.BUSINESS_COMMON) @PostMapping("/logFileWriter") @ApiOperation("日志文件备份") - public HttpResult logFileWriter() { + public HttpResult logFileWriter(){ String methodDescribe = getMethodDescribe("logFileWriter"); auditService.logFileWriter(); return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,null,methodDescribe); @@ -94,6 +94,13 @@ public class AuditController extends BaseController { return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe); } - + @OperateInfo(info = LogEnum.SYSTEM_COMMON) + @PostMapping("/getMemoInfo") + @ApiOperation("获取表空间大小") + public HttpResult getMemoInfo() { + String methodDescribe = getMethodDescribe("getMemoInfo"); + Float memoInfo = auditService.getMemoInfo(); + return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, memoInfo, methodDescribe); + } } diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/controller/ConfigController.java b/pqs-system/system-boot/src/main/java/com/njcn/system/controller/ConfigController.java index c9a4c97a4..759c62842 100644 --- a/pqs-system/system-boot/src/main/java/com/njcn/system/controller/ConfigController.java +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/controller/ConfigController.java @@ -1,6 +1,7 @@ package com.njcn.system.controller; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.njcn.common.pojo.annotation.OperateInfo; import com.njcn.common.pojo.constant.OperateType; @@ -11,6 +12,7 @@ import com.njcn.common.pojo.exception.BusinessException; import com.njcn.common.pojo.response.HttpResult; import com.njcn.common.utils.HttpResultUtil; import com.njcn.common.utils.LogUtil; +import com.njcn.system.enums.SystemResponseEnum; import com.njcn.system.pojo.param.ConfigParam; import com.njcn.system.pojo.po.Config; import com.njcn.system.service.IConfigService; @@ -122,6 +124,13 @@ public class ConfigController extends BaseController { public HttpResult removeSysConfigById(@RequestParam("id") String id) { String methodDescribe = getMethodDescribe("removeSysConfigById"); LogUtil.njcnDebug(log, "{}", methodDescribe, id); + int count = iConfigService.count(new LambdaQueryWrapper() + .eq(Config::getState, DataStateEnum.ENABLE.getCode()) + .ne(Config::getId, id)); + if(count==0){ + //不可更改当前激活状态,必须保留一个激活系统 + throw new BusinessException(SystemResponseEnum.ACTIVATED_STATE); + } boolean res = iConfigService.removeById(id); if (res) { return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe); diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/excel/DemoDataListener.java b/pqs-system/system-boot/src/main/java/com/njcn/system/excel/DemoDataListener.java new file mode 100644 index 000000000..2b1de75cc --- /dev/null +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/excel/DemoDataListener.java @@ -0,0 +1,71 @@ +package com.njcn.system.excel; + +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.read.listener.ReadListener; +import com.alibaba.excel.util.ListUtils; +import com.njcn.system.mapper.UserLogMapper; + +import java.util.List; + +// 有个很重要的点 DemoDataListener 不能被spring管理,要每次读取excel都要new,然后里面用到spring可以构造方法传进去 +public class DemoDataListener implements ReadListener { + + + /** + * 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收 + */ + private static final int BATCH_COUNT = 5000; + /** + * 缓存的数据 + */ + private List cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); + /** + * 假设这个是一个DAO,当然有业务逻辑这个也可以是一个service。当然如果不用存储这个对象没用。 + */ + private UserLogMapper userLogMapper; + + /** + * 如果使用了spring,请使用这个构造方法。每次创建Listener的时候需要把spring管理的类传进来 + * + * @param userLogMapper + */ + public DemoDataListener(UserLogMapper userLogMapper) { + this.userLogMapper = userLogMapper; + } + + /** + * 这个每一条数据解析都会来调用 + * + * @param data one row value. Is is same as {@link AnalysisContext#readRowHolder()} + * @param context + */ + @Override + public void invoke(UserLogExcel data, AnalysisContext context) { + cachedDataList.add(data); + // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM + if (cachedDataList.size() >= BATCH_COUNT) { + saveData(); + // 存储完成清理 list + cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); + } + } + + /** + * 所有数据解析完成了 都会来调用 + * + * @param context + */ + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + // 这里也要保存数据,确保最后遗留的数据也存储到数据库 + saveData(); + cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); + } + + /** + * 加上存储数据库 + */ + public void saveData() { + userLogMapper.insertBatch(cachedDataList); + } +} \ No newline at end of file diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/excel/UserLogExcel.java b/pqs-system/system-boot/src/main/java/com/njcn/system/excel/UserLogExcel.java new file mode 100644 index 000000000..8a2e0cc7b --- /dev/null +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/excel/UserLogExcel.java @@ -0,0 +1,85 @@ +package com.njcn.system.excel; + +import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.annotation.write.style.ColumnWidth; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; + +import java.io.Serializable; + +/** + * @version 1.0.0 + * @author: chenchao + * @date: 2022/07/18 11:04 + */ +@Getter +@Setter +@EqualsAndHashCode +public class UserLogExcel implements Serializable { + + @ColumnWidth(20) + @ExcelProperty(value = "id主键") + private String id; + + @ColumnWidth(20) + @ExcelProperty(value = "登录名") + private String loginName; + + @ColumnWidth(20) + @ExcelProperty(value = "用户名") + private String userName; + + @ColumnWidth(20) + @ExcelProperty(value = "操作ip") + private String ip; + + @ColumnWidth(20) + @ExcelProperty(value = "操作内容") + private String operate; + + @ColumnWidth(15) + @ExcelProperty(value = "操作类型") + private String operateType; + + @ColumnWidth(20) + @ExcelProperty(value = "操作结果") + private Integer result; + + @ColumnWidth(20) + @ExcelProperty(value = "失败原因") + private String failReason; + + @ColumnWidth(30) + @ExcelProperty(value = "时间等级(严重度)") + private Integer level; + + @ColumnWidth(20) + @ExcelProperty(value = "事件类型") + private Integer type; + + @ColumnWidth(20) + @ExcelProperty(value = "模块名称") + private String serviceName="暂态"; + + @ColumnWidth(20) + @ExcelProperty(value = "告警标志") + private Integer state; + + @ColumnWidth(20) + @ExcelProperty(value = "创建用户") + private String createBy; + + @ColumnWidth(20) + @ExcelProperty(value = "创建时间" ) + private String createTime; + + @ColumnWidth(20) + @ExcelProperty(value = "更新用户") + private String updateBy; + + @ColumnWidth(20) + @ExcelProperty(value = "更新时间" ) + private String updateTime; + +} diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/mapper/AuditMapper.java b/pqs-system/system-boot/src/main/java/com/njcn/system/mapper/AuditMapper.java index 9712c8651..267dc3ede 100644 --- a/pqs-system/system-boot/src/main/java/com/njcn/system/mapper/AuditMapper.java +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/mapper/AuditMapper.java @@ -1,7 +1,8 @@ package com.njcn.system.mapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.njcn.system.pojo.dto.excel.UserLogExcel; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.njcn.system.excel.UserLogExcel; import com.njcn.system.pojo.param.AuditParam; import com.njcn.system.pojo.po.UserLog; import com.njcn.system.pojo.vo.AuditLogCusVO; @@ -25,6 +26,8 @@ public interface AuditMapper { * 查询需要备份的审计日志 */ List queryExportUser(@Param("ew") QueryWrapper queryWrapper); + + Page queryExportLimit(Page page, @Param("startTime")String time,@Param("endTime")String endTime); /** * 获取审计日志统计列表 */ diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/mapper/UserLogMapper.java b/pqs-system/system-boot/src/main/java/com/njcn/system/mapper/UserLogMapper.java index 2b855d37b..29749b077 100644 --- a/pqs-system/system-boot/src/main/java/com/njcn/system/mapper/UserLogMapper.java +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/mapper/UserLogMapper.java @@ -1,12 +1,9 @@ package com.njcn.system.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; -import com.njcn.system.pojo.dto.AreaTreeDTO; -import com.njcn.system.pojo.po.Area; +import com.njcn.system.excel.UserLogExcel; import com.njcn.system.pojo.po.UserLog; -import com.njcn.system.pojo.vo.AreaTreeVO; import org.apache.ibatis.annotations.Param; - import java.util.List; /** @@ -20,5 +17,23 @@ import java.util.List; public interface UserLogMapper extends BaseMapper { + /** + * 日志批量插入 + * @param userLogs + */ + void insertBatch(@Param("list") List userLogs); + /** + * 根据id查询数据库是否存在 + * @param idList + * @return + */ + List ids(@Param("ids")List idList); + + /** + * 查询表空间大小 + * @param schema + * @return + */ + Float getMemoInfo(@Param("schema") String schema); } diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/mapper/mapping/AuditMapper.xml b/pqs-system/system-boot/src/main/java/com/njcn/system/mapper/mapping/AuditMapper.xml index 98a089653..d702fe0e1 100644 --- a/pqs-system/system-boot/src/main/java/com/njcn/system/mapper/mapping/AuditMapper.xml +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/mapper/mapping/AuditMapper.xml @@ -91,13 +91,13 @@ AND operate != "unknown operate" AND update_time >= #{auditParam.searchBeginTime} AND #{auditParam.searchEndTime} >= update_time - + and login_name = #{auditParam.loginName} - + and type = #{auditParam.type} - + and operate_type = #{auditParam.operateType} GROUP BY login_name, operate_type @@ -118,5 +118,35 @@ FROM sys_user_log GROUP BY operate_type + \ No newline at end of file diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/mapper/mapping/UserLogMapper.xml b/pqs-system/system-boot/src/main/java/com/njcn/system/mapper/mapping/UserLogMapper.xml index b56f247cc..b4ea6e956 100644 --- a/pqs-system/system-boot/src/main/java/com/njcn/system/mapper/mapping/UserLogMapper.xml +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/mapper/mapping/UserLogMapper.xml @@ -2,4 +2,59 @@ + + INSERT INTO sys_user_log ( + id, + login_name, + user_name, + ip, + operate, + operate_type, + result, + fail_reason, + LEVEL, + type, + service_name, + state, + create_by, + create_time, + update_by, + update_time + ) + values + + ( #{item.id}, + #{item.loginName}, + #{item.userName}, + #{item.ip}, + #{item.operate}, + #{item.operateType}, + #{item.result}, + #{item.failReason}, + #{item.level}, + #{item.type}, + #{item.serviceName}, + #{item.state}, + #{item.createBy}, + #{item.createTime}, + #{item.updateBy}, + #{item.updateTime} ) + + + + + + diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/service/AuditService.java b/pqs-system/system-boot/src/main/java/com/njcn/system/service/AuditService.java index 4599d7d82..cd737607c 100644 --- a/pqs-system/system-boot/src/main/java/com/njcn/system/service/AuditService.java +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/service/AuditService.java @@ -45,4 +45,10 @@ public interface AuditService extends IService { * 清空6个月前的历史日志 */ void clearHistoryLog(); + + /** + * 获取表空间大小 + * @return + */ + Float getMemoInfo(); } diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/service/impl/AuditServiceImpl.java b/pqs-system/system-boot/src/main/java/com/njcn/system/service/impl/AuditServiceImpl.java index 2d4b263bb..3e4cc1c3d 100644 --- a/pqs-system/system-boot/src/main/java/com/njcn/system/service/impl/AuditServiceImpl.java +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/service/impl/AuditServiceImpl.java @@ -1,11 +1,17 @@ package com.njcn.system.service.impl; -import cn.afterturn.easypoi.excel.entity.ImportParams; -import cn.afterturn.easypoi.excel.imports.ExcelImportService; import cn.afterturn.easypoi.exception.excel.ExcelImportException; import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.date.DateUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.StrUtil; +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.ExcelWriter; +import com.alibaba.excel.support.ExcelTypeEnum; +import com.alibaba.excel.write.metadata.WriteSheet; +import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; @@ -13,9 +19,10 @@ import com.google.common.collect.Lists; import com.njcn.common.config.GeneralInfo; import com.njcn.common.pojo.exception.BusinessException; import com.njcn.system.enums.AuditLogEnum; +import com.njcn.system.excel.DemoDataListener; +import com.njcn.system.excel.UserLogExcel; import com.njcn.system.mapper.AuditMapper; import com.njcn.system.mapper.UserLogMapper; -import com.njcn.system.pojo.dto.excel.UserLogExcel; import com.njcn.system.pojo.param.AuditParam; import com.njcn.system.pojo.po.UserLog; import com.njcn.system.pojo.vo.AuditLogCusVO; @@ -23,20 +30,18 @@ import com.njcn.system.pojo.vo.AuditLogVO; import com.njcn.system.pojo.vo.LogParamVO; import com.njcn.system.pojo.vo.ValuePO; import com.njcn.system.service.AuditService; -import lombok.AllArgsConstructor; +import lombok.RequiredArgsConstructor; import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.MediaType; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.commons.CommonsMultipartFile; -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStream; -import java.io.OutputStream; +import java.io.*; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @@ -50,40 +55,33 @@ import java.util.stream.Collectors; * @date: 2022/07/11 20:56 */ @Service -@AllArgsConstructor +@RequiredArgsConstructor public class AuditServiceImpl extends ServiceImpl implements AuditService { private final AuditMapper auditMapper; - private final UserLogMapper userLogMapper; - + @Value("${spring.datasource.druid.url}") + private String urls; private final GeneralInfo generalInfo; @Override public Page getAuditLog(AuditParam auditParam) { - auditParam.setSearchBeginTime(auditParam.getSearchBeginTime() + StrUtil.SPACE + "00:00:00"); - auditParam.setSearchEndTime(auditParam.getSearchEndTime() + StrUtil.SPACE + "23:59:59"); List auditLogVOS = new ArrayList<>(); - Page page = new Page<>(); - page.setSize(auditParam.getPageSize()); - page.setCurrent(auditParam.getPageNum()); - - //待分页数据总量 - int count = auditMapper.getCount(auditParam); - page.setTotal(count); - //分页总页数 - int pages = (int)Math.ceil(count*1.0/auditParam.getPageSize()); - page.setPages(pages); - - auditParam.setPageNum(auditParam.getPageSize()*(auditParam.getPageNum()-1)); - List userLogs = auditMapper.selectAuditLog(auditParam); - if (!CollectionUtils.isEmpty(userLogs)) { - for (UserLog userLog: userLogs) { + Page info = this.page(new Page<>(auditParam.getPageNum(), auditParam.getPageSize()), new LambdaQueryWrapper() + .eq(StrUtil.isNotBlank(auditParam.getLoginName()), UserLog::getLoginName, auditParam.getLoginName()) + .eq(auditParam.getType() != null, UserLog::getLoginName, auditParam.getType()) + .eq(StrUtil.isNotBlank(auditParam.getOperateType()), UserLog::getLoginName, auditParam.getLoginName()) + .ge(StrUtil.isNotBlank(auditParam.getSearchBeginTime()), UserLog::getCreateTime, DateUtil.beginOfDay(DateUtil.parse(auditParam.getSearchBeginTime()))) + .le(StrUtil.isNotBlank(auditParam.getSearchEndTime()), UserLog::getCreateTime, DateUtil.endOfDay(DateUtil.parse(auditParam.getSearchEndTime()))) + ); + Page page = BeanUtil.copyProperties(info, Page.class); + if (CollUtil.isNotEmpty(info.getRecords())) { + for (UserLog userLog : info.getRecords()) { AuditLogVO auditLogVO = new AuditLogVO(); String updateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(userLog.getUpdateTime()); auditLogVO.setTime(updateTime); - if (userLog.getUserName()==null || userLog.getUserName().equals("")) { + if (userLog.getUserName() == null || userLog.getUserName().equals("")) { auditLogVO.setUserName(userLog.getLoginName()); } else { auditLogVO.setUserName(userLog.getUserName()); @@ -91,21 +89,23 @@ public class AuditServiceImpl extends ServiceImpl implem auditLogVO.setOperate(userLog.getOperate()); StringBuilder describe = new StringBuilder(); describe.append(auditLogVO.getUserName()).append("在").append(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").format(userLog.getUpdateTime())).append("在").append(userLog.getIp()).append("执行了").append(userLog.getOperate()).append(",结果为"); - if (userLog.getResult()==1) { + if (userLog.getResult() == 1) { describe.append("成功"); } - if (userLog.getResult()==0) { + if (userLog.getResult() == 0) { describe.append("失败").append(",失败原因为").append(userLog.getFailReason()); } auditLogVO.setDescribe(describe.toString()); auditLogVO.setType(userLog.getType() == 0 ? "业务事件" : "系统事件"); auditLogVO.setResult(userLog.getResult() == 0 ? "失败" : "成功"); auditLogVO.setIp(userLog.getIp()); - if (userLog.getLevel()==0) { + if (userLog.getLevel() == 0) { auditLogVO.setLevel("普通"); - }if (userLog.getLevel()==1) { + } + if (userLog.getLevel() == 1) { auditLogVO.setLevel("中等"); - }if (userLog.getLevel()==2) { + } + if (userLog.getLevel() == 2) { auditLogVO.setLevel("严重"); } auditLogVOS.add(auditLogVO); @@ -118,37 +118,61 @@ public class AuditServiceImpl extends ServiceImpl implem @Override public void logFileWriter() { - LocalDate nowDate = LocalDate.now(); - LocalDate agoDate = nowDate.minusMonths(6).with(TemporalAdjusters.firstDayOfMonth()); + LocalDateTime nowDate = LocalDateTime.now(); + LocalDateTime agoDate = nowDate.minusMonths(6).with(TemporalAdjusters.firstDayOfMonth()); String date = agoDate.format(DateTimeFormatter.ISO_LOCAL_DATE); - // String time = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(agoDate); - // queryWrapper.between("sys_user_log.update_time","2022-02-01","2022-07-31"); + //结束时间 + String endTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH_mm_ss").format(nowDate); + //文件名 + QueryWrapper aa = new QueryWrapper<>(); + aa.ge("sys_user_log.update_time", date); + aa.le("sys_user_log.update_time", endTime); + String nowTime = date + "至" + endTime; + //必须放到循环外,否则会刷新流 + ExcelWriter excelWriter = EasyExcel.write(generalInfo.getBusinessTempPath() + "//" + nowTime + ExcelTypeEnum.CSV.getValue(), UserLogExcel.class) + .excelType(ExcelTypeEnum.CSV) + .build(); - QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.ge("sys_user_log.update_time",date); - List excelList = auditMapper.queryExportUser(queryWrapper); + try { + //记录总数:实际中需要根据查询条件进行统计即可:一共多少条 + int totalCount = this.count(aa); + //每一个Sheet存放100w条数据 + Integer sheetDataRows = 1000000; + //每次写入的数据量20w,每页查询20W + Integer writeDataRows = 200000; + //计算需要的Sheet总数量 + Integer sheetNum = totalCount % sheetDataRows == 0 ? (totalCount / sheetDataRows) : (totalCount / sheetDataRows + 1); + //计算一般情况下每一个Sheet需要写入的次数(一般情况不包含最后一个sheet,因为最后一个sheet不确定会写入多少条数据) + Integer oneSheetWriteCount = sheetDataRows / writeDataRows; + //计算最后一个sheet需要写入的次数 + Integer lastSheetWriteCount = 0; + if (totalCount > sheetDataRows) { + lastSheetWriteCount = totalCount % sheetDataRows == 0 ? oneSheetWriteCount : (totalCount % sheetDataRows % writeDataRows == 0 ? (totalCount / sheetDataRows / writeDataRows) : (totalCount / sheetDataRows / writeDataRows + 1)); + } else { + lastSheetWriteCount = totalCount % sheetDataRows == 0 ? oneSheetWriteCount : oneSheetWriteCount + 1; + } - LocalDateTime nowDateTime = LocalDateTime.now(); - String nowTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH-mm-ss").format(nowDateTime); + //开始分批查询分次写入 + for (int i = 0; i < sheetNum; i++) { - File filePath = new File(generalInfo.getBusinessTempPath()); - filePath.mkdirs(); - File file = new File(filePath.getPath() + File.separator + nowTime +" sys_user_log.xlsx"); -// ExcelUtil.exportExcelWithTargetFile(file, UserLogExcel.class, excelList); + //循环写入次数: j的自增条件是当不是最后一个Sheet的时候写入次数为正常的每个Sheet写入的次数,如果是最后一个就需要使用计算的次数lastSheetWriteCount + for (int j = 0; j < (i != sheetNum - 1 ? oneSheetWriteCount : lastSheetWriteCount); j++) { + //分页查询一次20w + //查询分页列表---按照自己的业务查列表,分页这个一定要使用这个:page1.getPageNum(),page1.getPageSize()!!! + Page userLogExcelPage = auditMapper.queryExportLimit(new Page<>(j + 1 + oneSheetWriteCount * i, writeDataRows), date, endTime); + List list = userLogExcelPage.getRecords(); + //写入到excel: + WriteSheet writeSheet = EasyExcel.writerSheet(i, "Sheet" + (i + 1)).head(UserLogExcel.class) + .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build(); + excelWriter.write(list, writeSheet); + } + } - //备份为txt - // QueryWrapper queryWrapper = new QueryWrapper<>(); - // queryWrapper.ge("sys_user_log.update_time",date); - // List userLogs = userLogMapper.selectList(queryWrapper); - // try { - // File file = new File(SystemBaseConfig.getTempPath()+ File.separator + nowDate +" sys_user_log.txt"); - // //将list写入文件并生成路径 - // FileUtils.writeLines(file, userLogs, true); - // //读取文件大小 - // long sizeOf = FileUtils.sizeOf(new File(file.getPath())); - // } catch (Exception e) { - // e.printStackTrace(); - // } + } catch (Exception e) { + e.printStackTrace(); + } finally { + excelWriter.finish(); + } } @@ -173,48 +197,22 @@ public class AuditServiceImpl extends ServiceImpl implem if (!newFile.exists()) { throw new BusinessException(AuditLogEnum.NOT_FIND_FILE); } - - ImportParams params = new ImportParams(); - params.setHeadRows(1);//表头 - params.setTitleRows(0);//标题行数 - params.setNeedVerify(false); - //第一个sheet为台账信息 - params.setStartSheetIndex(0); - params.setSheetNum(1); - FileInputStream in = null; try { in = new FileInputStream(newFile); - //将Excel文件转成对象集合 - List excelList = new ExcelImportService().importExcelByIs(in, UserLogExcel.class, params, false).getList(); - if (excelList.get(0).getId().isEmpty()) { - throw new BusinessException(AuditLogEnum.LOG_EXCEPTION); - } - List oldIds = excelList.stream().map(UserLogExcel::getId).collect(Collectors.toList()); - //查找该备份文件是否已存在 - List logList = userLogMapper.selectBatchIds(oldIds); - if (CollectionUtils.isEmpty(logList)) { - List userLogs = new ArrayList<>(); - for (UserLogExcel excel: excelList) { - UserLog userLog = new UserLog(); - BeanUtil.copyProperties(excel,userLog); - userLogs.add(userLog); - } - this.saveBatch(userLogs); - } else { - List isIds = logList.stream().map(UserLog::getId).collect(Collectors.toList()); - List needSaveUserLogData = excelList.stream().filter(userLogExcelTemp -> !isIds.contains(userLogExcelTemp.getId())).collect(Collectors.toList()); - if (!CollectionUtils.isEmpty(needSaveUserLogData)) { - List userLogs = new ArrayList<>(); - for (UserLogExcel needSave: needSaveUserLogData) { - UserLog userLog = new UserLog(); - BeanUtil.copyProperties(needSave,userLog); - userLogs.add(userLog); - } - this.saveBatch(userLogs); - // this.saveOrUpdateBatch(userLogs); - } + String name = newFile.getName(); + String[] split = name.split(ExcelTypeEnum.CSV.getValue()); + + String[] splitTime = split[0].split("至"); + if (StrUtil.isBlank(splitTime[0]) || StrUtil.isBlank(splitTime[1])) { + throw new BusinessException(AuditLogEnum.LOG_EXCEPTIONTIME); } + LambdaQueryWrapper le = new LambdaQueryWrapper() + .ge(UserLog::getCreateTime, DateUtil.beginOfDay(DateUtil.parse(splitTime[0]))) + .le(UserLog::getCreateTime, splitTime[1].replace("_", ":")); + this.remove(le); + EasyExcel.read(newFile, UserLogExcel.class, new DemoDataListener(this.getBaseMapper())) + .excelType(ExcelTypeEnum.CSV).doReadAll(); } catch (Exception e) { e.printStackTrace(); @@ -222,19 +220,6 @@ public class AuditServiceImpl extends ServiceImpl implem } finally { IOUtils.closeQuietly(in); } - - // try { - // // FileInputStream in = new FileInputStream(file); - // MultipartFile multipartFile = getMultipartFile(file); - // - // List list = ExcelImportUtil.importExcelMore(multipartFile.getInputStream(), UserLogExcel.class, params).getList(); - // - // System.out.println(list); - // - // // this.saveOrUpdate(excelList.get(0)); - // } catch (Exception e) { - // throw new BusinessException(AuditLogEnum.LOG_EXCEPTION); - // } } @@ -249,7 +234,7 @@ public class AuditServiceImpl extends ServiceImpl implem List auditLogCusVOS = auditMapper.selectCensusAuditLog(auditParam); page.setTotal(auditLogCusVOS.size()); //分页总页数 - int pages = (int)Math.ceil(page.getTotal()*1.0/auditParam.getPageSize()); + int pages = (int) Math.ceil(page.getTotal() * 1.0 / auditParam.getPageSize()); page.setPages(pages); if (!CollectionUtils.isEmpty(auditLogCusVOS)) { @@ -268,10 +253,16 @@ public class AuditServiceImpl extends ServiceImpl implem String date = agoDate.format(DateTimeFormatter.ISO_LOCAL_DATE); QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.lt("sys_user_log.update_time",date); + queryWrapper.lt("sys_user_log.create_time", date); auditMapper.deleteByTime(queryWrapper); } + @Override + public Float getMemoInfo() { + String schema = urls.substring(urls.lastIndexOf("/")+1,urls.lastIndexOf("?")); + return this.baseMapper.getMemoInfo(schema); + } + @Override public LogParamVO saveLogParam() { LogParamVO logParamVO = new LogParamVO(); @@ -279,15 +270,15 @@ public class AuditServiceImpl extends ServiceImpl implem List operateType = auditMapper.selectOperateType(); List loginNameList = new ArrayList<>(); List operateTypeList = new ArrayList<>(); - for (int i = 0; i implem return logParamVO; } - /**返回一个最新修改日期的文件*/ + /** + * 返回一个最新修改日期的文件 + */ public File getLastFile() { File parentFile = new File(generalInfo.getBusinessTempPath()); - Date date = FileUtil.lastModifiedTime(parentFile); - System.out.println(">>>>>>>>>>当前文件夹最后修改时间>>>>>>>>>"+date); +// Date date = FileUtil.lastModifiedTime(parentFile); +// System.out.println(">>>>>>>>>>当前文件夹最后修改时间>>>>>>>>>" + date); //文件夹下的所有子文件数组 File[] files = parentFile.listFiles(); List list = Arrays.stream(files).map(tempFile -> FileUtil.lastModifiedTime(tempFile)).collect(Collectors.toList()); //Date集合排序 - Collections.sort(list, new Comparator() { - @Override - public int compare(Date o1, Date o2) { - return o2.compareTo(o1);//倒序排序 - // return o1.compareTo(o2);//正序排序 - } - }); + Collections.sort(list, Comparator.reverseOrder()); File newFile = Arrays.stream(files).filter(tempFile -> FileUtil.lastModifiedTime(tempFile).equals(list.get(0))).collect(Collectors.toList()).get(0); return newFile; } - - } diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/service/impl/ConfigServiceImpl.java b/pqs-system/system-boot/src/main/java/com/njcn/system/service/impl/ConfigServiceImpl.java index 48f8ba6c1..414073aec 100644 --- a/pqs-system/system-boot/src/main/java/com/njcn/system/service/impl/ConfigServiceImpl.java +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/service/impl/ConfigServiceImpl.java @@ -13,11 +13,9 @@ import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.List; import java.util.Objects; -import java.util.stream.Collectors; /** *

@@ -85,8 +83,7 @@ public class ConfigServiceImpl extends ServiceImpl impleme @Override public List getList() { List list = this.baseMapper.getList(); - List filterList = list.stream().filter(t -> t.getLogTime() > 0 && t.getLogSize().compareTo(new BigDecimal(0))==1).collect(Collectors.toList()); - return filterList; + return list; }