# 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
    WAIT_DISPATCH = 1
    WAIT_EVALUATE = 2
    EVALUATED = 3
    CANCEL = 4
    STATUS_CHOICES = (
        (WAIT_PAY, u'待付款'),
        (WAIT_DISPATCH, u'待发货'),
        (WAIT_EVALUATE, u'待评价'),
        (EVALUATED, u'已评价'),
        (CANCEL, u'已取消'),
    )

    no = models.CharField(max_length=50, verbose_name=u'订单号', editable=False)
    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.IntegerField(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, blank=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_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, blank=True, on_delete=models.PROTECT)
    name = models.CharField(verbose_name=u'收货人', max_length=20, null=True, blank=True)
    tel = models.CharField(verbose_name=u'收货电话', max_length=15, null=True, blank=True)
    user_address = models.CharField(verbose_name=u'详细地址', max_length=200, null=True, blank=True)

    express_no = models.CharField(max_length=50, verbose_name=u'快递单号', null=True, blank=True)
    express_company = models.ForeignKey(Option, verbose_name=u'快递公司', on_delete=models.PROTECT, null=True, blank=True)
    dispatch_time = models.DateTimeField(verbose_name=u'发货时间', null=True, blank=True)
    dispatch_user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='order_dispatch_user', verbose_name=u"发货人", on_delete=models.PROTECT, null=True, blank=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.BigIntegerField(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.IntegerField(verbose_name=u'总积分', null=True, default=0)

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