From 30f8d484d15f613b348912b4681f812434057dab Mon Sep 17 00:00:00 2001 From: caoqianming Date: Mon, 1 Sep 2025 13:17:35 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20batchlog=20batches=5Fto=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/wpm/models.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/apps/wpm/models.py b/apps/wpm/models.py index 809ad22e..a1766aec 100644 --- a/apps/wpm/models.py +++ b/apps/wpm/models.py @@ -16,6 +16,7 @@ from django.db.models import Max import re from django.db.models import Q import django.utils.timezone as timezone +from apps.utils.sql import query_all_dict # Create your models here. class SfLog(CommonADModel): @@ -803,7 +804,30 @@ class BatchLog(BaseModel): @classmethod def batches_to(cls, batch:str): - batches = BatchLog.objects.filter(source__batch=batch, relation_type="split").values_list("target__batch", flat=True).distinct() + + # query = """ + # SELECT batch FROM wpm_batchst + # WHERE batch ~ %s + # """ + query = """ + SELECT batch + FROM wpm_batchst + WHERE batch ~ %s + ORDER BY + -- 先按前缀部分排序(例如 'A') + SUBSTRING(batch FROM '^(.*)-') DESC, + -- 再按后缀的数值部分排序(将 '2', '11' 转为整数) + CAST(SUBSTRING(batch FROM '-([0-9]+)$') AS INTEGER) DESC + """ # 排序可在sql层处理 + query_ = """SELECT batch FROM wpm_batchst WHERE batch ~ %s""" + pattern = f'^{batch}-[0-9]+$' + + """可以用如下方法直接查询 + """ + # batches = BatchLog.objects.filter(source__batch=batch, relation_type="split").values_list("target__batch", flat=True).distinct() + # batches = sorted(list(batches), key=custom_key) + batches_r = query_all_dict(query_, params=(pattern,)) + batches = [b["batch"] for b in batches_r] batches = sorted(list(batches), key=custom_key) last_batch_num = None if batches: