
from django.db import models
from django.conf import settings
from django.db.models import F

class Config(models.Model):
    LEAVE_LEAD_TIME = "leave_lead_time"  # 付款码支付__付款码图片

    property = models.CharField(max_length=100, verbose_name='属性')
    value = models.TextField(blank=True, max_length=250, null=True, verbose_name='值')

    class Meta:
        db_table = "system_config"
        verbose_name = u"综合设置"
        default_permissions = ()
        permissions = [
            ('manage_system_config', u'管理'),
        ]

    @staticmethod
    def get_value(property):
        if property in [Config.LEAVE_LEAD_TIME,]:
            row = Config.objects.get(property=property)
            return row.value
        return ''

class Area(models.Model):
    PROVINCE = 0
    CITY = 1
    COUNTY = 2
    LEVEL_CHOICES = (
        (PROVINCE, u'省级'),
        (CITY, u'市级'),
        (COUNTY, u'县区级'),
    )

    name = models.CharField(verbose_name=u'名称', max_length=50, blank=True, null=True)
    level = models.IntegerField(verbose_name=u'层级', choices=LEVEL_CHOICES)
    province_name = models.CharField(u'所属省', max_length=50, blank=True, null=True)
    city_name = models.CharField(u'所属地市', max_length=50, blank=True, null=True)
    province = models.ForeignKey('Area', verbose_name='所属省', related_name='province_children', on_delete=models.PROTECT,
                                 blank=True, null=True)
    city = models.ForeignKey('Area', verbose_name='所属地市', related_name='city_children', on_delete=models.PROTECT,
                             blank=True, null=True)

    class Meta:
        verbose_name = u"行政区域"
        db_table = "area"

class School(models.Model):
    area = models.ForeignKey(Area, verbose_name=u'行政区域', related_name='school_area', on_delete=models.PROTECT)
    name = models.CharField(verbose_name=u'名称', max_length=100, blank=True, null=True)
    notes = models.CharField(verbose_name=u'备注', max_length=200, null=True)
    enable = models.BooleanField(verbose_name=u"是否在用", default=True)

    def __unicode__(self):
        return '%s' % (self.name)

    class Meta:
        verbose_name = u"学校"
        db_table = "school"
        ordering = ['-id']
        default_permissions = ()
        permissions = [
            ('browse_school', u'查看'),
            ('add_school', u'添加学校、班级'),
        ]

class Grade(models.Model):
    school = models.ForeignKey(School, verbose_name=u'学校', related_name='grade_school', on_delete=models.PROTECT)
    name = models.CharField(verbose_name=u'名称', max_length=100, blank=True, null=True)
    count = models.PositiveIntegerField(verbose_name="班级数量", default=1)

    class Meta:
        verbose_name = u"年级"
        db_table = "grade"
        ordering = ['-id']

class Classes(models.Model):
    grade = models.ForeignKey(Grade, verbose_name=u'年级', related_name='classes_grade', on_delete=models.PROTECT)
    name = models.CharField(verbose_name=u'名称', max_length=100, blank=True, null=True)
    enable = models.BooleanField(verbose_name=u"是否在用", default=True)

    def __unicode__(self):
        return '%s' % (self.name)

    class Meta:
        verbose_name = u"班级"
        db_table = "classes"
        ordering = ['-id']

class Student(models.Model):
    school = models.ForeignKey(School, verbose_name=u'学校',related_name='student_school', on_delete=models.PROTECT)
    classes = models.ForeignKey(Classes, verbose_name=u'班级', on_delete=models.PROTECT)
    name = models.CharField(verbose_name=u'姓名', max_length=20)
    gender = models.PositiveSmallIntegerField(choices=settings.GENDER_CHOICES, verbose_name=u"性别", default=settings.MALE)
    create_user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=u"创建人", related_name='student_create_user',
                                    on_delete=models.PROTECT, editable=False)
    create_time = models.DateTimeField(verbose_name=u'创建时间', auto_now_add=True, editable=False)
    enable = models.BooleanField(verbose_name=u"是否在用", default=True, editable=False)

    class Meta:
        verbose_name = u"学生"
        db_table = "student"
        ordering = ['-id']
        default_permissions = ()
        permissions = [
            ('browse_student', u'查看'),
        ]

class CommodityLevel(models.Model):
    ONE = 1
    TWO = 2
    THREE = 3
    LEVEL_CHOICES = (
        (ONE, u'大类'),
        (TWO, u'品种'),
        (THREE, u'科目'),
    )

    name = models.CharField(verbose_name=u'名称', max_length=50, blank=True, null=True)
    enable = models.BooleanField(verbose_name=u"是否在用", default=True)
    home_show = models.BooleanField(verbose_name=u"是否首页展示", default=False)
    level = models.IntegerField(verbose_name=u'层级', choices=LEVEL_CHOICES)
    category = models.ForeignKey('CommodityLevel', verbose_name='父类', related_name='commodiey_level_children', on_delete=models.PROTECT,
                             blank=True, null=True)

    class Meta:
        verbose_name = u"商品分类"
        db_table = "commodiey_level"
        ordering = ['-id']
        default_permissions = ()
        permissions = [
            ('browse_commodiey_level', u'管理'),
        ]

class CommodityCategory(models.Model):
    commodiey_level = models.ForeignKey(CommodityLevel, verbose_name=u'商品级别', related_name='commodiey_level', on_delete=models.PROTECT)
    name = models.CharField(verbose_name=u'名称', max_length=100, blank=True, null=True)
    enable = models.BooleanField(verbose_name=u"是否在用", default=True)

    class Meta:
        verbose_name = u"商品分类"
        db_table = "commodiey_category"
        ordering = ['-id']

class CommoditySubject(models.Model):
    commodiey_category = models.ForeignKey(CommodityCategory, verbose_name=u'商品分类', related_name='commodiey_category', on_delete=models.PROTECT)
    name = models.CharField(verbose_name=u'名称', max_length=100, blank=True, null=True)
    enable = models.BooleanField(verbose_name=u"是否在用", default=True)

    class Meta:
        verbose_name = u"商品科目"
        db_table = "commodiey_subject"
        ordering = ['-id']
