"""
@author: luojunhui
@description: 校验视频状态，若视频状态为不通过，则修改视频状态
"""
import traceback

from tqdm import tqdm

from applications import PQAPI, PQMySQL, bot


class VideoStatusManager(object):
    """
    视频状态校验 and 修改
    """
    def __init__(self):
        self.db_client = None
        self.pq_api = None

    def base_init(self):
        """
        初始化数据库连接和 pq 方法类
        """
        try:
            self.db_client = PQMySQL()
        except Exception as e:
            error_msg = traceback.format_exc()
            bot(
                title="视频状态校验任务，每 20 分钟执行一次，数据库初始化异常",
                detail={
                    "error": str(e),
                    "error_msg": error_msg
                }
            )
            return False
        try:
            self.pq_api = PQAPI()
        except Exception as e:
            error_msg = traceback.format_exc()
            bot(
                title="视频状态校验任务，每 20 分钟执行一次，apollo连接异常",
                detail={
                    "error": str(e),
                    "error_msg": error_msg
                }
            )
            return False
        return True

    def get_video_list_status(self, videoList):
        """
        获取视频 list 的状态，并且返回状态为不通过的视频 list
        :param videoList: 视频 id_list， 最长为 20
        :return: bad video list
        """
        response = self.pq_api.getPQVideoListDetail(video_list=videoList)
        detail_list = response.get('data', [])
        if detail_list:
            bad_video_list = [i for i in detail_list if i['auditStatus'] != 5]
            bad_id_list = [i['id'] for i in bad_video_list]
        else:
            bad_id_list = []
        return bad_id_list

    def get_published_video_ids_daily(self):
        """
        获取每日发布的视频 id 的状态
        :return:
        publish_time > {today_time_stamp} and
        """
        select_sql = f"""
        SELECT video_id
        FROM get_off_videos
        WHERE check_status = 0 and video_status = 1;
        """
        video_id_tuple = self.db_client.select(select_sql)
        video_id_list = [i[0] for i in video_id_tuple]
        return video_id_list

    def update_check_status(self, vid_list):
        """

        :param vid_list:
        :return:
        """
        sql = f"""
        UPDATE get_off_videos
        SET check_status = %s
        where video_id in %s;
        """
        self.db_client.update(
            sql=sql,
            params=(1, tuple(vid_list))
        )
        print("更新 check_status 成功")

    def deal(self):
        """
        Deal Function
        :return:
        """
        def chunk_iterator(arr, chunk_size):
            """生成器函数，将数组分组成指定大小的chunks"""
            for i in range(0, len(arr), chunk_size):
                yield arr[i:i + chunk_size]

        video_id_list = self.get_published_video_ids_daily()
        video_chunks = chunk_iterator(video_id_list, 10)

        bad_count = 0
        for video_temp in video_chunks:
            bad_id_list = self.get_video_list_status(video_temp)
            fail_list = []
            if bad_id_list:
                bad_count += len(bad_id_list)
                for bad_id in tqdm(bad_id_list):
                    response = self.pq_api.changeVideoStatus(bad_id)
                    if not response:
                        fail_list.append(bad_id)
            if fail_list:
                bot(
                    title="修改视频状态失败",
                    detail=fail_list
                )
            self.update_check_status(video_temp)
        print("total", len(video_id_list))
        print("bad_total", bad_count)


def main():
    """
    task
    :return:
    """

    VM = VideoStatusManager()
    if VM.base_init():
        VM.deal()


if __name__ == '__main__':
    main()
