Commit 774fb97d authored by 黎博's avatar 黎博

优化

parent 3dd2e2c9
...@@ -84,28 +84,37 @@ public class PipelineController { ...@@ -84,28 +84,37 @@ public class PipelineController {
@Transactional @Transactional
@PostMapping("/add") @PostMapping("/add")
public JsonResult addRobotAndPipeline(@RequestBody DingRobotPipelineVo dingRobotPipelineVo) { public JsonResult addRobotAndPipeline(@RequestBody DingRobotPipelineVo dingRobotPipelineVo) {
DingRobot dingRobot = dingRobotPipelineVo.getDingRobot(); try {
String projectName = dingRobot.getProjectName(); DingRobot dingRobot = dingRobotPipelineVo.getDingRobot();
String jiraProjectKey = dingRobot.getJiraProjectKey(); String projectName = dingRobot.getProjectName();
QueryWrapper<DingRobot> dingRobotQueryWrapper = new QueryWrapper<>(); String jiraProjectKey = dingRobot.getJiraProjectKey();
dingRobotQueryWrapper.eq("jira_project_key", jiraProjectKey); QueryWrapper<DingRobot> dingRobotQueryWrapper = new QueryWrapper<>();
dingRobotQueryWrapper.eq("project_name", projectName); dingRobotQueryWrapper.eq("jira_project_key", jiraProjectKey);
// 根据jira project key和项目名判断是否已存在 dingRobotQueryWrapper.eq("project_name", projectName);
if (dingRobotService.getOne(dingRobotQueryWrapper) != null) { // 根据jira project key和项目名判断是否已存在
return JsonResult.buildErrorStateResult("项目已存在", false); if (dingRobotService.getOne(dingRobotQueryWrapper) != null) {
} return JsonResult.buildErrorStateResult("项目已存在", false);
boolean dingRobotResult = dingRobotService.save(dingRobot); }
if (!dingRobotResult) { boolean dingRobotResult = dingRobotService.save(dingRobot);
return JsonResult.buildErrorStateResult("服务器异常,保存失败!", false); if (!dingRobotResult) {
} return JsonResult.buildErrorStateResult("服务器异常,保存失败!", false);
// 获取该robot id,并赋值给pipelineList }
DingRobot dingRobotNew = dingRobotService.getOne(dingRobotQueryWrapper); // 获取该robot id,并赋值给pipelineList
Integer dingRobotId = dingRobotNew.getId(); DingRobot dingRobotNew = dingRobotService.getOne(dingRobotQueryWrapper);
List<Pipeline> pipelineList = dingRobotPipelineVo.getPipelineList(); Integer dingRobotId = dingRobotNew.getId();
for (Pipeline pipeline: pipelineList) { List<Pipeline> pipelineList = dingRobotPipelineVo.getPipelineList();
pipeline.setDingRobotId(dingRobotId); // 判断pipelineList是否为空
if (pipelineList.size() > 0) {
for (Pipeline pipeline: pipelineList) {
pipeline.setDingRobotId(dingRobotId);
}
pipelineService.saveBatch(pipelineList);
}
return JsonResult.buildSuccessResult(true);
} catch (Exception e) {
e.printStackTrace();
return JsonResult.buildErrorStateResult("新增失败", false);
} }
return JsonResult.buildSuccessResult(pipelineService.saveBatch(pipelineList));
} }
/** /**
...@@ -116,37 +125,48 @@ public class PipelineController { ...@@ -116,37 +125,48 @@ public class PipelineController {
@Transactional @Transactional
@PostMapping("/modify") @PostMapping("/modify")
public JsonResult modifyPipeline(@RequestBody DingRobotPipelineVo dingRobotPipelineVo) { public JsonResult modifyPipeline(@RequestBody DingRobotPipelineVo dingRobotPipelineVo) {
DingRobot dingRobot = dingRobotPipelineVo.getDingRobot(); try {
boolean dingRobotResult = dingRobotService.saveOrUpdate(dingRobot); DingRobot dingRobot = dingRobotPipelineVo.getDingRobot();
if (!dingRobotResult) { boolean dingRobotResult = dingRobotService.saveOrUpdate(dingRobot);
return JsonResult.buildErrorStateResult("修改失败!", false); if (!dingRobotResult) {
} return JsonResult.buildErrorStateResult("修改失败!", false);
Integer dingRobotId = dingRobot.getId();
List<Pipeline> pipelineList = dingRobotPipelineVo.getPipelineList();
QueryWrapper<Pipeline> pipelineQueryWrapper = new QueryWrapper<>();
pipelineQueryWrapper.eq("ding_robot_id", dingRobotId);
List<Pipeline> pipelineListInDb = pipelineService.list(pipelineQueryWrapper);
// 判断是否有删除的部分
for (Pipeline pipelineDb: pipelineListInDb) {
boolean flag = false;
for (Pipeline pipeline: pipelineList) {
if (pipelineDb.getId().equals(pipeline.getId())) {
flag = true;
break;
}
} }
if (!flag) { Integer dingRobotId = dingRobot.getId();
pipelineService.removeById(pipelineDb.getId()); List<Pipeline> pipelineList = dingRobotPipelineVo.getPipelineList();
QueryWrapper<Pipeline> pipelineQueryWrapper = new QueryWrapper<>();
pipelineQueryWrapper.eq("ding_robot_id", dingRobotId);
List<Pipeline> pipelineListInDb = pipelineService.list(pipelineQueryWrapper);
// 判断是否有删除的部分
if (pipelineListInDb.size() > 0) {
for (Pipeline pipelineDb: pipelineListInDb) {
boolean flag = false;
if (pipelineList.size() > 0) {
for (Pipeline pipeline: pipelineList) {
if (pipelineDb.getId().equals(pipeline.getId())) {
flag = true;
break;
}
}
}
if (!flag) {
pipelineService.removeById(pipelineDb.getId());
}
}
} }
}
// 判断是否有新增的 // 判断是否有新增的
for (Pipeline pipeline: pipelineList) { for (Pipeline pipeline: pipelineList) {
if (pipeline.getId() == null) { if (pipeline.getId() == null) {
pipeline.setDingRobotId(dingRobotId); pipeline.setDingRobotId(dingRobotId);
}
} }
// 剩下的直接更新
pipelineService.saveOrUpdateBatch(pipelineList);
return JsonResult.buildSuccessResult(true);
} catch (Exception e) {
e.printStackTrace();
return JsonResult.buildErrorStateResult("修改失败!", false);
} }
// 剩下的直接更新
return JsonResult.buildSuccessResult(pipelineService.saveOrUpdateBatch(pipelineList));
} }
/** /**
......
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