Commit c5476999 authored by hanbing's avatar hanbing

[update] 策略管理,添加唯一值校验

parent 8212c996
......@@ -32,6 +32,11 @@ public class GlobalExceptionHandler {
return jsonViewObject.fail(message);
}
@ExceptionHandler(value = RuntimeException.class)
public JsonViewObject runtimeExceptionHandler(RuntimeException e) {
return JsonViewObject.newInstance().fail(e);
}
private Map<String, String> getErrors(BindingResult result) {
Map<String, String> map = new HashMap<>();
List<FieldError> list = result.getFieldErrors();
......
package net.wanji.opt.common.exception;
/**
* @author Kent HAN
* @date 2023/3/2 15:26
*/
public class UniqueException extends RuntimeException{
public UniqueException() {
super();
}
public UniqueException(String message) {
super(message);
}
public UniqueException(String message, Exception e) {
super(message,e);
}
}
\ No newline at end of file
......@@ -11,6 +11,7 @@ import net.wanji.opt.dto.strategy.AddOrUpdateIdeaDTO;
import net.wanji.opt.dto.strategy.QueryIdeaDTO;
import net.wanji.opt.po.strategy.IdeaPO;
import net.wanji.opt.service.strategy.IdeaService;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -43,10 +44,12 @@ public class IdeaController {
@ApiResponse(code = 200, message = "OK", response = JsonViewObject.class),
})
public JsonViewObject addOrUpdateIdea(@RequestBody @Valid AddOrUpdateIdeaDTO addOrUpdateIdeaDTO) {
ideaService.addOrUpdateIdea(addOrUpdateIdeaDTO);
JsonViewObject jsonViewObject = JsonViewObject.newInstance();
return jsonViewObject.success();
try {
ideaService.addOrUpdateIdea(addOrUpdateIdeaDTO);
} catch (DuplicateKeyException e) {
throw new RuntimeException("方法编号或方法名称不可重复");
}
return JsonViewObject.newInstance().success();
}
@ApiOperation(value = "删除方法", notes = "删除方法", response = JsonViewObject.class,
......
......@@ -6,10 +6,12 @@ import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import net.wanji.common.framework.rest.JsonViewObject;
import net.wanji.opt.common.exception.UniqueException;
import net.wanji.opt.dto.IntegerIdsDTO;
import net.wanji.opt.dto.strategy.AddOrUpdateSceneDTO;
import net.wanji.opt.dto.strategy.QuerySceneDTO;
import net.wanji.opt.service.strategy.SceneService;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -41,10 +43,14 @@ public class SceneController {
@ApiResponse(code = 200, message = "OK", response = JsonViewObject.class),
})
public JsonViewObject addOrUpdateScene(@RequestBody AddOrUpdateSceneDTO addOrUpdateSceneDTO) {
sceneService.addOrUpdateScene(addOrUpdateSceneDTO);
JsonViewObject jsonViewObject = JsonViewObject.newInstance();
return jsonViewObject.success();
try {
sceneService.addOrUpdateScene(addOrUpdateSceneDTO);
} catch (DuplicateKeyException e) {
throw new RuntimeException("场景编号或场景名称不可重复");
} catch (UniqueException e) {
throw new RuntimeException("应用策略或优化方法不可重复");
}
return JsonViewObject.newInstance().success();
}
@ApiOperation(value = "删除场景", notes = "删除场景", response = JsonViewObject.class,
......
......@@ -11,6 +11,7 @@ import net.wanji.opt.dto.strategy.AddOrUpdateStrategyDTO;
import net.wanji.opt.dto.strategy.QueryStrategyDTO;
import net.wanji.opt.po.strategy.StrategyPO;
import net.wanji.opt.service.strategy.StrategyService;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -43,10 +44,12 @@ public class StrategyController {
@ApiResponse(code = 200, message = "OK", response = JsonViewObject.class),
})
public JsonViewObject addOrUpdateStrategy(@RequestBody @Valid AddOrUpdateStrategyDTO addOrUpdateStrategyDTO) {
strategyService.addOrUpdateStrategy(addOrUpdateStrategyDTO);
JsonViewObject jsonViewObject = JsonViewObject.newInstance();
return jsonViewObject.success();
try {
strategyService.addOrUpdateStrategy(addOrUpdateStrategyDTO);
} catch (DuplicateKeyException e) {
throw new RuntimeException("策略编号或策略名称不可重复");
}
return JsonViewObject.newInstance().success();
}
@ApiOperation(value = "删除策略", notes = "删除策略", response = JsonViewObject.class,
......
......@@ -3,6 +3,7 @@ package net.wanji.opt.service.strategy.impl;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.wanji.opt.common.exception.UniqueException;
import net.wanji.opt.dao.mapper.strategy.IdeaMapper;
import net.wanji.opt.dao.mapper.strategy.SceneMapper;
import net.wanji.opt.dao.mapper.strategy.SceneStrategyIdeaMapper;
......@@ -23,7 +24,9 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
......@@ -50,6 +53,7 @@ public class SceneServiceImpl implements SceneService {
@Override
@Transactional
public void addOrUpdateScene(AddOrUpdateSceneDTO addOrUpdateSceneDTO) {
checkUnique(addOrUpdateSceneDTO);
Integer id = addOrUpdateSceneDTO.getId();
if (ObjectUtil.isEmpty(id) || id == 0) {
// 不传ID为新增
......@@ -76,6 +80,27 @@ public class SceneServiceImpl implements SceneService {
}
}
private void checkUnique(AddOrUpdateSceneDTO addOrUpdateSceneDTO) {
Set<String> strategyNameSet = new HashSet<>();
Set<String> ideaNameSet = new HashSet<>();
List<AddOrUpdateSceneDTO.StrategyListElement> strategyList = addOrUpdateSceneDTO.getStrategyList();
for (AddOrUpdateSceneDTO.StrategyListElement strategyListElement : strategyList) {
String strategyName = strategyListElement.getStrategyName();
boolean addStrategy = strategyNameSet.add(strategyName);
if (!addStrategy) {
throw new UniqueException();
}
List<AddOrUpdateSceneDTO.IdeaListElement> ideaList = strategyListElement.getIdeaList();
for (AddOrUpdateSceneDTO.IdeaListElement ideaListElement : ideaList) {
String ideaName = ideaListElement.getIdeaName();
boolean addIdea = ideaNameSet.add(ideaName);
if (!addIdea) {
throw new UniqueException();
}
}
}
}
private void updateRelationship(AddOrUpdateSceneDTO addOrUpdateSceneDTO, Integer id) {
List<AddOrUpdateSceneDTO.StrategyListElement> strategyList = addOrUpdateSceneDTO.getStrategyList();
for (AddOrUpdateSceneDTO.StrategyListElement strategy : strategyList) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment