
from django.db import models
from django.conf import settings
from django.db.models import F
from PIL import Image
from utils.file_operation import UploadFile

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 Balance(models.Model):
    balance = models.IntegerField(verbose_name=u'余额', default=0)
    create_user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=u"创建人", related_name='balance_create_user',
                                    on_delete=models.PROTECT, editable=False)

    class Meta:
        verbose_name = u"用户余额"
        db_table = "user_balance"
        ordering = ['-id']

    @staticmethod
    def update_balance(user, amount, desc):
        Balance.objects.filter(create_user=user).update(balance=F('balance') + amount)
        BalanceLog.objects.create(description=desc, amount=amount,create_user=user)

class BalanceLog(models.Model):
    description = models.CharField(verbose_name=u'描述', max_length=100)
    amount = models.IntegerField(verbose_name=u'金额', default=0)
    create_user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=u"创建人", related_name='balance_log_create_user',
                                    on_delete=models.PROTECT, editable=False)
    create_time = models.DateTimeField(verbose_name=u'创建时间', auto_now_add=True, editable=False)

    class Meta:
        verbose_name = u"余额明细"
        db_table = "user_balance_log"
        ordering = ['-id']

class Poster(models.Model):

    create_user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=u"创建人", on_delete=models.PROTECT,
                                    editable=False)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    enable = models.BooleanField(verbose_name=u"在用", default=True)
    image = models.TextField(verbose_name=u'图片路径', null=True)
    width = models.IntegerField(verbose_name=u"图片宽度", null=True)
    height = models.IntegerField(verbose_name=u"图片高度", null=True)

    class Meta:
        db_table = 'poster'
        ordering = ['-id']
        verbose_name = u'轮播广告'

    def _add_img(self, file):
        width = None
        height = None

        path = "poster/"
        filename = UploadFile(file, path)
        fullname = "%s%s" % (settings.MEDIA_ROOT, filename)

        try:
            img = Image.open(fullname)
            width, height = img.size
            # 缩略图压缩宽或高最大200
            if width > 1440:
                img = img.resize((1440, int((height / width) * 1440)), Image.ANTIALIAS)
                img.save(fullname)
                width, height = img.size
        except:
            pass

        self.image = "%s%s" % (settings.MEDIA_URL, filename)
        self.width = width
        self.height = height
        self.save()
