feat: 新增代码生成功能
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
{% set pkField = pkColumn.python_field %}
|
||||
{% set pk_field = pkColumn.python_field | camel_to_snake %}
|
||||
from datetime import datetime
|
||||
from fastapi import APIRouter, Depends, Form, Request
|
||||
from pydantic_validation_decorator import ValidateFields
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from config.enums import BusinessType
|
||||
from config.get_db import get_db
|
||||
from module_admin.annotation.log_annotation import Log
|
||||
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
|
||||
from module_admin.service.login_service import LoginService
|
||||
from {{ packageName }}.service.{{ businessName }}_service import {{ BusinessName }}Service
|
||||
from {{ packageName }}.entity.vo.{{ businessName }}_vo import Delete{{ BusinessName }}Model, {{ BusinessName }}Model, {{ BusinessName }}PageQueryModel
|
||||
from {{ packageName }}.entity.vo.user_vo import CurrentUserModel
|
||||
from utils.common_util import bytes2file_response
|
||||
from utils.log_util import logger
|
||||
from utils.page_util import PageResponseModel
|
||||
from utils.response_util import ResponseUtil
|
||||
|
||||
|
||||
{{ businessName }}Controller = APIRouter(prefix='/{{ moduleName }}/{{ businessName }}', dependencies=[Depends(LoginService.get_current_user)])
|
||||
|
||||
|
||||
@{{ businessName }}Controller.get(
|
||||
'/list', response_model=PageResponseModel, dependencies=[Depends(CheckUserInterfaceAuth('{{ permissionPrefix }}:list'))]
|
||||
)
|
||||
async def get_{{ moduleName }}_{{ businessName }}_list(
|
||||
request: Request,
|
||||
{{ businessName }}_page_query: {{ BusinessName }}PageQueryModel = Depends({{ BusinessName }}PageQueryModel.as_query),
|
||||
query_db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
{% 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 %}
|
||||
{{ businessName }}_query_result = await {{ BusinessName }}Service.get_{{ businessName }}_list_services(query_db, {{ businessName }}_query)
|
||||
logger.info('获取成功')
|
||||
|
||||
return ResponseUtil.success(data={{ businessName }}_query_result)
|
||||
{% endif %}
|
||||
|
||||
|
||||
@{{ businessName }}Controller.post('', dependencies=[Depends(CheckUserInterfaceAuth('{{ permissionPrefix }}:add'))])
|
||||
@ValidateFields(validate_model='add_{{ businessName }}')
|
||||
@Log(title='{{ functionName }}', business_type=BusinessType.INSERT)
|
||||
async def add_{{ moduleName }}_{{ businessName }}(
|
||||
request: Request,
|
||||
add_{{ businessName }}: {{ BusinessName }}Model,
|
||||
query_db: AsyncSession = Depends(get_db),
|
||||
current_user: CurrentUserModel = Depends(LoginService.get_current_user),
|
||||
):
|
||||
{% for column in columns %}
|
||||
{% if column.python_field == "createBy" %}
|
||||
add_{{ businessName }}.create_by = current_user.user.user_name
|
||||
{% elif column.python_field == "createTime" %}
|
||||
add_{{ businessName }}.create_time = datetime.now()
|
||||
{% elif column.python_field == "updateBy" %}
|
||||
add_{{ businessName }}.update_by = current_user.user.user_name
|
||||
{% elif column.python_field == "updateTime" %}
|
||||
add_{{ businessName }}.update_time = datetime.now()
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
add_{{ businessName }}_result = await {{ BusinessName }}Service.add_{{ businessName }}_services(query_db, add_{{ businessName }})
|
||||
logger.info(add_{{ businessName }}_result.message)
|
||||
|
||||
return ResponseUtil.success(msg=add_{{ businessName }}_result.message)
|
||||
|
||||
|
||||
@{{ businessName }}Controller.put('', dependencies=[Depends(CheckUserInterfaceAuth('{{ permissionPrefix }}:edit'))])
|
||||
@ValidateFields(validate_model='edit_{{ businessName }}')
|
||||
@Log(title='{{ functionName }}', business_type=BusinessType.UPDATE)
|
||||
async def edit_{{ moduleName }}_{{ businessName }}(
|
||||
request: Request,
|
||||
edit_{{ businessName }}: {{ BusinessName }}Model,
|
||||
query_db: AsyncSession = Depends(get_db),
|
||||
current_user: CurrentUserModel = Depends(LoginService.get_current_user),
|
||||
):
|
||||
edit_{{ businessName }}.update_by = current_user.user.user_name
|
||||
edit_{{ businessName }}.update_time = datetime.now()
|
||||
edit_{{ businessName }}_result = await {{ BusinessName }}Service.edit_{{ businessName }}_services(query_db, edit_{{ businessName }})
|
||||
logger.info(edit_{{ businessName }}_result.message)
|
||||
|
||||
return ResponseUtil.success(msg=edit_{{ businessName }}_result.message)
|
||||
|
||||
|
||||
@{{ businessName }}Controller.delete('/{% raw %}{{% endraw %}{{ pk_field }}s{% raw %}}{% endraw %}', dependencies=[Depends(CheckUserInterfaceAuth('{{ permissionPrefix }}:remove'))])
|
||||
@Log(title='{{ functionName }}', business_type=BusinessType.DELETE)
|
||||
async def delete_{{ moduleName }}_{{ businessName }}(request: Request, {{ pk_field }}s: str, query_db: AsyncSession = Depends(get_db)):
|
||||
delete_{{ businessName }} = Delete{{ BusinessName }}Model({{ pkField }}s={{ pk_field }}s)
|
||||
delete_{{ businessName }}_result = await {{ BusinessName }}Service.delete_{{ businessName }}_services(query_db, delete_{{ businessName }})
|
||||
logger.info(delete_{{ businessName }}_result.message)
|
||||
|
||||
return ResponseUtil.success(msg=delete_{{ businessName }}_result.message)
|
||||
|
||||
|
||||
@{{ businessName }}Controller.get(
|
||||
'/{% raw %}{{% endraw %}{{ pk_field }}{% raw %}}{% endraw %}', response_model={{ BusinessName }}Model, dependencies=[Depends(CheckUserInterfaceAuth('{{ permissionPrefix }}:query'))]
|
||||
)
|
||||
async def query_detail_{{ moduleName }}_{{ businessName }}(request: Request, {{ pk_field }}: int, query_db: AsyncSession = Depends(get_db)):
|
||||
{{ businessName }}_detail_result = await {{ BusinessName }}Service.{{ businessName }}_detail_services(query_db, {{ pk_field }})
|
||||
logger.info(f'获取{{ pk_field }}为{% raw %}{{% endraw %}{{ pk_field }}{% raw %}}{% endraw %}的信息成功')
|
||||
|
||||
return ResponseUtil.success(data={{ businessName }}_detail_result)
|
||||
|
||||
|
||||
@{{ businessName }}Controller.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('{{ permissionPrefix }}:export'))])
|
||||
@Log(title='{{ functionName }}', business_type=BusinessType.EXPORT)
|
||||
async def export_{{ moduleName }}_{{ businessName }}_list(
|
||||
request: Request,
|
||||
{{ businessName }}_page_query: {{ BusinessName }}PageQueryModel = Form(),
|
||||
query_db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
# 获取全量数据
|
||||
{{ 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)
|
||||
logger.info('导出成功')
|
||||
|
||||
return ResponseUtil.streaming(data=bytes2file_response({{ businessName }}_export_result))
|
@@ -0,0 +1,139 @@
|
||||
{% set pkField = pkColumn.python_field %}
|
||||
{% set pk_field = pkColumn.python_field | camel_to_snake %}
|
||||
from datetime import datetime, time
|
||||
from sqlalchemy import delete, select, update
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
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
|
||||
|
||||
|
||||
class {{ BusinessName }}Dao:
|
||||
"""
|
||||
{{ functionName }}模块数据库操作层
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
async def get_{{ businessName }}_detail_by_id(cls, db: AsyncSession, {{ pk_field }}: int):
|
||||
"""
|
||||
根据{{ functionName }}id获取{{ functionName }}详细信息
|
||||
|
||||
:param db: orm对象
|
||||
:param {{ pk_field }}: 通知公告id
|
||||
:return: {{ functionName }}信息对象
|
||||
"""
|
||||
{{ businessName }}_info = (await db.execute(select({{ ClassName }}).where({{ ClassName }}.{{ pk_field }} == {{ pk_field }}))).scalars().first()
|
||||
|
||||
return {{ businessName }}_info
|
||||
|
||||
@classmethod
|
||||
async def get_{{ businessName }}_detail_by_info(cls, db: AsyncSession, {{ businessName }}: {{ BusinessName }}Model):
|
||||
"""
|
||||
根据{{ functionName }}参数获取{{ functionName }}信息
|
||||
|
||||
:param db: orm对象
|
||||
:param {{ businessName }}: {{ functionName }}参数对象
|
||||
:return: {{ functionName }}信息对象
|
||||
"""
|
||||
{{ businessName }}_info = (
|
||||
(
|
||||
await db.execute(
|
||||
select({{ ClassName }}).where(
|
||||
{% for column in columns %}
|
||||
{% if column.required %}
|
||||
{{ ClassName }}.{{ column.python_field | camel_to_snake }} == {{ businessName }}.{{ column.python_field | camel_to_snake }},
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
)
|
||||
)
|
||||
)
|
||||
.scalars()
|
||||
.first()
|
||||
)
|
||||
|
||||
return {{ businessName }}_info
|
||||
|
||||
@classmethod
|
||||
async def get_{{ businessName }}_list(cls, db: AsyncSession, query_object: {{ BusinessName }}PageQueryModel, is_page: bool = False):
|
||||
"""
|
||||
根据查询参数获取{{ functionName }}列表信息
|
||||
|
||||
:param db: orm对象
|
||||
:param query_object: 查询参数对象
|
||||
:param is_page: 是否开启分页
|
||||
:return: {{ functionName }}列表信息对象
|
||||
"""
|
||||
query = (
|
||||
select({{ ClassName }})
|
||||
.where(
|
||||
{% for column in columns %}
|
||||
{% set field = column.python_field | camel_to_snake %}
|
||||
{% if column.query %}
|
||||
{% if column.query_type == "EQ" %}
|
||||
{{ ClassName }}.{{ field }} == query_object.{{ field }} if query_object.{{ field }} else True,
|
||||
{% elif column.query_type == "NE" %}
|
||||
{{ ClassName }}.{{ field }} != query_object.{{ field }} if query_object.{{ field }} else True,
|
||||
{% elif column.query_type == "GT" %}
|
||||
{{ ClassName }}.{{ field }} > query_object.{{ field }} if query_object.{{ field }} else True,
|
||||
{% elif column.query_type == "GTE" %}
|
||||
{{ ClassName }}.{{ field }} >= query_object.{{ field }} if query_object.{{ field }} else True,
|
||||
{% elif column.query_type == "LT" %}
|
||||
{{ ClassName }}.{{ field }} < query_object.{{ field }} if query_object.{{ field }} else True,
|
||||
{% elif column.query_type == "LTE" %}
|
||||
{{ ClassName }}.{{ field }} <= query_object.{{ field }} if query_object.{{ field }} else True,
|
||||
{% elif column.query_type == "LIKE" %}
|
||||
{{ ClassName }}.{{ field }}.like(f'%{% raw %}{{% endraw %}query_object.{{ field }}{% raw %}}{% endraw %}%') if query_object.{{ field }} else True,
|
||||
{% elif column.query_type == "BETWEEN" %}
|
||||
{{ ClassName }}.{{ field }}.between(
|
||||
datetime.combine(datetime.strptime(query_object.begin_time, '%Y-%m-%d'), time(00, 00, 00)),
|
||||
datetime.combine(datetime.strptime(query_object.end_time, '%Y-%m-%d'), time(23, 59, 59)),
|
||||
)
|
||||
if query_object.begin_time and query_object.end_time
|
||||
else True,
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
)
|
||||
.order_by({{ ClassName }}.{{ pk_field }})
|
||||
.distinct()
|
||||
)
|
||||
{{ businessName }}_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)
|
||||
|
||||
return {{ businessName }}_list
|
||||
|
||||
@classmethod
|
||||
async def add_{{ businessName }}_dao(cls, db: AsyncSession, {{ businessName }}: {{ BusinessName }}Model):
|
||||
"""
|
||||
新增{{ functionName }}数据库操作
|
||||
|
||||
:param db: orm对象
|
||||
:param notice: {{ functionName }}对象
|
||||
:return:
|
||||
"""
|
||||
db_{{ businessName }} = {{ ClassName }}(**{{ businessName }}.model_dump())
|
||||
db.add(db_{{ businessName }})
|
||||
await db.flush()
|
||||
|
||||
return db_{{ businessName }}
|
||||
|
||||
@classmethod
|
||||
async def edit_{{ businessName }}_dao(cls, db: AsyncSession, {{ businessName }}: dict):
|
||||
"""
|
||||
编辑{{ functionName }}数据库操作
|
||||
|
||||
:param db: orm对象
|
||||
:param notice: 需要更新的{{ functionName }}字典
|
||||
:return:
|
||||
"""
|
||||
await db.execute(update({{ ClassName }}), [{{ businessName }}])
|
||||
|
||||
@classmethod
|
||||
async def delete_{{ businessName }}_dao(cls, db: AsyncSession, {{ businessName }}: {{ BusinessName }}Model):
|
||||
"""
|
||||
删除{{ functionName }}数据库操作
|
||||
|
||||
:param db: orm对象
|
||||
:param notice: {{ functionName }}对象
|
||||
:return:
|
||||
"""
|
||||
await db.execute(delete({{ ClassName }}).where({{ ClassName }}.{{ pk_field }}.in_([{{ businessName }}.{{ pk_field }}])))
|
@@ -0,0 +1,40 @@
|
||||
from datetime import datetime
|
||||
from sqlalchemy import Column, DateTime, Integer, String
|
||||
{% if table.sub %}
|
||||
from sqlalchemy.orm import relationship
|
||||
{% endif %}
|
||||
from config.database import Base
|
||||
|
||||
|
||||
class {{ ClassName }}(Base):
|
||||
"""
|
||||
{{ functionName }}表
|
||||
"""
|
||||
|
||||
__tablename__ = '{{ tableName }}'
|
||||
|
||||
{% for column in 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 %}
|
||||
{{ table.sub_table.business_name }} = relationship('{{ table.sub_table.class_name }}', back_populates='{{ businessName }}')
|
||||
{% endif %}
|
||||
|
||||
|
||||
{% if table.sub %}
|
||||
class {{ table.sub_table.class_name }}(Base):
|
||||
"""
|
||||
{{ table.sub_table.function_name }}表
|
||||
"""
|
||||
|
||||
__tablename__ = '{{ table.sub_table.table_name }}'
|
||||
|
||||
{% for column in table.sub_table.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 }}')
|
||||
{% endif %}
|
||||
{% endif %}
|
@@ -0,0 +1,171 @@
|
||||
{% set pkField = pkColumn.python_field %}
|
||||
{% set pk_field = pkColumn.python_field | camel_to_snake %}
|
||||
{% set pk_field_comment = pkColumn.comment %}
|
||||
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
|
||||
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
|
||||
|
||||
|
||||
class {{ BusinessName }}Service:
|
||||
"""
|
||||
{{ functionName }}模块服务层
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
async def get_{{ businessName }}_list_services(
|
||||
cls, query_db: AsyncSession, query_object: {{ BusinessName }}PageQueryModel, is_page: bool = False
|
||||
):
|
||||
"""
|
||||
获取{{ functionName }}列表信息service
|
||||
|
||||
:param query_db: orm对象
|
||||
:param query_object: 查询参数对象
|
||||
:param is_page: 是否开启分页
|
||||
:return: {{ functionName }}列表信息对象
|
||||
"""
|
||||
{{ businessName }}_list_result = await {{ BusinessName }}Dao.get_{{ businessName }}_list(query_db, query_object, is_page)
|
||||
|
||||
return {{ businessName }}_list_result
|
||||
|
||||
{% 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 %}
|
||||
@classmethod
|
||||
async def check_{{ column.python_field | camel_to_snake }}_unique_services(cls, query_db: AsyncSession, page_object: {{ BusinessName }}Model):
|
||||
"""
|
||||
检查{{ comment }}是否唯一service
|
||||
|
||||
:param query_db: orm对象
|
||||
:param page_object: {{ functionName }}对象
|
||||
:return: 校验结果
|
||||
"""
|
||||
{{ pk_field }} = -1 if page_object.{{ pk_field }} is None else page_object.{{ pk_field }}
|
||||
{{ businessName }} = await {{ BusinessName }}Dao.get_{{ businessName }}_detail_by_info(query_db, {{ BusinessName }}Model({{ column.python_field }}=page_object.{{ column.python_field | camel_to_snake }}))
|
||||
if {{ businessName }} and {{ businessName }}.{{ pk_field }} != {{ pk_field }}:
|
||||
return CommonConstant.NOT_UNIQUE
|
||||
return CommonConstant.UNIQUE
|
||||
{% if not loop.last %}{{ "\n" }}{% endif %}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
|
||||
@classmethod
|
||||
async def add_{{ businessName }}_services(cls, query_db: AsyncSession, page_object: {{ BusinessName }}Model):
|
||||
"""
|
||||
新增{{ functionName }}信息service
|
||||
|
||||
:param query_db: orm对象
|
||||
: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
|
||||
|
||||
@classmethod
|
||||
async def edit_{{ businessName }}_services(cls, query_db: AsyncSession, page_object: {{ BusinessName }}Model):
|
||||
"""
|
||||
编辑{{ functionName }}信息service
|
||||
|
||||
:param query_db: orm对象
|
||||
:param page_object: 编辑{{ functionName }}对象
|
||||
: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:
|
||||
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
|
||||
else:
|
||||
raise ServiceException(message='{{ functionName }}不存在')
|
||||
|
||||
@classmethod
|
||||
async def delete_{{ businessName }}_services(cls, query_db: AsyncSession, page_object: Delete{{ BusinessName }}Model):
|
||||
"""
|
||||
删除{{ functionName }}信息service
|
||||
|
||||
:param query_db: orm对象
|
||||
:param page_object: 删除{{ functionName }}对象
|
||||
:return: 删除{{ functionName }}校验结果
|
||||
"""
|
||||
if page_object.{{ pk_field }}s:
|
||||
{{ 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 }}))
|
||||
await query_db.commit()
|
||||
return CrudResponseModel(is_success=True, message='删除成功')
|
||||
except Exception as e:
|
||||
await query_db.rollback()
|
||||
raise e
|
||||
else:
|
||||
raise ServiceException(message='传入{{ pk_field_comment }}为空')
|
||||
|
||||
@classmethod
|
||||
async def {{ businessName }}_detail_services(cls, query_db: AsyncSession, {{ pk_field }}: int):
|
||||
"""
|
||||
获取{{ functionName }}详细信息service
|
||||
|
||||
:param query_db: orm对象
|
||||
:param post_id: 岗位id
|
||||
:return: 岗位id对应的信息
|
||||
"""
|
||||
{{ businessName }} = await {{ BusinessName }}Dao.get_{{ businessName }}_detail_by_id(query_db, {{ pk_field }}={{ pk_field }})
|
||||
if {{ businessName }}:
|
||||
result = {{ BusinessName }}Model(**CamelCaseUtil.transform_result({{ businessName }}))
|
||||
else:
|
||||
result = {{ BusinessName }}Model(**dict())
|
||||
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
async def export_{{ businessName }}_list_services({{ businessName }}_list: List):
|
||||
"""
|
||||
导出{{ functionName }}信息service
|
||||
|
||||
:param {{ businessName }}_list: {{ functionName }}信息列表
|
||||
:return: {{ functionName }}信息对应excel的二进制数据
|
||||
"""
|
||||
# 创建一个映射字典,将英文键映射到中文键
|
||||
mapping_dict = {
|
||||
{% for column in columns %}
|
||||
{% set parentheseIndex = column.column_comment.find("(") %}
|
||||
{% set comment = column.column_comment[:parentheseIndex] if parentheseIndex != -1 else column.column_comment %}
|
||||
'{{ column.python_field }}': '{{ comment }}',
|
||||
{% endfor %}
|
||||
}
|
||||
|
||||
for item in {{ businessName }}_list:
|
||||
if item.get('status') == '0':
|
||||
item['status'] = '正常'
|
||||
else:
|
||||
item['status'] = '停用'
|
||||
binary_data = ExcelUtil.export_list2excel({{ businessName }}_list, mapping_dict)
|
||||
|
||||
return binary_data
|
@@ -0,0 +1,72 @@
|
||||
{% set pkField = pkColumn.python_field %}
|
||||
{% set pk_field = pkColumn.python_field | camel_to_snake %}
|
||||
{% set pk_field_comment = pkColumn.comment %}
|
||||
from datetime import datetime
|
||||
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 module_admin.annotation.pydantic_annotation import as_query
|
||||
|
||||
|
||||
class {{ BusinessName }}Model(BaseModel):
|
||||
"""
|
||||
{{ functionName }}表对应pydantic模型
|
||||
"""
|
||||
|
||||
model_config = ConfigDict(alias_generator=to_camel, from_attributes=True)
|
||||
|
||||
{% for column in columns %}
|
||||
{{ column.column_name }}: Optional[{{ column.python_type }}] = Field(default=None, description='{{ column.column_comment }}')
|
||||
{% endfor %}
|
||||
|
||||
{% for column in columns %}
|
||||
{% if column.required %}
|
||||
{% set parentheseIndex = column.column_comment.find("(") %}
|
||||
{% set comment = column.column_comment[:parentheseIndex] if parentheseIndex != -1 else column.column_comment %}
|
||||
@NotBlank(field_name='{{ column.column_name }}', message='{{ comment }}不能为空')
|
||||
def get_{{ column.column_name }}(self):
|
||||
return self.{{ column.column_name }}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
|
||||
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 %}
|
||||
|
||||
|
||||
class {{ BusinessName }}QueryModel({{ BusinessName }}Model):
|
||||
"""
|
||||
{{ functionName }}不分页查询模型
|
||||
"""
|
||||
|
||||
begin_time: Optional[str] = Field(default=None, description='开始时间')
|
||||
end_time: Optional[str] = Field(default=None, description='结束时间')
|
||||
|
||||
|
||||
@as_query
|
||||
class {{ BusinessName }}PageQueryModel({{ BusinessName }}QueryModel):
|
||||
"""
|
||||
{{ functionName }}分页查询模型
|
||||
"""
|
||||
|
||||
page_num: int = Field(default=1, description='当前页码')
|
||||
page_size: int = Field(default=10, description='每页记录数')
|
||||
|
||||
|
||||
class Delete{{ BusinessName }}Model(BaseModel):
|
||||
"""
|
||||
删除{{ functionName }}模型
|
||||
"""
|
||||
|
||||
model_config = ConfigDict(alias_generator=to_camel)
|
||||
|
||||
{{ pk_field }}s: str = Field(description='需要删除的{{ pk_field_comment }}')
|
Reference in New Issue
Block a user