from flask import request

from ruoyi_common.base.model import AjaxResponse, TableResponse
from ruoyi_common.constant import HttpStatus
from ruoyi_common.descriptor.serializer import JsonSerializer
from ruoyi_common.descriptor.validator import QueryValidator, BodyValidator, PathValidator, FileDownloadValidator, FileUploadValidator
from ruoyi_common.utils.base import ExcelUtil
from ruoyi_framework.descriptor.permission import HasPerm, PreAuthorize
from {{ get_import_path(table.package_name, table.module_name, 'controller') }} import {{ underscore(table.class_name) }} as {{ underscore(table.class_name) }}_bp
from {{ get_import_path(table.package_name, table.module_name, 'domain', table.class_name) }} import {{ underscore(table.class_name) }}_po
from {{ get_import_path(table.package_name, table.module_name, 'domain') }} import {{ table.class_name }}
from {{ get_import_path(table.package_name, table.module_name, 'service') }}.{{ underscore(table.class_name) }}_service import {{ table.class_name }}Service

# 使用 controller/__init__.py 中定义的蓝图
gen = {{ underscore(table.class_name) }}_bp

{{ table.class_name|lower }}_service = {{ table.class_name }}Service()

@gen.route('/list', methods=["GET"])
@QueryValidator(is_page=True)
@PreAuthorize(HasPerm('{{ table.module_name }}:{{ table.business_name }}:list'))
@JsonSerializer()
def {{ table.business_name }}_list(dto: {{ underscore(table.class_name) }}_po):
    """查询{{ table.function_name }}列表"""
    {{ underscore(table.class_name) }}_entity = {{ table.class_name }}()
    # 转换PO到Entity对象
    for attr in dto.model_fields.keys():
        if hasattr({{ underscore(table.class_name) }}_entity, attr):
            setattr({{ underscore(table.class_name) }}_entity, attr, getattr(dto, attr))
    {{ table.business_name }}s, total = {{ table.class_name|lower }}_service.select_{{ table.business_name }}_list({{ underscore(table.class_name) }}_entity)
    return TableResponse(code=HttpStatus.SUCCESS, msg='查询成功', rows={{ table.business_name }}s, total=total)

{% if table.pk_column %}
@gen.route('/<int:{{ underscore(table.pk_column.java_field) }}>', methods=['GET'])
@PathValidator()
@PreAuthorize(HasPerm('{{ table.module_name }}:{{ table.business_name }}:query'))
@JsonSerializer()
def get_{{ table.business_name }}({{ underscore(table.pk_column.java_field) }}: int):
    """获取{{ table.function_name }}详细信息"""
    {{ underscore(table.class_name) }}_entity = {{ table.class_name|lower }}_service.select_{{ table.business_name }}_by_id({{ underscore(table.pk_column.java_field) }})
    return AjaxResponse.from_success(data={{ underscore(table.class_name) }}_entity)
{% endif %}

@gen.route('', methods=['POST'])
@BodyValidator()
@PreAuthorize(HasPerm('{{ table.module_name }}:{{ table.business_name }}:add'))
@JsonSerializer()
def add_{{ table.business_name }}(dto: {{ underscore(table.class_name) }}_po):
    """新增{{ table.function_name }}"""
    {{ underscore(table.class_name) }}_entity = {{ table.class_name }}()
    # 转换PO到Entity对象
    for attr in dto.model_fields.keys():
        if hasattr({{ underscore(table.class_name) }}_entity, attr):
            setattr({{ underscore(table.class_name) }}_entity, attr, getattr(dto, attr))
    result = {{ table.class_name|lower }}_service.insert_{{ table.business_name }}({{ underscore(table.class_name) }}_entity)
    return AjaxResponse.from_success(msg='新增成功' if result > 0 else '新增失败')

