172 lines
7.8 KiB
Plaintext
172 lines
7.8 KiB
Plaintext
|
|
{% 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
|