Commit 155bec87 authored by hanbing's avatar hanbing

[update] 策略管理,添加参数校验

parent 6f26352a
package net.wanji.opt.common.exception;
import net.wanji.common.framework.rest.JsonViewObject;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 全局异常处理
*
* @author Kent HAN
* @date 2023/1/5 13:18
*/
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public JsonViewObject methodArgumentExceptionHandler(MethodArgumentNotValidException e) {
BindingResult bindingResult = e.getBindingResult();
List<ObjectError> allErrors = bindingResult.getAllErrors();
String message = "";
for (ObjectError error : allErrors) {
message = error.getDefaultMessage();
}
JsonViewObject jsonViewObject = JsonViewObject.newInstance();
return jsonViewObject.fail(message);
}
private Map<String, String> getErrors(BindingResult result) {
Map<String, String> map = new HashMap<>();
List<FieldError> list = result.getFieldErrors();
for (FieldError error : list) {
map.put(error.getField(), error.getDefaultMessage());
}
return map;
}
}
\ No newline at end of file
...@@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.RequestBody; ...@@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
/** /**
...@@ -41,7 +42,7 @@ public class IdeaController { ...@@ -41,7 +42,7 @@ public class IdeaController {
@ApiResponses({ @ApiResponses({
@ApiResponse(code = 200, message = "OK", response = JsonViewObject.class), @ApiResponse(code = 200, message = "OK", response = JsonViewObject.class),
}) })
public JsonViewObject addOrUpdateIdea(@RequestBody AddOrUpdateIdeaDTO addOrUpdateIdeaDTO) { public JsonViewObject addOrUpdateIdea(@RequestBody @Valid AddOrUpdateIdeaDTO addOrUpdateIdeaDTO) {
ideaService.addOrUpdateIdea(addOrUpdateIdeaDTO); ideaService.addOrUpdateIdea(addOrUpdateIdeaDTO);
JsonViewObject jsonViewObject = JsonViewObject.newInstance(); JsonViewObject jsonViewObject = JsonViewObject.newInstance();
......
...@@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.RequestBody; ...@@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
/** /**
...@@ -41,7 +42,7 @@ public class StrategyController { ...@@ -41,7 +42,7 @@ public class StrategyController {
@ApiResponses({ @ApiResponses({
@ApiResponse(code = 200, message = "OK", response = JsonViewObject.class), @ApiResponse(code = 200, message = "OK", response = JsonViewObject.class),
}) })
public JsonViewObject addOrUpdateStrategy(@RequestBody AddOrUpdateStrategyDTO addOrUpdateStrategyDTO) { public JsonViewObject addOrUpdateStrategy(@RequestBody @Valid AddOrUpdateStrategyDTO addOrUpdateStrategyDTO) {
strategyService.addOrUpdateStrategy(addOrUpdateStrategyDTO); strategyService.addOrUpdateStrategy(addOrUpdateStrategyDTO);
JsonViewObject jsonViewObject = JsonViewObject.newInstance(); JsonViewObject jsonViewObject = JsonViewObject.newInstance();
......
...@@ -4,6 +4,8 @@ import io.swagger.annotations.ApiModel; ...@@ -4,6 +4,8 @@ import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import javax.validation.constraints.Pattern;
/** /**
* @author Kent HAN * @author Kent HAN
* @date 2023/2/27 9:39 * @date 2023/2/27 9:39
...@@ -14,14 +16,17 @@ public class AddOrUpdateIdeaDTO { ...@@ -14,14 +16,17 @@ public class AddOrUpdateIdeaDTO {
@ApiModelProperty(value = "方法ID(不传ID为新增,传ID为修改)",notes = "") @ApiModelProperty(value = "方法ID(不传ID为新增,传ID为修改)",notes = "")
private Integer id ; private Integer id ;
@ApiModelProperty(value = "编号",required = true, notes = "") @ApiModelProperty(value = "编号",required = true, notes = "")
@Pattern(regexp = "^[\\u4E00-\\u9FA5\\w\\-]{0,16}$", message = "编号只能包含中文、英文、数字、下划线和中横线,0~16个字符")
private String ideaCode ; private String ideaCode ;
/** 优化方法 */ /** 优化方法 */
@ApiModelProperty(value = "优化方法",required = true,notes = "") @ApiModelProperty(value = "优化方法",required = true,notes = "")
@Pattern(regexp = "^[\\u4E00-\\u9FA5\\w\\-]{0,50}$", message = "优化方法只能包含中文、英文、数字、下划线和中横线,0~50个字符")
private String ideaName ; private String ideaName ;
/** 应用对象 1路口;2干线;3区域 */ /** 应用对象 1路口;2干线;3区域 */
@ApiModelProperty(value = "应用对象 1路口;2干线;3区域",required = true,notes = "") @ApiModelProperty(value = "应用对象 1路口;2干线;3区域",required = true,notes = "")
private Integer ideaTarget; private Integer ideaTarget;
/** 优化详情 */ /** 优化详情 */
@ApiModelProperty(value = "优化详情",required = true,notes = "") @ApiModelProperty(value = "优化详情",required = true,notes = "")
@Pattern(regexp = "^[\\u4E00-\\u9FA5\\w\\-]{0,50}$", message = "优化详情只能包含中文、英文、数字、下划线和中横线,0~50个字符")
private String ideaDetail ; private String ideaDetail ;
} }
...@@ -5,6 +5,7 @@ import io.swagger.annotations.ApiModelProperty; ...@@ -5,6 +5,7 @@ import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import javax.validation.constraints.Pattern;
import java.util.List; import java.util.List;
/** /**
...@@ -18,8 +19,10 @@ public class AddOrUpdateSceneDTO { ...@@ -18,8 +19,10 @@ public class AddOrUpdateSceneDTO {
@ApiModelProperty(value = "场景ID(不传ID为新增,传ID为修改)",notes = "") @ApiModelProperty(value = "场景ID(不传ID为新增,传ID为修改)",notes = "")
private Integer id; private Integer id;
@ApiModelProperty(value = "编号",required = true, notes = "") @ApiModelProperty(value = "编号",required = true, notes = "")
@Pattern(regexp = "^[\\u4E00-\\u9FA5\\w\\-]{0,16}$", message = "编号只能包含中文、英文、数字、下划线和中横线,0~16个字符")
private String sceneCode; private String sceneCode;
@ApiModelProperty(value = "场景名称",required = true, notes = "") @ApiModelProperty(value = "场景名称",required = true, notes = "")
@Pattern(regexp = "^[\\u4E00-\\u9FA5\\w\\-]{0,50}$", message = "场景名称只能包含中文、英文、数字、下划线和中横线,0~50个字符")
private String sceneName; private String sceneName;
@ApiModelProperty(value = "应用对象 1路口;2干线;3区域",required = true,notes = "") @ApiModelProperty(value = "应用对象 1路口;2干线;3区域",required = true,notes = "")
private Integer secneTarget; private Integer secneTarget;
......
...@@ -4,6 +4,8 @@ import io.swagger.annotations.ApiModel; ...@@ -4,6 +4,8 @@ import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import javax.validation.constraints.Pattern;
/** /**
* @author Kent HAN * @author Kent HAN
* @date 2023/2/27 9:39 * @date 2023/2/27 9:39
...@@ -14,14 +16,17 @@ public class AddOrUpdateStrategyDTO { ...@@ -14,14 +16,17 @@ public class AddOrUpdateStrategyDTO {
@ApiModelProperty(value = "策略ID(不传ID为新增,传ID为修改)",notes = "") @ApiModelProperty(value = "策略ID(不传ID为新增,传ID为修改)",notes = "")
private Integer id ; private Integer id ;
@ApiModelProperty(value = "编号",required = true, notes = "") @ApiModelProperty(value = "编号",required = true, notes = "")
@Pattern(regexp = "^[\\u4E00-\\u9FA5\\w\\-]{0,16}$", message = "编号只能包含中文、英文、数字、下划线和中横线,0~16个字符")
private String strategyCode ; private String strategyCode ;
/** 优化方法 */ /** 优化方法 */
@ApiModelProperty(value = "策略名称",required = true,notes = "") @ApiModelProperty(value = "策略名称",required = true,notes = "")
@Pattern(regexp = "^[\\u4E00-\\u9FA5\\w\\-]{0,50}$", message = "策略名称只能包含中文、英文、数字、下划线和中横线,0~50个字符")
private String strategyName ; private String strategyName ;
/** 应用对象 1路口;2干线;3区域 */ /** 应用对象 1路口;2干线;3区域 */
@ApiModelProperty(value = "应用对象 1路口;2干线;3区域",required = true,notes = "") @ApiModelProperty(value = "应用对象 1路口;2干线;3区域",required = true,notes = "")
private Integer strategyTarget; private Integer strategyTarget;
/** 优化详情 */ /** 优化详情 */
@Pattern(regexp = "^[\\u4E00-\\u9FA5\\w\\-]{0,50}$", message = "策略详情只能包含中文、英文、数字、下划线和中横线,0~50个字符")
@ApiModelProperty(value = "策略详情",required = true,notes = "") @ApiModelProperty(value = "策略详情",required = true,notes = "")
private String strategyDetail ; private String strategyDetail ;
} }
...@@ -24,6 +24,7 @@ import org.springframework.transaction.annotation.Transactional; ...@@ -24,6 +24,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
/** /**
* @author Kent HAN * @author Kent HAN
...@@ -163,7 +164,10 @@ public class SceneServiceImpl implements SceneService { ...@@ -163,7 +164,10 @@ public class SceneServiceImpl implements SceneService {
strategyListElement.setIdeaList(ideaList); strategyListElement.setIdeaList(ideaList);
strategyList.add(strategyListElement); strategyList.add(strategyListElement);
} }
return strategyList; List<AddOrUpdateSceneDTO.StrategyListElement> collect = strategyList.stream()
.sorted(Comparator.comparing(AddOrUpdateSceneDTO.StrategyListElement::getPriority).reversed())
.collect(Collectors.toList());
return collect;
} }
private List<AddOrUpdateSceneDTO.IdeaListElement> buildIdeaList(Integer sceneId, Integer strategyId) { private List<AddOrUpdateSceneDTO.IdeaListElement> buildIdeaList(Integer sceneId, Integer strategyId) {
......
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