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

import json

from django.conf import settings

from message.models import SystemMessageDeleteInfo, SystemMessageReadInfo
from utils.redis.rkeys import SYSTEM_MESSAGE_DELETED_INFO, SYSTEM_MESSAGE_READ_INFO


r = settings.REDIS_CACHE


# 系统消息相关


def set_system_message_read_info(user_id):
    """ 设置系统消息读取信息 """
    read_messages = SystemMessageReadInfo.objects.filter(user_id=user_id, status=True)
    read_message_ids = [msg.msg_id for msg in read_messages]
    r.setex(SYSTEM_MESSAGE_READ_INFO % user_id, settings.REDIS_EXPIRED_MONTH, json.dumps(read_message_ids))
    return read_message_ids


def get_system_message_read_info(user_id):
    """ 获取系统消息读取信息 """
    return json.loads(r.get(SYSTEM_MESSAGE_READ_INFO % user_id) or '[]') or set_system_message_read_info(user_id)


def set_system_message_delete_info(user_id):
    """ 设置系统消息删除信息 """
    deleted_messages = SystemMessageDeleteInfo.objects.filter(user_id=user_id, status=True)
    deleted_message_ids = [msg.msg_id for msg in deleted_messages]
    r.setex(SYSTEM_MESSAGE_DELETED_INFO % user_id, settings.REDIS_EXPIRED_MONTH, json.dumps(deleted_message_ids))
    return deleted_message_ids


def get_system_message_delete_info(user_id):
    """ 获取系统消息删除信息 """
    return json.loads(r.get(SYSTEM_MESSAGE_DELETED_INFO % user_id) or '[]') or set_system_message_delete_info(user_id)
