# coding=utf-8

import json

from django.conf import settings
from rest_framework import serializers
from django.db.models import Q, Sum

from apps.order.models import Order
from apps.rebate.models import CashRebate, PointRebate, TransferCashRebate, TransferCashRebateDetail
from apps.base import Formater


class OrderRebateSerializer(serializers.ModelSerializer):
    cash_rebate = serializers.SerializerMethodField()
    point_rebate = serializers.SerializerMethodField()
    status_text = serializers.CharField(source='get_status_display', read_only=True)
    customer_name = serializers.CharField(source='customer.name', read_only=True)
    customer_tel = serializers.CharField(source='customer.tel', read_only=True)
    total_amount = serializers.SerializerMethodField()
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M', read_only=True)
    transfer = serializers.SerializerMethodField()

    class Meta:
        model = Order
        fields = '__all__'

    def get_cash_rebate(self, obj):
        cash_bate = CashRebate.objects.filter(order=obj).aggregate(sum_amount=Sum('amount'))['sum_amount'] or 0
        return Formater.formatValueShow(cash_bate)

    def get_point_rebate(self, obj):
        point_rebate = PointRebate.objects.filter(order=obj).aggregate(sum_point=Sum('amount'))['sum_point'] or 0
        return point_rebate

    def get_total_amount(self, obj):
        return Formater.formatValueShow(obj.total_amount)

    def get_transfer(self, obj):
        transfer = TransferCashRebate.objects.filter(order=obj).count()
        return transfer


class CashRebateSerializer(serializers.ModelSerializer):
    amount = serializers.SerializerMethodField()
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M', read_only=True)
    customer_name = serializers.CharField(source='customer.name', read_only=True)

    class Meta:
        model = CashRebate
        fields = '__all__'

    def get_amount(self, obj):
        return Formater.formatValueShow(obj.amount)


class PointRebateSerializer(serializers.ModelSerializer):
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M', read_only=True)
    customer_name = serializers.CharField(source='customer.name', read_only=True)

    class Meta:
        model = PointRebate
        fields = '__all__'


class TransferCashRebateSerializer(serializers.ModelSerializer):
    transfer_amount = serializers.SerializerMethodField()
    success_amount = serializers.SerializerMethodField()
    fail_amount = serializers.SerializerMethodField()
    transfer_status_text = serializers.CharField(source='get_transfer_status_display', read_only=True)
    transfer_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M', read_only=True)
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M', read_only=True)

    class Meta:
        model = TransferCashRebate
        fields = '__all__'

    def get_transfer_amount(self, obj):
        return Formater.formatValueShow(obj.transfer_amount)

    def get_success_amount(self, obj):
        return Formater.formatValueShow(obj.success_amount)

    def get_fail_amount(self, obj):
        return Formater.formatValueShow(obj.fail_amount)


class TransferCashRebateDetailSerializer(serializers.ModelSerializer):
    amount = serializers.SerializerMethodField()
    status_text = serializers.CharField(source='get_status_display', read_only=True)
    initiate_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M', read_only=True)
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M', read_only=True)
    customer_name = serializers.CharField(source='customer.name', read_only=True)
    tel = serializers.CharField(source='customer.tel', read_only=True)
    transfer = serializers.SerializerMethodField()

    class Meta:
        model = TransferCashRebateDetail
        fields = '__all__'

    def get_amount(self, obj):
        return Formater.formatValueShow(obj.amount)

    def get_transfer(self, obj):
        count = TransferCashRebateDetail.objects.filter(Q(rebate=obj.rebate), ~Q(status=TransferCashRebateDetail.FAIL)).count()
        return count
