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

from django.conf import settings
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django_models_ext import BaseModelMixin
from jsonfield import JSONField

from utils.rdm_utils import randnum


class RegisterStatisticInfo(BaseModelMixin):
    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    ymd = models.IntegerField(_(u'ymd'), default=0, help_text=u'年月日', db_index=True)  # 例：20171208, tc.local_string(format='%Y%m%d')
    num = models.IntegerField(_(u'num'), default=0, help_text=u'数量')

    class Meta:
        verbose_name = _(u'注册用户统计')
        verbose_name_plural = _(u'注册用户统计')

    def __unicode__(self):
        return unicode(self.pk)

    @property
    def data(self):
        return {
            'ymd': self.ymd,
            'num': randnum() if settings.DEBUG_STATISTIC_DATA_FLAG else self.num,
        }


# 经销商维度


class SaleStatisticInfo(BaseModelMixin):
    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    ymd = models.IntegerField(_(u'ymd'), default=0, help_text=u'年月日', db_index=True)  # 例：20171208, tc.local_string(format='%Y%m%d')
    num = models.IntegerField(_(u'num'), default=0, help_text=u'数量')

    class Meta:
        verbose_name = _(u'[经销商维度]销量统计')
        verbose_name_plural = _(u'[经销商维度]销量统计')

    def __unicode__(self):
        return unicode(self.pk)

    @property
    def data(self):
        return {
            'ymd': self.ymd,
            'num': randnum() if settings.DEBUG_STATISTIC_DATA_FLAG else self.num,
        }


class ModelSaleStatisticInfo(BaseModelMixin):
    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    model_id = models.CharField(_(u'model_id'), max_length=32, help_text=u'型号唯一标识', db_index=True)
    model_name = models.CharField(_(u'model_name'), max_length=255, blank=True, null=True, help_text=u'型号名称')
    ymd = models.IntegerField(_(u'ymd'), default=0, help_text=u'年月日', db_index=True)  # 例：20171208, tc.local_string(format='%Y%m%d'), 0 为全部
    num = models.IntegerField(_(u'num'), default=0, help_text=u'数量')

    class Meta:
        verbose_name = _(u'[经销商维度]型号销量统计')
        verbose_name_plural = _(u'[经销商维度]型号销量统计')

    def __unicode__(self):
        return unicode(self.pk)

    @property
    def data(self):
        return {
            'model_id': self.model_id,
            'model_name': self.model_name,
            'ymd': self.ymd,
            'num': randnum() if settings.DEBUG_STATISTIC_DATA_FLAG else self.num,
        }

    # TODO: ROI Calc
    @property
    def roi(self):
        return {
            'model_id': self.model_id,
            'model_name': self.model_name,
            'roi': randnum() if settings.DEBUG_STATISTIC_DATA_FLAG else self.num,
        }


class DistributorSaleStatisticInfo(BaseModelMixin):
    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    distributor_id = models.CharField(_(u'distributor_id'), max_length=32, help_text=u'经销商唯一标识', db_index=True)
    distributor_name = models.CharField(_(u'distributor_name'), max_length=255, blank=True, null=True, help_text=u'经销商名称')
    ymd = models.IntegerField(_(u'ymd'), default=0, help_text=u'年月日', db_index=True)  # 例：20171208, tc.local_string(format='%Y%m%d'), 0 为全部
    num = models.IntegerField(_(u'num'), default=0, help_text=u'数量')

    class Meta:
        verbose_name = _(u'[经销商维度]经销商销量统计')
        verbose_name_plural = _(u'[经销商维度]经销商销量统计')

    def __unicode__(self):
        return unicode(self.pk)

    @property
    def data(self):
        return {
            'distributor_id': self.distributor_id,
            'distributor_name': self.distributor_name,
            'ymd': self.ymd,
            'num': randnum() if settings.DEBUG_STATISTIC_DATA_FLAG else self.num,
        }


