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

from django.conf import settings
from django.db import transaction
from django.db.models import Sum
from django_logit import logit
from django_models_ext.provincemodels import ProvinceShortModelMixin
from django_query import get_query_value
from django_response import response
from TimeConvert import TimeConvert as tc

from mch.models import BrandInfo, DistributorInfo, ModelInfo, SaleclerkInfo
from statistic.models import (ConsumeDistributorSaleStatisticInfo, ConsumeModelSaleStatisticInfo,
                              ConsumeProvinceSaleStatisticInfo, ConsumeSaleStatisticInfo, ConsumeUserStatisticInfo,
                              DistributorSaleStatisticInfo, ModelSaleStatisticInfo, ProvinceSaleStatisticInfo,
                              RegisterStatisticInfo, SaleclerkSaleStatisticInfo, SaleStatisticInfo)
from utils.rdm_utils import randnum


@logit
def tj_distributor(request):
    brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)

    ymd = int(tc.local_string(format='%Y%m%d'))

    # 注册用户统计 & 今日注册用户
    try:
        register_num = RegisterStatisticInfo.objects.get(brand_id=brand_id, 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(brand_id=brand_id, 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(brand_id=brand_id, ymd=ymd, status=True).order_by('-num')
    model_sales = [m.data for m in model_sales]

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

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

    # TOADD: ROI
    rois = ModelSaleStatisticInfo.objects.filter(brand_id=brand_id, ymd=ymd, status=True)
    rois = [m.roi for m in rois]

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


@logit
def tj_consumer(request):
    brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)

    ymd = int(tc.local_string(format='%Y%m%d'))

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

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

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

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

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

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

    # 各地区实时销量
    province_sales = ConsumeProvinceSaleStatisticInfo.objects.filter(brand_id=brand_id, ymd=ymd, 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_STATISTIC_DATA_FLAG else register_num,  # 注册用户统计 & 今日注册用户
        'register_trends': register_trends,  # 注册用户数趋势
        'sale_num': randnum() if settings.DEBUG_STATISTIC_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):
    # 1 0 * * * curl http://kodo.xfoto.com.cn/api/tj/generate

    __tj_generate(ymd=None)

    return response()


@transaction.atomic
def __tj_generate(ymd=None):
    ymd = ymd or int(tc.local_string(format='%Y%m%d'))

    brands = BrandInfo.objects.filter(status=True)

    for brand in brands:
        for pcode, pname in ProvinceShortModelMixin.PROVINCE_CODE_NAME_DICT.items():
            pssi, created = ProvinceSaleStatisticInfo.objects.get_or_create(
                brand_id=brand.brand_id,
                province_code=pcode,
                ymd=ymd,
            )
            pssi.province_name = pname
            pssi.save()

            cpssi, created = ConsumeProvinceSaleStatisticInfo.objects.get_or_create(
                brand_id=brand.brand_id,
                province_code=pcode,
                ymd=ymd,
            )
            cpssi.province_name = pname
            cpssi.save()

        models = ModelInfo.objects.filter(brand_id=brand.brand_id, status=True)
        for mdl in models:
            mssi, created = ModelSaleStatisticInfo.objects.get_or_create(
                brand_id=brand.brand_id,
                model_id=mdl.model_id,
                ymd=ymd,
            )
            mssi.model_name = mdl.model_name
            mssi.save()

            cmssi, created = ConsumeModelSaleStatisticInfo.objects.get_or_create(
                brand_id=brand.brand_id,
                model_name=mdl.model_uni_name,
                ymd=ymd,
            )
            cmssi.save()

        distributors = DistributorInfo.objects.filter(brand_id=brand.brand_id, status=True)
        for dtbt in distributors:
            dssi, created = DistributorSaleStatisticInfo.objects.get_or_create(
                brand_id=brand.brand_id,
                distributor_id=dtbt.distributor_id,
                ymd=ymd,
            )
            dssi.distributor_name = dtbt.distributor_name
            dssi.save()

            cdssi, created = ConsumeDistributorSaleStatisticInfo.objects.get_or_create(
                brand_id=brand.brand_id,
                distributor_id=dtbt.distributor_id,
                ymd=ymd,
            )
            cdssi.distributor_name = dtbt.distributor_name
            cdssi.save()

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

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

        ConsumeSaleStatisticInfo.objects.select_for_update().get_or_create(
            brand_id=brand.brand_id,
            ymd=ymd,
        )


def ytj(brand_id):
    # [消费者维度] 周期内扫描用户人数
    cusis = ConsumeUserStatisticInfo.objects.filter(brand_id=brand_id, ymd__lt=9999)
    users = []
    for cusi in cusis:
        users += cusi.users
    scan_user_count = len(set(users))

    # [消费者维度] 周期内镜头销售支数
    sell_volume_count = ConsumeSaleStatisticInfo.objects.filter(brand_id=brand_id, ymd__lt=9999).aggregate(Sum('num')).get('num__sum', 0) or 0

    startd = tc.local_string(months=-12, format='%Y%m%d')
    endd = tc.local_string(format='%Y%m%d')

    # [消费者维度] 统计周期内型号扫描排行数据，请按顺序返回
    models = ConsumeModelSaleStatisticInfo.objects.filter(
        brand_id=brand_id,
        ymd__gt=startd,
        ymd__lte=endd,
        status=True
    ).order_by(
        'model_name'
    ).values(
        'model_id', 'model_name'
    ).annotate(
        num=Sum('num')
    ).order_by(
        '-num'
    )[:20]

    # [经销商维度] 统计周期内销售员排行数据，请按顺序返回
    salesmen = SaleclerkSaleStatisticInfo.objects.filter(
        brand_id=brand_id,
        ymd__gt=startd,
        ymd__lte=endd,
        status=True
    ).order_by(
        'clerk_id'
    ).values(
        'clerk_id'
    ).annotate(
        num=Sum('num')
    ).order_by(
        '-num'
    )[:20]
    clerks = SaleclerkInfo.objects.filter(brand_id=brand_id, status=True)
    clerks = {clerk.clerk_id: {'distributor_id': clerk.distributor_id, 'distributor_name': clerk.distributor_name, 'clerk_name': clerk.clerk_name, 'salesman_id': clerk.clerk_id, 'salesman_name': clerk.clerk_name} for clerk in clerks}
    salesmen = [dict(sm, **clerks.get(sm.get('clerk_id', ''), {})) for sm in salesmen]

    # [收费者维度] 统计周期内省份销量排行数据，请按顺序返回
    provinces = ConsumeProvinceSaleStatisticInfo.objects.filter(
        brand_id=brand_id,
        ymd__gt=startd,
        ymd__lte=endd,
        status=True
    )
    # ).order_by(
    #     'province_code'
    # ).values(
    #     'province_code'
    # ).annotate(
    #     num=Sum('num')
    # ).order_by(
    #     '-num'
    # )
    provinces_users = {}
    for province in provinces:
        if provinces_users.get(province.province_code):
            provinces_users[province.province_code] += province.users
        else:
            provinces_users[province.province_code] = province.users
    provinces = [{'province_code': province_code, 'num': len(set(users))} for province_code, users in provinces_users.iteritems()]
    provinces = sorted(provinces, key=lambda p: p['num'], reverse=True)

    return {
        'scan_user_count': scan_user_count,
        'sell_volume_count': sell_volume_count,
        'user_count_increase_pct': -1,  # 与上个统计周期数据的用户人数比例
        'volume_count_increase_pct': -1,  # 与上个统计周期数据的销售支数比例
        'models': list(models),
        'salesmen': salesmen,
        'provinces': provinces,
    }


def ymdtj(brand_id, ymd, lastymd):
    # [消费者维度] 周期内扫描用户人数
    try:
        scan_user_count = ConsumeUserStatisticInfo.objects.get(brand_id=brand_id, ymd=ymd).num
    except ConsumeUserStatisticInfo.DoesNotExist:
        scan_user_count = 0

    try:
        last_scan_user_count = ConsumeUserStatisticInfo.objects.get(brand_id=brand_id, ymd=lastymd).num
    except ConsumeUserStatisticInfo.DoesNotExist:
        last_scan_user_count = 0

    # [消费者维度] 周期内镜头销售支数
    try:
        sell_volume_count = ConsumeSaleStatisticInfo.objects.get(brand_id=brand_id, ymd=ymd).num
    except ConsumeSaleStatisticInfo.DoesNotExist:
        sell_volume_count = 0

    try:
        last_sell_volume_count = ConsumeSaleStatisticInfo.objects.get(brand_id=brand_id, ymd=lastymd).num
    except ConsumeSaleStatisticInfo.DoesNotExist:
        last_sell_volume_count = 0

    # 与上个统计周期数据的销售支数比例
    volume_count_increase_pct = '%.2f' % (sell_volume_count * 100.0 / last_sell_volume_count) if last_sell_volume_count != 0 else -1

    # 与上个统计周期数据的用户人数比例
    user_count_increase_pct = '%.2f' % (scan_user_count * 100.0 / last_scan_user_count) if last_scan_user_count != 0 else -1

    # [消费者维度] 统计周期内型号扫描排行数据，请按顺序返回
    current_models = ConsumeModelSaleStatisticInfo.objects.filter(brand_id=brand_id, ymd=ymd, status=True).order_by('-num')
    models = [m.data for m in current_models[:20]]

    # [经销商维度] 统计周期内销售员排行数据，请按顺序返回
    salesmen = SaleclerkSaleStatisticInfo.objects.filter(brand_id=brand_id, ymd=ymd, status=True).order_by('-num')
    salesmen = [s.data for s in salesmen[:20]]

    # [收费者维度] 统计周期内省份销量排行数据，请按顺序返回
    provinces = ConsumeProvinceSaleStatisticInfo.objects.filter(brand_id=brand_id, ymd=ymd, status=True).order_by('-num')
    provinces = [p.data for p in provinces]

    return {
        'scan_user_count': scan_user_count,
        'sell_volume_count': sell_volume_count,
        'user_count_increase_pct': user_count_increase_pct,
        'volume_count_increase_pct': volume_count_increase_pct,
        'models': models,
        'salesmen': salesmen,
        'provinces': provinces,
    }


@logit
def v2_tj_distributor(request):
    brand_id = get_query_value(request, 'brand_id', settings.KODO_DEFAULT_BRAND_ID)

    # year = tc.local_string(format='%Y')
    month = tc.local_string(format='%Y%m')
    day = tc.local_string(format='%Y%m%d')

    # lastyear = tc.local_string(tc.several_time_ago(years=1), format='%Y')
    lastmonth = tc.local_string(tc.several_time_ago(months=1), format='%Y%m')
    lastday = tc.local_string(tc.several_time_ago(days=1), format='%Y%m%d')

    year_data = ytj(brand_id)
    month_data = ymdtj(brand_id, month, lastmonth)
    day_data = ymdtj(brand_id, day, lastday)

    return response(200, 'Get TJ Data Success', u'获取统计数据成功', data={
        'year_data': year_data,
        'month_data': month_data,
        'day_data': day_data,
    })
