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

优化

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