新增规则接口文档 - 添加规则
新增规则接口文档
接口地址
/addOneRegular
请求方法
POST
请求参数
| 参数名称 | 类型 | 说明 | 是否必填 |
|---|---|---|---|
| adminId | long | 管理员ID | 是 |
| regularName | string | 规则名称 | 是 |
请求示例
{
"adminId": 1,
"regularName": "新规则名称"
}
返回结果
| 状态码 | 说明 |
|---|---|
| 200 | 成功 |
| 400 | 失败 |
| 401 | 权限不足 |
| 403 | 规则名称重复 |
代码示例
@PostMapping("/addOneRegular")
public Result<Object> addOneRegular(@RequestBody Map<String, Object> params, HttpServletRequest request) {
//权限验证
String token = (String) request.getAttribute("claims_admin");
if (token == null || "".equals(token)) {
throw new RuntimeException("权限不足!");
}
log.info("addOneRegular请求报文:" + params);
//前端传递参数
long adminId;
if (params.get("adminId") != null) {
adminId = Long.parseLong(params.get("adminId").toString());
} else {
log.error("adminId为空!");
return new Result<>(ResultCode.FAIL);
}
String regularName = (String) params.get("regularName"); //规则名称
if (StringUtils.isEmpty(regularName)) {
log.error("regularName为空!");
return new Result<>(ResultCode.FAIL);
}
List<Regular> regulars = regularService.queryRegularByName(regularName, adminId);
if (!CollectionUtils.isEmpty(regulars)) {
log.error("规则名称重复!不可添加!");
return new Result<>(ResultCode.REGULAR_NAME_EXIST);
}
//创建一个规则对象
Regular regular = new Regular();
//将前端传递过来的参数赋值给新创建的规则对象
regular.setAdminId(adminId);
regular.setRegularName(regularName);
regular.setEnableState(1);
//调用新增规则方法
int ret = regularService.addOneRegular(regular);
if (ret > 0) {
log.info("批量新增规则成功!");
//参考表查出所有数据
List<AllProjectDemeritInfo> allProjectDemeritInfos = new ArrayList<>(allProjectDemeritInfoService.queryAllProjects());
//存放批量新增具体规则
List<SpecificRegular> specificRegularList = allProjectDemeritInfos.stream().map(allProjectDemeritInfo -> {
SpecificRegular specificRegular = new SpecificRegular();
specificRegular.setJudgeState(true); //默认评判
specificRegular.setAllPrDemeritInfoId(allProjectDemeritInfo.getAllPrDemeritInfoId());
specificRegular.setProjectDescribe(allProjectDemeritInfo.getProjectDescribe());
specificRegular.setSpecificJudgeParam(allProjectDemeritInfo.getSpecificJudgeParam());
specificRegular.setVoiceBroadcastText(allProjectDemeritInfo.getVoiceBroadcastText());
specificRegular.setDemeritState(allProjectDemeritInfo.getDemeritState());
specificRegular.setProjectId(allProjectDemeritInfo.getProjectId());
specificRegular.setAudioFileId(allProjectDemeritInfo.getAudioFileId());
specificRegular.setRegularId(regular.getRegularId());
specificRegular.setEnableState(1); //表示启用
return specificRegular;
}).collect(Collectors.toList());
CompletableFuture.runAsync(() -> {
try {
boolean batch = specificRegularService.saveBatch(specificRegularList);
if (!batch) {
throw new RuntimeException("插入数据失败!");
}
springToGameService.reloadAddOneRegularWithSpecificRegular(regular, specificRegularList);
log.info("插入数据成功!");
} catch (Exception e) {
log.error("批量新增规则失败!", e);
throw new RuntimeException("批量新增规则失败!");
}
}).exceptionally(e -> {
log.error("异步操作失败!", e);
return new Result<>(ResultCode.FAIL);
});
return new Result<>(ResultCode.SUCCESS);
} else {
log.error("批量新增规则失败!");
return new Result<>(ResultCode.FAIL);
}
}
注意事项
- 异步操作中,如果出现异常,请在
exceptionally方法中进行处理,并返回相应的错误码和错误信息。 - 为了提高接口的安全性,建议对请求参数进行校验,并对返回值进行统一格式化处理。
- 在实际开发中,请根据项目的具体需求对接口进行完善。
原文地址: https://www.cveoy.top/t/topic/b4PM 著作权归作者所有。请勿转载和采集!