# Create your models here.
import hashlib

from django.db import models


#自定义管理器类
class PostsManager(models.Manager):
    #重写父类的get_queryset
    def get_queryset(self):
        return super().get_queryset().filter(isdel=0)

class CategoryManager(models.Manager):
    def bigCategory(self):
        return  Category.objects.filter(parentid=0)
    def smallCategory(self):
        return Category.objects.filter(parentid__gt=0)


# Create your models here.
class User(models.Model):
    #自动生成主键（id)
    #字段默认为非空
    uid = models.AutoField(primary_key=True)
    username = models.CharField(max_length=100,unique=True)
    password_hash = models.CharField(max_length=128,db_column='password')
    portrait = models.CharField(max_length=200,null=True)
    gender = models.IntegerField(default=0,null=True)
    usertype = models.IntegerField(choices=[(0,'普通用户'),(1,'管理员')],default=0)
    isactive = models.IntegerField(choices=[(0,'锁定用户'),(1,'活动用户')],default=1)
    regtime = models.DateTimeField(auto_now_add=True,null=True)
    email = models.EmailField(max_length=200,null=True)
    score = models.IntegerField(null=True,default=50)
    autologin = models.IntegerField(choices=[(0,'不自动登录'),(1,'自动登录')],default=0)

    @property
    def password(self):
        return self.password_hash
    @password.setter
    def password(self,value):
        self.password_hash = hashlib.sha1(value.encode('utf8')).hexdigest()


    #说明表自身信息
    class Meta:
        db_table = 'bbs_user'
        managed = False  #不迁移模型
        # ordering = ['-score']
class Category(models.Model):
    cid = models.AutoField(primary_key=True)
    cname = models.CharField(max_length=60)
    parentid = models.IntegerField()
    compere = models.CharField(max_length=30, blank=True, null=True)
    description = models.CharField(max_length=1000, blank=True, null=True)
    orderby = models.IntegerField(blank=True, null=True)
    namestyle = models.CharField(max_length=10, blank=True, null=True)
    logo = models.CharField(max_length=200, blank=True, null=True)
    themenum = models.IntegerField(blank=True, null=True)
    replynum = models.IntegerField(blank=True, null=True)
    lastpost = models.CharField(max_length=600, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'bbs_category'

    objects = models.Manager()
    categoryObjects = CategoryManager()

class Posts(models.Model):
    title = models.CharField(max_length=600)
    content = models.TextField(blank=True, null=True)
    hits = models.IntegerField(blank=True, null=True)
    istop = models.IntegerField(blank=True, null=True)
    iselite = models.IntegerField(blank=True, null=True)
    ishot = models.IntegerField(blank=True, null=True)
    price = models.IntegerField(blank=True, null=True)
    isdel = models.SmallIntegerField(blank=True, null=True)
    cid = models.ForeignKey(Category, models.DO_NOTHING, db_column='cid', blank=True, null=True)
    uid = models.ForeignKey('User', models.DO_NOTHING, db_column='uid', blank=True, null=True)
    publishtime = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'bbs_posts'

    objects = models.Manager()
    postObjects = PostsManager()