diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/TaskVariableDAO.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/TaskVariableDAO.java index 03c72ed2df..8fc79aa8c9 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/TaskVariableDAO.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/TaskVariableDAO.java @@ -92,6 +92,14 @@ public interface TaskVariableDAO { */ boolean deleteVariableById(long parentId, long id); + /** + * 根据 父资源ID 删除变量信息 + * + * @param parentId 父资源ID + * @return 是否删除成功 + */ + int deleteVariableByParentId(long parentId); + /** * 保留变量 ID 批量插入变量信息 * diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/plan/impl/TaskPlanVariableDAOImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/plan/impl/TaskPlanVariableDAOImpl.java index 56267ce0af..6465292c85 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/plan/impl/TaskPlanVariableDAOImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/plan/impl/TaskPlanVariableDAOImpl.java @@ -194,6 +194,13 @@ public boolean deleteVariableById(long parentId, long id) { return 1 == context.deleteFrom(TABLE).where(conditions).limit(1).execute(); } + @Override + public int deleteVariableByParentId(long parentId) { + List conditions = new ArrayList<>(); + conditions.add(TABLE.PLAN_ID.eq(ULong.valueOf(parentId))); + return context.deleteFrom(TABLE).where(conditions).execute(); + } + @Override public boolean batchInsertVariableWithId(List variableList) { return false; diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/template/impl/TaskTemplateVariableDAOImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/template/impl/TaskTemplateVariableDAOImpl.java index 02d3a07eaa..ad502719bf 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/template/impl/TaskTemplateVariableDAOImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/template/impl/TaskTemplateVariableDAOImpl.java @@ -219,6 +219,13 @@ public boolean deleteVariableById(long parentId, long id) { return 1 == context.deleteFrom(TABLE).where(conditions).limit(1).execute(); } + @Override + public int deleteVariableByParentId(long parentId) { + List conditions = new ArrayList<>(); + conditions.add(TABLE.TEMPLATE_ID.eq(ULong.valueOf(parentId))); + return context.deleteFrom(TABLE).where(conditions).execute(); + } + @Override public boolean batchInsertVariableWithId(List variableList) { InsertValuesStep8 templateVariableList = + taskTemplateVariableService.listVariablesByParentId(taskPlan.getTemplateId()); + // 始终使用模板最新变量作为执行方案变量并更新到DB + setPlanIdForVariables(taskPlan.getId(), templateVariableList); + int deletedVarNum = taskPlanVariableService.deleteVariableByParentId(taskPlan.getId()); + List insertedVarIds = taskPlanVariableService.batchInsertVariable(templateVariableList); + log.debug( + "sync template variable to debug plan:{} taskPlan variable deleted, {} variable inserted:{}", + deletedVarNum, + insertedVarIds.size(), + insertedVarIds + ); taskPlan.setStepList(taskPlanStepService.listStepsByParentId(taskPlan.getId())); taskPlan.setVariableList(taskPlanVariableService.listVariablesByParentId(taskPlan.getId())); return taskPlan; @@ -330,6 +346,13 @@ public TaskPlanInfoDTO getDebugTaskPlan(String username, Long appId, Long templa return taskPlan; } + private void setPlanIdForVariables(Long planId, List variableList) { + if (CollectionUtils.isEmpty(variableList)) { + return; + } + variableList.forEach(variable -> variable.setPlanId(planId)); + } + @Override public List listPlanBasicInfoByIds(Long appId, List planIdList) { List taskPlanInfoList = taskPlanDAO.listTaskPlanByIds(