1. 添加防护对象文件上传下载配置

2. 修改SqlSessionWrapper,添加注释
This commit is contained in:
松岳 陈
2024-01-07 17:54:28 +08:00
parent 0fb8dd87fe
commit db02907f0a
8 changed files with 85 additions and 33 deletions

View File

@@ -5,7 +5,6 @@ import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.realtime.protection.configuration.entity.defense.object.ProtectObject;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
@@ -36,7 +35,7 @@ public class ProjectObjectDataListener implements ReadListener<ProtectObject> {
private void saveData() {
Boolean success = protectObjectService.newProtectObjects(cachedDataList);
if (!success) {
throw new RuntimeException("Error reading data in newProtectObjects");
throw new RuntimeException("Error reading data in /proobj/new");
}
}
}

View File

@@ -1,15 +1,18 @@
package com.realtime.protection.server.defense.object;
import com.alibaba.excel.EasyExcel;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.realtime.protection.configuration.entity.defense.object.ProtectObject;
import com.realtime.protection.configuration.response.ResponseResult;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
@RestController
@@ -38,13 +41,25 @@ public class ProtectObjectController {
.setData("success", true);
}
@PutMapping("/new")
public ResponseResult newProtectObjectFromFile(MultipartFile updateFile) throws IOException {
EasyExcel.read(updateFile.getInputStream(), ProtectObject.class,
@PostMapping("/upload")
public ResponseResult uploadFile(MultipartFile uploadFile) throws IOException {
EasyExcel.read(uploadFile.getInputStream(), ProtectObject.class,
new ProjectObjectDataListener(protectObjectService)).sheet().doRead();
return ResponseResult.ok();
}
@GetMapping("/download")
public void downloadTemplate(HttpServletResponse response) throws IOException {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("防护对象", StandardCharsets.UTF_8).replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), ProtectObject.class)
.sheet("防护对象")
.doWrite(List.of());
}
@GetMapping("/query")
public ResponseResult queryProtectObjects(@RequestParam(value = "proobj_name", required = false)
String protectObjectName,
@@ -88,7 +103,7 @@ public class ProtectObjectController {
}
@PostMapping("/delete")
public ResponseResult deleteProtectObject(@RequestBody @JsonProperty("proobj_ids") List<Integer> protectObjectIds) {
public ResponseResult deleteProtectObject(@RequestBody List<Integer> protectObjectIds) {
return ResponseResult.ok()
.setData("proobj_ids", protectObjectIds)
.setData("success", protectObjectService.deleteProtectObjects(protectObjectIds));

View File

@@ -23,7 +23,7 @@ public interface ProtectObjectMapper {
Boolean deleteProtectObject(@Param("proobj_id") Integer protectObjectId);
Boolean deleteProtectObjects(@Param("proobj_ids") List<Integer> protectObjectIds);
void deleteProtectObjects(@Param("proobj_ids") List<Integer> protectObjectIds);
Boolean changeProtectObjectAuditStatus(@Param("proobj_id") Integer protectObjectId,
@Param("proobj_audit_status") Integer protectObjectAuditStatus);

View File

@@ -4,6 +4,7 @@ import com.alibaba.excel.util.ListUtils;
import com.realtime.protection.configuration.entity.defense.object.ProtectObject;
import com.realtime.protection.configuration.utils.AuditStatusValidator;
import com.realtime.protection.configuration.utils.SqlSessionWrapper;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -34,16 +35,22 @@ public class ProtectObjectService {
public Boolean newProtectObjects(List<ProtectObject> protectObjectList) {
Function<ProtectObjectMapper, Function<List<ProtectObject>, Boolean>> newProtectObjectFunction = mapper -> list -> {
if (list == null || list.isEmpty()) {
return false;
}
List<ProtectObject> protectObjectBatch = ListUtils.newArrayListWithExpectedSize(100);
for (ProtectObject protectObject : protectObjectList) {
protectObjectBatch.add(protectObject);
if (protectObjectBatch.size() < 1000) {
if (protectObjectBatch.size() < 100) {
continue;
}
mapper.newProtectObjects(protectObjectBatch);
protectObjectBatch.clear();
}
mapper.newProtectObjects(protectObjectBatch);
if (!protectObjectBatch.isEmpty()) {
mapper.newProtectObjects(protectObjectBatch);
}
return true;
};
@@ -67,10 +74,12 @@ public class ProtectObjectService {
}
public Boolean deleteProtectObjects(List<Integer> protectObjectIds) {
Function<ProtectObjectMapper, Function<List<Integer>, Void>> deleteProtectObjectFunction = mapper -> list -> {
Function<ProtectObjectMapper, Function<List<Integer>, Boolean>> deleteProtectObjectFunction = mapper -> list -> {
if (list == null || list.isEmpty()) {
return null;
return false;
}
boolean success = true;
Integer result;
List<Integer> protectObjectBatch = ListUtils.newArrayListWithExpectedSize(100);
for (Integer protectObjectId : list) {
@@ -78,15 +87,16 @@ public class ProtectObjectService {
if (protectObjectBatch.size() < 100) {
continue;
}
mapper.deleteProtectObjects(protectObjectIds);
mapper.deleteProtectObjects(protectObjectBatch);
protectObjectBatch.clear();
}
mapper.deleteProtectObjects(protectObjectBatch);
return null;
if (!protectObjectBatch.isEmpty()) {
mapper.deleteProtectObjects(protectObjectBatch);;
}
return success;
};
sqlSessionWrapper.startBatchSession(ProtectObjectMapper.class, deleteProtectObjectFunction, protectObjectIds);
return true;
return sqlSessionWrapper.startBatchSession(ProtectObjectMapper.class, deleteProtectObjectFunction, protectObjectIds);
}
@Transactional