# coding=utf-8

from django.db import models
from django.db.models import Q
from django.conf import settings
from django.utils import timezone

from apps.customer.models import Customer
from apps.order.models import Order

'''
现金返利及现金变动记录
'''


class CashLog(models.Model):
    CASH_REBATE = 1
    TYPE_CHOICES = (
        (CASH_REBATE, u'现金返利'),
    )

    customer = models.ForeignKey(Customer, verbose_name=u'客户', on_delete=models.PROTECT)
    type = models.PositiveSmallIntegerField(choices=TYPE_CHOICES, verbose_name=u"类型")
    happen_time = models.DateTimeField(verbose_name=u"发生时间", default=timezone.now)
    amount = models.BigIntegerField(verbose_name=u"变动额")
    balance = models.BigIntegerField(verbose_name=u"余额")

    class Meta:
        db_table = "cash_log"
        verbose_name = u'现金日志'
        ordering = []
        index_together = ()
        default_permissions = ()
        permissions = []


class CashRebate(models.Model):

    order = models.ForeignKey(Order, verbose_name=u'订单', related_name='cash_rebate_order', editable=False, on_delete=models.PROTECT)
    ratio = models.FloatField(verbose_name=u'返利比例', default=0)
    amount = models.BigIntegerField(verbose_name=u'返利金额', default=0)
    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)

    class Meta:
        db_table = "cash_rebate"
        verbose_name = u'现金返利'
        ordering = []
        index_together = ()
        default_permissions = ()
        permissions = []


class CashRebateLog(models.Model):
    rebate = models.OneToOneField(CashRebate, verbose_name=u'现金返利', related_name='rebate', editable=False, on_delete=models.PROTECT)
    log = models.OneToOneField(CashLog, verbose_name=u'日志', related_name='rebate_log', editable=False, on_delete=models.PROTECT)

    class Meta:
        db_table = "cahs_rebate_log"
        verbose_name = u'现金返利日志'
        ordering = []
        index_together = ()
        default_permissions = ()
        permissions = []

'''
积分返利及积分变动记录
'''


class PointLog(models.Model):

    POINT_REBATE = 1
    POINT_ORDER = 2
    TYPE_CHOICES = (
        (POINT_REBATE, u'积分返利'),
        (POINT_ORDER, u'积分兑换'),
    )

    customer = models.ForeignKey(Customer, verbose_name=u'客户', on_delete=models.PROTECT)
    type = models.PositiveSmallIntegerField(choices=TYPE_CHOICES, verbose_name=u"类型")
    happen_time = models.DateTimeField(verbose_name=u"发生时间", default=timezone.now)
    amount = models.IntegerField(verbose_name=u"变动额")
    balance = models.IntegerField(verbose_name=u"余额")

    class Meta:
        db_table = "point_log"
        verbose_name = u'积分日志'
        ordering = []
        index_together = ()
        default_permissions = ()
        permissions = []


class PointRebate(models.Model):

    order = models.ForeignKey(Order, verbose_name=u'订单', related_name='point_rebate_order', editable=False, on_delete=models.PROTECT)
    ratio = models.FloatField(verbose_name=u'返利比例', default=0)
    amount = models.IntegerField(verbose_name=u'返利积分', default=0)
    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)

    class Meta:
        db_table = "point_rebate"
        verbose_name = u'积分返利'
        ordering = []
        index_together = ()
        default_permissions = ()
        permissions = []


class PointLogOrder(models.Model):

    order = models.OneToOneField(Order, verbose_name=u'订单', related_name='order', editable=False, on_delete=models.PROTECT)
    log = models.OneToOneField(PointLog, verbose_name=u'日志', related_name='order_log', editable=False, on_delete=models.PROTECT)

    class Meta:
        db_table = "point_log_order"
        verbose_name = u'积分兑换日志'
        ordering = []
        index_together = ()
        default_permissions = ()
        permissions = []


class PointLogRebate(models.Model):
    rebate = models.OneToOneField(PointRebate, verbose_name=u'积分返利', related_name='rebate', editable=False, on_delete=models.PROTECT)
    log = models.OneToOneField(PointLog, verbose_name=u'日志', related_name='rebate_log', editable=False, on_delete=models.PROTECT)

    class Meta:
        db_table = "point_log_rebate"
        verbose_name = u'积分返利日志'
        ordering = []
        index_together = ()
        default_permissions = ()
        permissions = []