class ProvinceSaleStatisticInfo(BaseModelMixin):
    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    province_code = models.CharField(_(u'province_code'), max_length=6, help_text=u'省份编码', db_index=True)
    province_name = models.CharField(_(u'province_name'), max_length=8, blank=True, null=True, help_text=u'省份名称')
    ymd = models.IntegerField(_(u'ymd'), default=0, help_text=u'年月日', db_index=True)  # 例：20171208, tc.local_string(format='%Y%m%d'), 0 为全部
    num = models.IntegerField(_(u'num'), default=0, help_text=u'数量')

    position = models.IntegerField(_(u'position'), default=1, help_text=u'排序')

    class Meta:
        verbose_name = _(u'[经销商维度]省份销量统计')
        verbose_name_plural = _(u'[经销商维度]省份销量统计')

    def __unicode__(self):
        return unicode(self.pk)

    @property
    def data(self):
        return {
            'province_code': self.province_code,
            'province_name': self.province_name,
            # 'ymd': self.ymd,
            'num': randnum() if settings.DEBUG_STATISTIC_DATA_FLAG else self.num,
        }


class SaleclerkSaleStatisticInfo(BaseModelMixin):
    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    distributor_id = models.CharField(_(u'distributor_id'), max_length=32, blank=True, null=True, help_text=u'经销商唯一标识', db_index=True)
    distributor_name = models.CharField(_(u'distributor_name'), max_length=32, blank=True, null=True, help_text=u'经销商名称')
    distributor_short_name = models.CharField(_(u'distributor_short_name'), max_length=8, blank=True, null=True, help_text=u'经销商简称')
    clerk_id = models.CharField(_(u'clerk_id'), max_length=32, blank=True, null=True, help_text=u'店员唯一标识', db_index=True)
    clerk_name = models.CharField(_(u'clerk_name'), max_length=32, blank=True, null=True, help_text=u'店员名称')
    ymd = models.IntegerField(_(u'ymd'), default=0, help_text=u'年月日', db_index=True)  # 例：20171208, tc.local_string(format='%Y%m%d'), 0 为全部
    num = models.IntegerField(_(u'num'), default=0, help_text=u'数量')

    class Meta:
        verbose_name = _(u'[经销商维度]销售员销量统计')
        verbose_name_plural = _(u'[经销商维度]销售员销量统计')

    def __unicode__(self):
        return unicode(self.pk)

    @property
    def data(self):
        return {
            'distributor_name': self.distributor_short_name or self.distributor_name or '',
            'salesman_id': self.clerk_id,
            'salesman_name': self.clerk_name,
            # 'ymd': self.ymd,
            'num': randnum() if settings.DEBUG_STATISTIC_DATA_FLAG else self.num,
        }


# 消费者维度


class ConsumeUserStatisticInfo(BaseModelMixin):
    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    ymd = models.IntegerField(_(u'ymd'), default=0, help_text=u'年月日', db_index=True)  # 例：20171208, tc.local_string(format='%Y%m%d')
    users = JSONField(_(u'users'), default=[], help_text=u'用户列表')
    num = models.IntegerField(_(u'num'), default=0, help_text=u'数量')

    class Meta:
        verbose_name = _(u'[消费者维度]用户统计')
        verbose_name_plural = _(u'[消费者维度]用户统计')

    def __unicode__(self):
        return unicode(self.pk)

    @property
    def data(self):
        return {
            'ymd': self.ymd,
            'num': randnum() if settings.DEBUG_STATISTIC_DATA_FLAG else self.num,
        }


class ConsumeSaleStatisticInfo(BaseModelMixin):
    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    ymd = models.IntegerField(_(u'ymd'), default=0, help_text=u'年月日', db_index=True)  # 例：20171208, tc.local_string(format='%Y%m%d')
    num = models.IntegerField(_(u'num'), default=0, help_text=u'数量')

    class Meta:
        verbose_name = _(u'[消费者维度]销量统计')
        verbose_name_plural = _(u'[消费者维度]销量统计')

    def __unicode__(self):
        return unicode(self.pk)

    @property
    def data(self):
        return {
            'ymd': self.ymd,
            'num': randnum() if settings.DEBUG_STATISTIC_DATA_FLAG else self.num,
        }