{% if table.pk_column %}
@gen.route('', methods=['PUT'])
@BodyValidator()
@PreAuthorize(HasPerm('{{ table.module_name }}:{{ table.business_name }}:edit'))
@JsonSerializer()
def update_{{ table.business_name }}(dto: {{ underscore(table.class_name) }}_po):
    """修改{{ table.function_name }}"""
    {{ underscore(table.class_name) }}_entity = {{ table.class_name }}()
    # 转换PO到Entity对象
    for attr in dto.model_fields.keys():
        if hasattr({{ underscore(table.class_name) }}_entity, attr):
            setattr({{ underscore(table.class_name) }}_entity, attr, getattr(dto, attr))
    result = {{ table.class_name|lower }}_service.update_{{ table.business_name }}({{ underscore(table.class_name) }}_entity)
    return AjaxResponse.from_success(msg='修改成功' if result > 0 else '修改失败')
{% endif %}

{% if table.pk_column %}
@gen.route('/<ids>', methods=['DELETE'])
@PathValidator()
@PreAuthorize(HasPerm('{{ table.module_name }}:{{ table.business_name }}:remove'))
@JsonSerializer()
def delete_{{ table.business_name }}(ids: str):
    """删除{{ table.function_name }}"""
    try:
        id_list = [int(id) for id in ids.split(',')]
        result = {{ table.class_name|lower }}_service.delete_{{ table.business_name }}_by_ids(id_list)
        return AjaxResponse.from_success(msg='删除成功' if result > 0 else '删除失败')
    except Exception as e:
        return AjaxResponse.from_error(msg=f'删除失败: {str(e)}')
{% endif %}

@gen.route('/export', methods=['POST'])
@FileDownloadValidator()
@QueryValidator()
@PreAuthorize(HasPerm('{{ table.module_name }}:{{ table.business_name }}:export'))
def export_{{ table.business_name }}(dto: {{ underscore(table.class_name) }}_po):
    """导出{{ table.function_name }}列表"""
    {{ underscore(table.class_name) }}_entity = {{ table.class_name }}()
    # 转换PO到Entity对象
    for attr in dto.model_fields.keys():
        if hasattr({{ underscore(table.class_name) }}_entity, attr):
            setattr({{ underscore(table.class_name) }}_entity, attr, getattr(dto, attr))
    {{ table.business_name }}s, total = {{ table.class_name|lower }}_service.select_{{ table.business_name }}_list({{ underscore(table.class_name) }}_entity)
    # 使用ExcelUtil导出Excel文件
    excel_util = ExcelUtil({{ underscore(table.class_name) }}_po)
    return excel_util.export_response({{ table.business_name }}s, "{{ table.function_name }}数据")

@gen.route('/importTemplate', methods=['POST'])
@FileDownloadValidator()
@PreAuthorize(HasPerm('{{ table.module_name }}:{{ table.business_name }}:import'))
def import_template():
    """下载{{ table.function_name }}导入模板"""
    excel_util = ExcelUtil({{ underscore(table.class_name) }}_po)
    return excel_util.import_template_response("{{ table.function_name }}导入模板")

@gen.route('/importData', methods=['POST'])
@FileUploadValidator()
@PreAuthorize(HasPerm('{{ table.module_name }}:{{ table.business_name }}:import'))
@JsonSerializer()
def import_data():
    """导入{{ table.function_name }}数据"""
    file = request.files.get('file')
    if not file:
        return AjaxResponse.from_error(msg='上传文件不能为空')
    
    # 获取更新支持参数
    update_support = request.form.get('updateSupport', '0') != '0'
    
    try:
        # 使用ExcelUtil读取Excel文件
        excel_util = ExcelUtil({{ underscore(table.class_name) }}_po)
        po_list = excel_util.import_file(file, sheetname="{{ table.function_name }}数据")
        
        # 转换为Entity对象列表
        {{ table.business_name }}_list = []
        for po in po_list:
            {{ underscore(table.class_name) }}_entity = {{ table.class_name }}()
            for attr in po.model_fields.keys():
                if hasattr({{ underscore(table.class_name) }}_entity, attr):
                    setattr({{ underscore(table.class_name) }}_entity, attr, getattr(po, attr))
            {{ table.business_name }}_list.append({{ underscore(table.class_name) }}_entity)
        
        # 调用Service层处理导入逻辑
        msg = {{ table.class_name|lower }}_service.import_{{ table.business_name }}({{ table.business_name }}_list, update_support)
        return AjaxResponse.from_success(msg=msg)
    except Exception as e:
        return AjaxResponse.from_error(msg=f'导入失败: {str(e)}')