# coding=utf-8
import datetime
from django.db import models
from django.conf import settings
from utils.exceptions import CustomError
from apps.upload.models import Upload
from apps.option.models import CommodityLevel
from django.core.cache import cache
from django.db.models.signals import post_save

class Commodity(models.Model):
    name = models.CharField(max_length=20, verbose_name=u"名称")
    introduce = models.CharField(max_length=200, verbose_name=u'简介')
    price = models.IntegerField(verbose_name=u'销售单价', null=True, default=0)
    discount = models.IntegerField(verbose_name=u'折扣', null=True, default=100)
    stock = models.IntegerField(verbose_name=u'库存', null=True, default=0)
    publish_date = models.CharField(max_length=100, verbose_name=u'出版日期', null=True)
    view_counts = models.IntegerField(verbose_name='浏览量', null=True, editable=False, default=0)
    total_sales = models.IntegerField(verbose_name='总销量', null=True, editable=False, default=0)
    status = models.PositiveSmallIntegerField(choices=settings.SALES_STATUS_CHOICES, verbose_name=u"状态",
                                              default=settings.ONLINE)
    category = models.ForeignKey(CommodityLevel, verbose_name='分类', on_delete=models.PROTECT, null=True,
                                   related_name='main_category')
    main_image = models.ForeignKey(Upload, verbose_name='主图', on_delete=models.SET_NULL, null=True,
                                   related_name='main_image')
    list_image = models.ForeignKey(Upload, verbose_name='列表图片', on_delete=models.SET_NULL, null=True,
                                   related_name='list_image')

    notes = models.CharField(max_length=500, verbose_name=u"备注", null=True)
    create_user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=u'添加人', related_name='commodity_create_user',
                                    editable=False, on_delete=models.PROTECT)
    create_time = models.DateTimeField(verbose_name=u'创建时间', auto_now_add=True, editable=False)
    refresh_time = models.DateTimeField(verbose_name=u'刷新时间', auto_now_add=True)

    class Meta:
        db_table = 'commodity'
        verbose_name = u'商品信息'
        ordering = ['-id']
        default_permissions = ()
        permissions = [
            ('view_commodity', u'查看'),
            ('add_commodity', u'添加修改'),
        ]

class CommodityImages(models.Model):
    DETAILS = 1
    PARAMETER = 2
    TYPE_CHOICE = (
        (DETAILS, '详情'),
        (PARAMETER, '参数')
    )
    name = models.CharField(verbose_name=u'名称', max_length=200)
    product = models.ForeignKey(Commodity, verbose_name=u'产品', on_delete=models.PROTECT, related_name='product_images')
    type = models.PositiveSmallIntegerField(choices=TYPE_CHOICE, default=DETAILS)
    image = models.ForeignKey(Upload, verbose_name=u'图片', on_delete=models.PROTECT)

    class Meta:
        db_table = "commodity_images"
        verbose_name = u"产品图片"
        ordering = ('name',)
        default_permissions = ()

    @staticmethod
    def getById(id):
        try:
            id = int(id)
        except:
            raise CustomError(u'无效的ID')

        instance = CommodityImages.objects.filter(pk=id).first()
        if not instance:
            raise CustomError(u'未找到相应的产品图片')
        return instance
