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.visitor_type_dao import Visitor_typeDao
from module_admin.entity.vo.visitor_type_vo import DeleteVisitor_typeModel, Visitor_typeModel, Visitor_typePageQueryModel
from utils.common_util import CamelCaseUtil
from utils.excel_util import ExcelUtil


class Visitor_typeService:
    """
    访客类型模块服务层
    """

    @classmethod
    async def get_system_visitor_type_list(cls, query_db: AsyncSession):
        return await Visitor_typeDao.get_system_visitor_type_list(query_db)

    @classmethod
    async def get_visitor_type_list_services(
        cls, query_db: AsyncSession, query_object: Visitor_typePageQueryModel, is_page: bool = False
    ):
        """
        获取访客类型列表信息service

        :param query_db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 访客类型列表信息对象
        """
        visitor_type_list_result = await Visitor_typeDao.get_visitor_type_list(query_db, query_object, is_page)

        return visitor_type_list_result


    @classmethod
    async def add_visitor_type_services(cls, query_db: AsyncSession, page_object: Visitor_typeModel):
        """
        新增访客类型信息service

        :param query_db: orm对象
        :param page_object: 新增访客类型对象
        :return: 新增访客类型校验结果
        """
        try:
            await Visitor_typeDao.add_visitor_type_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_visitor_type_services(cls, query_db: AsyncSession, page_object: Visitor_typeModel):
        """
        编辑访客类型信息service

        :param query_db: orm对象
        :param page_object: 编辑访客类型对象
        :return: 编辑访客类型校验结果
        """
        edit_visitor_type = page_object.model_dump(exclude_unset=True, exclude={'create_time', 'create_by', })
        visitor_type_info = await cls.visitor_type_detail_services(query_db, page_object.type_id)
        if visitor_type_info.type_id:
            try:
                await Visitor_typeDao.edit_visitor_type_dao(query_db, edit_visitor_type)
                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_visitor_type_services(cls, query_db: AsyncSession, page_object: DeleteVisitor_typeModel):
        """
        删除访客类型信息service

        :param query_db: orm对象
        :param page_object: 删除访客类型对象
        :return: 删除访客类型校验结果
        """
        if page_object.type_ids:
            type_id_list = page_object.type_ids.split(',')
            try:
                for type_id in type_id_list:
                    await Visitor_typeDao.delete_visitor_type_dao(query_db, Visitor_typeModel(typeId=type_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 visitor_type_detail_services(cls, query_db: AsyncSession, type_id: int):
        """
        获取访客类型详细信息service

        :param query_db: orm对象
        :param type_id: 访客类型ID
        :return: 访客类型ID对应的信息
        """
        visitor_type = await Visitor_typeDao.get_visitor_type_detail_by_id(query_db, type_id=type_id)
        if visitor_type:
            result = Visitor_typeModel(**CamelCaseUtil.transform_result(visitor_type))
        else:
            result = Visitor_typeModel(**dict())

        return result

    @staticmethod
    async def export_visitor_type_list_services(visitor_type_list: List):
        """
        导出访客类型信息service

        :param visitor_type_list: 访客类型信息列表
        :return: 访客类型信息对应excel的二进制数据
        """
        # 创建一个映射字典，将英文键映射到中文键
        mapping_dict = {
            'typeId': '访客类型ID',
            'name': '类型名称',
            'value': '类型值',
            'createTime': '创建时间',
            'createBy': '创建者',
            'updateTime': '修改时间',
            'updateBy': '修改者',
        }
        binary_data = ExcelUtil.export_list2excel(visitor_type_list, mapping_dict)

        return binary_data
