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

from __future__ import division

from mch.models import BrandInfo, DistributorInfo, ModelInfo
from statistic.models import (ConsumeDistributorSaleStatisticInfo, ConsumeModelSaleStatisticInfo,
                              ConsumeProvinceSaleStatisticInfo, DistributorSaleStatisticInfo, ModelSaleStatisticInfo,
                              ProvinceSaleStatisticInfo)


PROVINCE_LIST = {
    "110000": "北京",
    "120000": "天津",
    "130000": "河北",
    "140000": "山西",
    "150000": "内蒙古",
    "210000": "辽宁",
    "220000": "吉林",
    "230000": "黑龙江",
    "310000": "上海",
    "320000": "江苏",
    "330000": "浙江",
    "340000": "安徽",
    "350000": "福建",
    "360000": "江西",
    "370000": "山东",
    "410000": "河南",
    "420000": "湖北",
    "430000": "湖南",
    "440000": "广东",
    "450000": "广西",
    "460000": "海南",
    "500000": "重庆",
    "510000": "四川",
    "520000": "贵州",
    "530000": "云南",
    "540000": "西藏",
    "610000": "陕西",
    "620000": "甘肃",
    "630000": "青海",
    "640000": "宁夏",
    "650000": "新疆",
    "710000": "台湾",
    "810000": "香港",
    "820000": "澳门"
}


def pre_provinces():
    brands = BrandInfo.objects.filter(status=True)
    for brand in brands:
        for pcode, pname in PROVINCE_LIST.items():
            pssi, created = ProvinceSaleStatisticInfo.objects.get_or_create(brand_id=brand.brand_id, province_code=pcode, ymd=0)
            pssi.province_name = pname
            pssi.save()

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


def pre_models():
    brands = BrandInfo.objects.filter(status=True)
    for brand in brands:
        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=0)
            mssi.model_name = mdl.model_name
            mssi.save()

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


def pre_distributors():
    brands = BrandInfo.objects.filter(status=True)
    for brand in brands:
        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=0)
            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=0)
            cdssi.distributor_name = dtbt.distributor_name
            cdssi.save()


def pre_all():
    pre_provinces()
    pre_models()
    pre_distributors()
