# -*- coding: utf-8 -*-

from django.conf import settings
from django.db import transaction
from django_logit import logit
from django_response import response
from TimeConvert import TimeConvert as tc

from statistic.models import (ConsumeDistributorSaleStatisticInfo, ConsumeModelSaleStatisticInfo,
                              ConsumeProvinceSaleStatisticInfo, ConsumeSaleStatisticInfo, DistributorSaleStatisticInfo,
                              ModelSaleStatisticInfo, ProvinceSaleStatisticInfo, RegisterStatisticInfo,
                              SaleStatisticInfo)
from utils.rdm_utils import randnum


@logit
def tj_distributor(request):
    ymd = int(tc.local_string(format='%Y%m%d'))

    # 注册用户统计 & 今日注册用户
    try:
        register_num = RegisterStatisticInfo.objects.get(ymd=ymd).num
    except RegisterStatisticInfo.DoesNotExist:
        register_num = 0

    # # 注册用户数趋势
    # register_trends = RegisterStatisticInfo.objects.filter(status=True).order_by('-ymd')
    # register_trends = [r.data for r in register_trends]

    # 销量统计 & 今日销量
    try:
        sale_num = SaleStatisticInfo.objects.get(ymd=ymd).num
    except SaleStatisticInfo.DoesNotExist:
        sale_num = 0

    # # 商品销量趋势
    # sale_trends = SaleStatisticInfo.objects.filter(status=True).order_by('-ymd')
    # sale_trends = [s.data for s in sale_trends]

    # 型号销量统计 & 热销商品榜
    model_sales = ModelSaleStatisticInfo.objects.filter(ymd=0, status=True).order_by('-num')
    model_sales = [m.data for m in model_sales]

    # 经销商销量统计 & 经销商榜
    distributor_sales = DistributorSaleStatisticInfo.objects.filter(ymd=0, status=True).order_by('-num')
    distributor_sales = [d.data for d in distributor_sales]

    # 各地区实时销量
    province_sales = ProvinceSaleStatisticInfo.objects.filter(ymd=0, status=True).order_by('position')
    province_sales = [p.data for p in province_sales]

    # TOADD: ROI

    return response(200, 'Get TJ Data Success', u'获取统计数据成功', {
        'register_num': randnum() if settings.DEBUG_DATA_FLAG else register_num,  # 注册用户统计 & 今日注册用户
        # 'register_trends': register_trends,  # 注册用户数趋势
        'sale_num': randnum() if settings.DEBUG_DATA_FLAG else sale_num,  # 销量统计 & 今日销量
        # 'sale_trends': sale_trends,  # 商品销量趋势
        'model_sales': model_sales,  # 型号销量统计 & 热销商品榜
        'distributor_sales': distributor_sales,  # 经销商销量统计 & 经销商榜
        'province_sales': province_sales,  # 各地区实时销量
    })


@logit
def tj_consumer(request):
    ymd = int(tc.local_string(format='%Y%m%d'))

    # 注册用户统计 & 今日注册用户
    try:
        register_num = RegisterStatisticInfo.objects.get(ymd=ymd).num
    except RegisterStatisticInfo.DoesNotExist:
        register_num = 0

    # 注册用户数趋势
    register_trends = RegisterStatisticInfo.objects.filter(status=True).order_by('-ymd')
    register_trends = [r.data for r in register_trends]

    # 销量统计 & 今日销量
    try:
        sale_num = ConsumeSaleStatisticInfo.objects.get(ymd=ymd).num
    except ConsumeSaleStatisticInfo.DoesNotExist:
        sale_num = 0

    # 商品销量趋势
    sale_trends = ConsumeSaleStatisticInfo.objects.filter(status=True).order_by('-ymd')
    sale_trends = [s.data for s in sale_trends]

    # 型号销量统计 & 热销商品榜
    model_sales = ConsumeModelSaleStatisticInfo.objects.filter(ymd=0, status=True).order_by('-num')
    model_sales = [m.data for m in model_sales]

    # 经销商销量统计 & 经销商榜
    distributor_sales = ConsumeDistributorSaleStatisticInfo.objects.filter(ymd=0, status=True).order_by('-num')
    distributor_sales = [d.data for d in distributor_sales]

    # 各地区实时销量
    province_sales = ConsumeProvinceSaleStatisticInfo.objects.filter(ymd=0, status=True).order_by('position')
    province_sales = [p.data for p in province_sales]

    return response(200, 'Get TJ Data Success', u'获取统计数据成功', {
        'register_num': randnum() if settings.DEBUG_DATA_FLAG else register_num,  # 注册用户统计 & 今日注册用户
        'register_trends': register_trends,  # 注册用户数趋势
        'sale_num': randnum() if settings.DEBUG_DATA_FLAG else sale_num,  # 销量统计 & 今日销量
        'sale_trends': sale_trends,  # 商品销量趋势
        'model_sales': model_sales,  # 型号销量统计 & 热销商品榜
        'distributor_sales': distributor_sales,  # 经销商销量统计 & 经销商榜
        'province_sales': province_sales,  # 各地区实时销量
    })


@logit
@transaction.atomic
def tj_generate(request):
    ymd = int(tc.local_string(format='%Y%m%d'))

    RegisterStatisticInfo.objects.select_for_update().get_or_create(
        ymd=ymd,
    )

    SaleStatisticInfo.objects.select_for_update().get_or_create(
        ymd=ymd,
    )

    return response()
