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_content_dao import Explanation_contentDao
from module_admin.entity.vo.explanation_content_vo import DeleteExplanation_contentModel, Explanation_contentModel, Explanation_contentPageQueryModel
from utils.common_util import CamelCaseUtil
from utils.excel_util import ExcelUtil


class Explanation_contentService:
    """
    讲解内容模块服务层
    """

    @classmethod
    async def get_explanation_content_list_services(
        cls, query_db: AsyncSession, query_object: Explanation_contentPageQueryModel, is_page: bool = False
    ):
        """
        获取讲解内容列表信息service

        :param query_db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 讲解内容列表信息对象
        """
        explanation_content_list_result = await Explanation_contentDao.get_explanation_content_list(query_db, query_object, is_page)

        return explanation_content_list_result


    @classmethod
    async def add_explanation_content_services(cls, query_db: AsyncSession, page_object: Explanation_contentModel):
        """
        新增讲解内容信息service

        :param query_db: orm对象
        :param page_object: 新增讲解内容对象
        :return: 新增讲解内容校验结果
        """
        try:
            await Explanation_contentDao.add_explanation_content_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_content_services(cls, query_db: AsyncSession, page_object: Explanation_contentModel):
        """
        编辑讲解内容信息service

        :param query_db: orm对象
        :param page_object: 编辑讲解内容对象
        :return: 编辑讲解内容校验结果
        """
        edit_explanation_content = page_object.model_dump(exclude_unset=True, exclude={'create_time', 'create_by', })
        explanation_content_info = await cls.explanation_content_detail_services(query_db, page_object.explanation_content_id)
        if explanation_content_info.explanation_content_id:
            try:
                await Explanation_contentDao.edit_explanation_content_dao(query_db, edit_explanation_content)
                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_content_services(cls, query_db: AsyncSession, page_object: DeleteExplanation_contentModel):
        """
        删除讲解内容信息service

        :param query_db: orm对象
        :param page_object: 删除讲解内容对象
        :return: 删除讲解内容校验结果
        """
        if page_object.explanation_content_ids:
            explanation_content_id_list = page_object.explanation_content_ids.split(',')
            try:
                for explanation_content_id in explanation_content_id_list:
                    await Explanation_contentDao.delete_explanation_content_dao(query_db, Explanation_contentModel(explanationContentId=explanation_content_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_content_detail_services(cls, query_db: AsyncSession, explanation_content_id: int):
        """
        获取讲解内容详细信息service

        :param query_db: orm对象
        :param explanation_content_id: 主键ID
        :return: 主键ID对应的信息
        """
        explanation_content = await Explanation_contentDao.get_explanation_content_detail_by_id(query_db, explanation_content_id=explanation_content_id)
        if explanation_content:
            result = Explanation_contentModel(**CamelCaseUtil.transform_result(explanation_content))
        else:
            result = Explanation_contentModel(**dict())

        return result

    @staticmethod
    async def export_explanation_content_list_services(explanation_content_list: List):
        """
        导出讲解内容信息service

        :param explanation_content_list: 讲解内容信息列表
        :return: 讲解内容信息对应excel的二进制数据
        """
        # 创建一个映射字典，将英文键映射到中文键
        mapping_dict = {
            'explanationContentId': '主键ID',
            'title': '标题',
            'content': '讲解内容',
            'type': '类型(0科技类)',
            'spendTime': '预计时长(分钟)',
            'language': '语言(0中文,1英文)',
            'status': '状态(0未开始,1已就绪,2播放中,3已完成)',
            'createTime': '创建时间',
            'createBy': '创建者',
            'updateTime': '修改时间',
            'updateBy': '修改者',
        }
        binary_data = ExcelUtil.export_list2excel(explanation_content_list, mapping_dict)

        return binary_data
