perf: 优化代码生成功能
This commit is contained in:
@@ -119,7 +119,7 @@ async def export_{{ moduleName }}_{{ businessName }}_list(
|
||||
):
|
||||
# 获取全量数据
|
||||
{{ businessName }}_query_result = await {{ BusinessName }}Service.get_{{ businessName }}_list_services(query_db, {{ businessName }}_page_query, is_page=False)
|
||||
{{ businessName }}_export_result = await {{ BusinessName }}Service.export_{{ businessName }}_list_services({{ businessName }}_query_result)
|
||||
{{ businessName }}_export_result = await {{ BusinessName }}Service.export_{{ businessName }}_list_services({% if dicts %}request, {% endif %}{{ businessName }}_query_result)
|
||||
logger.info('导出成功')
|
||||
|
||||
return ResponseUtil.streaming(data=bytes2file_response({{ businessName }}_export_result))
|
||||
|
@@ -2,7 +2,11 @@
|
||||
{% 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 %}
|
||||
{% for column in columns %}
|
||||
{% if column.python_field == "createTime" %}
|
||||
from datetime import datetime, time
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
from sqlalchemy import delete, select, update
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
{% if table.sub %}
|
||||
@@ -27,7 +31,23 @@ class {{ BusinessName }}Dao:
|
||||
:param {{ pk_field }}: {{ pk_field_comment }}
|
||||
:return: {{ functionName }}信息对象
|
||||
"""
|
||||
{{ businessName }}_info = (await db.execute(select({{ ClassName }}).where({{ ClassName }}.{{ pk_field }} == {{ pk_field }}))).scalars().first()
|
||||
{{ businessName }}_info = (
|
||||
(
|
||||
await db.execute(
|
||||
{% if table.sub %}
|
||||
select({{ ClassName }})
|
||||
.options(selectinload({{ ClassName }}.{{ subclassName }}_list))
|
||||
{% else %}
|
||||
select({{ ClassName }})
|
||||
{% endif %}
|
||||
.where(
|
||||
{{ ClassName }}.{{ pk_field }} == {{ pk_field }}
|
||||
)
|
||||
)
|
||||
)
|
||||
.scalars()
|
||||
.first()
|
||||
)
|
||||
|
||||
return {{ businessName }}_info
|
||||
|
||||
@@ -69,9 +89,9 @@ class {{ BusinessName }}Dao:
|
||||
:return: {{ functionName }}列表信息对象
|
||||
"""
|
||||
query = (
|
||||
{% if table.sub %%}
|
||||
{% if table.sub %}
|
||||
select({{ ClassName }})
|
||||
.options(selectinload{{ ClassName }}.{{ subTable.business_name }})
|
||||
.options(selectinload({{ ClassName }}.{{ subclassName }}_list))
|
||||
{% else %}
|
||||
select({{ ClassName }})
|
||||
{% endif %}
|
||||
@@ -117,10 +137,10 @@ class {{ BusinessName }}Dao:
|
||||
新增{{ functionName }}数据库操作
|
||||
|
||||
:param db: orm对象
|
||||
:param notice: {{ functionName }}对象
|
||||
:param {{ businessName }}: {{ functionName }}对象
|
||||
:return:
|
||||
"""
|
||||
db_{{ businessName }} = {{ ClassName }}(**{{ businessName }}.model_dump())
|
||||
db_{{ businessName }} = {{ ClassName }}(**{{ businessName }}.model_dump({%if table.sub %}exclude={'{{ subclassName }}_list'}{% endif %}))
|
||||
db.add(db_{{ businessName }})
|
||||
await db.flush()
|
||||
|
||||
@@ -132,7 +152,7 @@ class {{ BusinessName }}Dao:
|
||||
编辑{{ functionName }}数据库操作
|
||||
|
||||
:param db: orm对象
|
||||
:param notice: 需要更新的{{ functionName }}字典
|
||||
:param {{ businessName }}: 需要更新的{{ functionName }}字典
|
||||
:return:
|
||||
"""
|
||||
await db.execute(update({{ ClassName }}), [{{ businessName }}])
|
||||
@@ -143,7 +163,46 @@ class {{ BusinessName }}Dao:
|
||||
删除{{ functionName }}数据库操作
|
||||
|
||||
:param db: orm对象
|
||||
:param notice: {{ functionName }}对象
|
||||
:param {{ businessName }}: {{ functionName }}对象
|
||||
:return:
|
||||
"""
|
||||
await db.execute(delete({{ ClassName }}).where({{ ClassName }}.{{ pk_field }}.in_([{{ businessName }}.{{ pk_field }}])))
|
||||
|
||||
{% if table.sub %}
|
||||
@classmethod
|
||||
async def add_{{ subTable.business_name }}_dao(cls, db: AsyncSession, {{ subTable.business_name }}: {{ subTable.business_name | capitalize }}Model):
|
||||
"""
|
||||
新增{{ subTable.function_name }}数据库操作
|
||||
|
||||
:param db: orm对象
|
||||
:param {{ subTable.business_name }}: {{ subTable.function_name }}对象
|
||||
:return:
|
||||
"""
|
||||
db_{{ subTable.business_name }} = {{ subClassName }}(**{{ subTable.business_name }}.model_dump())
|
||||
db.add(db_{{ subTable.business_name }})
|
||||
await db.flush()
|
||||
|
||||
return db_{{ subTable.business_name }}
|
||||
|
||||
@classmethod
|
||||
async def edit_{{ subTable.business_name }}_dao(cls, db: AsyncSession, {{ subTable.business_name }}: dict):
|
||||
"""
|
||||
编辑{{ subTable.function_name }}数据库操作
|
||||
|
||||
:param db: orm对象
|
||||
:param {{ subTable.business_name }}: 需要更新的{{ subTable.function_name }}字典
|
||||
:return:
|
||||
"""
|
||||
await db.execute(update({{ subClassName }}), [{{ subTable.business_name }}])
|
||||
|
||||
@classmethod
|
||||
async def delete_{{ subTable.business_name }}_dao(cls, db: AsyncSession, {{ subTable.business_name }}: {{ subTable.business_name | capitalize }}Model):
|
||||
"""
|
||||
删除{{ subTable.function_name }}数据库操作
|
||||
|
||||
:param db: orm对象
|
||||
:param {{ subTable.business_name }}: {{ subTable.function_name }}对象
|
||||
:return:
|
||||
"""
|
||||
await db.execute(delete({{ subClassName }}).where({{ subClassName }}.{{ subTable.pk_column.python_field | camel_to_snake }}.in_([{{ subTable.business_name }}.{{ subTable.pk_column.python_field | camel_to_snake }}])))
|
||||
{% endif %}
|
||||
|
@@ -19,12 +19,12 @@ class {{ ClassName }}(Base):
|
||||
{% endfor %}
|
||||
|
||||
{% if table.sub %}
|
||||
{{ subTable.business_name }} = relationship('{{ subTable.class_name }}', back_populates='{{ businessName }}')
|
||||
{{ subclassName }}_list = relationship('{{ subClassName }}', back_populates='{{ businessName }}')
|
||||
{% endif %}
|
||||
|
||||
|
||||
{% if table.sub %}
|
||||
class {{ subTable.class_name }}(Base):
|
||||
class {{ subClassName }}(Base):
|
||||
"""
|
||||
{{ subTable.function_name }}表
|
||||
"""
|
||||
@@ -36,6 +36,6 @@ class {{ subTable.class_name }}(Base):
|
||||
{% endfor %}
|
||||
|
||||
{% if table.sub %}
|
||||
{{ businessName }} = relationship('{{ ClassName }}', back_populates='{{ subTable.business_name }}')
|
||||
{{ businessName }} = relationship('{{ ClassName }}', back_populates='{{ subclassName }}_list')
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
|
@@ -2,12 +2,18 @@
|
||||
{% 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 %}
|
||||
{% if dicts %}
|
||||
from fastapi import Request
|
||||
{% endif %}
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from typing import List
|
||||
from config.constant import CommonConstant
|
||||
from exceptions.exception import ServiceException
|
||||
from {{ packageName }}.dao.{{ businessName }}_dao import {{ BusinessName }}Dao
|
||||
from module_admin.entity.vo.common_vo import CrudResponseModel
|
||||
{% if dicts %}
|
||||
from module_admin.service.dict_service import DictDataService
|
||||
{% endif %}
|
||||
from {{ packageName }}.dao.{{ businessName }}_dao import {{ BusinessName }}Dao
|
||||
from {{ packageName }}.entity.vo.{{ businessName }}_vo import Delete{{ BusinessName }}Model, {{ BusinessName }}Model, {{ BusinessName }}PageQueryModel
|
||||
from utils.common_util import CamelCaseUtil
|
||||
from utils.excel_util import ExcelUtil
|
||||
@@ -65,18 +71,28 @@ class {{ BusinessName }}Service:
|
||||
:param page_object: 新增{{ functionName }}对象
|
||||
:return: 新增{{ functionName }}校验结果
|
||||
"""
|
||||
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):
|
||||
raise ServiceException(message=f'新增岗位{page_object.post_name}失败,岗位编码已存在')
|
||||
else:
|
||||
try:
|
||||
await {{ BusinessName }}Dao.add_{{ businessName }}_dao(query_db, page_object)
|
||||
await query_db.commit()
|
||||
return CrudResponseModel(is_success=True, message='新增成功')
|
||||
except Exception as e:
|
||||
await query_db.rollback()
|
||||
raise e
|
||||
{% for column in columns %}
|
||||
{% set parentheseIndex = column.column_comment.find("(") %}
|
||||
{% set comment = column.column_comment[:parentheseIndex] if parentheseIndex != -1 else column.column_comment %}
|
||||
{% if column.required %}
|
||||
if not await cls.check_{{ column.python_field | camel_to_snake }}_unique_services(query_db, page_object)
|
||||
raise ServiceException(message=f'新增{{ functionName }}{page_object.{{ column.python_field | camel_to_snake }}}失败,{{ comment }}已存在')
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
try:
|
||||
{% if table.sub %}
|
||||
add_{{ businessName }} = await {{ BusinessName }}Dao.add_{{ businessName }}_dao(query_db, page_object)
|
||||
if add_{{ businessName }}:
|
||||
for sub_table in page_object.{{ subclassName }}_list:
|
||||
await {{ BusinessName }}Dao.add_{{ subTable.business_name }}_dao(query_db, sub_table)
|
||||
{% else %}
|
||||
await {{ BusinessName }}Dao.add_{{ businessName }}_dao(query_db, page_object)
|
||||
{% endif %}
|
||||
await query_db.commit()
|
||||
return CrudResponseModel(is_success=True, message='新增成功')
|
||||
except Exception as e:
|
||||
await query_db.rollback()
|
||||
raise e
|
||||
|
||||
@classmethod
|
||||
async def edit_{{ businessName }}_services(cls, query_db: AsyncSession, page_object: {{ BusinessName }}Model):
|
||||
@@ -87,21 +103,28 @@ class {{ BusinessName }}Service:
|
||||
:param page_object: 编辑{{ functionName }}对象
|
||||
:return: 编辑{{ functionName }}校验结果
|
||||
"""
|
||||
edit_{{ businessName }} = page_object.model_dump(exclude_unset=True)
|
||||
edit_{{ businessName }} = page_object.model_dump(exclude_unset=True{% if table.sub %}, exclude={'{{ subclassName }}_list'}{% endif %})
|
||||
{{ 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):
|
||||
raise ServiceException(message=f'修改岗位{page_object.post_name}失败,岗位编码已存在')
|
||||
else:
|
||||
try:
|
||||
await {{ BusinessName }}Dao.edit_{{ businessName }}_dao(query_db, edit_{{ businessName }})
|
||||
await query_db.commit()
|
||||
return CrudResponseModel(is_success=True, message='更新成功')
|
||||
except Exception as e:
|
||||
await query_db.rollback()
|
||||
raise e
|
||||
{% for column in columns %}
|
||||
{% set parentheseIndex = column.column_comment.find("(") %}
|
||||
{% set comment = column.column_comment[:parentheseIndex] if parentheseIndex != -1 else column.column_comment %}
|
||||
{% if column.required %}
|
||||
if not await cls.check_{{ column.python_field | camel_to_snake }}_unique_services(query_db, page_object)
|
||||
raise ServiceException(message=f'修改{{ functionName }}{page_object.{{ column.python_field | camel_to_snake }}}失败,{{ comment }}已存在')
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
try:
|
||||
await {{ BusinessName }}Dao.edit_{{ businessName }}_dao(query_db, edit_{{ businessName }})
|
||||
{% if table.sub %}
|
||||
for sub_table in page_object.{{ subclassName }}_list:
|
||||
await {{ BusinessName }}Dao.edit_{{ subTable.business_name }}_dao(query_db, sub_table)
|
||||
{% endif %}
|
||||
await query_db.commit()
|
||||
return CrudResponseModel(is_success=True, message='更新成功')
|
||||
except Exception as e:
|
||||
await query_db.rollback()
|
||||
raise e
|
||||
else:
|
||||
raise ServiceException(message='{{ functionName }}不存在')
|
||||
|
||||
@@ -118,8 +141,12 @@ class {{ BusinessName }}Service:
|
||||
{{ pk_field }}_list = page_object.{{ pk_field }}s.split(',')
|
||||
try:
|
||||
for {{ pk_field }} in {{ pk_field }}_list:
|
||||
{{ businessName }} = await cls.{{ businessName }}_detail_services(query_db, int({{ pk_field }}))
|
||||
await {{ BusinessName }}Dao.delete_{{ businessName }}_dao(query_db, {{ BusinessName }}Model({{ pkField }}={{ pk_field }}))
|
||||
{% if table.sub %}
|
||||
{{ businessName }} = await cls.{{ businessName }}_detail_services(query_db, int({{ pk_field }}))
|
||||
for sub_table in {{ businessName }}.{{ subclassName }}_list:
|
||||
await {{ BusinessName }}Dao.delete_{{ subTable.business_name }}_dao(query_db, sub_table)
|
||||
{% endif %}
|
||||
await query_db.commit()
|
||||
return CrudResponseModel(is_success=True, message='删除成功')
|
||||
except Exception as e:
|
||||
@@ -146,7 +173,7 @@ class {{ BusinessName }}Service:
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
async def export_{{ businessName }}_list_services({{ businessName }}_list: List):
|
||||
async def export_{{ businessName }}_list_services({% if dicts %}request: Request, {% endif %}{{ businessName }}_list: List):
|
||||
"""
|
||||
导出{{ functionName }}信息service
|
||||
|
||||
@@ -161,12 +188,22 @@ class {{ BusinessName }}Service:
|
||||
'{{ column.python_field }}': '{{ comment }}',
|
||||
{% endfor %}
|
||||
}
|
||||
|
||||
{% if dicts %}
|
||||
{% for dict_type in dicts.split(", ") %}
|
||||
{{ dict_type[1:-1] }}_list = await DictDataService.query_dict_data_list_from_cache_services(
|
||||
request.app.state.redis, dict_type={{ dict_type }}
|
||||
)
|
||||
{{ dict_type[1:-1] }}_option = [dict(label=item.get('dictLabel'), value=item.get('dictValue')) for item in {{ dict_type[1:-1] }}_list]
|
||||
{{ dict_type[1:-1] }}_option_dict = {item.get('value'): item for item in {{ dict_type[1:-1] }}_option}
|
||||
{% endfor %}
|
||||
for item in {{ businessName }}_list:
|
||||
if item.get('status') == '0':
|
||||
item['status'] = '正常'
|
||||
else:
|
||||
item['status'] = '停用'
|
||||
{% for column in columns %}
|
||||
{% if column.dict_type %}
|
||||
if str(item.get('{{ column.python_field }}')) in {{ column.dict_type }}_option_dict.keys():
|
||||
item['{{ column.python_field }}'] = {{ column.dict_type }}_option_dict.get(str(item.get('{{ column.python_field }}'))).get('label')
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
binary_data = ExcelUtil.export_list2excel({{ businessName }}_list, mapping_dict)
|
||||
|
||||
return binary_data
|
||||
|
@@ -2,13 +2,33 @@
|
||||
{% 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 %}
|
||||
{% set vo_field_required = namespace(has_required=False) %}
|
||||
{% for column in columns %}
|
||||
{% if column.required %}
|
||||
{% set vo_field_required.has_required = True %}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% if table.sub %}
|
||||
{% set sub_vo_field_required = namespace(has_required=False) %}
|
||||
{% for sub_column in subTable.columns %}
|
||||
{% if sub_column.required %}
|
||||
{% set sub_vo_field_required.has_required = True %}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% for vo_import in voImportList %}
|
||||
{{ vo_import }}
|
||||
{% endfor %}
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
from pydantic.alias_generators import to_camel
|
||||
{% if vo_field_required.has_required %}
|
||||
from pydantic_validation_decorator import NotBlank
|
||||
{% endif %}
|
||||
{% if table.sub %}
|
||||
from typing import List, Optional
|
||||
{% else %}
|
||||
from typing import Optional
|
||||
{% endif %}
|
||||
from module_admin.annotation.pydantic_annotation import as_query
|
||||
|
||||
|
||||
@@ -22,6 +42,9 @@ class {{ BusinessName }}Model(BaseModel):
|
||||
{% for column in columns %}
|
||||
{{ column.column_name }}: Optional[{{ column.python_type }}] = Field(default=None, description='{{ column.column_comment }}')
|
||||
{% endfor %}
|
||||
{% if table.sub %}
|
||||
{{ subclassName }}_list: Optional[List['{{ subTable.business_name | capitalize }}Model']] = Field(default=None, description='子表列信息')
|
||||
{% endif %}
|
||||
|
||||
{% for column in columns %}
|
||||
{% if column.required %}
|
||||
@@ -33,19 +56,49 @@ class {{ BusinessName }}Model(BaseModel):
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
|
||||
{% if vo_field_required.has_required %}
|
||||
def validate_fields(self):
|
||||
{% set vo_field_required = namespace(has_required=False) %}
|
||||
{% for column in columns %}
|
||||
{% if column.required %}
|
||||
self.get_{{ column.column_name }}()
|
||||
{% set vo_field_required.has_required = True %}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% if not vo_field_required.has_required %}
|
||||
pass
|
||||
{% endif %}
|
||||
|
||||
|
||||
{% if table.sub %}
|
||||
class {{ subTable.business_name | capitalize }}Model(BaseModel):
|
||||
"""
|
||||
{{ subTable.function_name }}表对应pydantic模型
|
||||
"""
|
||||
|
||||
model_config = ConfigDict(alias_generator=to_camel, from_attributes=True)
|
||||
|
||||
{% for sub_column in subTable.columns %}
|
||||
{{ sub_column.column_name }}: Optional[{{ sub_column.python_type }}] = Field(default=None, description='{{ sub_column.column_comment}}')
|
||||
{% endfor %}
|
||||
|
||||
{% for sub_column in subTable.columns %}
|
||||
{% if sub_column.required %}
|
||||
{% set parentheseIndex = sub_column.column_comment.find("(") %}
|
||||
{% set comment = sub_column.column_comment[:parentheseIndex] if parentheseIndex != -1 else sub_column.column_comment %}
|
||||
@NotBlank(field_name='{{ sub_column.column_name }}', message='{{ comment }}不能为空')
|
||||
def get_{{ sub_column.column_name }}(self):
|
||||
return self.{{ sub_column.column_name }}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
|
||||
{% if sub_vo_field_required.has_required %}
|
||||
def validate_fields(self):
|
||||
{% for sub_column in subTable.columns %}
|
||||
{% if sub_column.required %}
|
||||
self.get_{{ sub_column.column_name }}()
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
|
||||
|
||||
class {{ BusinessName }}QueryModel({{ BusinessName }}Model):
|
||||
"""
|
||||
{{ functionName }}不分页查询模型
|
||||
|
@@ -127,6 +127,7 @@
|
||||
{% else %}
|
||||
<el-table-column label="{{ comment }}" align="center" prop="{{ pythonField }}" />
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
|
||||
<template slot-scope="scope">
|
||||
@@ -135,7 +136,7 @@
|
||||
type="text"
|
||||
icon="el-icon-edit"
|
||||
@click="handleUpdate(scope.row)"
|
||||
v-hasPermi="['{{ moduleName }:{{ businessName }}:edit']"
|
||||
v-hasPermi="['{{ moduleName }}:{{ businessName }}:edit']"
|
||||
>修改</el-button>
|
||||
<el-button
|
||||
size="mini"
|
||||
|
Reference in New Issue
Block a user