diff --git a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/constant/Order.java b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/constant/Order.java index be95134776..f510ec1ce3 100644 --- a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/constant/Order.java +++ b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/constant/Order.java @@ -30,7 +30,7 @@ public enum Order { DESCENDING(0), ASCENDING(1); - private int order; + private final int order; Order(int order) { this.order = order; diff --git a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/common/consts/script/ScriptTypeEnum.java b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/common/consts/script/ScriptTypeEnum.java index a8c0dd58e2..bc0f87064d 100644 --- a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/common/consts/script/ScriptTypeEnum.java +++ b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/common/consts/script/ScriptTypeEnum.java @@ -34,8 +34,12 @@ public enum ScriptTypeEnum { /** * 脚本类型枚举类 */ - SHELL(1, "shell", ".sh"), BAT(2, "bat", ".bat"), PERL(3, "perl", ".pl"), PYTHON(4, "python", ".py"), - POWERSHELL(5, "powershell", ".ps1"), SQL(6, "sql", ".sql"); + SHELL(1, "shell", ".sh"), + BAT(2, "bat", ".bat"), + PERL(3, "perl", ".pl"), + PYTHON(4, "python", ".py"), + POWERSHELL(5, "powershell", ".ps1"), + SQL(6, "sql", ".sql"); private final Integer value; private final String name; @@ -98,4 +102,16 @@ public static String getExtByValue(int value) { return ""; } } + + /** + * 获取脚本类型的字典名称的排序 + * + * @return 排序 + */ + public static ScriptTypeEnum[] getScriptTypeNameAscSort() { + // 按照脚本语言名称字典顺序排序.Bat(2)->Perl(3)->Powershell(5)->Python(4)->Shell(1)->SQL(6) + return new ScriptTypeEnum[]{ + BAT, PERL, POWERSHELL, PYTHON, SHELL, SQL + }; + } } diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/ScriptDAOImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/ScriptDAOImpl.java index 4a36e2be6a..ab5d6df3f2 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/ScriptDAOImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/ScriptDAOImpl.java @@ -24,6 +24,7 @@ package com.tencent.bk.job.manage.dao.impl; +import com.tencent.bk.job.common.constant.Order; import com.tencent.bk.job.common.model.BaseSearchCondition; import com.tencent.bk.job.common.model.PageData; import com.tencent.bk.job.common.util.JobUUID; @@ -54,6 +55,7 @@ import org.springframework.stereotype.Repository; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -87,25 +89,29 @@ public PageData listPageScript(ScriptQuery scriptQuery, String orderField = baseSearchCondition.getOrderField(); if ("name".equals(orderField)) { //升序 - if (baseSearchCondition.getOrder() == 1) { + if (baseSearchCondition.getOrder() == Order.ASCENDING.getOrder()) { orderFields.add(TB_SCRIPT.NAME.asc()); } else { orderFields.add(TB_SCRIPT.NAME.desc()); } } else if ("type".equals(orderField)) { - if (baseSearchCondition.getOrder() == 1) { - orderFields.add(TB_SCRIPT.TYPE.asc()); + String sortExpr = Arrays.stream(ScriptTypeEnum.getScriptTypeNameAscSort()) + .map(scriptTypeEnum -> scriptTypeEnum.getValue().toString()) + .reduce((a, b) -> a + "," + b) + .orElse(""); + if (baseSearchCondition.getOrder() == Order.ASCENDING.getOrder()) { + orderFields.add(DSL.field("field({0}," + sortExpr + ")", TB_SCRIPT.TYPE).asc()); } else { - orderFields.add(TB_SCRIPT.TYPE.desc()); + orderFields.add(DSL.field("field({0}," + sortExpr + ")", TB_SCRIPT.TYPE).desc()); } } else if ("creator".equals(orderField)) { - if (baseSearchCondition.getOrder() == 1) { + if (baseSearchCondition.getOrder() == Order.ASCENDING.getOrder()) { orderFields.add(TB_SCRIPT.CREATOR.asc()); } else { orderFields.add(TB_SCRIPT.CREATOR.desc()); } } else if ("lastModifyTime".equals(orderField)) { - if (baseSearchCondition.getOrder() == 1) { + if (baseSearchCondition.getOrder() == Order.ASCENDING.getOrder()) { orderFields.add(TB_SCRIPT.LAST_MODIFY_TIME.asc()); } else { orderFields.add(TB_SCRIPT.LAST_MODIFY_TIME.desc());