feat: 优化代码生成器功能并修复多个问题

- 前端表格列添加溢出提示工具
- 后端登录IP归属地查询与设置统一处理
- 分页服务明确仅用于非SQL数据源场景
- 代码生成器添加字段筛选和帮助面板
- 修复菜单高亮和布局切换问题
- 更新文档中的BUG状态和解决方案
- 优化AI聊天界面样式和交互
- 添加数据库分页支持到多个模块
- 代码生成器表单校验和流程优化
- 修复PostgreSQL日期类型问题
This commit is contained in:
zhangtao
2026-03-30 00:37:16 +08:00
parent c5ec09f06f
commit 837d8a5aa7
56 changed files with 1931 additions and 663 deletions
@@ -14,14 +14,18 @@ from app.core.base_schema import BatchSetAvailable
from .service import {{ class_name }}Service
from .schema import {{ class_name }}CreateSchema, {{ class_name }}UpdateSchema, {{ class_name }}QueryParam
{{ class_name }}Router = APIRouter(prefix='/{{ business_name }}', tags=["{{ function_name }}模块"])
{% if is_example_style %}
{{ class_name }}Router = APIRouter(prefix='/{{ module_name }}{% if business_name %}/{{ business_name }}{% endif %}', tags=["{{ function_name }}模块"])
{% else %}
{{ class_name }}Router = APIRouter(prefix='/{{ business_name }}', tags=["{{ function_name }}模块"])
{% endif %}
@{{ class_name }}Router.get(
"/detail/{id}",
summary="获取{{ function_name }}详情",
description="获取{{ function_name }}详情"
)
async def get_{{ business_name }}_detail_controller(
async def get_{{ business_name_slug }}_detail_controller(
id: int = Path(..., description="ID"),
auth: AuthSchema = Depends(AuthPermission(["{{ permission_prefix }}:query"]))
) -> JSONResponse:
@@ -35,7 +39,7 @@ async def get_{{ business_name }}_detail_controller(
返回:
- JSONResponse - 包含{{ function_name }}详情的JSON响应
"""
result_dict = await {{ class_name }}Service.detail_{{ business_name }}_service(auth=auth, id=id)
result_dict = await {{ class_name }}Service.detail_{{ business_name_slug }}_service(auth=auth, id=id)
log.info(f"获取{{ function_name }}详情成功 {id}")
return SuccessResponse(data=result_dict, msg="获取{{ function_name }}详情成功")
@@ -44,7 +48,7 @@ async def get_{{ business_name }}_detail_controller(
summary="查询{{ function_name }}列表",
description="查询{{ function_name }}列表"
)
async def get_{{ business_name }}_list_controller(
async def get_{{ business_name_slug }}_list_controller(
page: PaginationQueryParam = Depends(),
search: {{ class_name }}QueryParam = Depends(),
auth: AuthSchema = Depends(AuthPermission(["{{ permission_prefix }}:query"]))
@@ -60,7 +64,7 @@ async def get_{{ business_name }}_list_controller(
返回:
- JSONResponse - 包含{{ function_name }}列表的JSON响应
"""
result_dict = await {{ class_name }}Service.page_{{ business_name }}_service(
result_dict = await {{ class_name }}Service.page_{{ business_name_slug }}_service(
auth=auth,
page_no=page.page_no if page.page_no is not None else 1,
page_size=page.page_size if page.page_size is not None else 10,
@@ -75,7 +79,7 @@ async def get_{{ business_name }}_list_controller(
summary="创建{{ function_name }}",
description="创建{{ function_name }}"
)
async def create_{{ business_name }}_controller(
async def create_{{ business_name_slug }}_controller(
data: {{ class_name }}CreateSchema,
auth: AuthSchema = Depends(AuthPermission(["{{ permission_prefix }}:create"]))
) -> JSONResponse:
@@ -89,7 +93,7 @@ async def create_{{ business_name }}_controller(
返回:
- JSONResponse - 包含创建{{ function_name }}结果的JSON响应
"""
result_dict = await {{ class_name }}Service.create_{{ business_name }}_service(auth=auth, data=data)
result_dict = await {{ class_name }}Service.create_{{ business_name_slug }}_service(auth=auth, data=data)
log.info("创建{{ function_name }}成功")
return SuccessResponse(data=result_dict, msg="创建{{ function_name }}成功")
@@ -98,7 +102,7 @@ async def create_{{ business_name }}_controller(
summary="修改{{ function_name }}",
description="修改{{ function_name }}"
)
async def update_{{ business_name }}_controller(
async def update_{{ business_name_slug }}_controller(
data: {{ class_name }}UpdateSchema,
id: int = Path(..., description="ID"),
auth: AuthSchema = Depends(AuthPermission(["{{ permission_prefix }}:update"]))
@@ -114,7 +118,7 @@ async def update_{{ business_name }}_controller(
返回:
- JSONResponse - 包含修改{{ function_name }}结果的JSON响应
"""
result_dict = await {{ class_name }}Service.update_{{ business_name }}_service(auth=auth, id=id, data=data)
result_dict = await {{ class_name }}Service.update_{{ business_name_slug }}_service(auth=auth, id=id, data=data)
log.info("修改{{ function_name }}成功")
return SuccessResponse(data=result_dict, msg="修改{{ function_name }}成功")
@@ -123,7 +127,7 @@ async def update_{{ business_name }}_controller(
summary="删除{{ function_name }}",
description="删除{{ function_name }}"
)
async def delete_{{ business_name }}_controller(
async def delete_{{ business_name_slug }}_controller(
ids: list[int] = Body(..., description="ID列表"),
auth: AuthSchema = Depends(AuthPermission(["{{ permission_prefix }}:delete"]))
) -> JSONResponse:
@@ -137,7 +141,7 @@ async def delete_{{ business_name }}_controller(
返回:
- JSONResponse - 包含删除{{ function_name }}结果的JSON响应
"""
await {{ class_name }}Service.delete_{{ business_name }}_service(auth=auth, ids=ids)
await {{ class_name }}Service.delete_{{ business_name_slug }}_service(auth=auth, ids=ids)
log.info(f"删除{{ function_name }}成功: {ids}")
return SuccessResponse(msg="删除{{ function_name }}成功")
@@ -146,7 +150,7 @@ async def delete_{{ business_name }}_controller(
summary="批量修改{{ function_name }}状态",
description="批量修改{{ function_name }}状态"
)
async def batch_set_available_{{ business_name }}_controller(
async def batch_set_available_{{ business_name_slug }}_controller(
data: BatchSetAvailable,
auth: AuthSchema = Depends(AuthPermission(["{{ permission_prefix }}:patch"]))
) -> JSONResponse:
@@ -160,7 +164,7 @@ async def batch_set_available_{{ business_name }}_controller(
返回:
- JSONResponse - 包含批量修改{{ function_name }}状态结果的JSON响应
"""
await {{ class_name }}Service.set_available_{{ business_name }}_service(auth=auth, data=data)
await {{ class_name }}Service.set_available_{{ business_name_slug }}_service(auth=auth, data=data)
log.info(f"批量修改{{ function_name }}状态成功: {data.ids}")
return SuccessResponse(msg="批量修改{{ function_name }}状态成功")
@@ -169,7 +173,7 @@ async def batch_set_available_{{ business_name }}_controller(
summary="导出{{ function_name }}",
description="导出{{ function_name }}"
)
async def export_{{ business_name }}_list_controller(
async def export_{{ business_name_slug }}_list_controller(
search: {{ class_name }}QueryParam = Depends(),
auth: AuthSchema = Depends(AuthPermission(["{{ permission_prefix }}:export"]))
) -> StreamingResponse:
@@ -183,8 +187,8 @@ async def export_{{ business_name }}_list_controller(
返回:
- StreamingResponse - 包含导出{{ function_name }}数据的流式响应
"""
result_dict_list = await {{ class_name }}Service.list_{{ business_name }}_service(search=search, auth=auth)
export_result = await {{ class_name }}Service.batch_export_{{ business_name }}_service(obj_list=result_dict_list)
result_dict_list = await {{ class_name }}Service.list_{{ business_name_slug }}_service(search=search, auth=auth)
export_result = await {{ class_name }}Service.batch_export_{{ business_name_slug }}_service(obj_list=result_dict_list)
log.info('导出{{ function_name }}成功')
return StreamResponse(
data=bytes2file_response(export_result),
@@ -197,7 +201,7 @@ async def export_{{ business_name }}_list_controller(
summary="导入{{ function_name }}",
description="导入{{ function_name }}"
)
async def import_{{ business_name }}_list_controller(
async def import_{{ business_name_slug }}_list_controller(
file: UploadFile,
auth: AuthSchema = Depends(AuthPermission(["{{ permission_prefix }}:import"]))
) -> JSONResponse:
@@ -211,7 +215,7 @@ async def import_{{ business_name }}_list_controller(
返回:
- JSONResponse - 包含导入{{ function_name }}结果的JSON响应
"""
batch_import_result = await {{ class_name }}Service.batch_import_{{ business_name }}_service(file=file, auth=auth, update_support=True)
batch_import_result = await {{ class_name }}Service.batch_import_{{ business_name_slug }}_service(file=file, auth=auth, update_support=True)
log.info("导入{{ function_name }}成功")
return SuccessResponse(data=batch_import_result, msg="导入{{ function_name }}成功")
@@ -221,14 +225,14 @@ async def import_{{ business_name }}_list_controller(
description="获取{{ function_name }}导入模板",
dependencies=[Depends(AuthPermission(["{{ permission_prefix }}:download"]))]
)
async def export_{{ business_name }}_template_controller() -> StreamingResponse:
async def export_{{ business_name_slug }}_template_controller() -> StreamingResponse:
"""
获取{{ function_name }}导入模板接口
返回:
- StreamingResponse - 包含{{ function_name }}导入模板的流式响应
"""
import_template_result = await {{ class_name }}Service.import_template_download_{{ business_name }}_service()
import_template_result = await {{ class_name }}Service.import_template_download_{{ business_name_slug }}_service()
log.info('获取{{ function_name }}导入模板成功')
return StreamResponse(
data=bytes2file_response(import_template_result),
@@ -20,7 +20,7 @@ class {{ class_name }}CRUD(CRUDBase[{{ class_name }}Model, {{ class_name }}Creat
"""
super().__init__(model={{ class_name }}Model, auth=auth)
async def get_by_id_{{ business_name }}_crud(self, id: int, preload: list | None = None) -> {{ class_name }}Model | None:
async def get_by_id_{{ business_name_slug }}_crud(self, id: int, preload: list | None = None) -> {{ class_name }}Model | None:
"""
详情
@@ -33,7 +33,7 @@ class {{ class_name }}CRUD(CRUDBase[{{ class_name }}Model, {{ class_name }}Creat
"""
return await self.get(id=id, preload=preload)
async def list_{{ business_name }}_crud(self, search: dict | None = None, order_by: list[dict] | None = None, preload: list | None = None) -> Sequence[{{ class_name }}Model]:
async def list_{{ business_name_slug }}_crud(self, search: dict | None = None, order_by: list[dict] | None = None, preload: list | None = None) -> Sequence[{{ class_name }}Model]:
"""
列表查询
@@ -47,7 +47,7 @@ class {{ class_name }}CRUD(CRUDBase[{{ class_name }}Model, {{ class_name }}Creat
"""
return await self.list(search=search, order_by=order_by, preload=preload)
async def create_{{ business_name }}_crud(self, data: {{ class_name }}CreateSchema) -> {{ class_name }}Model | None:
async def create_{{ business_name_slug }}_crud(self, data: {{ class_name }}CreateSchema) -> {{ class_name }}Model | None:
"""
创建
@@ -59,7 +59,7 @@ class {{ class_name }}CRUD(CRUDBase[{{ class_name }}Model, {{ class_name }}Creat
"""
return await self.create(data=data)
async def update_{{ business_name }}_crud(self, id: int, data: {{ class_name }}UpdateSchema) -> {{ class_name }}Model | None:
async def update_{{ business_name_slug }}_crud(self, id: int, data: {{ class_name }}UpdateSchema) -> {{ class_name }}Model | None:
"""
更新
@@ -72,7 +72,7 @@ class {{ class_name }}CRUD(CRUDBase[{{ class_name }}Model, {{ class_name }}Creat
"""
return await self.update(id=id, data=data)
async def delete_{{ business_name }}_crud(self, ids: list[int]) -> None:
async def delete_{{ business_name_slug }}_crud(self, ids: list[int]) -> None:
"""
批量删除
@@ -84,7 +84,7 @@ class {{ class_name }}CRUD(CRUDBase[{{ class_name }}Model, {{ class_name }}Creat
"""
return await self.delete(ids=ids)
async def set_available_{{ business_name }}_crud(self, ids: list[int], status: str) -> None:
async def set_available_{{ business_name_slug }}_crud(self, ids: list[int], status: str) -> None:
"""
批量设置可用状态
@@ -97,7 +97,7 @@ class {{ class_name }}CRUD(CRUDBase[{{ class_name }}Model, {{ class_name }}Creat
"""
return await self.set(ids=ids, status=status)
async def page_{{ business_name }}_crud(self, offset: int, limit: int, order_by: list[dict] | None = None, search: dict | None = None, preload: list | None = None) -> dict:
async def page_{{ business_name_slug }}_crud(self, offset: int, limit: int, order_by: list[dict] | None = None, search: dict | None = None, preload: list | None = None) -> dict:
"""
分页查询
@@ -25,7 +25,7 @@ class {{ class_name }}Service:
"""
@classmethod
async def detail_{{ business_name }}_service(cls, auth: AuthSchema, id: int) -> dict:
async def detail_{{ business_name_slug }}_service(cls, auth: AuthSchema, id: int) -> dict:
"""
详情
@@ -36,13 +36,13 @@ class {{ class_name }}Service:
返回:
- dict - 数据详情
"""
obj = await {{ class_name }}CRUD(auth).get_by_id_{{ business_name }}_crud(id=id)
obj = await {{ class_name }}CRUD(auth).get_by_id_{{ business_name_slug }}_crud(id=id)
if not obj:
raise CustomException(msg="该数据不存在")
return {{ class_name }}OutSchema.model_validate(obj).model_dump()
@classmethod
async def list_{{ business_name }}_service(cls, auth: AuthSchema, search: {{ class_name }}QueryParam | None = None, order_by: list[dict] | None = None) -> list[dict]:
async def list_{{ business_name_slug }}_service(cls, auth: AuthSchema, search: {{ class_name }}QueryParam | None = None, order_by: list[dict] | None = None) -> list[dict]:
"""
列表查询
@@ -55,11 +55,11 @@ class {{ class_name }}Service:
- list[dict] - 数据列表
"""
search_dict = search.__dict__ if search else None
obj_list = await {{ class_name }}CRUD(auth).list_{{ business_name }}_crud(search=search_dict, order_by=order_by)
obj_list = await {{ class_name }}CRUD(auth).list_{{ business_name_slug }}_crud(search=search_dict, order_by=order_by)
return [{{ class_name }}OutSchema.model_validate(obj).model_dump() for obj in obj_list]
@classmethod
async def page_{{ business_name }}_service(cls, auth: AuthSchema, page_no: int, page_size: int, search: {{ class_name }}QueryParam | None = None, order_by: list[dict] | None = None) -> dict:
async def page_{{ business_name_slug }}_service(cls, auth: AuthSchema, page_no: int, page_size: int, search: {{ class_name }}QueryParam | None = None, order_by: list[dict] | None = None) -> dict:
"""
分页查询(数据库分页)
@@ -76,7 +76,7 @@ class {{ class_name }}Service:
search_dict = search.__dict__ if search else {}
order_by_list = order_by or [{'{{ pk_column_name }}': 'asc'}]
offset = (page_no - 1) * page_size
result = await {{ class_name }}CRUD(auth).page_{{ business_name }}_crud(
result = await {{ class_name }}CRUD(auth).page_{{ business_name_slug }}_crud(
offset=offset,
limit=page_size,
order_by=order_by_list,
@@ -85,7 +85,7 @@ class {{ class_name }}Service:
return result
@classmethod
async def create_{{ business_name }}_service(cls, auth: AuthSchema, data: {{ class_name }}CreateSchema) -> dict:
async def create_{{ business_name_slug }}_service(cls, auth: AuthSchema, data: {{ class_name }}CreateSchema) -> dict:
"""
创建
@@ -103,11 +103,11 @@ class {{ class_name }}Service:
raise CustomException(msg='创建失败,{{ column.column_comment }}已存在')
{% endif %}
{% endfor %}
obj = await {{ class_name }}CRUD(auth).create_{{ business_name }}_crud(data=data)
obj = await {{ class_name }}CRUD(auth).create_{{ business_name_slug }}_crud(data=data)
return {{ class_name }}OutSchema.model_validate(obj).model_dump()
@classmethod
async def update_{{ business_name }}_service(cls, auth: AuthSchema, id: int, data: {{ class_name }}UpdateSchema) -> dict:
async def update_{{ business_name_slug }}_service(cls, auth: AuthSchema, id: int, data: {{ class_name }}UpdateSchema) -> dict:
"""
更新
@@ -120,7 +120,7 @@ class {{ class_name }}Service:
- dict - 更新结果
"""
# 检查数据是否存在
obj = await {{ class_name }}CRUD(auth).get_by_id_{{ business_name }}_crud(id=id)
obj = await {{ class_name }}CRUD(auth).get_by_id_{{ business_name_slug }}_crud(id=id)
if not obj:
raise CustomException(msg='更新失败,该数据不存在')
@@ -133,11 +133,11 @@ class {{ class_name }}Service:
{% endif %}
{% endfor %}
obj = await {{ class_name }}CRUD(auth).update_{{ business_name }}_crud(id=id, data=data)
obj = await {{ class_name }}CRUD(auth).update_{{ business_name_slug }}_crud(id=id, data=data)
return {{ class_name }}OutSchema.model_validate(obj).model_dump()
@classmethod
async def delete_{{ business_name }}_service(cls, auth: AuthSchema, ids: list[int]) -> None:
async def delete_{{ business_name_slug }}_service(cls, auth: AuthSchema, ids: list[int]) -> None:
"""
删除
@@ -151,13 +151,13 @@ class {{ class_name }}Service:
if len(ids) < 1:
raise CustomException(msg='删除失败,删除对象不能为空')
for id in ids:
obj = await {{ class_name }}CRUD(auth).get_by_id_{{ business_name }}_crud(id=id)
obj = await {{ class_name }}CRUD(auth).get_by_id_{{ business_name_slug }}_crud(id=id)
if not obj:
raise CustomException(msg=f'删除失败,ID为{id}的数据不存在')
await {{ class_name }}CRUD(auth).delete_{{ business_name }}_crud(ids=ids)
await {{ class_name }}CRUD(auth).delete_{{ business_name_slug }}_crud(ids=ids)
@classmethod
async def set_available_{{ business_name }}_service(cls, auth: AuthSchema, data: BatchSetAvailable) -> None:
async def set_available_{{ business_name_slug }}_service(cls, auth: AuthSchema, data: BatchSetAvailable) -> None:
"""
批量设置状态
@@ -168,10 +168,10 @@ class {{ class_name }}Service:
返回:
- None
"""
await {{ class_name }}CRUD(auth).set_available_{{ business_name }}_crud(ids=data.ids, status=data.status)
await {{ class_name }}CRUD(auth).set_available_{{ business_name_slug }}_crud(ids=data.ids, status=data.status)
@classmethod
async def batch_export_{{ business_name }}_service(cls, obj_list: list[dict]) -> bytes:
async def batch_export_{{ business_name_slug }}_service(cls, obj_list: list[dict]) -> bytes:
"""
批量导出
@@ -201,7 +201,7 @@ class {{ class_name }}Service:
return ExcelUtil.export_list2excel(list_data=data, mapping_dict=mapping_dict)
@classmethod
async def batch_import_{{ business_name }}_service(cls, auth: AuthSchema, file: UploadFile, update_support: bool = False) -> str:
async def batch_import_{{ business_name_slug }}_service(cls, auth: AuthSchema, file: UploadFile, update_support: bool = False) -> str:
"""
批量导入
@@ -279,7 +279,7 @@ class {{ class_name }}Service:
{% endif %}
{% endfor %}
await {{ class_name }}CRUD(auth).create_{{ business_name }}_crud(data=create_schema)
await {{ class_name }}CRUD(auth).create_{{ business_name_slug }}_crud(data=create_schema)
success_count += 1
except Exception as e:
error_msgs.append(f"第{count}行: {str(e)}")
@@ -295,7 +295,7 @@ class {{ class_name }}Service:
raise CustomException(msg=f"导入失败: {str(e)}")
@classmethod
async def import_template_download_{{ business_name }}_service(cls) -> bytes:
async def import_template_download_{{ business_name_slug }}_service(cls) -> bytes:
"""
下载导入模板