1、新增动态规则审批和批量审批,动态规则新增audit_status字段
This commit is contained in:
@@ -6,7 +6,9 @@ import jakarta.validation.Valid;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("dynamicrule")
|
||||
@@ -19,7 +21,7 @@ public class DynamicRuleController implements DynamicRuleControllerApi {
|
||||
this.dynamicRuleService = dynamicRuleService;
|
||||
}
|
||||
|
||||
// 新增 要关联防护对象!!!!
|
||||
//
|
||||
@Override
|
||||
@PostMapping("/new")
|
||||
public ResponseResult newDynamicRuleObject(@RequestBody @Valid DynamicRuleObject dynamicRuleObject) {
|
||||
@@ -120,6 +122,58 @@ public class DynamicRuleController implements DynamicRuleControllerApi {
|
||||
//详情查看?? 就是按id查询吧
|
||||
|
||||
|
||||
//审核?不需要
|
||||
/**
|
||||
* 审批
|
||||
*/
|
||||
@GetMapping("/{id}/audit/{auditStatus}")
|
||||
public ResponseResult updateDynamicRuleAuditStatus(@PathVariable Integer id, @PathVariable Integer auditStatus) {
|
||||
if (id <= 0 || auditStatus < 0 || auditStatus > 2) {
|
||||
return new ResponseResult(400, "id or status is invalid")
|
||||
.setData("staticRule_id", id)
|
||||
.setData("success", false);
|
||||
}
|
||||
return ResponseResult.ok()
|
||||
.addDataMap(dynamicRuleService.updateAuditStatus(id, auditStatus))
|
||||
.setData("dynamicRule_id", id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量审批
|
||||
*/
|
||||
@PostMapping("/auditbatch")
|
||||
public ResponseResult updateDynamicRuleAuditStatusBatch(@RequestBody Map<Integer, Integer> idsWithAuditStatusMap) {
|
||||
List<Integer> errorIds = new ArrayList<>();
|
||||
for (Map.Entry<Integer, Integer> entry: idsWithAuditStatusMap.entrySet()) {
|
||||
Integer id = entry.getKey();
|
||||
Integer auditStatus = entry.getValue();
|
||||
if (id <= 0 || auditStatus < 0 || auditStatus > 2) {
|
||||
errorIds.add(id);
|
||||
}
|
||||
}
|
||||
if (!errorIds.isEmpty()){
|
||||
return new ResponseResult(400, "id or status is invalid")
|
||||
.setData("staticRule_id", errorIds)
|
||||
.setData("success", false);
|
||||
}
|
||||
|
||||
return ResponseResult.ok()
|
||||
.setData("success", dynamicRuleService.updateAuditStatusBatch(idsWithAuditStatusMap));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取统计数据
|
||||
*/
|
||||
@GetMapping("/statistics")
|
||||
public ResponseResult getStaticRuleStatisticsData(){
|
||||
|
||||
return ResponseResult.ok()
|
||||
.setData("dynamic_rule_num", dynamicRuleService.queryDynamicRuleTotalNum(null, null,
|
||||
null, null))
|
||||
.setData("dynamic_rule_used_num", dynamicRuleService.queryUsedDynamicRuleTotalNum())
|
||||
.setData("dynamic_rule_audit_num", dynamicRuleService.queryAuditDynamicRuleTotalNum(1))
|
||||
.setData("dynamic_rule_unaudit_num", dynamicRuleService.queryAuditDynamicRuleTotalNum(0));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Mapper
|
||||
public interface DynamicRuleMapper {
|
||||
@@ -47,4 +48,14 @@ public interface DynamicRuleMapper {
|
||||
List<DynamicRuleObject> queryDynamicRuleByIds(List<Integer> ids);
|
||||
|
||||
Integer queryTaskStatusBydynamicRuleId(Integer dynamicRuleId);
|
||||
|
||||
Integer queryUsedDynamicRuleTotalNum();
|
||||
|
||||
Integer queryAuditDynamicRuleTotalNum(int auditStatus);
|
||||
|
||||
Integer queryAuditStatusById(Integer dynamicRuleId);
|
||||
|
||||
Boolean updateAuditStatusById(Integer dynamicRuleId, Integer auditStatus);
|
||||
|
||||
void updateAuditStatusByIdBatch(@Param("idWithAuditStatusBatch") Map<Integer, Integer> idWithAuditStatusBatch);
|
||||
}
|
||||
|
||||
@@ -5,11 +5,14 @@ import com.realtime.protection.configuration.entity.defense.template.Template;
|
||||
import com.realtime.protection.configuration.entity.rule.dynamicrule.DynamicRuleObject;
|
||||
import com.realtime.protection.configuration.utils.SqlSessionWrapper;
|
||||
import com.realtime.protection.configuration.utils.enums.StateEnum;
|
||||
import com.realtime.protection.configuration.utils.enums.audit.AuditStatusValidator;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
@Service
|
||||
@@ -73,6 +76,9 @@ public class DynamicRuleService {
|
||||
public void deleteDynamicRuleObject(Integer dynamicRuleId) {
|
||||
//根据任务状态判断能否删除
|
||||
Integer taskStatus = dynamicRuleMapper.queryTaskStatusBydynamicRuleId(dynamicRuleId);
|
||||
if (taskStatus == null){
|
||||
dynamicRuleMapper.deleteDynamicRuleObject(dynamicRuleId);
|
||||
}else{
|
||||
switch (StateEnum.getStateEnumByNum(taskStatus)){
|
||||
case RUNNING:
|
||||
throw new IllegalArgumentException("使用该动态规则的任务处于运行状态");
|
||||
@@ -83,6 +89,8 @@ public class DynamicRuleService {
|
||||
}
|
||||
//不需要使用 join,在数据库中设置了级联删除 ON DELETE CASCADE,在删除在从父表中删除数据时自动删除子表中的数据
|
||||
dynamicRuleMapper.deleteDynamicRuleObject(dynamicRuleId);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -177,4 +185,55 @@ public class DynamicRuleService {
|
||||
public List<DynamicRuleObject> queryDynamicRuleByIds(List<Integer> ids) {
|
||||
return dynamicRuleMapper.queryDynamicRuleByIds(ids);
|
||||
}
|
||||
|
||||
public Integer queryUsedDynamicRuleTotalNum() {
|
||||
return dynamicRuleMapper.queryUsedDynamicRuleTotalNum();
|
||||
}
|
||||
|
||||
public Integer queryAuditDynamicRuleTotalNum(int auditStatus) {
|
||||
return dynamicRuleMapper.queryAuditDynamicRuleTotalNum(auditStatus);
|
||||
}
|
||||
|
||||
public Map<String, Object> updateAuditStatus(Integer id, Integer auditStatus) {
|
||||
Integer originalAuditStatus = dynamicRuleMapper.queryAuditStatusById(id);
|
||||
if (originalAuditStatus == null) {
|
||||
throw new IllegalArgumentException("cannot find audit status of static rule " + id + ", maybe static rule doesn't exist?");
|
||||
}
|
||||
if (!AuditStatusValidator.setOriginal(originalAuditStatus).checkValidate(auditStatus)) {
|
||||
throw new IllegalArgumentException("invalid audit status");
|
||||
}
|
||||
Boolean success = dynamicRuleMapper.updateAuditStatusById(id, auditStatus);
|
||||
|
||||
Map<String, Object> resultMap = new HashMap<>();
|
||||
resultMap.put("success", success);
|
||||
resultMap.put("audit_status", auditStatus);
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
public Object updateAuditStatusBatch(Map<Integer, Integer> idsWithAuditStatusMap) {
|
||||
Function<DynamicRuleMapper, Function<Map<Integer, Integer>, Boolean>> updateDynamicRuleAuditStatusFunction =
|
||||
mapper -> map -> {
|
||||
if (map == null || map.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Map<Integer, Integer> idWithAuditStatusBatch = new HashMap<>();
|
||||
for (Map.Entry<Integer, Integer> item : map.entrySet()) {
|
||||
idWithAuditStatusBatch.put(item.getKey(), item.getValue());
|
||||
if (idWithAuditStatusBatch.size() < 100) {
|
||||
continue;
|
||||
}
|
||||
//mapper指的就是外层函数输入的参数,也就是WhiteListMapper
|
||||
mapper.updateAuditStatusByIdBatch(idWithAuditStatusBatch);
|
||||
idWithAuditStatusBatch.clear();
|
||||
}
|
||||
if (!idWithAuditStatusBatch.isEmpty()) {
|
||||
mapper.updateAuditStatusByIdBatch(idWithAuditStatusBatch);
|
||||
}
|
||||
return true;
|
||||
};
|
||||
//实现事务操作
|
||||
return sqlSessionWrapper.startBatchSession(DynamicRuleMapper.class, updateDynamicRuleAuditStatusFunction, idsWithAuditStatusMap);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,6 +101,26 @@
|
||||
where
|
||||
dynamic_rule_id = #{dynamicRuleId}
|
||||
</update>
|
||||
<update id="updateAuditStatusById">
|
||||
update t_dynamic_rule
|
||||
set audit_status = #{auditStatus}
|
||||
where dynamic_rule_id = #{dynamicRuleId}
|
||||
</update>
|
||||
|
||||
<!-- 用于批量更新审核状态 -->
|
||||
<update id="updateAuditStatusByIdBatch">
|
||||
update t_dynamic_rule
|
||||
set audit_status = CASE dynamic_rule_id
|
||||
<foreach collection="idWithAuditStatusBatch" index="id" item="auditStatus" separator=" ">
|
||||
WHEN #{id} THEN #{auditStatus}
|
||||
</foreach>
|
||||
ELSE audit_status
|
||||
END
|
||||
WHERE dynamic_rule_id IN
|
||||
<foreach collection="idWithAuditStatusBatch" index="id" open="(" separator="," close=")">
|
||||
#{id}
|
||||
</foreach>
|
||||
</update>
|
||||
|
||||
|
||||
<resultMap id="dynamicRuleMap"
|
||||
@@ -248,5 +268,17 @@
|
||||
where dynamic_rule_id = #{dynamicRuleId}
|
||||
</select>
|
||||
|
||||
<select id="queryUsedDynamicRuleTotalNum" resultType="java.lang.Integer">
|
||||
|
||||
</select>
|
||||
<select id="queryAuditDynamicRuleTotalNum" resultType="java.lang.Integer">
|
||||
|
||||
</select>
|
||||
<select id="queryAuditStatusById" resultType="java.lang.Integer">
|
||||
select audit_status
|
||||
from t_dynamic_rule
|
||||
where dynamic_rule_id = #{dynamicRuleId}
|
||||
</select>
|
||||
|
||||
|
||||
</mapper>
|
||||
@@ -14,7 +14,9 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@SpringBootTest
|
||||
public class DynamicRuleServiceTest extends ProtectionApplicationTests {
|
||||
@@ -136,4 +138,16 @@ public class DynamicRuleServiceTest extends ProtectionApplicationTests {
|
||||
null,null,null,null);
|
||||
System.out.println(num);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateDynamicRuleAuditStatusBatch(){
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
map.put(101, 1);
|
||||
map.put(102, 2);
|
||||
map.put(103, 2);
|
||||
|
||||
|
||||
System.out.println(dynamicRuleService.updateAuditStatusBatch(map));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user