# -*- coding: utf-8 -*-
# @Author  : {{ table.function_author }}
# @FileName: {{ underscore(table.class_name) }}_service.py
# @Time    : {{ datetime }}

from typing import List, Tuple

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, 'mapper') }}.{{ underscore(table.class_name) }}_mapper import {{ table.class_name }}Mapper

class {{ table.class_name }}Service:
    """{{ table.function_name }}服务类"""

    def select_{{ table.business_name }}_list(self, {{ table.business_name }}: {{ table.class_name }}) -> Tuple[List[{{ table.class_name }}], int]:
        """
        查询{{ table.function_name }}列表

        Args:
            {{ table.business_name }} ({{ table.class_name }}): {{ table.function_name }}对象

        Returns:
            Tuple[List[{{ table.class_name }}], int]: {{ table.function_name }}列表和总数
        """
        list_result = {{ table.class_name }}Mapper.select_list({{ table.business_name }})
        count_result = {{ table.class_name }}Mapper.select_count({{ table.business_name }})
        return list_result, count_result

    {% if table.pk_column %}
    def select_{{ table.business_name }}_by_id(self, {{ underscore(table.pk_column.java_field) }}: int) -> {{ table.class_name }}:
        """
        根据ID查询{{ table.function_name }}

        Args:
            {{ underscore(table.pk_column.java_field) }} (int): {{ table.pk_column.column_comment }}

        Returns:
            {{ table.class_name }}: {{ table.function_name }}对象
        """
        return {{ table.class_name }}Mapper.select_by_id({{ underscore(table.pk_column.java_field) }})
    {% endif %}

    def insert_{{ table.business_name }}(self, {{ table.business_name }}: {{ table.class_name }}) -> int:
        """
        新增{{ table.function_name }}

        Args:
            {{ table.business_name }} ({{ table.class_name }}): {{ table.function_name }}对象

        Returns:
            int: 插入的记录数
        """
        return {{ table.class_name }}Mapper.insert({{ table.business_name }})

    {% if table.pk_column %}
    def update_{{ table.business_name }}(self, {{ table.business_name }}: {{ table.class_name }}) -> int:
        """
        修改{{ table.function_name }}

        Args:
            {{ table.business_name }} ({{ table.class_name }}): {{ table.function_name }}对象

        Returns:
            int: 更新的记录数
        """
        return {{ table.class_name }}Mapper.update({{ table.business_name }})
    {% endif %}

    {% if table.pk_column %}
    def delete_{{ table.business_name }}_by_ids(self, ids: List[int]) -> int:
        """
        批量删除{{ table.function_name }}

        Args:
            ids (List[int]): ID列表

        Returns:
            int: 删除的记录数
        """
        return {{ table.class_name }}Mapper.delete_by_ids(ids)
    {% endif %}

    def import_{{ table.business_name }}(self, {{ table.business_name }}_list: List[{{ table.class_name }}], update_support: bool = False) -> str:
        """
        导入{{ table.function_name }}数据

        Args:
            {{ table.business_name }}_list (List[{{ table.class_name }}]): {{ table.function_name }}列表
            update_support (bool): 是否更新已存在的数据

        Returns:
            str: 导入结果消息
        """
        if not {{ table.business_name }}_list:
            raise Exception("导入{{ table.function_name }}数据不能为空")
        
        success_count = 0
        failure_count = 0
        failure_msg = []
        
        for index, {{ table.business_name }} in enumerate({{ table.business_name }}_list):
            try:
                {% if table.pk_column %}
                # 检查是否已存在
                existing = {{ table.class_name }}Mapper.select_by_id({{ table.business_name }}.{{ underscore(table.pk_column.java_field) }})
                if existing:
                    if update_support:
                        # 更新数据
                        result = {{ table.class_name }}Mapper.update({{ table.business_name }})
                        if result > 0:
                            success_count += 1
                        else:
                            failure_count += 1
                            failure_msg.append(f"第{index + 2}行更新失败")
                    else:
                        failure_count += 1
                        failure_msg.append(f"第{index + 2}行数据已存在")
                else:
                    # 新增数据
                    result = {{ table.class_name }}Mapper.insert({{ table.business_name }})
                    if result > 0:
                        success_count += 1
                    else:
                        failure_count += 1
                        failure_msg.append(f"第{index + 2}行导入失败")
                {% else %}
                # 没有主键，直接插入
                result = {{ table.class_name }}Mapper.insert({{ table.business_name }})
                if result > 0:
                    success_count += 1
                else:
                    failure_count += 1
                    failure_msg.append(f"第{index + 2}行导入失败")
                {% endif %}
            except Exception as e:
                failure_count += 1
                failure_msg.append(f"第{index + 2}行导入失败: {str(e)}")
        
        if failure_count == 0:
            return f'成功导入{success_count}条数据'
        else:
            error_msg = f'成功导入{success_count}条，失败{failure_count}条'
            if failure_msg:
                error_msg += f'。失败原因：{"; ".join(failure_msg[:5])}'
            raise Exception(error_msg)