# coding=utf-8

from django.db import models
from django.conf import settings
from django.utils import timezone
from apps.option.models import Option
from apps.commodity.models import Commodity
from apps.customer.models import Customer, CustomerAddress


class Pay(models.Model):
    WAIT = 1
    PAY = 2
    UNDO = 3
    STATUS_CHOICES = (
        (WAIT, u'待付款'),
        (PAY, u'已付款'),
        (UNDO, u'已取消'),
    )

    no = models.CharField(max_length=64, verbose_name=u"单号")
    status = models.PositiveSmallIntegerField(choices=STATUS_CHOICES, verbose_name=u'状态', default=WAIT)
    precreate_amount = models.BigIntegerField(verbose_name=u"预支付金额")
    amount = models.BigIntegerField(verbose_name=u"支付金额", null=True)
    transaction_id = models.CharField(max_length=100, verbose_name=u"微信支付订单号", null=True)
    customer = models.ForeignKey(Customer, verbose_name=u'下单人', related_name='pay_customer', on_delete=models.PROTECT)
    create_time = models.DateTimeField(verbose_name=u"创建时间", default=timezone.now)

    class Meta:
        db_table = "pay"
        verbose_name = u"支付信息"
        ordering = ('-id',)
        index_together = (
            'create_time',
            'status',
        )
        unique_together = (
            'no',
        )
        default_permissions = ()


class ShoppingCart(models.Model):
    commodity_details = models.ForeignKey(Commodity, verbose_name=u'产品明细', on_delete=models.PROTECT)
    quantity = models.PositiveIntegerField(verbose_name=u'数量', default=1)
    create_time = models.DateTimeField(verbose_name=u'创建时间', auto_now_add=True)
    customer = models.ForeignKey(Customer, related_name='shopping_cart_customer', verbose_name=u"下单人", on_delete=models.PROTECT, editable=False)

    class Meta:
        db_table = 'shopping_cart'
        verbose_name = u'购物车'
        ordering = ['-id', ]
        default_permissions = ()


class Order(models.Model):
    WAIT_PAY = 0
    CONFIRM_PAY = 1
    WAIT_DISPATCH = 2
    CONFIRM_DISPATCH = 3
    FINISH = 4
    CANCEL = 5
    STATUS_CHOICES = (
        (WAIT_PAY, u'待付款'),
        (CONFIRM_PAY, u'已付款'),
        (WAIT_DISPATCH, u'待发货'),
        (CONFIRM_DISPATCH, u'已发货'),
        (FINISH, u'已完成'),
        (CANCEL, u'已取消'),
    )

    no = models.CharField(max_length=50, verbose_name=u'订单号', blank=True)
    pay = models.ForeignKey(Pay, verbose_name=u'支付信息', on_delete=models.PROTECT, null=True, editable=False)

    total_count = models.IntegerField(verbose_name=u'总数量', default=0, editable=False)
    total_amount = models.BigIntegerField(verbose_name=u'总金额', default=0, editable=False)
    total_point = models.BigIntegerField(verbose_name=u'总积分', default=0, editable=False)
    status = models.PositiveSmallIntegerField(choices=STATUS_CHOICES, verbose_name=u"订单状态", default=WAIT_PAY)
    notes = models.CharField(max_length=500, verbose_name=u"备注", null=True)

    customer = models.ForeignKey(Customer, verbose_name=u'创建人', editable=False, on_delete=models.PROTECT)
    create_time = models.DateTimeField(verbose_name=u"创建时间", default=timezone.now, editable=False)
    cancel = models.BooleanField(verbose_name=u"取消", default=False)
    cancel_reason = models.CharField(max_length=100, verbose_name=u"取消原因", null=True, blank=True)
    cancel_user =models.ForeignKey(settings.AUTH_USER_MODEL, related_name='order_cancel_user', verbose_name=u"取消人", on_delete=models.PROTECT, null=True, blank=True)
    cancel_time = models.DateTimeField(verbose_name=u"取消时间", null=True, blank=True)

    address = models.ForeignKey(CustomerAddress, verbose_name=u'收货地址', related_name='order_address', null=True, on_delete=models.PROTECT)
    name = models.CharField(verbose_name=u'姓名', max_length=20, null=True)
    tel = models.CharField(verbose_name=u'手机', max_length=15, null=True)
    user_address = models.CharField(verbose_name=u'详细地址', max_length=200, null=True)

    express_no = models.CharField(max_length=50, verbose_name=u'快递单号', blank=True)
    express_company = models.ForeignKey(Option, verbose_name=u'快递公司', on_delete=models.PROTECT)
    dispatch_time = models.DateField(verbose_name=u'发货时间', null=True)

    class Meta:
        db_table = "order"
        verbose_name = u"客户订单"
        ordering = ('-id',)
        default_permissions = ()
        permissions = []


class OrderDetails(models.Model):

    order = models.ForeignKey(Order, verbose_name=u'订单', on_delete=models.PROTECT)
    commodity = models.ForeignKey(Commodity, verbose_name=u'商品', on_delete=models.PROTECT)
    price = models.IntegerField(verbose_name=u'价格', null=True, default=0)
    point = models.IntegerField(verbose_name=u'积分', null=True, default=0)
    count = models.IntegerField(verbose_name=u'数量', null=True, default=0)
    amount = models.BigIntegerField(verbose_name=u'总金额', null=True, default=0)
    point_amount = models.BigIntegerField(verbose_name=u'总积分', null=True, default=0)

    class Meta:
        db_table = u'order_details'
        verbose_name = u'订单明细'
        ordering = ('-id', )
        default_permissions = ()
        permissions = []
