perf: 优化代码生成功能
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
{% set pkField = pkColumn.python_field %}
|
||||
{% set pk_field = pkColumn.python_field | camel_to_snake %}
|
||||
{% for column in columns %}
|
||||
{% if column.python_field == "createTime" %}
|
||||
from datetime import datetime
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
from fastapi import APIRouter, Depends, Form, Request
|
||||
from pydantic_validation_decorator import ValidateFields
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
@@ -29,18 +33,18 @@ async def get_{{ moduleName }}_{{ businessName }}_list(
|
||||
{{ businessName }}_page_query: {{ BusinessName }}PageQueryModel = Depends({{ BusinessName }}PageQueryModel.as_query),
|
||||
query_db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
{% if table.crud or table.sub %}
|
||||
{% if table.crud or table.sub %}
|
||||
# 获取分页数据
|
||||
{{ businessName }}_page_query_result = await {{ BusinessName }}Service.get_{{ businessName }}_list_services(query_db, {{ businessName }}_page_query, is_page=True)
|
||||
logger.info('获取成功')
|
||||
|
||||
return ResponseUtil.success(model_content={{ businessName }}_page_query_result)
|
||||
{% elif table.tree %}
|
||||
{% elif table.tree %}
|
||||
{{ businessName }}_query_result = await {{ BusinessName }}Service.get_{{ businessName }}_list_services(query_db, {{ businessName }}_query)
|
||||
logger.info('获取成功')
|
||||
|
||||
return ResponseUtil.success(data={{ businessName }}_query_result)
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
|
||||
|
||||
@{{ businessName }}Controller.post('', dependencies=[Depends(CheckUserInterfaceAuth('{{ permissionPrefix }}:add'))])
|
||||
@@ -52,17 +56,17 @@ async def add_{{ moduleName }}_{{ businessName }}(
|
||||
query_db: AsyncSession = Depends(get_db),
|
||||
current_user: CurrentUserModel = Depends(LoginService.get_current_user),
|
||||
):
|
||||
{% for column in columns %}
|
||||
{% if column.python_field == "createBy" %}
|
||||
{% for column in columns %}
|
||||
{% if column.python_field == "createBy" %}
|
||||
add_{{ businessName }}.create_by = current_user.user.user_name
|
||||
{% elif column.python_field == "createTime" %}
|
||||
{% elif column.python_field == "createTime" %}
|
||||
add_{{ businessName }}.create_time = datetime.now()
|
||||
{% elif column.python_field == "updateBy" %}
|
||||
{% elif column.python_field == "updateBy" %}
|
||||
add_{{ businessName }}.update_by = current_user.user.user_name
|
||||
{% elif column.python_field == "updateTime" %}
|
||||
{% elif column.python_field == "updateTime" %}
|
||||
add_{{ businessName }}.update_time = datetime.now()
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
add_{{ businessName }}_result = await {{ BusinessName }}Service.add_{{ businessName }}_services(query_db, add_{{ businessName }})
|
||||
logger.info(add_{{ businessName }}_result.message)
|
||||
|
||||
|
@@ -1,8 +1,13 @@
|
||||
{% set pkField = pkColumn.python_field %}
|
||||
{% set pk_field = pkColumn.python_field | camel_to_snake %}
|
||||
{% set pkParentheseIndex = pkColumn.column_comment.find("(") %}
|
||||
{% set pk_field_comment = pkColumn.column_comment[:pkParentheseIndex] if pkParentheseIndex != -1 else pkColumn.column_comment %}
|
||||
from datetime import datetime, time
|
||||
from sqlalchemy import delete, select, update
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
{% if table.sub %}
|
||||
from sqlalchemy.orm import selectinload
|
||||
{% endif %}
|
||||
from {{ packageName }}.entity.do.{{ businessName }}_do import {{ ClassName }}
|
||||
from {{ packageName }}.entity.vo.{{ businessName }}_vo import {{ BusinessName }}Model, {{ BusinessName }}PageQueryModel
|
||||
from utils.page_util import PageUtil
|
||||
@@ -16,10 +21,10 @@ class {{ BusinessName }}Dao:
|
||||
@classmethod
|
||||
async def get_{{ businessName }}_detail_by_id(cls, db: AsyncSession, {{ pk_field }}: int):
|
||||
"""
|
||||
根据{{ functionName }}id获取{{ functionName }}详细信息
|
||||
根据{{ pk_field_comment }}获取{{ functionName }}详细信息
|
||||
|
||||
:param db: orm对象
|
||||
:param {{ pk_field }}: 通知公告id
|
||||
:param {{ pk_field }}: {{ pk_field_comment }}
|
||||
:return: {{ functionName }}信息对象
|
||||
"""
|
||||
{{ businessName }}_info = (await db.execute(select({{ ClassName }}).where({{ ClassName }}.{{ pk_field }} == {{ pk_field }}))).scalars().first()
|
||||
@@ -64,7 +69,12 @@ class {{ BusinessName }}Dao:
|
||||
:return: {{ functionName }}列表信息对象
|
||||
"""
|
||||
query = (
|
||||
{% if table.sub %%}
|
||||
select({{ ClassName }})
|
||||
.options(selectinload{{ ClassName }}.{{ subTable.business_name }})
|
||||
{% else %}
|
||||
select({{ ClassName }})
|
||||
{% endif %}
|
||||
.where(
|
||||
{% for column in columns %}
|
||||
{% set field = column.python_field | camel_to_snake %}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
from datetime import datetime
|
||||
from sqlalchemy import Column, DateTime, Integer, String
|
||||
{% for do_import in doImportList %}
|
||||
{{ do_import }}
|
||||
{% endfor %}
|
||||
{% if table.sub %}
|
||||
from sqlalchemy.orm import relationship
|
||||
{% endif %}
|
||||
@@ -18,23 +19,23 @@ class {{ ClassName }}(Base):
|
||||
{% endfor %}
|
||||
|
||||
{% if table.sub %}
|
||||
{{ table.sub_table.business_name }} = relationship('{{ table.sub_table.class_name }}', back_populates='{{ businessName }}')
|
||||
{{ subTable.business_name }} = relationship('{{ subTable.class_name }}', back_populates='{{ businessName }}')
|
||||
{% endif %}
|
||||
|
||||
|
||||
{% if table.sub %}
|
||||
class {{ table.sub_table.class_name }}(Base):
|
||||
class {{ subTable.class_name }}(Base):
|
||||
"""
|
||||
{{ table.sub_table.function_name }}表
|
||||
{{ subTable.function_name }}表
|
||||
"""
|
||||
|
||||
__tablename__ = '{{ table.sub_table.table_name }}'
|
||||
__tablename__ = '{{ subTableName }}'
|
||||
|
||||
{% for column in table.sub_table.columns %}
|
||||
{% for column in subTable.columns %}
|
||||
{{ column.column_name }} = Column({{ column.column_type | get_sqlalchemy_type }}, {% if column.pk %}primary_key=True, {% endif %}{% if column.increment %}autoincrement=True, {% endif %}{% if column.required %}nullable=True{% else %}nullable=False{% endif %}, comment='{{ column.column_comment }}')
|
||||
{% endfor %}
|
||||
|
||||
{% if table.sub %}
|
||||
{{ businessName }} = relationship('{{ ClassName }}', back_populates='{{ table.sub_table.business_name }}')
|
||||
{{ businessName }} = relationship('{{ ClassName }}', back_populates='{{ subTable.business_name }}')
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
{% set pkField = pkColumn.python_field %}
|
||||
{% set pk_field = pkColumn.python_field | camel_to_snake %}
|
||||
{% set pk_field_comment = pkColumn.comment %}
|
||||
{% set pkParentheseIndex = pkColumn.column_comment.find("(") %}
|
||||
{% set pk_field_comment = pkColumn.column_comment[:pkParentheseIndex] if pkParentheseIndex != -1 else pkColumn.column_comment %}
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from typing import List
|
||||
from config.constant import CommonConstant
|
||||
@@ -87,8 +88,8 @@ class {{ BusinessName }}Service:
|
||||
:return: 编辑{{ functionName }}校验结果
|
||||
"""
|
||||
edit_{{ businessName }} = page_object.model_dump(exclude_unset=True)
|
||||
{{ businessName }}_info = await cls.{{ businessName }}_detail_services(query_db, page_object.post_id)
|
||||
if {{ businessName }}_info.post_id:
|
||||
{{ businessName }}_info = await cls.{{ businessName }}_detail_services(query_db, page_object.{{ pk_field }})
|
||||
if {{ businessName }}_info.{{ pk_field }}:
|
||||
if not await cls.check_post_name_unique_services(query_db, page_object):
|
||||
raise ServiceException(message=f'修改岗位{page_object.post_name}失败,岗位名称已存在')
|
||||
elif not await cls.check_post_code_unique_services(query_db, page_object):
|
||||
@@ -133,8 +134,8 @@ class {{ BusinessName }}Service:
|
||||
获取{{ functionName }}详细信息service
|
||||
|
||||
:param query_db: orm对象
|
||||
:param post_id: 岗位id
|
||||
:return: 岗位id对应的信息
|
||||
:param {{ pk_field }}: {{ pk_field_comment }}
|
||||
:return: {{ pk_field_comment }}对应的信息
|
||||
"""
|
||||
{{ businessName }} = await {{ BusinessName }}Dao.get_{{ businessName }}_detail_by_id(query_db, {{ pk_field }}={{ pk_field }})
|
||||
if {{ businessName }}:
|
||||
|
@@ -1,11 +1,14 @@
|
||||
{% set pkField = pkColumn.python_field %}
|
||||
{% set pk_field = pkColumn.python_field | camel_to_snake %}
|
||||
{% set pk_field_comment = pkColumn.comment %}
|
||||
from datetime import datetime
|
||||
{% set pkParentheseIndex = pkColumn.column_comment.find("(") %}
|
||||
{% set pk_field_comment = pkColumn.column_comment[:pkParentheseIndex] if pkParentheseIndex != -1 else pkColumn.column_comment %}
|
||||
{% for vo_import in voImportList %}
|
||||
{{ vo_import }}
|
||||
{% endfor %}
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
from pydantic.alias_generators import to_camel
|
||||
from pydantic_validation_decorator import NotBlank
|
||||
from typing import Literal, Optional
|
||||
from typing import Optional
|
||||
from module_admin.annotation.pydantic_annotation import as_query
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user