import traceback
from datetime import date, datetime, timedelta
from db_helper import RedisHelper
from config import set_config
from log import Log

config_ = set_config()
log_ = Log()


def user2new(app_type, mid, uid):
    """
    管理后台指定用户恢复成新用户
    :param app_type: 产品标识
    :param mid: mid
    :param uid: uid
    :return: None
    """
    redis_helper = RedisHelper()
    if app_type is None:
        app_type_list = [value for _, value in config_.APP_TYPE.items()]
    else:
        app_type_list = [app_type]

    for app_type_item in app_type_list:
        # 清除对应的预曝光列表
        preview_key = f"{config_.PREVIEW_KEY_PREFIX}{app_type_item}:{mid}"
        redis_helper.del_keys(key_name=preview_key)

        # 清除对应的小时级数据及数据记录
        h_key_prefix_list = [
            # config_.H_WITH_MID_RECALL_KEY_NAME_PREFIX,
            # config_.H_WITH_MID_RECORD_KEY_NAME_PREFIX,
            # config_.H_WITH_MID_RECALL_KEY_NAME_PREFIX_24H,
            # config_.H_WITH_MID_RECORD_KEY_NAME_PREFIX_24H,
            # config_.H_WITH_MID_RECALL_KEY_NAME_PREFIX_REGION_24H,
            # config_.H_WITH_MID_RECORD_KEY_NAME_PREFIX_REGION_24H,
            config_.LAST_VIDEO_FROM_REGION_DUP3_48H_PREFIX,
            config_.RECORD_KEY_NAME_PREFIX_LAST_VIDEO_REGION_DUP3_48H,
            config_.LAST_VIDEO_FROM_REGION_DUP2_48H_PREFIX,
            config_.RECORD_KEY_NAME_PREFIX_LAST_VIDEO_REGION_DUP2_48H,
            config_.LAST_VIDEO_FROM_REGION_DUP3_24H_PREFIX,
            config_.RECORD_KEY_NAME_PREFIX_LAST_VIDEO_REGION_DUP3_24H,
            config_.LAST_VIDEO_FROM_REGION_DUP2_24H_PREFIX,
            config_.RECORD_KEY_NAME_PREFIX_LAST_VIDEO_REGION_DUP2_24H,
            config_.LAST_VIDEO_FROM_REGION_DUP1_24H_PREFIX,
            config_.RECORD_KEY_NAME_PREFIX_LAST_VIDEO_REGION_DUP1_24H,
            config_.LAST_VIDEO_FROM_REGION_H_PREFIX,
            config_.RECORD_KEY_NAME_PREFIX_LAST_VIDEO_REGION_H,
            config_.LAST_VIDEO_FROM_30DAY_PREFIX,
            config_.RECORD_KEY_NAME_PREFIX_LAST_VIDEO_30DAY,
        ]
        for h_key_prefix in h_key_prefix_list:
            h_key_name = f"{h_key_prefix}{app_type_item}:{mid}"
            redis_helper.del_keys(key_name=h_key_name)

        # 清除用户上一次列表中对应的位置记录数据
        last_idx_key_prefix_list = [
            config_.LAST_VIDEO_FROM_ROV_POOL_PREFIX,
            # config_.LAST_VIDEO_FROM_RULE_DAY_POOL_PREFIX,
            # config_.LAST_VIDEO_FROM_ROV_POOL_PRE_PREFIX,
            # config_.LAST_VIDEO_FROM_ROV_POOL_NOW_PREFIX,
            # f"{config_.LAST_VIDEO_FROM_REGION_DUP_PREFIX}{1}.",
            # f"{config_.LAST_VIDEO_FROM_REGION_DUP_PREFIX}{2}.",
            # config_.LAST_VIDEO_FROM_WHOLE_MOVIES_PREFIX,
            config_.LAST_VIDEO_FROM_SPECIAL_POOL_PREFIX,
        ]
        # 天级
        now_date = date.today().strftime('%Y%m%d')
        pre_date = (date.today() - timedelta(days=1)).strftime('%Y%m%d')
        date_list = [now_date, pre_date]
        for dt in date_list:
            for last_idx_key_prefix in last_idx_key_prefix_list:
                h_key_name = f"{last_idx_key_prefix}{app_type_item}:{mid}:{dt}"
                redis_helper.del_keys(key_name=h_key_name)
        # 小时级
        h = datetime.now().hour
        h_list = [h]
        if h == 0:
            h_list.append(23)
        else:
            h_list.append(h-1)
        for redis_h in h_list:
            for last_idx_key_prefix in last_idx_key_prefix_list:
                h_key_name = f"{last_idx_key_prefix}{app_type_item}:{mid}:{redis_h}"
                redis_helper.del_keys(key_name=h_key_name)
