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.robot_action_dao import Robot_actionDao
from module_admin.entity.vo.robot_action_vo import DeleteRobot_actionModel, Robot_actionModel, Robot_actionPageQueryModel
from utils.common_util import CamelCaseUtil
from utils.excel_util import ExcelUtil


class Robot_actionService:
    """
    机器人动作模块服务层
    """

    @classmethod
    async def get_robot_action_list_services(
        cls, query_db: AsyncSession, query_object: Robot_actionPageQueryModel, is_page: bool = False
    ):
        """
        获取机器人动作列表信息service

        :param query_db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 机器人动作列表信息对象
        """
        robot_action_list_result = await Robot_actionDao.get_robot_action_list(query_db, query_object, is_page)

        return robot_action_list_result


    @classmethod
    async def add_robot_action_services(cls, query_db: AsyncSession, page_object: Robot_actionModel):
        """
        新增机器人动作信息service

        :param query_db: orm对象
        :param page_object: 新增机器人动作对象
        :return: 新增机器人动作校验结果
        """
        try:
            await Robot_actionDao.add_robot_action_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_robot_action_services(cls, query_db: AsyncSession, page_object: Robot_actionModel):
        """
        编辑机器人动作信息service

        :param query_db: orm对象
        :param page_object: 编辑机器人动作对象
        :return: 编辑机器人动作校验结果
        """
        edit_robot_action = page_object.model_dump(exclude_unset=True, exclude={})
        robot_action_info = await cls.robot_action_detail_services(query_db, page_object.id)
        if robot_action_info.id:
            try:
                await Robot_actionDao.edit_robot_action_dao(query_db, edit_robot_action)
                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_robot_action_services(cls, query_db: AsyncSession, page_object: DeleteRobot_actionModel):
        """
        删除机器人动作信息service

        :param query_db: orm对象
        :param page_object: 删除机器人动作对象
        :return: 删除机器人动作校验结果
        """
        if page_object.ids:
            id_list = page_object.ids.split(',')
            try:
                for id in id_list:
                    await Robot_actionDao.delete_robot_action_dao(query_db, Robot_actionModel(id=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='传入主键 自增为空')

    @classmethod
    async def robot_action_detail_services(cls, query_db: AsyncSession, id: int):
        """
        获取机器人动作详细信息service

        :param query_db: orm对象
        :param id: 主键 自增
        :return: 主键 自增对应的信息
        """
        robot_action = await Robot_actionDao.get_robot_action_detail_by_id(query_db, id=id)
        if robot_action:
            result = Robot_actionModel(**CamelCaseUtil.transform_result(robot_action))
        else:
            result = Robot_actionModel(**dict())

        return result

    @staticmethod
    async def export_robot_action_list_services(robot_action_list: List):
        """
        导出机器人动作信息service

        :param robot_action_list: 机器人动作信息列表
        :return: 机器人动作信息对应excel的二进制数据
        """
        # 创建一个映射字典，将英文键映射到中文键
        mapping_dict = {
            'id': '主键 自增',
            'name': '动作名',
            'description': '描述',
            'intervalMin': '最小时间间隔 单位s',
            'intervalMax': '最大时间间隔 单位s',
            'status': '动作状态 0 未启用, 1已启用',
            'updateTime': '修改时间',
        }
        binary_data = ExcelUtil.export_list2excel(robot_action_list, mapping_dict)

        return binary_data
