from sqlalchemy.ext.asyncio import AsyncSession
from typing import List
from config.constant import CommonConstant
from exceptions.exception import ServiceException
from module_admin.entity.vo.common_vo import CrudResponseModel
from module_admin.dao.explanation_style_dao import Explanation_styleDao
from module_admin.entity.vo.explanation_style_vo import DeleteExplanation_styleModel, Explanation_styleModel, Explanation_stylePageQueryModel
from utils.common_util import CamelCaseUtil
from utils.excel_util import ExcelUtil


class Explanation_styleService:
    """
    讲解风格模块服务层
    """

    @classmethod
    async def get_explanation_style_list_services(
        cls, query_db: AsyncSession, query_object: Explanation_stylePageQueryModel, is_page: bool = False
    ):
        """
        获取讲解风格列表信息service

        :param query_db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 讲解风格列表信息对象
        """
        explanation_style_list_result = await Explanation_styleDao.get_explanation_style_list(query_db, query_object, is_page)

        return explanation_style_list_result


    @classmethod
    async def add_explanation_style_services(cls, query_db: AsyncSession, page_object: Explanation_styleModel):
        """
        新增讲解风格信息service

        :param query_db: orm对象
        :param page_object: 新增讲解风格对象
        :return: 新增讲解风格校验结果
        """
        try:
            await Explanation_styleDao.add_explanation_style_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_explanation_style_services(cls, query_db: AsyncSession, page_object: Explanation_styleModel):
        """
        编辑讲解风格信息service

        :param query_db: orm对象
        :param page_object: 编辑讲解风格对象
        :return: 编辑讲解风格校验结果
        """
        edit_explanation_style = page_object.model_dump(exclude_unset=True, exclude={'create_time', 'create_by', })
        explanation_style_info = await cls.explanation_style_detail_services(query_db, page_object.explanation_style_id)
        if explanation_style_info.explanation_style_id:
            try:
                await Explanation_styleDao.edit_explanation_style_dao(query_db, edit_explanation_style)
                await query_db.commit()
                return CrudResponseModel(is_success=True, message='更新成功')
            except Exception as e:
                await query_db.rollback()
                raise e
        else:
            raise ServiceException(message='讲解风格不存在')

    @classmethod
    async def delete_explanation_style_services(cls, query_db: AsyncSession, page_object: DeleteExplanation_styleModel):
        """
        删除讲解风格信息service

        :param query_db: orm对象
        :param page_object: 删除讲解风格对象
        :return: 删除讲解风格校验结果
        """
        if page_object.explanation_style_ids:
            explanation_style_id_list = page_object.explanation_style_ids.split(',')
            try:
                for explanation_style_id in explanation_style_id_list:
                    await Explanation_styleDao.delete_explanation_style_dao(query_db, Explanation_styleModel(explanationStyleId=explanation_style_id))
                await query_db.commit()
                return CrudResponseModel(is_success=True, message='删除成功')
            except Exception as e:
                await query_db.rollback()
                raise e
        else:
            raise ServiceException(message='传入主键ID为空')

    @classmethod
    async def explanation_style_detail_services(cls, query_db: AsyncSession, explanation_style_id: int):
        """
        获取讲解风格详细信息service

        :param query_db: orm对象
        :param explanation_style_id: 主键ID
        :return: 主键ID对应的信息
        """
        explanation_style = await Explanation_styleDao.get_explanation_style_detail_by_id(query_db, explanation_style_id=explanation_style_id)
        if explanation_style:
            result = Explanation_styleModel(**CamelCaseUtil.transform_result(explanation_style))
        else:
            result = Explanation_styleModel(**dict())

        return result

    @staticmethod
    async def export_explanation_style_list_services(explanation_style_list: List):
        """
        导出讲解风格信息service

        :param explanation_style_list: 讲解风格信息列表
        :return: 讲解风格信息对应excel的二进制数据
        """
        # 创建一个映射字典，将英文键映射到中文键
        mapping_dict = {
            'explanationStyleId': '主键ID',
            'name': '风格名称',
            'detail': '风格简介',
            'createTime': '创建时间',
            'createBy': '创建者',
            'updateTime': '修改时间',
            'updateBy': '修改者',
        }
        binary_data = ExcelUtil.export_list2excel(explanation_style_list, mapping_dict)

        return binary_data
