from django.db import models
from django.db.models import Q
from sqlite3 import IntegrityError
from common import stat


class Swiped(models.Model):
    '''滑动记录'''
    STYPES = (
        ('like', '右滑'),
        ('superlike', '上滑'),
        ('dislike', '左滑'),
    )
    uid = models.IntegerField(verbose_name='用户 ID')
    sid = models.IntegerField(verbose_name='被滑动用户的 ID')
    stype = models.CharField(max_length=10, choices=STYPES, verbose_name='滑动的类型')
    stime = models.DateTimeField(auto_now_add=True, verbose_name='滑动的时间')

    class Meta:
        unique_together = ('uid', 'sid')  # uid 与 sid 联合唯一

    @classmethod
    def swiper(cls, self, uid, sid, styple):
        '''执行一次滑动'''
        try:
            return cls.objects.create(uid=uid, sid=sid, style='like')
        except IntegrityError:
            raise stat.RepeatSwipeErr

    @classmethod
    def has_liked(cls, uid, sid):
        '''检查是否喜欢过某人'''
        return cls.objects.filter(uid=uid, sid=sid , stype__in=['like','superlike']).exists()


class Friend(models.Model):
    '''好友表'''
    uid1 = models.IntegerField(verbose_name='用户 ID')
    uid2 = models.IntegerField(verbose_name='用户 ID')

    class Meta:
        unique_together = ('uid1', 'uid2')  # uid1 与 uid2 联合唯一

    @classmethod
    def make_friends(cls, uid1, uid2):
        '''添加好友关系'''
        # 调整 uid1 和 uid2 的顺序，小的值放前面
        uid1,uid2 = (uid2,uid1)if uid1>uid2 else (uid1,uid2)
        try:
            return cls.objects.create(uid1=uid1,uid2=uid2)
        except IntegrityError:
            raise stat.AreadyFriends

    @classmethod
    def break_off(cls,uid1,uid2):
        '''绝交'''
        # 调整 uid1 和 uid2 的顺序，小的值放前面
        uid1, uid2 = (uid2, uid1) if uid1 > uid2 else (uid1, uid2)
        cls.objects.filter(uid1=uid1,uid2=uid2).delete()

    @classmethod
    def get_my_friends_id(cls, uid):
        '''获取用户自己的好友 ID 列表'''
        query_condition = Q(uid1=uid) | Q(uid2=uid)
        friendship = cls.objects.filter(query_condition)

        # 取出所有好友的 UID
        friend_id_list = []
        for f_obj in friendship:
            if f_obj.uid1 == uid:
                friend_id_list.append(f_obj.uid2)
            else:
                friend_id_list.append(f_obj.uid1)
        return friend_id_list

from django.db import models
from django.db.models import Q
from sqlite3 import IntegrityError
from common import stat


class Swiped(models.Model):
    '''滑动记录'''
    STYPES = (
        ('like', '右滑'),
        ('superlike', '上滑'),
        ('dislike', '左滑'),
    )
    uid = models.IntegerField(verbose_name='用户 ID')
    sid = models.IntegerField(verbose_name='被滑动用户的 ID')
    stype = models.CharField(max_length=10, choices=STYPES, verbose_name='滑动的类型')
    stime = models.DateTimeField(auto_now_add=True, verbose_name='滑动的时间')

    class Meta:
        unique_together = ('uid', 'sid')  # uid 与 sid 联合唯一

    @classmethod
    def swiper(cls, self, uid, sid, styple):
        '''执行一次滑动'''
        try:
            return cls.objects.create(uid=uid, sid=sid, style='like')
        except IntegrityError:
            raise stat.RepeatSwipeErr

    @classmethod
    def has_liked(cls, uid, sid):
        '''检查是否喜欢过某人'''
        return cls.objects.filter(uid=uid, sid=sid , stype__in=['like','superlike']).exists()


class Friend(models.Model):
    '''好友表'''
    uid1 = models.IntegerField(verbose_name='用户 ID')
    uid2 = models.IntegerField(verbose_name='用户 ID')

    class Meta:
        unique_together = ('uid1', 'uid2')  # uid1 与 uid2 联合唯一

    @classmethod
    def make_friends(cls, uid1, uid2):
        '''添加好友关系'''
        # 调整 uid1 和 uid2 的顺序，小的值放前面
        uid1,uid2 = (uid2,uid1)if uid1>uid2 else (uid1,uid2)
        try:
            return cls.objects.create(uid1=uid1,uid2=uid2)
        except IntegrityError:
            raise stat.AreadyFriends

    @classmethod
    def break_off(cls,uid1,uid2):
        '''绝交'''
        # 调整 uid1 和 uid2 的顺序，小的值放前面
        uid1, uid2 = (uid2, uid1) if uid1 > uid2 else (uid1, uid2)
        cls.objects.filter(uid1=uid1,uid2=uid2).delete()

    @classmethod
    def get_my_friends_id(cls, uid):
        '''获取用户自己的好友 ID 列表'''
        query_condition = Q(uid1=uid) | Q(uid2=uid)
        friendship = cls.objects.filter(query_condition)

        # 取出所有好友的 UID
        friend_id_list = []
        for f_obj in friendship:
            if f_obj.uid1 == uid:
                friend_id_list.append(f_obj.uid2)
            else:
                friend_id_list.append(f_obj.uid1)
        return friend_id_list

滑动记录 # uid 与 sid 联合唯一 执行一次滑动 检查是否喜欢过某人 好友表 # uid1 与 uid2 联合唯一 添加好友关系 # 调整 uid1 和 uid2 的顺序，小的值放前面 绝交 # 调整 uid1 和 uid2 的顺序，小的值放前面 获取用户自己的好友 ID 列表 # 取出所有好友的 UID