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

from django.core.serializers.json import DjangoJSONEncoder

from utils.redis.connect import r
from utils.redis.rkeys import LENSMAN_PHOTO_ORDER_RECORD


def set_lensman_order_record(porder):
    """ 设置摄影师照片购买记录 """
    r.setexjson(LENSMAN_PHOTO_ORDER_RECORD % (porder.photo_id, porder.user_id), r.REDIS_EXPIRED_ONE_MONTH, porder.porder_info, cls=DjangoJSONEncoder)
    return porder.porder_info


def set_lensman_order_record_by_id(photo_id, user_id):
    """ 设置摄影师照片购买记录 """
    from group.models import GroupPhotoOrderInfo
    try:
        porder = GroupPhotoOrderInfo.objects.get(photo_id=photo_id, user_id=user_id)
    except GroupPhotoOrderInfo.DoesNotExist:
        return {}
    return set_lensman_order_record(porder)


def get_lensman_order_record(photo_id, user_id):
    """ 获取摄影师照片购买记录 """
    return r.getjson(LENSMAN_PHOTO_ORDER_RECORD % (photo_id, user_id)) or set_lensman_order_record_by_id(photo_id, user_id)
