From 87ceb1c46804c85c7af0748204011b7cd499079d Mon Sep 17 00:00:00 2001 From: martsforever Date: Wed, 15 Oct 2025 19:47:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20general=E8=87=AA=E5=8A=A8=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E5=88=9B=E5=BB=BA=E6=97=B6=E9=97=B4=E4=B8=8E=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/general/general_utils/sql_service.py | 27 ++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/app/general/general_utils/sql_service.py b/app/general/general_utils/sql_service.py index 42b4bf4..a844c6a 100644 --- a/app/general/general_utils/sql_service.py +++ b/app/general/general_utils/sql_service.py @@ -1,3 +1,4 @@ +import datetime import traceback from app.config.env import env @@ -121,10 +122,19 @@ async def insert(session: AsyncSessionDep, query_config, module_config, debug_da row_id = get_value(row, 'id', None) + # 自动设置row_id if row_id is None: row_id = (await get_id(session, 1))[0] row['id'] = row_id + # 自动设置创建时间 + if get_value(row, 'createdAt', None): + row['createdAt'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + + # 自动设置更新时间 + if get_value(row, 'updatedAt', None): + row['updatedAt'] = row['createdAt'] + await invoke_interceptor('before_insert', module_config['base'], row, session) try: @@ -170,12 +180,22 @@ async def batch_insert(session: AsyncSessionDep, query_config, module_config, de } create_convertor(module_config)['encode_list'](rows) + # 自动填充row id rows_without_id = [row for row in rows if get_value(row, 'id', None) is None] if rows_without_id: new_id_list = await get_id(session, len(rows_without_id)) for index, row in enumerate(rows_without_id): row['id'] = new_id_list[index] + for row in rows: + # 自动设置创建时间 + if get_value(row, 'createdAt', None): + row['createdAt'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + + # 自动设置更新时间 + if get_value(row, 'updatedAt', None): + row['updatedAt'] = row['createdAt'] + await invoke_interceptor('before_batch_insert', module_config['base'], rows, session) try: @@ -230,6 +250,8 @@ async def update(session: AsyncSessionDep, query_config, module_config, debug_da "error": "row is missing field: id", } + row['updatedAt'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + create_convertor(module_config)['encode_list']([row]) await invoke_interceptor('before_update', module_config['base'], row, session) @@ -286,6 +308,11 @@ async def batch_update(session: AsyncSessionDep, query_config, module_config, de "rows": rows_without_id, } + current_datetime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + + for row in rows: + row['updatedAt'] = current_datetime + await invoke_interceptor('before_batch_update', module_config['base'], rows, session) try: