feat: general:新增、更新的时候更新创建人更新人id
This commit is contained in:
@@ -8,6 +8,7 @@ from app.general.general_utils.build_insert_sql import build_insert_sql
|
||||
from app.general.general_utils.build_query_sql import build_query_sql
|
||||
from app.general.general_utils.build_update_sql import build_update_sql
|
||||
from app.general.general_utils.sql_utils import get_value, create_convertor
|
||||
from app.model.UserModel import UserServiceModel
|
||||
from app.utils.db_utils import AsyncSessionDep
|
||||
|
||||
|
||||
@@ -36,7 +37,7 @@ async def get_id(session: AsyncSessionDep, len: int | None = 1):
|
||||
return [val for key, val in result[0].items()]
|
||||
|
||||
|
||||
async def list(session: AsyncSessionDep, query_config, module_config, debug_data=[]):
|
||||
async def list(session: AsyncSessionDep, query_config, module_config, debug_data=[], user: UserServiceModel | None = None):
|
||||
conn = await session.connection()
|
||||
|
||||
n_page = get_value(query_config, 'page', 0)
|
||||
@@ -54,7 +55,7 @@ async def list(session: AsyncSessionDep, query_config, module_config, debug_data
|
||||
"orders": get_default_orders(query_config, module_config)
|
||||
}
|
||||
|
||||
await invoke_interceptor('before_list', module_config['base'], target_query_config, session)
|
||||
await invoke_interceptor('before_list', module_config['base'], query_config=target_query_config, session=session, user=user)
|
||||
|
||||
sql, values = build_query_sql(target_query_config, module_config)
|
||||
|
||||
@@ -66,7 +67,7 @@ async def list(session: AsyncSessionDep, query_config, module_config, debug_data
|
||||
|
||||
create_convertor(module_config)['decode_list'](result)
|
||||
|
||||
await invoke_interceptor('after_list', module_config['base'], result, session)
|
||||
await invoke_interceptor('after_list', module_config['base'], rows=result, session=session, user=user)
|
||||
|
||||
if n_only_count:
|
||||
return {
|
||||
@@ -88,7 +89,7 @@ async def list(session: AsyncSessionDep, query_config, module_config, debug_data
|
||||
}
|
||||
|
||||
|
||||
async def item(session: AsyncSessionDep, query_config, module_config, debug_data=[]):
|
||||
async def item(session: AsyncSessionDep, query_config, module_config, debug_data=[], user: UserServiceModel | None = None):
|
||||
target_query_config = {
|
||||
"offset": 0,
|
||||
"size": 1,
|
||||
@@ -102,7 +103,7 @@ async def item(session: AsyncSessionDep, query_config, module_config, debug_data
|
||||
"operator": "="
|
||||
})
|
||||
|
||||
result = await list(session=session, query_config=target_query_config, module_config=module_config, debug_data=debug_data)
|
||||
result = await list(session=session, query_config=target_query_config, module_config=module_config, debug_data=debug_data, user=user)
|
||||
|
||||
if "error" in result:
|
||||
return result
|
||||
@@ -110,7 +111,7 @@ async def item(session: AsyncSessionDep, query_config, module_config, debug_data
|
||||
return {"result": None if "list" not in result or len(result['list']) == 0 else result['list'][0]}
|
||||
|
||||
|
||||
async def insert(session: AsyncSessionDep, query_config, module_config, debug_data=[]):
|
||||
async def insert(session: AsyncSessionDep, query_config, module_config, debug_data=[], user: UserServiceModel | None = None):
|
||||
conn = await session.connection()
|
||||
|
||||
row = get_value(query_config, 'row', None)
|
||||
@@ -135,7 +136,11 @@ async def insert(session: AsyncSessionDep, query_config, module_config, debug_da
|
||||
if get_value(row, 'updatedAt', None):
|
||||
row['updatedAt'] = row['createdAt']
|
||||
|
||||
await invoke_interceptor('before_insert', module_config['base'], row, session)
|
||||
if user:
|
||||
row['createdBy'] = user.id
|
||||
row['updatedBy'] = user.id
|
||||
|
||||
await invoke_interceptor('before_insert', module_config['base'], row=row, session=session, user=user)
|
||||
|
||||
try:
|
||||
sql, values = build_insert_sql(module_config, row)
|
||||
@@ -151,7 +156,7 @@ async def insert(session: AsyncSessionDep, query_config, module_config, debug_da
|
||||
|
||||
item_dict = get_value(result, 'result', None)
|
||||
|
||||
await invoke_interceptor('after_insert', module_config['base'], item_dict, session)
|
||||
await invoke_interceptor('after_insert', module_config['base'], row=item_dict, session=session, user=user)
|
||||
|
||||
if item_dict is not None:
|
||||
return {
|
||||
@@ -170,7 +175,7 @@ async def insert(session: AsyncSessionDep, query_config, module_config, debug_da
|
||||
}
|
||||
|
||||
|
||||
async def batch_insert(session: AsyncSessionDep, query_config, module_config, debug_data=[]):
|
||||
async def batch_insert(session: AsyncSessionDep, query_config, module_config, debug_data=[], user: UserServiceModel | None = None):
|
||||
conn = await session.connection()
|
||||
|
||||
rows = get_value(query_config, 'rows', None)
|
||||
@@ -196,7 +201,11 @@ async def batch_insert(session: AsyncSessionDep, query_config, module_config, de
|
||||
if get_value(row, 'updatedAt', None):
|
||||
row['updatedAt'] = row['createdAt']
|
||||
|
||||
await invoke_interceptor('before_batch_insert', module_config['base'], rows, session)
|
||||
if user:
|
||||
row['createdBy'] = user.id
|
||||
row['updatedBy'] = user.id
|
||||
|
||||
await invoke_interceptor('before_batch_insert', module_config['base'], rows=rows, session=session, user=user)
|
||||
|
||||
try:
|
||||
for row in rows:
|
||||
@@ -214,7 +223,7 @@ async def batch_insert(session: AsyncSessionDep, query_config, module_config, de
|
||||
|
||||
if len(result) > 0:
|
||||
|
||||
await invoke_interceptor('after_batch_insert', module_config['base'], result, session)
|
||||
await invoke_interceptor('after_batch_insert', module_config['base'], rows=result, session=session, user=user)
|
||||
|
||||
return {
|
||||
"result": result
|
||||
@@ -232,7 +241,7 @@ async def batch_insert(session: AsyncSessionDep, query_config, module_config, de
|
||||
}
|
||||
|
||||
|
||||
async def update(session: AsyncSessionDep, query_config, module_config, debug_data=[]):
|
||||
async def update(session: AsyncSessionDep, query_config, module_config, debug_data=[], user: UserServiceModel | None = None):
|
||||
conn = await session.connection()
|
||||
|
||||
row = get_value(query_config, 'row', None)
|
||||
@@ -251,10 +260,12 @@ async def update(session: AsyncSessionDep, query_config, module_config, debug_da
|
||||
}
|
||||
|
||||
row['updatedAt'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
if user:
|
||||
row['updatedBy'] = user.id
|
||||
|
||||
create_convertor(module_config)['encode_list']([row])
|
||||
|
||||
await invoke_interceptor('before_update', module_config['base'], row, session)
|
||||
await invoke_interceptor('before_update', module_config['base'], row=row, session=session, user=user)
|
||||
|
||||
try:
|
||||
sql, values = build_update_sql(module_config, row, row.keys() if update_by_fields else None)
|
||||
@@ -270,7 +281,7 @@ async def update(session: AsyncSessionDep, query_config, module_config, debug_da
|
||||
|
||||
item_dict = get_value(result, 'result', None)
|
||||
|
||||
await invoke_interceptor('after_update', module_config['base'], item_dict, session)
|
||||
await invoke_interceptor('after_update', module_config['base'], row=item_dict, session=session, user=user)
|
||||
|
||||
if item_dict is not None:
|
||||
return {
|
||||
@@ -289,7 +300,7 @@ async def update(session: AsyncSessionDep, query_config, module_config, debug_da
|
||||
}
|
||||
|
||||
|
||||
async def batch_update(session: AsyncSessionDep, query_config, module_config, debug_data=[]):
|
||||
async def batch_update(session: AsyncSessionDep, query_config, module_config, debug_data=[], user: UserServiceModel | None = None):
|
||||
conn = await session.connection()
|
||||
|
||||
rows = get_value(query_config, 'rows', None)
|
||||
@@ -312,8 +323,10 @@ async def batch_update(session: AsyncSessionDep, query_config, module_config, de
|
||||
|
||||
for row in rows:
|
||||
row['updatedAt'] = current_datetime
|
||||
if user:
|
||||
row['updatedBy'] = user.id
|
||||
|
||||
await invoke_interceptor('before_batch_update', module_config['base'], rows, session)
|
||||
await invoke_interceptor('before_batch_update', module_config['base'], rows=rows, session=session, user=user)
|
||||
|
||||
try:
|
||||
for row in rows:
|
||||
@@ -331,7 +344,7 @@ async def batch_update(session: AsyncSessionDep, query_config, module_config, de
|
||||
|
||||
if len(result) > 0:
|
||||
|
||||
await invoke_interceptor('after_batch_update', module_config['base'], result, session)
|
||||
await invoke_interceptor('after_batch_update', module_config['base'], rows=result, session=session, user=user)
|
||||
|
||||
return {
|
||||
"result": result
|
||||
@@ -349,7 +362,7 @@ async def batch_update(session: AsyncSessionDep, query_config, module_config, de
|
||||
}
|
||||
|
||||
|
||||
async def delete(session: AsyncSessionDep, query_config, module_config, debug_data=[]):
|
||||
async def delete(session: AsyncSessionDep, query_config, module_config, debug_data=[], user: UserServiceModel | None = None):
|
||||
conn = await session.connection()
|
||||
|
||||
id = get_value(query_config, 'id', None)
|
||||
@@ -358,7 +371,7 @@ async def delete(session: AsyncSessionDep, query_config, module_config, debug_da
|
||||
"error": "id parameter is missing",
|
||||
}
|
||||
|
||||
await invoke_interceptor('before_delete', module_config['base'], id, session)
|
||||
await invoke_interceptor('before_delete', module_config['base'], query_config=query_config, session=session, user=user)
|
||||
|
||||
try:
|
||||
sql, values = build_delete_sql(module_config, id)
|
||||
@@ -369,7 +382,7 @@ async def delete(session: AsyncSessionDep, query_config, module_config, debug_da
|
||||
|
||||
if deleted_rows >= 1:
|
||||
|
||||
await invoke_interceptor('after_delete', module_config['base'], id, session)
|
||||
await invoke_interceptor('after_delete', module_config['base'], query_config=query_config, session=session, user=user)
|
||||
|
||||
return {"deletedRows": deleted_rows}
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user