class ConsumeModelSaleStatisticInfo(BaseModelMixin):
    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    model_id = models.CharField(_(u'model_id'), max_length=32, help_text=u'型号唯一标识', db_index=True)
    model_name = models.CharField(_(u'model_name'), max_length=255, blank=True, null=True, help_text=u'型号名称', db_index=True)
    ymd = models.IntegerField(_(u'ymd'), default=0, help_text=u'年月日', db_index=True)  # 例：20171208, tc.local_string(format='%Y%m%d'), 0 为全部
    num = models.IntegerField(_(u'num'), default=0, help_text=u'数量')

    class Meta:
        verbose_name = _(u'[消费者维度]型号销量统计')
        verbose_name_plural = _(u'[消费者维度]型号销量统计')

    def __unicode__(self):
        return unicode(self.pk)

    @property
    def data(self):
        return {
            'model_id': self.model_id,
            'model_name': self.model_name,
            # 'ymd': self.ymd,
            'num': randnum() if settings.DEBUG_STATISTIC_DATA_FLAG else self.num,
        }


class ConsumeDistributorSaleStatisticInfo(BaseModelMixin):
    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    distributor_id = models.CharField(_(u'distributor_id'), max_length=32, help_text=u'经销商唯一标识', db_index=True)
    distributor_name = models.CharField(_(u'distributor_name'), max_length=255, blank=True, null=True, help_text=u'经销商名称')
    ymd = models.IntegerField(_(u'ymd'), default=0, help_text=u'年月日', db_index=True)  # 例：20171208, tc.local_string(format='%Y%m%d'), 0 为全部
    num = models.IntegerField(_(u'num'), default=0, help_text=u'数量')

    class Meta:
        verbose_name = _(u'[消费者维度]经销商销量统计')
        verbose_name_plural = _(u'[消费者维度]经销商销量统计')

    def __unicode__(self):
        return unicode(self.pk)

    @property
    def data(self):
        return {
            'distributor_id': self.distributor_id,
            'distributor_name': self.distributor_name,
            'ymd': self.ymd,
            'num': randnum() if settings.DEBUG_STATISTIC_DATA_FLAG else self.num,
        }


class ConsumeProvinceSaleStatisticInfo(BaseModelMixin):
    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    province_code = models.CharField(_(u'province_code'), max_length=6, help_text=u'省份编码', db_index=True)
    province_name = models.CharField(_(u'province_name'), max_length=8, blank=True, null=True, help_text=u'省份名称')
    ymd = models.IntegerField(_(u'ymd'), default=0, help_text=u'年月日', db_index=True)  # 例：20171208, tc.local_string(format='%Y%m%d'), 0 为全部
    users = JSONField(_(u'users'), default=[], help_text=u'用户列表')
    num = models.IntegerField(_(u'num'), default=0, help_text=u'人数')
    num2 = models.IntegerField(_(u'num2'), default=0, help_text=u'支数')

    position = models.IntegerField(_(u'position'), default=1, help_text=u'排序')

    class Meta:
        verbose_name = _(u'[消费者维度]省份销量统计')
        verbose_name_plural = _(u'[消费者维度]省份销量统计')

    def __unicode__(self):
        return unicode(self.pk)

    @property
    def data(self):
        return {
            'province_code': self.province_code,
            'province_name': self.province_name,
            'ymd': self.ymd,
            'num': randnum() if settings.DEBUG_STATISTIC_DATA_FLAG else self.num,
            'num2': randnum() if settings.DEBUG_STATISTIC_DATA_FLAG else self.num2,
        }
