From 3e60c5b5f9efea6b85d65c4dcb71cf9e27530ee2 Mon Sep 17 00:00:00 2001 From: ahui Date: Thu, 17 Jul 2025 11:00:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=A5=E5=85=B7=E5=87=BD=E6=95=B0=E5=90=8C?= =?UTF-8?q?=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/util/currency.py | 78 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 backend/util/currency.py diff --git a/backend/util/currency.py b/backend/util/currency.py new file mode 100644 index 0000000..9c5436e --- /dev/null +++ b/backend/util/currency.py @@ -0,0 +1,78 @@ +from uuid import uuid4 +from datetime import datetime + +from django.db import connection +from django.db.models import Model +from django.core.cache import cache + + +def create_code(model,prefix): + current_date = datetime.now().strftime('%Y%m%d%H%M%S') + code = f"{prefix}{current_date}" + str(uuid4().int)[:6] + return code + +def lock(key): + if callable(key): # @lock + def inner(*args, **kwargs): + with cache.lock(key='lock'): + return key(*args, **kwargs) + inner.__name__ = key.__name__ + else: # @lock(key='aaa') + def inner(func): + def _inner(*args, **kwargs): + with cache.lock(key=key): + return func(*args, **kwargs) + _inner.__name__ = func.__name__ + return _inner + return inner + +def recursion_down_fast(instance:Model, parent='parent', key='id') -> list[int]: + """向下递归instance的所有子级,且返回一维列表,使用sql优化,速度非常快""" + if not instance: + return [] + sql = f""" + WITH RECURSIVE children AS ( + SELECT id, {key} AS param_{key} FROM {instance.__class__._meta.db_table} WHERE {parent}_id = %s UNION ALL + SELECT a.id, a.{key} AS param_{key} FROM {instance.__class__._meta.db_table} a + INNER JOIN children b ON a.{parent}_id = b.id + ) SELECT param_{key} FROM children; + """ + with connection.cursor() as cursor: + cursor.execute(sql, [getattr(instance, key)]) + data = cursor.fetchall() + return [getattr(instance, key), *[i[0] for i in data]] + +def recursion_up_fast(instance: Model, parent='parent', key='id') -> list[int]: + """向上递归instance的所有父级,使用sql优化,速度非常快""" + if not instance: + return [] + sql = f""" + WITH RECURSIVE parents AS ( + SELECT id, {key} as param_{key}, {parent}_id FROM {instance.__class__._meta.db_table} WHERE id = %s UNION ALL + SELECT a.id, a.{key} as param_{key}, a.{parent}_id FROM {instance.__class__._meta.db_table} a + INNER JOIN parents b ON a.id = b.{parent}_id + ) SELECT param_{key} FROM parents; + """ + with connection.cursor() as cursor: + cursor.execute(sql, [getattr(instance, key)]) + data = cursor.fetchall() + return [getattr(instance, key), *[i[0] for i in data]] + +def recursion_up_joint(instance: Model, parent='parent', key='name', joint='/') -> str: + """向上递归instance所有父级并拼接需要的值,返回完整路径,使用sql优化,速度非常快""" + if instance is None: + return '' + sql = f""" + WITH RECURSIVE parents AS ( + SELECT id, {parent}_id, {key}::TEXT AS path FROM {instance.__class__._meta.db_table} WHERE {key} = %s AND id = %s UNION ALL + SELECT a.id, a.{parent}_id, (a.{key} || '{joint}' || b.path)::TEXT FROM {instance.__class__._meta.db_table} a + INNER JOIN parents b ON a.id = b.{parent}_id + ) SELECT path FROM parents where {parent}_id IS NULL; + """ + with connection.cursor() as cursor: + cursor.execute(sql, [getattr(instance, key), instance.pk]) + data = cursor.fetchall() + try: + return data[0][0] + except IndexError: + raise Exception('找不到初始数据') \ No newline at end of file