from datetime import datetime, time
from sqlalchemy import delete, select, update, desc
from sqlalchemy.ext.asyncio import AsyncSession
from module_admin.entity.do.words_do import GuideWords
from module_admin.entity.vo.words_vo import WordsModel, WordsPageQueryModel
from utils.page_util import PageUtil


class WordsDao:
    """
    引导词模块数据库操作层
    """
    
    @classmethod
    async def get_words_by_type(cls, db: AsyncSession, type: str):
        """
        获取随机引导词

        :param db: orm对象
        :param type: 引导词类型
        :return: 引导词对象
        """
        guide_word = (
            (
                await db.execute(
                    select(GuideWords.text)
                    .where(
                        GuideWords.type == type,
                    )
                )
            )
        )
        return guide_word.scalars().all()
        

    @classmethod
    async def edit_del_words(cls, db: AsyncSession, guide_ids: str, del_flag:str):

        guide_id_list = guide_ids.strip(' ').split(',')

        stmt = (
            update(GuideWords)
            .values(del_flag = del_flag)
            .where(GuideWords.guide_id.in_(guide_id_list))
        )

        return await db.execute(stmt)

    @classmethod
    async def get_words_detail_by_id(cls, db: AsyncSession, guide_id: int):
        """
        根据引导词id获取引导词详细信息

        :param db: orm对象
        :param guide_id: 引导词id
        :return: 引导词信息对象
        """
        words_info = (
            (
                await db.execute(
                    select(GuideWords)
                    .where(
                        GuideWords.guide_id == guide_id
                    )
                )
            )
            .scalars()
            .first()
        )

        return words_info

    @classmethod
    async def get_words_detail_by_info(cls, db: AsyncSession, words: WordsModel):
        """
        根据引导词参数获取引导词信息

        :param db: orm对象
        :param words: 引导词参数对象
        :return: 引导词信息对象
        """
        words_info = (
            (
                await db.execute(
                    select(GuideWords).where(
                    )
                )
            )
            .scalars()
            .first()
        )

        return words_info

    @classmethod
    async def get_words_list(cls, db: AsyncSession, query_object: WordsPageQueryModel, is_page: bool = False):
        """
        根据查询参数获取引导词列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 引导词列表信息对象
        """
        query = (
            select(GuideWords)
            .where(
                GuideWords.type == query_object.type if query_object.type else True,
                GuideWords.text.like(f'%{query_object.text}%') if query_object.text else True,
                GuideWords.create_time.between(
                    datetime.combine(datetime.strptime(query_object.begin_time, '%Y-%m-%d'), time(00, 00, 00)),
                    datetime.combine(datetime.strptime(query_object.end_time, '%Y-%m-%d'), time(23, 59, 59)),
                )
                if query_object.begin_time and query_object.end_time
                else True,
            )
            .order_by(desc(GuideWords.create_time))
            .distinct()
        )
        words_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return words_list

    @classmethod
    async def add_words_dao(cls, db: AsyncSession, words: WordsModel):
        """
        新增引导词数据库操作

        :param db: orm对象
        :param words: 引导词对象
        :return:
        """
        db_words = GuideWords(**words.model_dump(exclude={}))
        db.add(db_words)
        await db.flush()

        return db_words

    @classmethod
    async def edit_words_dao(cls, db: AsyncSession, words: dict):
        """
        编辑引导词数据库操作

        :param db: orm对象
        :param words: 需要更新的引导词字典
        :return:
        """
        await db.execute(update(GuideWords), [words])

    @classmethod
    async def delete_words_dao(cls, db: AsyncSession, words: WordsModel):
        """
        删除引导词数据库操作

        :param db: orm对象
        :param words: 引导词对象
        :return:
        """
        await db.execute(delete(GuideWords).where(GuideWords.guide_id.in_([words.guide_id])))

