1、DynamicRule实现新增、删除、修改、id查询、分页查询功能。并在crud时与ProtectObject关联。
2、StaticRule添加批量导入、模板文件下载功能,使用sqlSessionWrapper重写批量删除 3、WhiteList添加模板文件下载功能
This commit is contained in:
@@ -1,12 +1,18 @@
|
||||
package com.realtime.protection.server.rule.staticrule;
|
||||
|
||||
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import com.realtime.protection.configuration.entity.rule.staticrule.StaticRuleObject;
|
||||
import com.realtime.protection.configuration.response.ResponseResult;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@@ -31,22 +37,37 @@ public class StaticRuleController {
|
||||
return ResponseResult.ok().setData("static_rule_name",object.getStaticRuleName());
|
||||
}
|
||||
|
||||
//以Excel方式批量导入静态规则
|
||||
@PostMapping("/upload")
|
||||
public ResponseResult uploadFile(MultipartFile uploadFile) throws IOException {
|
||||
EasyExcel.read(uploadFile.getInputStream(), StaticRuleObject.class,
|
||||
new StaticRuleDataListener(staticRuleService)).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(), StaticRuleObject.class)
|
||||
.sheet("静态规则")
|
||||
.doWrite(List.of());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除静态规则
|
||||
* 删除静态规则(有的删了,有的没删,也返回false)
|
||||
*/
|
||||
@DeleteMapping("/{ids}")
|
||||
public ResponseResult delete(@PathVariable List<Integer> ids){
|
||||
log.info("根据id删除静态规则:{}",ids);
|
||||
//调用service删除
|
||||
// if(staticRuleService.deleteStaticRule(ids) == false){
|
||||
// return ResponseResult.error()
|
||||
// .setData("static_rule_id",ids)
|
||||
// .setData("success",false);
|
||||
// //有的删了,有的没删,也返回false
|
||||
// }
|
||||
//
|
||||
return ResponseResult.ok()
|
||||
.setData("static_rule_id",ids)
|
||||
.setData("success",true);
|
||||
.setData("success",staticRuleService.deleteStaticRules(ids));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.realtime.protection.server.rule.staticrule;
|
||||
|
||||
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.rule.staticrule.StaticRuleObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
public class StaticRuleDataListener implements ReadListener<StaticRuleObject> {
|
||||
private static final int batchCount = 100;
|
||||
private final List<StaticRuleObject> cachedDataList
|
||||
= ListUtils.newArrayListWithExpectedSize(batchCount);
|
||||
|
||||
private final StaticRuleService staticRuleService;
|
||||
|
||||
public StaticRuleDataListener(StaticRuleService staticRuleService) {
|
||||
this.staticRuleService = staticRuleService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(StaticRuleObject object, AnalysisContext analysisContext) {
|
||||
log.info("解析到一条数据:{}", object.toString());
|
||||
cachedDataList.add(object);
|
||||
if (cachedDataList.size() > batchCount) {
|
||||
saveData();
|
||||
cachedDataList.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
|
||||
saveData();
|
||||
}
|
||||
|
||||
/**
|
||||
* 加上存储数据库
|
||||
*/
|
||||
private void saveData() {
|
||||
log.info("{}条数据,开始存储数据库!", cachedDataList.size());
|
||||
Boolean success = staticRuleService.newStaticRuleObjects(cachedDataList);
|
||||
log.info("存储数据库成功!");
|
||||
if (!success) {
|
||||
throw new RuntimeException("Error reading data in /proobj/new");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,10 @@ public interface StaticRuleMapper {
|
||||
|
||||
//根据主键删除静态规则
|
||||
@Delete("delete from t_static_rule where static_rule_id = #{id}")
|
||||
Boolean deleteStaticRule(Integer id);
|
||||
Boolean deleteStaticRuleById(Integer id);
|
||||
|
||||
|
||||
|
||||
|
||||
//修改静态规则
|
||||
void updateStaticRule(StaticRuleObject object);
|
||||
@@ -29,4 +32,7 @@ public interface StaticRuleMapper {
|
||||
Integer page, Integer pageSize);
|
||||
|
||||
|
||||
void deleteStaticRules(@Param("whiteListIds") List<Integer> staticRuleBatch);
|
||||
|
||||
void newStaticRules(List<StaticRuleObject> staticRuleBatch);
|
||||
}
|
||||
|
||||
@@ -1,19 +1,25 @@
|
||||
package com.realtime.protection.server.rule.staticrule;
|
||||
|
||||
import com.alibaba.excel.util.ListUtils;
|
||||
import com.realtime.protection.configuration.entity.rule.staticrule.StaticRuleObject;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import com.realtime.protection.configuration.utils.SqlSessionWrapper;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
@Service
|
||||
public class StaticRuleService {
|
||||
|
||||
private final StaticRuleMapper staticRuleMapper;
|
||||
private final SqlSessionWrapper sqlSessionWrapper;
|
||||
|
||||
public StaticRuleService(StaticRuleMapper staticRuleMapper, SqlSessionWrapper sqlSessionWrapper) {
|
||||
|
||||
public StaticRuleService(StaticRuleMapper staticRuleMapper) {
|
||||
this.staticRuleMapper = staticRuleMapper;
|
||||
this.sqlSessionWrapper = sqlSessionWrapper;
|
||||
}
|
||||
|
||||
|
||||
@@ -32,17 +38,36 @@ public class StaticRuleService {
|
||||
return object.getStaticRuleId();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Boolean deleteStaticRule(List<Integer> staticRuleIds) {
|
||||
public Object deleteStaticRules(List<Integer> staticRuleIds) {
|
||||
//判断当前静态规则是否能够删除---是否存在任务选择的静态规则??
|
||||
|
||||
//删除静态规则
|
||||
// for (Integer id : staticRuleIds) {
|
||||
// staticRuleMapper.deleteStaticRule(id);
|
||||
// }
|
||||
return staticRuleIds.stream().allMatch(staticRuleMapper::deleteStaticRule);
|
||||
|
||||
Function<StaticRuleMapper, Function<List<Integer>, Boolean>> deleteStaticRulesFunction =
|
||||
mapper -> list -> {
|
||||
if (list == null || list.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
List<Integer> staticRuleBatch = ListUtils.newArrayListWithExpectedSize(100);
|
||||
for (Integer staticRuleId : list) {
|
||||
staticRuleBatch.add(staticRuleId);
|
||||
if (staticRuleBatch.size() < 100) {
|
||||
continue;
|
||||
}
|
||||
//mapper指的就是外层函数输入的参数,也就是WhiteListMapper
|
||||
mapper.deleteStaticRules(staticRuleBatch);
|
||||
staticRuleBatch.clear();
|
||||
}
|
||||
if (!staticRuleBatch.isEmpty()) {
|
||||
mapper.deleteStaticRules(staticRuleBatch);
|
||||
}
|
||||
return true;
|
||||
};
|
||||
//实现事务操作
|
||||
return sqlSessionWrapper.startBatchSession(StaticRuleMapper.class, deleteStaticRulesFunction, staticRuleIds);
|
||||
}
|
||||
|
||||
public void updateStaticRule(StaticRuleObject object) {
|
||||
@@ -66,4 +91,32 @@ public class StaticRuleService {
|
||||
return staticRuleMapper.queryStaticRule(static_rule_name,static_rule_id,page,pageSize);
|
||||
}
|
||||
|
||||
public Boolean newStaticRuleObjects(List<StaticRuleObject> staticRuleList) {
|
||||
Function<StaticRuleMapper, Function<List<StaticRuleObject>, Boolean>> newStaticRuleFunction =
|
||||
mapper -> list -> {
|
||||
if (list == null || list.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
List<StaticRuleObject> StaticRuleBatch = ListUtils.newArrayListWithExpectedSize(100);
|
||||
for (StaticRuleObject staticRule : staticRuleList) {
|
||||
staticRule.setStaticRuleCreateTime(LocalDateTime.now());
|
||||
StaticRuleBatch.add(staticRule);
|
||||
if (StaticRuleBatch.size() < 100) {
|
||||
continue;
|
||||
}
|
||||
//mapper指的就是外层函数输入的参数,也就是WhiteListMapper
|
||||
mapper.newStaticRules(StaticRuleBatch);
|
||||
StaticRuleBatch.clear();
|
||||
}
|
||||
if (!StaticRuleBatch.isEmpty()) {
|
||||
mapper.newStaticRules(StaticRuleBatch);
|
||||
}
|
||||
return true;
|
||||
};
|
||||
//实现事务操作
|
||||
return sqlSessionWrapper.startBatchSession(StaticRuleMapper.class, newStaticRuleFunction, staticRuleList);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user