from config import set_config
from log import Log
from db_helper import RedisHelper

config_ = set_config()
log_ = Log()


def get_video_list(ab_exp_code, search_time, data_list_type, region_code, page_num=1, page_size=100):
    """
    管理后台算法视频列表可视化 - 获取视频列表
    :param ab_exp_code: 实验id，type-string
    :param search_time: 时间，type-string，格式: %y%m%d.%h，eg: '20220714.05'
    :param data_list_type: 视频数据表类型，type-int
    :param region_code: 地域（省份），type-string，eg: '110000
    :param page_num: 页码，type-int，默认: 1
    :param page_size: 每页请求数，type-int，默认: 100
    :return: result
    """
    redis_helper = RedisHelper()
    if ab_exp_code is None or search_time is None or data_list_type is None or region_code is None \
            or region_code == '0':
        # 不支持获取全部地域数据列表
        # data = []
        # data_count = 0
        result = {'code': -1, 'message': 'request data error!'}
        return result
    search_time_seg = search_time.split('.')
    if search_time_seg[1][0] == '0':
        search_time_seg[1] = search_time_seg[1][1]
    search_time = ':'.join(search_time_seg)
    data = []
    key_params = config_.AB_EXP_CODE.get(ab_exp_code)
    # key_params = config_.VIDEO_DATA_LIST_AB_EXP_CODE_MAPPING.get(ab_exp_code)
    # app_type = key_params.get('app_type')
    data_key = key_params.get('data_key')
    rule_key = key_params.get('rule_key')
    rule_key_30day = key_params.get('30day_rule_key')
    if data_list_type == 0:
        # 视频数据表类型 选择为全部
        key_prefix_list = config_.VIDEO_DATA_LIST_MAPPING.get(data_list_type).get('keyPrefixList')
        if ab_exp_code not in ['139']:
            key_prefix_list = key_prefix_list[1:]
        for item in key_prefix_list:
            data_list_code = item.get('dataListCode')
            key_prefix = item.get('keyPrefix')
            if data_list_code == 1:
                key_name = f"{key_prefix}{data_key}:{rule_key_30day}:{search_time_seg[0]}"
            else:
                key_name = f"{key_prefix}{region_code}:{data_key}:{rule_key}:{search_time}"
            key_data = redis_helper.get_all_data_from_zset(key_name=key_name, desc=True, with_scores=True)
            if key_data is None:
                continue
            videos = [
                {
                    'abExpCode': ab_exp_code,
                    'searchTime': search_time,
                    'dataListType': data_list_code,
                    'regionCode': region_code,
                    'videoId': int(video_id),
                    'score': float(score),
                }
                for video_id, score in key_data
            ]
            data.extend(videos)
    else:
        key_prefix = config_.VIDEO_DATA_LIST_MAPPING.get(data_list_type).get('keyPrefix')
        if data_list_type == 1:
            if ab_exp_code not in ['139']:
                key_data = None
            else:
                key_name = f"{key_prefix}{data_key}:{rule_key_30day}:{search_time_seg[0]}"
                key_data = redis_helper.get_all_data_from_zset(key_name=key_name, desc=True, with_scores=True)
        else:
            key_name = f"{key_prefix}{region_code}:{data_key}:{rule_key}:{search_time}"
            key_data = redis_helper.get_all_data_from_zset(key_name=key_name, desc=True, with_scores=True)
        if key_data is None:
            videos = []
        else:
            videos = [
                {
                    'abExpCode': ab_exp_code,
                    'searchTime': search_time,
                    'dataListType': data_list_type,
                    'regionCode': region_code,
                    'videoId': int(video_id),
                    'score': float(score),
                }
                for video_id, score in key_data
            ]
        data.extend(videos)
    # 添加"rank"
    final_data = []
    for i, item in enumerate(data):
        item['rank'] = i + 1
        final_data.append(item)
    result = {
        "code": 200,
        "message": "success",
        "dataCount": len(final_data),
        "data": final_data[(page_num-1)*page_size:page_num*page_size]
    }
    return result


