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.identification_statistics_dao import Identification_statisticsDao
from module_admin.entity.vo.identification_statistics_vo import DeleteIdentification_statisticsModel, Identification_statisticsModel, Identification_statisticsPageQueryModel
from utils.common_util import CamelCaseUtil
from utils.excel_util import ExcelUtil


class Identification_statisticsService:
    """
    识别统计模块服务层
    """

    @classmethod
    async def get_today_identification_statistics_services(cls, query_db: AsyncSession ):
        """
        获取识别统计列表
        :param query_object:
        :param is_page:
        :return:
        """
        result = await Identification_statisticsDao.get_today_identification_statistics(query_db)
        return result

    @classmethod
    async def get_identification_statistics_list_services(
        cls, query_db: AsyncSession, query_object: Identification_statisticsPageQueryModel, is_page: bool = False
    ):
        """
        获取识别统计列表信息service

        :param query_db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 识别统计列表信息对象
        """
        identification_statistics_list_result = await Identification_statisticsDao.get_identification_statistics_list(query_db, query_object, is_page)

        return identification_statistics_list_result


    @classmethod
    async def add_identification_statistics_services(cls, query_db: AsyncSession, page_object: Identification_statisticsModel):
        """
        新增识别统计信息service

        :param query_db: orm对象
        :param page_object: 新增识别统计对象
        :return: 新增识别统计校验结果
        """
        try:
            await Identification_statisticsDao.add_identification_statistics_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_identification_statistics_services(cls, query_db: AsyncSession, page_object: Identification_statisticsModel):
        """
        编辑识别统计信息service

        :param query_db: orm对象
        :param page_object: 编辑识别统计对象
        :return: 编辑识别统计校验结果
        """
        edit_identification_statistics = page_object.model_dump(exclude_unset=True, exclude={'create_time', 'create_by'})
        identification_statistics_info = await cls.identification_statistics_detail_services(query_db, page_object.id)
        if identification_statistics_info.id:
            try:
                await Identification_statisticsDao.edit_identification_statistics_dao(query_db, edit_identification_statistics)
                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_identification_statistics_services(cls, query_db: AsyncSession, page_object: DeleteIdentification_statisticsModel):
        """
        删除识别统计信息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 Identification_statisticsDao.delete_identification_statistics_dao(query_db, Identification_statisticsModel(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 identification_statistics_detail_services(cls, query_db: AsyncSession, id: int):
        """
        获取识别统计详细信息service

        :param query_db: orm对象
        :param id: 主键 自增
        :return: 主键 自增对应的信息
        """
        identification_statistics = await Identification_statisticsDao.get_identification_statistics_detail_by_id(query_db, id=id)
        if identification_statistics:
            result = Identification_statisticsModel(**CamelCaseUtil.transform_result(identification_statistics))
        else:
            result = Identification_statisticsModel(**dict())

        return result

    @staticmethod
    async def export_identification_statistics_list_services(identification_statistics_list: List):
        """
        导出识别统计信息service

        :param identification_statistics_list: 识别统计信息列表
        :return: 识别统计信息对应excel的二进制数据
        """
        # 创建一个映射字典，将英文键映射到中文键
        mapping_dict = {
            'id': '主键 自增',
            'employ': '员工识别成功数',
            'visitor': '访客识别成功数',
            'stranger': '位置人员识别失败数',
            'createTime': '创建时间',
            'createBy': '创建者',
        }
        binary_data = ExcelUtil.export_list2excel(identification_statistics_list, mapping_dict)

        return binary_data
