# coding=utf-8

from django.db import models
from django.utils import timezone
from apps.product.models import Product
from apps.customer.models import Customer, CustomerAddress


class ProductOrder(models.Model):
    WAIT_PAY = 1
    PAID = 2
    FINISHED = 3
    REVOKE = 4
    STATUS_CHOICES = (
        (WAIT_PAY, u'待支付'),
        (PAID, u'已支付'),
        (FINISHED, u'已完成'),
        (REVOKE, u'已取消'),
    )
    product = models.ForeignKey(Product, verbose_name=u'商品', on_delete=models.PROTECT)
    create_time = models.DateTimeField(verbose_name=u"添加时间", auto_now_add=True, editable=False)
    no = models.CharField(max_length=64, verbose_name=u"单号", null=True, editable=False, blank=True)
    status = models.PositiveSmallIntegerField(choices=STATUS_CHOICES, verbose_name=u'状态', default=WAIT_PAY)
    customer = models.ForeignKey(Customer, verbose_name=u'客户', on_delete=models.PROTECT, editable=False)
    customer_address = models.ForeignKey(CustomerAddress, verbose_name=u'收货信息', on_delete=models.PROTECT)
    count = models.IntegerField(verbose_name=u'购买数量')
    price = models.BigIntegerField(verbose_name=u'单价', editable=False)
    amount = models.BigIntegerField(verbose_name=u'合计金额', editable=False)
    notes = models.CharField(max_length=1000, verbose_name=u'备注', null=True)
    delete = models.BooleanField(verbose_name=u'删除', default=False, editable=False)

    def generateNo(self):
        now = timezone.now()
        no = '%s-%s-%04d' % ('TC', now.strftime('%Y%m%d'), self.id)
        return no

    class Meta:
        db_table = 'product_order'
        verbose_name = u'商品订单'
        ordering = ('-id',)
        default_permissions = ()