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_robot_pair_dao import Explanation_style_robot_pairDao
from module_admin.entity.vo.explanation_style_robot_pair_vo import DeleteExplanation_style_robot_pairModel, \
    Explanation_style_robot_pairModel, Explanation_style_robot_pairPageQueryModel, SwitchExplanationStyleModel
from utils.common_util import CamelCaseUtil
from utils.excel_util import ExcelUtil


class Explanation_style_robot_pairService:
    """
    讲解风格--机器人配对模块服务层
    """

    @classmethod
    async def switch_explanation_style_services(cls, query_db: AsyncSession, switch_explanation_style: SwitchExplanationStyleModel):
        try:
            # 现将所有风格都置为0
            result1 = await Explanation_style_robot_pairDao.switch_explanation_style(query_db, switch_explanation_style.robot_id, None, switch_explanation_style.robot_role_id ,"0", switch_explanation_style.update_by)
            # 再将选择的风格置为1
            result2 = await Explanation_style_robot_pairDao.switch_explanation_style(query_db, None, switch_explanation_style.pairing_id,None ,"1", switch_explanation_style.update_by)
            count = result2.rowcount

            await query_db.commit()
            if count == 0:
                await query_db.rollback()
                return CrudResponseModel(is_success=False, message='修改失败')

            return CrudResponseModel(is_success=True, message='修改成功')
        except Exception as e:
            await query_db.rollback()
            raise e


    @classmethod
    async def get_style_robot_pair_list_services(cls, query_db: AsyncSession, robot_id: int):
        return await Explanation_style_robot_pairDao.get_style_robot_pair_list(query_db, robot_id)

    @classmethod
    async def get_explanation_style_robot_pair_list_services(
        cls, query_db: AsyncSession, query_object: Explanation_style_robot_pairPageQueryModel, is_page: bool = False
    ):
        """
        获取讲解风格--机器人配对列表信息service

        :param query_db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 讲解风格--机器人配对列表信息对象
        """
        explanation_style_robot_pair_list_result = await Explanation_style_robot_pairDao.get_explanation_style_robot_pair_list(query_db, query_object, is_page)

        return explanation_style_robot_pair_list_result


    @classmethod
    async def add_explanation_style_robot_pair_services(cls, query_db: AsyncSession, page_object: Explanation_style_robot_pairModel):
        """
        新增讲解风格--机器人配对信息service

        :param query_db: orm对象
        :param page_object: 新增讲解风格--机器人配对对象
        :return: 新增讲解风格--机器人配对校验结果
        """
        try:
            await Explanation_style_robot_pairDao.add_explanation_style_robot_pair_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_robot_pair_services(cls, query_db: AsyncSession, page_object: Explanation_style_robot_pairModel):
        """
        编辑讲解风格--机器人配对信息service

        :param query_db: orm对象
        :param page_object: 编辑讲解风格--机器人配对对象
        :return: 编辑讲解风格--机器人配对校验结果
        """
        edit_explanation_style_robot_pair = page_object.model_dump(exclude_unset=True, exclude={'create_time', 'create_by', })
        explanation_style_robot_pair_info = await cls.explanation_style_robot_pair_detail_services(query_db, page_object.pairing_id)
        if explanation_style_robot_pair_info.pairing_id:
            try:
                await Explanation_style_robot_pairDao.edit_explanation_style_robot_pair_dao(query_db, edit_explanation_style_robot_pair)
                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_robot_pair_services(cls, query_db: AsyncSession, page_object: DeleteExplanation_style_robot_pairModel):
        """
        删除讲解风格--机器人配对信息service

        :param query_db: orm对象
        :param page_object: 删除讲解风格--机器人配对对象
        :return: 删除讲解风格--机器人配对校验结果
        """
        if page_object.pairing_ids:
            pairing_id_list = page_object.pairing_ids.split(',')
            try:
                for pairing_id in pairing_id_list:
                    await Explanation_style_robot_pairDao.delete_explanation_style_robot_pair_dao(query_db, Explanation_style_robot_pairModel(pairingId=pairing_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_robot_pair_detail_services(cls, query_db: AsyncSession, pairing_id: int):
        """
        获取讲解风格--机器人配对详细信息service

        :param query_db: orm对象
        :param pairing_id: 主键ID
        :return: 主键ID对应的信息
        """
        explanation_style_robot_pair = await Explanation_style_robot_pairDao.get_explanation_style_robot_pair_detail_by_id(query_db, pairing_id=pairing_id)
        if explanation_style_robot_pair:
            result = Explanation_style_robot_pairModel(**CamelCaseUtil.transform_result(explanation_style_robot_pair))
        else:
            result = Explanation_style_robot_pairModel(**dict())

        return result

    @staticmethod
    async def export_explanation_style_robot_pair_list_services(explanation_style_robot_pair_list: List):
        """
        导出讲解风格--机器人配对信息service

        :param explanation_style_robot_pair_list: 讲解风格--机器人配对信息列表
        :return: 讲解风格--机器人配对信息对应excel的二进制数据
        """
        # 创建一个映射字典，将英文键映射到中文键
        mapping_dict = {
            'pairingId': '主键ID',
            'robotId': '机器人ID',
            'explanationStyleId': '讲解风格ID',
            'prompt': '提示词',
            'status': '状态(0停用,1使用)',
            'createTime': '创建时间',
            'createBy': '创建者',
            'updateTime': '修改时间',
            'updateBy': '修改者',
        }
        binary_data = ExcelUtil.export_list2excel(explanation_style_robot_pair_list, mapping_dict)

        return binary_data