def search_video(ab_exp_code, search_time, data_list_type, region_code, video_id, page_num=1, page_size=100):
    """
    管理后台算法视频列表可视化 - 搜索视频id
    :param ab_exp_code: 实验id，type-string
    :param search_time: 时间，type-string，格式: %y%m%d.%h，eg: '20220714.05'
    :param data_list_type: 视频数据表类型，type-int
    :param region_code: 地域（省份），type-string，eg: '110000
    :param video_id: 视频id，type-int
    :param page_num: 页码，type-int，默认: 1
    :param page_size: 每页请求数，type-int，默认: 100
    :return: result
    """
    redis_helper = RedisHelper()
    if ab_exp_code is None or search_time is None or data_list_type is None or region_code is None or video_id is None:
        # data = []
        # data_count = 0
        result = {'code': -1, 'message': 'request data error!'}
        return result
    search_time_seg = search_time.split('.')
    if search_time_seg[1][0] == '0':
        search_time_seg[1] = search_time_seg[1][1]
    search_time = ':'.join(search_time_seg)
    data = []
    key_params = config_.AB_EXP_CODE.get(ab_exp_code)
    # key_params = config_.VIDEO_DATA_LIST_AB_EXP_CODE_MAPPING.get(ab_exp_code)
    # app_type = key_params.get('app_type')
    data_key = key_params.get('data_key')
    rule_key = key_params.get('rule_key')
    rule_key_30day = key_params.get('30day_rule_key')
    if data_list_type == 0 and region_code == '0':
        # 视频数据表类型 和 地域 选择都为全部
        key_prefix_list = config_.VIDEO_DATA_LIST_MAPPING.get(data_list_type).get('keyPrefixList')
        if ab_exp_code not in ['139']:
            key_prefix_list = key_prefix_list[1:]
        code_list = [code for _, code in config_.REGION_CODE.items()]
        for item in key_prefix_list:
            data_list_code = item.get('dataListCode')
            key_prefix = item.get('keyPrefix')
            if data_list_code == 1:
                key_name = f"{key_prefix}{data_key}:{rule_key_30day}:{search_time_seg[0]}"
                rank = redis_helper.get_rank_with_value(key_name=key_name, value=int(video_id), desc=True)
                score = redis_helper.get_score_with_value(key_name=key_name, value=int(video_id))
                if rank is None or score is None:
                    continue
                videos = {
                    'abExpCode': ab_exp_code,
                    'searchTime': search_time,
                    'dataListType': data_list_code,
                    'regionCode': '0',
                    'videoId': int(video_id),
                    'score': float(score),
                    'rank': int(rank) + 1,
                }
                data.append(videos)
            else:
                for code in code_list:
                    key_name = f"{key_prefix}{code}:{data_key}:{rule_key}:{search_time}"
                    rank = redis_helper.get_rank_with_value(key_name=key_name, value=int(video_id), desc=True)
                    score = redis_helper.get_score_with_value(key_name=key_name, value=int(video_id))
                    if rank is None or score is None:
                        continue
                    videos = {
                        'abExpCode': ab_exp_code,
                        'searchTime': search_time,
                        'dataListType': data_list_code,
                        'regionCode': code,
                        'videoId': int(video_id),
                        'score': float(score),
                        'rank': int(rank) + 1,
                    }
                    data.append(videos)
        data.sort(key=lambda x: (x['dataListType'], x['regionCode']), reverse=False)

    elif data_list_type == 0:
        # 视频数据表类型 选择为全部
        key_prefix_list = config_.VIDEO_DATA_LIST_MAPPING.get(data_list_type).get('keyPrefixList')
        if ab_exp_code not in ['139']:
            key_prefix_list = key_prefix_list[1:]
        for item in key_prefix_list:
            data_list_code = item.get('dataListCode')
            key_prefix = item.get('keyPrefix')
            if data_list_code == 1:
                key_name = f"{key_prefix}{data_key}:{rule_key_30day}:{search_time_seg[0]}"
            else:
                key_name = f"{key_prefix}{region_code}:{data_key}:{rule_key}:{search_time}"
            rank = redis_helper.get_rank_with_value(key_name=key_name, value=int(video_id), desc=True)
            score = redis_helper.get_score_with_value(key_name=key_name, value=int(video_id))
            if rank is None or score is None:
                continue
            videos = {
                'abExpCode': ab_exp_code,
                'searchTime': search_time,
                'dataListType': data_list_code,
                'regionCode': region_code,
                'videoId': int(video_id),
                'score': float(score),
                'rank': int(rank) + 1,
            }
            data.append(videos)
        data.sort(key=lambda x: x['dataListType'], reverse=False)

    elif region_code == '0':
        # 地域 选择为全部
        code_list = [code for _, code in config_.REGION_CODE.items()]
        key_prefix = config_.VIDEO_DATA_LIST_MAPPING.get(data_list_type).get('keyPrefix')
        for code in code_list:
            if data_list_type == 1:
                if ab_exp_code not in ['139']:
                    continue
                key_name = f"{key_prefix}{data_key}:{rule_key_30day}:{search_time_seg[0]}"
            else:
                key_name = f"{key_prefix}{code}:{data_key}:{rule_key}:{search_time}"
            rank = redis_helper.get_rank_with_value(key_name=key_name, value=int(video_id), desc=True)
            score = redis_helper.get_score_with_value(key_name=key_name, value=int(video_id))
            if rank is None or score is None:
                continue
            videos = {
                'abExpCode': ab_exp_code,
                'searchTime': search_time,
                'dataListType': data_list_type,
                'regionCode': code,
                'videoId': int(video_id),
                'score': float(score),
                'rank': int(rank) + 1,
            }
            data.append(videos)
        data.sort(key=lambda x: x['regionCode'], reverse=False)

    else:
        key_prefix = config_.VIDEO_DATA_LIST_MAPPING.get(data_list_type).get('keyPrefix')
        if data_list_type == 1:
            if ab_exp_code not in ['139']:
                rank, score = None, None
            else:
                key_name = f"{key_prefix}{data_key}:{rule_key_30day}:{search_time_seg[0]}"
                rank = redis_helper.get_rank_with_value(key_name=key_name, value=int(video_id), desc=True)
                score = redis_helper.get_score_with_value(key_name=key_name, value=int(video_id))
        else:
            key_name = f"{key_prefix}{region_code}:{data_key}:{rule_key}:{search_time}"
            rank = redis_helper.get_rank_with_value(key_name=key_name, value=int(video_id), desc=True)
            score = redis_helper.get_score_with_value(key_name=key_name, value=int(video_id))
        if rank is not None and score is not None:
            videos = {
                'abExpCode': ab_exp_code,
                'searchTime': search_time,
                'dataListType': data_list_type,
                'regionCode': region_code,
                'videoId': int(video_id),
                'score': float(score),
                'rank': int(rank) + 1,
            }
            data.append(videos)

    result = {
        "code": 200,
        "message": "success",
        "dataCount": len(data),
        "data": data[(page_num - 1) * page_size:page_num * page_size]
    }
    return result


if __name__ == '__main__':
    get_video_list('139', '20220909.01', 0, '110000')
