# coding=utf-8

from django.utils import timezone

from apps.rebate.models import CashRebate, PointRebate, CashRebateLog, PointRebateLog, PointGive, PointGiveLog
from apps.customer.models import SuperiorDistributor
from apps.order.models import Order
from apps.config.models import Config


class CustomerRebate(object):

    def __init__(self, pay):
        self.pay = pay
        self.order = Order.objects.filter(pay=self.pay).first()

    def cash_rebate(self, ratio, amount, customer):
        '''
        计算现金返利  1、2、3级分销商 返现金
        :param ratio:
        :param amount:
        :param customer:
        :return:
        '''
        rebate = CashRebate.objects.create(
            order=self.order,
            ratio=ratio,
            amount=amount,
            customer=customer,
            create_time=timezone.now()
        )
        CashRebateLog.addnew(rebate)

    def point_rebate(self, ratio, amount, customer):
        '''
        计算积分返利  4、5级分销商 返积分
        :param ratio:
        :param amount:
        :param customer:
        :return:
        '''
        rebate = PointRebate.objects.create(
            order=self.order,
            ratio=ratio,
            amount=amount,
            customer=customer,
            create_time=timezone.now()
        )
        PointRebateLog.addnew(rebate)

    def point_give(self, ratio, amount, customer):
        '''
        计算购买商品赠送的积分  购买商品时根据后台设置的比例赠送相应的积分给购买者
        :param ratio:
        :param amount:
        :param customer:
        :return:
        '''
        give = PointGive.objects.create(
            order=self.order,
            ratio=ratio,
            amount=amount,
            customer=customer,
            create_time=timezone.now()
        )
        PointGiveLog.addnew(give)

    def calcul_rebate(self):
        '''
        计算返利并保存log
        :return:
        '''
        if not self.order:
            return
        if self.order.total_amount <= 0:
            return
        if self.order.status == Order.WAIT_PAY or self.order.status == Order.CANCEL:
            return

        customer = self.order.customer
        sd = SuperiorDistributor.objects.filter(customer=customer).first()
        if not sd:
            return
        # 判断是首次购买还是再次购买  首次购买与再次购买返利的比例不同
        count = Order.objects.filter(customer=customer, status__in=[Order.WAIT_DISPATCH, Order.CONFIRM_DISPATCH]).count()
        rebate_dic = {}
        if count > 1:
            rebate_dic = Config.get_rebate(False)
        elif count == 1:
            rebate_dic = Config.get_rebate(True)
        # 现金保留两位小数， 积分整数
        total_amount = float(self.order.total_amount or 0)

        if sd.one_level and sd.one_level.is_distributor and rebate_dic['one_level'] > 0:
            amount = int(round(total_amount * rebate_dic['one_level'] / 100.0, 0))
            self.cash_rebate(rebate_dic['one_level'], amount, sd.one_level)

        if sd.two_level and sd.two_level.is_distributor and rebate_dic['two_level'] > 0:
            amount = int(round(total_amount * rebate_dic['two_level']/ 100.0, 0))
            self.cash_rebate(rebate_dic['two_level'], amount, sd.two_level)

        if sd.three_level and sd.three_level.is_distributor and rebate_dic['three_level'] > 0:
            amount = int(round(total_amount * rebate_dic['three_level'] / 100.0, 0))
            self.cash_rebate(rebate_dic['three_level'], amount, sd.three_level)

        if sd.four_level and sd.four_level.is_distributor and rebate_dic['four_level'] > 0:
            amount = int(round((total_amount / 100.0) * (rebate_dic['four_level'] / 100.0), 0))
            self.point_rebate(rebate_dic['four_level'], amount, sd.four_level)

        if sd.five_level and sd.five_level.is_distributor and rebate_dic['five_level'] > 0:
            amount = int(round((total_amount / 100.0) * (rebate_dic['five_level'] / 100.0), 0))
            self.point_rebate(rebate_dic['five_level'], amount, sd.five_level)

        # 计算购买商品 赠送积分
        rule = Config.get_value(Config.KEY_POINT_RULE)
        if rule > 0:
            amount = int(round((total_amount / 100.0) * (rule / 100.0), 0))
            self.point_give(rule, amount, customer)
