# -*- coding: utf-8 -*-

from utils.redis.connect import r
from utils.redis.rkeys import SYSTEM_MESSAGE_DELETED_INFO, SYSTEM_MESSAGE_READ_INFO


def set_system_message_read_info(user_id):
    """ 设置系统消息读取信息 """
    from message.models import SystemMessageReadInfo
    read_messages = SystemMessageReadInfo.objects.filter(user_id=user_id, status=True)
    read_message_ids = [msg.msg_id for msg in read_messages]
    r.setexjson(SYSTEM_MESSAGE_READ_INFO % user_id, r.REDIS_EXPIRED_ONE_MONTH, read_message_ids)
    return read_message_ids


def get_system_message_read_info(user_id):
    """ 获取系统消息读取信息 """
    return r.getjson(SYSTEM_MESSAGE_READ_INFO % user_id, default='[]') or set_system_message_read_info(user_id)


def set_system_message_delete_info(user_id):
    """ 设置系统消息删除信息 """
    from message.models import SystemMessageDeleteInfo
    deleted_messages = SystemMessageDeleteInfo.objects.filter(user_id=user_id, status=True)
    deleted_message_ids = [msg.msg_id for msg in deleted_messages]
    r.setexjson(SYSTEM_MESSAGE_DELETED_INFO % user_id, r.REDIS_EXPIRED_ONE_MONTH, deleted_message_ids)
    return deleted_message_ids


def get_system_message_delete_info(user_id):
    """ 获取系统消息删除信息 """
    return r.getjson(SYSTEM_MESSAGE_DELETED_INFO % user_id, default='[]') or set_system_message_delete_info(user_id)
