From 43abcbaa4872da6fcf453df35248dbcebeee7619 Mon Sep 17 00:00:00 2001 From: caoqianming Date: Fri, 9 Jan 2026 15:55:56 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9F=A5=E8=AF=A2-n=E6=89=B9=E6=AC=A1?= =?UTF-8?q?=E4=BB=8E=E6=AD=A3=E5=88=99=E6=94=B9=E7=94=A8like=E4=BB=A5?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/wpm/models.py | 75 +++++++++++++++++++++++++--------------------- apps/wpmw/views.py | 25 ++++++++++++---- 2 files changed, 61 insertions(+), 39 deletions(-) diff --git a/apps/wpm/models.py b/apps/wpm/models.py index e45d71c2..26e37796 100644 --- a/apps/wpm/models.py +++ b/apps/wpm/models.py @@ -17,6 +17,8 @@ import re from django.db.models import Q import django.utils.timezone as timezone from apps.utils.sql import query_all_dict +import logging +myLogger = logging.getLogger('log') # Create your models here. class SfLog(CommonADModel): @@ -837,41 +839,46 @@ class BatchLog(BaseModel): @classmethod def batches_to(cls, batch:str): - - # 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]+$' + SELECT + batch, + CAST(substring(batch FROM LENGTH(%s) + 2) AS INTEGER) AS batch_num + FROM wpm_batchst + WHERE batch LIKE %s AND translate( + substring(batch FROM LENGTH(%s) + 2), + '0123456789', + '' + ) = '' + ORDER BY batch_num DESC + """ - """可以用如下方法直接查询 - """ - # 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: - last_batch = batches[-1] - last_batch_list = last_batch.split("-") - if last_batch_list: - try: - last_batch_num = int(last_batch_list[-1]) - except Exception: - pass - return {"batches": batches, "last_batch_num": last_batch_num, "last_batch": last_batch} - return {"batches": [], "last_batch_num": None, "last_batch": None} + prefix = batch + params = ( + prefix, + f"{prefix}-%", + prefix + ) + + try: + rows = query_all_dict(query, params=params) + except Exception as e: + myLogger.error(f"BatchLog.batches_to error: {(str(e), query, params)}") + raise + + if not rows: + return { + "batches": [], + "last_batch_num": None, + "last_batch": None, + } + + batches = [r["batch"] for r in rows] + last = rows[0] + + return { + "batches": batches, + "last_batch_num": last["batch_num"], + "last_batch": last["batch"], + } \ No newline at end of file diff --git a/apps/wpmw/views.py b/apps/wpmw/views.py index e99d2473..3663f180 100644 --- a/apps/wpmw/views.py +++ b/apps/wpmw/views.py @@ -92,11 +92,20 @@ class WprViewSet(CustomListModelMixin, RetrieveModelMixin, ComplexQueryMixin, Cu # 使用原始sql query = """ SELECT id, number_out FROM wpmw_wpr - WHERE number_out ~ %s order by number_out desc limit 1 + WHERE number_out LIKE %s + AND translate( + substring(number_out FROM LENGTH(%s) + 2), + '0123456789', + '' + ) = '' + order by number_out desc limit 1 """ - pattern = f"^{prefix}[0-9]+$" + params = ( + f"{prefix}-%", + prefix + ) number_outs = [] - wpr_qs_last = query_one_dict(query, [pattern]) + wpr_qs_last = query_one_dict(query, [params]) if wpr_qs_last: number_outs.append(wpr_qs_last["number_out"]) # 查找未出库的记录 @@ -106,9 +115,15 @@ class WprViewSet(CustomListModelMixin, RetrieveModelMixin, ComplexQueryMixin, Cu query2 = """ select mioitemw.id, mioitemw.number_out from inm_mioitemw mioitemw left join inm_mioitem mioitem on mioitem.id = mioitemw.mioitem_id left join inm_mio mio on mio.id = mioitem.mio_id - where mio.submit_time is null and mioitemw.number_out ~ %s order by mioitemw.number_out desc limit 1 + where mio.submit_time is null and mioitemw.number_out LIKE %s + AND translate( + substring(mioitemw.number_out FROM LENGTH(%s) + 2), + '0123456789', + '' + ) = '' + order by mioitemw.number_out desc limit 1 """ - mioitemw_last = query_one_dict(query2, [pattern]) + mioitemw_last = query_one_dict(query2, [params]) if mioitemw_last: number_outs.append(mioitemw_last["number_out"]) if number_outs: