# -*- 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, ProvinceShortModelMixin, SexModelMixin, upload_file_path,
                               upload_file_url, upload_path)
from jsonfield import JSONField
from shortuuidfield import ShortUUIDField
from TimeConvert import TimeConvert as tc

from kodo.basemodels import BrandInfoMixin
from coupon.models import CouponInfo


class AdministratorInfo(BaseModelMixin):
    ADMINISTRATOR = 0
    MAINTENANCE = 1
    OPERATOR = 2
    SALESMAN = 3

    USER_TYPE_TUPLE = (
        (ADMINISTRATOR, u'管理员'),
        (SALESMAN, u'销售员'),
        (MAINTENANCE, u'维修员'),
        (OPERATOR, u'运营人员'),
    )

    ACTIVATED = 1
    DISABLED = 2
    DELETED = 3

    USER_STATUS_TUPLE = (
        (ACTIVATED, u'已激活'),
        (DISABLED, u'已禁用'),
        (DELETED, u'已删除'),
    )

    admin_id = ShortUUIDField(_(u'admin_id'), max_length=32, blank=True, null=True, help_text=u'管理员唯一标识', db_index=True, unique=True)

    admin_type = models.IntegerField(_(u'admin_type'), choices=USER_TYPE_TUPLE, default=ADMINISTRATOR, help_text=u'管理员类型', db_index=True)

    phone = models.CharField(_(u'phone'), max_length=11, blank=True, null=True, help_text=u'管理员电话', db_index=True)
    password = models.CharField(_(u'password'), max_length=255, blank=True, null=True, help_text=u'管理员密码')
    encryption = models.CharField(_(u'encryption'), max_length=255, blank=True, null=True, help_text=u'管理员密码')

    name = models.CharField(_(u'name'), max_length=255, blank=True, null=True, help_text=u'管理员姓名')

    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    brand_name = models.CharField(_(u'brand_name'), max_length=255, blank=True, null=True, help_text=u'品牌名称')

    user_status = models.IntegerField(_(u'user_status'), choices=USER_STATUS_TUPLE, default=ACTIVATED, help_text=u'管理员状态', db_index=True)

    class Meta:
        verbose_name = _(u'管理员信息')
        verbose_name_plural = _(u'管理员信息')

    def __unicode__(self):
        return u'{}-{}'.format(self.name, self.phone)

    @property
    def admindata(self):
        return {
            'admin_id': self.admin_id,
            'admin_type': self.admin_type,
            'phone': self.phone,
            'name': self.name,
            'created_at': tc.local_string(utc_dt=self.created_at, format='%Y-%m-%d'),
        }


class OperatorInfo(BaseModelMixin):
    ACTIVATED = 1
    DISABLED = 2
    DELETED = 3

    USER_STATUS_TUPLE = (
        (ACTIVATED, u'已激活'),
        (DISABLED, u'已禁用'),
        (DELETED, u'已删除'),
    )

    operator_id = ShortUUIDField(_(u'operator_id'), max_length=32, blank=True, null=True, help_text=u'操作员唯一标识', db_index=True, unique=True)

    phone = models.CharField(_(u'phone'), max_length=11, blank=True, null=True, help_text=u'操作员电话', db_index=True)
    password = models.CharField(_(u'password'), max_length=255, blank=True, null=True, help_text=u'操作员密码')
    encryption = models.CharField(_(u'encryption'), max_length=255, blank=True, null=True, help_text=u'操作员密码')

    name = models.CharField(_(u'name'), max_length=255, blank=True, null=True, help_text=u'操作员姓名')

    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    brand_name = models.CharField(_(u'brand_name'), max_length=255, blank=True, null=True, help_text=u'品牌名称')
    brand_domain = models.CharField(_(u'brand_domain'), max_length=255, blank=True, null=True, help_text=u'品牌域名')

    user_status = models.IntegerField(_(u'user_status'), choices=USER_STATUS_TUPLE, default=ACTIVATED, help_text=u'操作员状态', db_index=True)

    class Meta:
        verbose_name = _(u'操作员信息')
        verbose_name_plural = _(u'操作员信息')

    def __unicode__(self):
        return u'{}-{}'.format(self.name, self.phone)

    @property
    def data(self):
        return {
            'operator_id': self.operator_id,
            'phone': self.phone,
            'name': self.name,
        }

    @property
    def kododata(self):
        return {
            'optor_id': self.operator_id,
            'brand_domain': self.brand_domain or settings.KODO_DEFAULT_BRAND_DOMAIN,
        }


class BrandInfo(BaseModelMixin):
    brand_id = ShortUUIDField(_(u'brand_id'), max_length=32, help_text=u'品牌唯一标识', db_index=True, unique=True)
    brand_name = models.CharField(_(u'brand_name'), max_length=255, blank=True, null=True, help_text=u'品牌名称')
    brand_descr = models.TextField(_(u'brand_descr'), max_length=255, blank=True, null=True, help_text=u'品牌描述')

    brand_logo = models.ImageField(_(u'brand_logo'), upload_to=upload_path, blank=True, null=True, help_text=u'品牌商标')
    brand_domain = models.CharField(_(u'brand_domain'), max_length=255, blank=True, null=True, 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 '%d' % self.pk

    @property
    def brand_logo_url(self):
        return upload_file_url(self.brand_logo)

    @property
    def data(self):
        return {
            'brand_id': str(self.pk),
            'brand_name': self.brand_name,
            'brand_descr': self.brand_descr,
        }


class ModelInfo(BaseModelMixin):
    MOUNT_TUPLE = (
        ('SONY', u'索尼'),
        ('FUJIFILM', u'富士'),
        ('CANON', u'佳能'),
        ('NIKON', u'尼康'),
    )

    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    brand_name = models.CharField(_(u'brand_name'), max_length=255, blank=True, null=True, help_text=u'品牌名称')

    jancode = models.CharField(_(u'jancode'), max_length=16, blank=True, null=True, help_text=u'JAN_CODE', db_index=True)

    model_id = ShortUUIDField(_(u'model_id'), max_length=32, help_text=u'型号唯一标识', db_index=True, unique=True)
    model_name = models.CharField(_(u'model_name'), max_length=32, blank=True, null=True, help_text=u'型号名称')
    model_uni_name = models.CharField(_(u'model_uni_name'), max_length=32, blank=True, null=True, help_text=u'型号统一名称')
    model_uni_full_name = models.CharField(_(u'model_uni_full_name'), max_length=255, blank=True, null=True, help_text=u'型号统一全名称')
    model_full_name = models.CharField(_(u'model_full_name'), max_length=255, blank=True, null=True, help_text=u'型号全名称')
    model_descr = models.TextField(_(u'model_descr'), max_length=255, blank=True, null=True, help_text=u'型号描述')
    model_mount = models.CharField(_(u'model_mount'), max_length=32, choices=MOUNT_TUPLE, default='SONY', help_text=u'镜头卡口')

    image = models.ImageField(_(u'image'), upload_to=upload_path, blank=True, null=True, help_text=u'横图')
    url = models.CharField(_(u'url'), max_length=255, blank=True, null=True, help_text=u'链接')

    image2 = models.ImageField(_(u'image2'), upload_to=upload_path, blank=True, null=True, help_text=u'方图')

    integral = models.IntegerField(_(u'integral'), default=100, help_text=u'【销售员】卡路里')

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

    display = models.BooleanField(_(u'display'), default=True, help_text=u'是否显示')

    is_important = models.BooleanField(_(u'is_important'), default=False, help_text=u'是否重要型号')

    shot_type_id = models.CharField(_(u'shot_type_id'), max_length=32, blank=True, null=True, help_text=u'镜头类型唯一标识', db_index=True)
    shot_member_integral = models.IntegerField(_(u'shot_member_integral'), default=0, help_text=u'【消费者】镜头会员积分')
    shot_member_image = models.ImageField(_(u'shot_member_image'), upload_to=upload_path, blank=True, null=True, help_text=u'镜头会员图片')
    shot_member_name = models.CharField(_(u'shot_member_name'), max_length=255, blank=True, null=True, help_text=u'型号全名称')
    is_show_shot = models.BooleanField(_(u'is_show_shot'), default=False, help_text=u'是否在镜头类型里显示')

    class Meta:
        verbose_name = _(u'型号信息')
        verbose_name_plural = _(u'型号信息')

    def __unicode__(self):
        return '%d' % self.pk

    @property
    def image_path(self):
        return upload_file_path(self.image)

    @property
    def image_url(self):
        return upload_file_url(self.image)

    @property
    def image2_path(self):
        return upload_file_path(self.image2)

    @property
    def image2_url(self):
        return upload_file_url(self.image2)

    @property
    def shot_member_image_path(self):
        return upload_file_path(self.shot_member_image)

    @property
    def shot_member_image_url(self):
        return upload_file_url(self.shot_member_image)

    @property
    def data(self):
        return {
            'jancode': self.jancode,
            'model_id': str(self.pk),
            'model_name': self.model_name,
            'model_descr': self.model_descr,
        }

    @property
    def imgdata(self):
        return {
            'image_url': self.image_url,
            'url': self.url,
        }

    @property
    def imgdata1(self):
        return self.shot_member_image_url

    @property
    def images(self):
        return [self.imgdata] if self.image else []
        # imgs = ModelImageInfo.objects.filter(model_id=self.model_id, status=True)
        # return [img.data for img in imgs]

    @property
    def admindata(self):
        return {
            'jancode': self.jancode,
            'model_id': self.model_id,
            'model_name': self.model_name,
            'model_uni_name': self.model_uni_name,
            'model_full_name': self.model_full_name,
            'model_descr': self.model_descr,
            'image_path': self.image_path,
            'image_url': self.image_url,
            'image2_path': self.image2_path,
            'image2_url': self.image2_url,
            'integral': self.integral,
            'is_important': self.is_important,
        }

    fulldata = admindata

    @property
    def member_shot_data(self):
        return {
            'shot_id': self.model_id,
            'shot_name': self.shot_member_name,
            'shot_image': self.image2_url,
            'integral': self.shot_member_integral,
        }

    @property
    def consumer_shot_data(self):
        from member.models import ShotTypeInfo

        shot_type = ShotTypeInfo.objects.get(shot_type_id=self.shot_type_id)
        return {
            'model_id': self.model_id,
            'model_name': self.model_name,
            'model_uni_name': self.model_uni_name,
            'model_full_name': self.model_full_name,
            'model_desc': self.model_descr,
            'model_mount': self.model_mount,
            'shot_type': self.shot_type_id,
            'shot_name': self.shot_member_name,
            'shot_type_name': shot_type.shot_type_name
        }


class ModelImageInfo(BaseModelMixin):
    model_id = models.CharField(_(u'model_id'), max_length=32, blank=True, null=True, help_text=u'型号唯一标识', db_index=True)
    model_name = models.CharField(_(u'model_name'), max_length=255, blank=True, null=True, help_text=u'型号名称')

    image = models.ImageField(_(u'image'), upload_to=upload_path, blank=True, null=True, help_text=u'图片')
    url = models.CharField(_(u'url'), max_length=255, blank=True, null=True, 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 '%d' % self.pk

    @property
    def image_url(self):
        return upload_file_url(self.image)

    @property
    def data(self):
        return {
            'image_url': self.image_url,
            'url': self.url,
        }


class CameraModelInfo(BaseModelMixin):
    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    brand_name = models.CharField(_(u'brand_name'), max_length=255, blank=True, null=True, help_text=u'品牌名称')

    camera_brand_name = models.CharField(_(u'camera_brand_name'), max_length=255, blank=True, null=True, help_text=u'机身品牌')
    camera_name = models.CharField(_(u'camera_name'), max_length=255, blank=True, null=True, help_text=u'机身名称')
    camera_image = models.ImageField(_(u'camera_image'), upload_to=upload_path, blank=True, null=True, help_text=u'机身图片')
    camera_market_time = models.DateField(_(u'camera_market_time'), blank=True, null=True, help_text=u'机身上市日期')

    class Meta:
        verbose_name = _(u'机身信息')
        verbose_name_plural = _(u'机身信息')

    def __unicode__(self):
        return '%d' % self.pk


class ModelCameraBodyInfo(BaseModelMixin):
    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    brand_name = models.CharField(_(u'brand_name'), max_length=255, blank=True, null=True, help_text=u'品牌名称')

    model_name = models.CharField(_(u'model_name'), max_length=32, blank=True, null=True, help_text=u'型号名称')
    model_full_name = models.CharField(_(u'model_full_name'), max_length=255, blank=True, null=True, help_text=u'型号全名称')
    model_image = models.ImageField(_(u'model_image'), upload_to=upload_path, blank=True, null=True, help_text=u'镜头图片')
    is_important = models.BooleanField(_(u'is_important'), default=False, help_text=u'是否重要型号')

    camera_brand_name = models.CharField(_(u'camera_brand_name'), max_length=255, blank=True, null=True, help_text=u'机身品牌')
    camera_name = models.CharField(_(u'camera_name'), max_length=255, blank=True, null=True, help_text=u'机身名称')
    camera_image = models.ImageField(_(u'camera_image'), upload_to=upload_path, blank=True, null=True, help_text=u'机身图片')
    camera_market_time = models.DateField(_(u'camera_market_time'), blank=True, null=True, help_text=u'机身上市日期')
    is_ILDC = models.BooleanField(_(u'is_ILDC'), default=False, help_text=u'是否需要原厂转接环')
    remark = models.CharField(_(u'remark'), max_length=255, blank=True, null=True, help_text=u'备注')

    class Meta:
        verbose_name = _(u'型号机身适配信息')
        verbose_name_plural = _(u'型号机身适配信息')

    def __unicode__(self):
        return '%d' % self.pk

    @property
    def model_url(self):
        return upload_file_url(self.model_image)

    @property
    def camera_url(self):
        return upload_file_url(self.camera_image)

    @property
    def final_camera_market_time(self):
        if not self.camera_market_time:
            return ''
        return tc.local_string(utc_dt=self.camera_market_time, format='%Y-%m-%d')

    @property
    def data(self):
        return {
            'model_name': self.model_name,
            'model_full_name': self.model_full_name,
            'model_url': self.model_url,
            'camera_brand_name': self.camera_brand_name,
            'camera_name': self.camera_name,
            'camera_url': self.camera_url,
            'is_ILDC': self.is_ILDC,
            'remark': self.remark,
        }


class DistributorInfo(BaseModelMixin):
    DISTRIBOR_OFFICE_UNKNOWN = -1
    DISTRIBOR_OFFICE_BEIJING = 0
    DISTRIBOR_OFFICE_CHENGDOU = 1
    DISTRIBOR_OFFICE_GUANGZHOU = 2
    DISTRIBOR_OFFICE_SHANGHAI = 3
    DISTRIBOR_OFFICE_WUHAN = 4
    DISTRIBOR_OFFICE_XIAN = 5

    DISTRIBOR_OFFICE_SPACE = (
        (DISTRIBOR_OFFICE_UNKNOWN, u'未知'),
        (DISTRIBOR_OFFICE_BEIJING, u'北京所'),
        (DISTRIBOR_OFFICE_CHENGDOU, u'成都所'),
        (DISTRIBOR_OFFICE_GUANGZHOU, u'广州所'),
        (DISTRIBOR_OFFICE_SHANGHAI, u'上海所'),
        (DISTRIBOR_OFFICE_WUHAN, u'武汉所'),
        (DISTRIBOR_OFFICE_XIAN, u'西安所'),
    )

    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    brand_name = models.CharField(_(u'brand_name'), max_length=255, blank=True, null=True, help_text=u'品牌名称')

    distributor_id = ShortUUIDField(_(u'distributor_id'), max_length=32, help_text=u'经销商唯一标识', db_index=True, unique=True)
    distributor_name = models.CharField(_(u'distributor_name'), max_length=255, 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'经销商简称')
    distributor_descr = models.TextField(_(u'distributor_descr'), max_length=255, blank=True, null=True, help_text=u'经销商描述')

    distributor_province_code = models.CharField(_(u'distributor_province_code'), max_length=6, blank=True, null=True, help_text=u'经销商所在省份编码')
    distributor_province_name = models.CharField(_(u'distributor_province_name'), max_length=3, choices=ProvinceShortModelMixin.PROVINCE_NAME_TUPLE, default=ProvinceShortModelMixin.PROVINCE_DEFAULT_NAME, blank=True, null=True, help_text=u'经销商所在省份名称')

    department_id = models.IntegerField(_(u'department_id'), default=-1, help_text=u'企业微信部门ID')
    department_name = models.CharField(_(u'department_name'), max_length=32, blank=True, help_text=u'企业微信部门名称', db_index=True)
    sr_id = models.CharField(_(u'sr_id'), max_length=32, blank=True, null=True, help_text=u'销售担当唯一标识', db_index=True)
    office = models.IntegerField(_(u'office'), choices=DISTRIBOR_OFFICE_SPACE, default=DISTRIBOR_OFFICE_UNKNOWN, help_text=u'事务所', db_index=True)

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

    class Meta:
        verbose_name = _(u'经销商信息')
        verbose_name_plural = _(u'经销商信息')

    def __unicode__(self):
        return '%d' % self.pk

    @property
    def data(self):
        return {
            'distributor_id': str(self.pk),
            'distributor_name': self.distributor_name,
            'distributor_short_name': self.distributor_short_name,
            'distributor_descr': self.distributor_descr,
        }

    @property
    def admindata(self):
        return {
            'distributor_id': self.distributor_id,
            'distributor_name': self.distributor_name,
            'distributor_short_name': self.distributor_short_name,
            'distributor_descr': self.distributor_descr,
            'province_code': self.distributor_province_code,
            'province_name': self.distributor_province_name,
            'sr_id': self.sr_id,
            'office': self.office,
        }


class SaleclerkInfo(BaseModelMixin, SexModelMixin):
    REFUSED = -1
    UNVERIFIED = 0
    ACTIVATED = 1
    DISABLED = 2
    DELETED = 3
    ASSIGN = 10

    USER_STATUS = (
        (REFUSED, u'已拒绝'),
        (UNVERIFIED, u'未验证'),
        (ACTIVATED, u'已激活'),
        (DISABLED, u'已禁用'),
        (DELETED, u'已删除'),
        (ASSIGN, u'已分配'),
    )

    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    brand_name = models.CharField(_(u'brand_name'), max_length=255, blank=True, null=True, help_text=u'品牌名称')

    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'经销商名称')

    clerk_id = ShortUUIDField(_(u'clerk_id'), max_length=32, help_text=u'店员唯一标识', db_index=True, unique=True)
    clerk_name = models.CharField(_(u'clerk_name'), max_length=32, blank=True, null=True, help_text=u'店员名称')
    clerk_sex = models.IntegerField(_(u'clerk_sex'), choices=SexModelMixin.SEX_TUPLE, default=SexModelMixin.MALE, help_text=u'店员性别', db_index=True)
    clerk_phone = models.CharField(_(u'clerk_phone'), max_length=11, blank=True, null=True, help_text=u'店员联系电话', unique=True)

    user_id = models.CharField(_(u'user_id'), max_length=32, blank=True, null=True, help_text=u'用户唯一标识', db_index=True)
    unionid = models.CharField(_(u'unionid'), max_length=32, blank=True, null=True, help_text=u'微信 UnionID', db_index=True)
    openid = models.CharField(_(u'openid'), max_length=32, blank=True, null=True, help_text=u'微信 OpenID', db_index=True)
    wx_userid = models.CharField(_(u'wx_userid'), max_length=32, blank=True, null=True, help_text=u'企业微信 user_id', db_index=True)

    num = models.IntegerField(_(u'num'), default=0, help_text=u'支数')

    integral = models.IntegerField(_(u'integral'), default=0, help_text=u'积分')
    total_integral = models.IntegerField(_(u'total_integral'), default=0, help_text=u'全部积分')

    user_status = models.IntegerField(_(u'user_status'), choices=USER_STATUS, default=UNVERIFIED, help_text=u'用户状态', db_index=True)
    refused_reason = models.TextField(_(u'refused_reason'), blank=True, null=True, help_text=u'审核拒绝原因')

    is_auth = models.BooleanField(_(u'is_auth'), default=False, help_text=u'是否已授权')

    test_user = models.BooleanField(_(u'test_user'), default=False, help_text=u'是否为测试用户')

    is_online_sales = models.BooleanField(_(u'is_online_sales'), default=False, help_text=u'是否为网销')

    class Meta:
        verbose_name = _(u'经销商销售员信息')
        verbose_name_plural = _(u'经销商销售员信息')

        unique_together = (
            ('clerk_phone', 'brand_id'),
        )

    def __unicode__(self):
        return '%d' % self.pk

    @property
    def admindata(self):
        return {
            'distributor_id': self.distributor_id,
            'distributor_name': self.distributor_name,
            'clerk_id': self.clerk_id,
            'clerk_name': self.clerk_name,
            'clerk_sex': self.clerk_sex,
            'clerk_phone': self.clerk_phone,
            'num': self.num,
            'integral': self.integral,
            'total_integral': self.total_integral,
            'status': self.user_status,
            'refused_reason': self.refused_reason,
            'is_auth': self.is_auth,
            'is_online_sales': self.is_online_sales,
        }

    data = admindata


class MaintenancemanInfo(BaseModelMixin, SexModelMixin):
    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    brand_name = models.CharField(_(u'brand_name'), max_length=255, blank=True, null=True, help_text=u'品牌名称')

    user_id = models.CharField(_(u'user_id'), max_length=32, blank=True, null=True, help_text=u'用户唯一标识', db_index=True, unique=True)
    wx_userid = models.CharField(_(u'wx_userid'), max_length=32, blank=True, null=True, help_text=u'企业微信 user_id', db_index=True)

    maintenance_id = ShortUUIDField(_(u'maintenance_id'), max_length=32, help_text=u'维修员唯一标识', db_index=True, unique=True)
    maintenance_name = models.CharField(_(u'maintenance_name'), max_length=32, blank=True, null=True, help_text=u'维修员名称')
    maintenance_sex = models.IntegerField(_(u'maintenance_sex'), choices=SexModelMixin.SEX_TUPLE, default=SexModelMixin.MALE, help_text=u'维修员性别', db_index=True)
    maintenance_phone = models.CharField(_(u'maintenance_phone'), max_length=11, blank=True, null=True, help_text=u'维修员联系电话', unique=True)

    class Meta:
        verbose_name = _(u'维修员信息')
        verbose_name_plural = _(u'维修员信息')

        unique_together = (
            ('maintenance_phone', 'brand_id'),
        )

    def __unicode__(self):
        return '%d' % self.pk

    @property
    def data(self):
        return {
            'maintenance_id': self.maintenance_id,
            'maintenance_name': self.maintenance_name,
            'maintenance_phone': self.maintenance_phone,
        }


class BrandModelDistributorPriceInfo(BaseModelMixin):
    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    brand_name = models.CharField(_(u'brand_name'), max_length=255, blank=True, null=True, help_text=u'品牌名称')

    model_id = models.CharField(_(u'model_id'), max_length=32, blank=True, null=True, help_text=u'型号唯一标识', db_index=True)
    model_name = models.CharField(_(u'model_name'), max_length=255, blank=True, null=True, help_text=u'型号名称')

    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=255, blank=True, null=True, help_text=u'经销商名称')

    factory_yuan = models.FloatField(_(u'factory_yuan'), default=1000, help_text=u'出厂价(元)')
    factory_fee = models.IntegerField(_(u'factory_fee'), default=100000, help_text=u'出厂价(分)')

    integral = models.IntegerField(_(u'integral'), default=100, help_text=u'积分')

    class Meta:
        verbose_name = _(u'品牌/型号/代理商价格信息')
        verbose_name_plural = _(u'品牌/型号/代理商价格信息')

        unique_together = (
            ('brand_id', 'model_id', 'distributor_id'),
        )

    def __unicode__(self):
        return '%d' % self.pk


class LatestAppInfo(BaseModelMixin):
    latest_adr_version_code = models.IntegerField(_(u'latest_adr_version_code'), default=0, help_text=u'最新安卓版本号')
    latest_adr_version_name = models.CharField(_(u'latest_adr_version_name'), max_length=16, blank=True, null=True, help_text=u'最新安卓版本名')
    latest_adr_app = models.FileField(_(u'latest_adr_app'), upload_to=upload_path, blank=True, null=True, help_text=u'最新版安卓 APP')
    latest_adr_url = models.URLField(_(u'latest_adr_url'), max_length=255, blank=True, null=True, help_text=u'最新版 APP 链接')

    latest_ios_version_code = models.IntegerField(_(u'latest_ios_version_code'), default=0, help_text=u'最新 iOS 版本号')
    latest_ios_version_name = models.CharField(_(u'latest_ios_version_name'), max_length=16, blank=True, null=True, help_text=u'最新 iOS 版本名')
    latest_ios_url = models.URLField(_(u'latest_ios_url'), max_length=255, blank=True, null=True, help_text=u'最新版 iOS 链接')

    class Meta:
        verbose_name = _(u'升级配置信息')
        verbose_name_plural = _(u'升级配置信息')

    def __unicode__(self):
        return u'{0.pk}'.format(self)

    @property
    def final_latest_adr_url(self):
        return self.latest_adr_url or upload_file_url(self.latest_adr_app)

    @property
    def data(self):
        return {
            'latest_adr_version_code': self.latest_adr_version_code,
            'latest_adr_version_name': self.latest_adr_version_name,
            'latest_adr_url': self.final_latest_adr_url,
            'latest_ios_version_code': self.latest_ios_version_code,
            'latest_ios_version_name': self.latest_ios_version_name,
            'latest_ios_url': self.latest_ios_url,
        }

    @property
    def adr(self):
        return {
            'latest_adr_version_code': self.latest_adr_version_code,
            'latest_adr_version_name': self.latest_adr_version_name,
            'latest_adr_url': self.final_latest_adr_url,
        }


class LatestAppScreenInfo(BaseModelMixin):
    latest_adr_version_code = models.IntegerField(_(u'latest_adr_version_code'), default=0, help_text=u'最新安卓版本号')
    latest_adr_version_name = models.CharField(_(u'latest_adr_version_name'), max_length=16, blank=True, null=True, help_text=u'最新安卓版本名')
    latest_adr_app = models.FileField(_(u'latest_adr_app'), upload_to=upload_path, blank=True, null=True, help_text=u'最新版安卓 APP')
    latest_adr_url = models.URLField(_(u'latest_adr_url'), max_length=255, blank=True, null=True, help_text=u'最新版 APP 链接')

    latest_ios_version_code = models.IntegerField(_(u'latest_ios_version_code'), default=0, help_text=u'最新 iOS 版本号')
    latest_ios_version_name = models.CharField(_(u'latest_ios_version_name'), max_length=16, blank=True, null=True, help_text=u'最新 iOS 版本名')
    latest_ios_url = models.URLField(_(u'latest_ios_url'), max_length=255, blank=True, null=True, help_text=u'最新版 iOS 链接')

    class Meta:
        verbose_name = _(u'升级配置信息(数据大屏)')
        verbose_name_plural = _(u'升级配置信息(数据大屏)')

    def __unicode__(self):
        return u'{0.pk}'.format(self)

    @property
    def final_latest_adr_url(self):
        return self.latest_adr_url or upload_file_url(self.latest_adr_app)

    @property
    def data(self):
        return {
            'latest_adr_version_code': self.latest_adr_version_code,
            'latest_adr_version_name': self.latest_adr_version_name,
            'latest_adr_url': self.final_latest_adr_url,
            'latest_ios_version_code': self.latest_ios_version_code,
            'latest_ios_version_name': self.latest_ios_version_name,
            'latest_ios_url': self.latest_ios_url,
        }

    @property
    def adr(self):
        return {
            'latest_adr_version_code': self.latest_adr_version_code,
            'latest_adr_version_name': self.latest_adr_version_name,
            'latest_adr_url': self.final_latest_adr_url,
        }


class ConsumeInfoSubmitLogInfo(BaseModelMixin):

    UNDELETE = 0
    RETURN = 1
    UNBINDING = 2

    DELETE_TYPE = (
        (UNDELETE, u'未删除'),
        (RETURN, u'退货'),
        (UNBINDING, u'解绑'),
    )

    user_id = models.CharField(_(u'user_id'), max_length=32, blank=True, null=True, help_text=u'用户唯一标识', db_index=True)

    phone = models.CharField(_(u'phone'), max_length=11, blank=True, null=True, help_text=u'用户手机', db_index=True)
    iv = models.CharField(_(u'iv'), max_length=255, blank=True, null=True, help_text=u'iv')
    encryptedData = models.CharField(_(u'encryptedData'), max_length=255, blank=True, null=True, help_text=u'encryptedData')

    lat = models.FloatField(_(u'lat'), default=1.0, help_text=u'纬度')
    lon = models.FloatField(_(u'lon'), default=1.0, help_text=u'经度')

    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    brand_name = models.CharField(_(u'brand_name'), max_length=255, blank=True, null=True, help_text=u'品牌名称')

    model_id = models.CharField(_(u'model_id'), max_length=32, blank=True, null=True, help_text=u'型号唯一标识', db_index=True)
    model_name = models.CharField(_(u'model_name'), max_length=255, blank=True, null=True, help_text=u'型号名称')
    model_uni_name = models.CharField(_(u'model_uni_name'), max_length=255, blank=True, null=True, help_text=u'型号统称')

    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=255, blank=True, null=True, help_text=u'经销商名称')

    serialNo = models.CharField(_(u'serialNo'), max_length=16, blank=True, null=True, help_text=u'序列号', db_index=True)

    verifyResult = models.IntegerField(_(u'verifyResult'), default=0, help_text=u'验证结果', db_index=True)

    dupload = models.BooleanField(_(u'dupload'), default=False, help_text=u'是否为重复提交')

    submit_during_activity = models.BooleanField(_(u'submit_during_activity'), default=False, help_text=u'是否为活动期间上传')
    activity_id = models.IntegerField(_(u'activity_id'), default=0, help_text=u'活动唯一标识')
    coupon_expire_at = models.DateTimeField(_(u'coupon_expire_at'), blank=True, null=True, help_text=u'维修券过期时间')
    coupon_value = models.IntegerField(_(u'coupon_value'), default=0, help_text=u'维修券金额(单位:分)')
    has_used = models.BooleanField(_(u'has_used'), default=False, help_text=u'是否已核销')
    admin_id = models.CharField(_(u'admin_id'), max_length=32, blank=True, null=True, help_text=u'核销员唯一标识', db_index=True)
    used_at = models.DateTimeField(_(u'used_at'), blank=True, null=True, help_text=u'维修券核销时间')

    code_version = models.IntegerField(_(u'code_version'), default=1, help_text=u'统览码版本', db_index=True)

    ym = models.IntegerField(_(u'ym'), default=0, help_text=u'年月', db_index=True)  # 例:201712, tc.local_string(format='%Y%m')
    ymd = models.IntegerField(_(u'ymd'), default=0, help_text=u'年月日', db_index=True)  # 例:20171208, tc.local_string(format='%Y%m%d')

    integral = models.IntegerField(_(u'integral'), default=0, help_text=u'积分')

    province = models.CharField(_(u'province'), max_length=32, blank=True, null=True, help_text=u'省份', db_index=True)

    test_user = models.BooleanField(_(u'test_user'), default=False, help_text=u'是否为测试用户')

    unbound = models.BooleanField(_(u'unbound'), default=False, help_text=u'是否为已解绑镜头')
    delete_type = models.IntegerField(_(u'delete_type'), choices=DELETE_TYPE, default=UNDELETE, help_text=u'删除类型', db_index=True)

    class Meta:
        verbose_name = _(u'消费者信息提交记录')
        verbose_name_plural = _(u'消费者信息提交记录')

    def __unicode__(self):
        return '%d' % self.pk

    @property
    def model_info(self):
        try:
            info = ModelInfo.objects.get(model_id=self.model_id).fulldata
        except ModelInfo.DoesNotExist:
            info = {}
        return info

    @property
    def coupon_info(self):
        return {
            'coupon_expire_at': '',
            'coupon_value': 0,
            'coupon_has_expired': True,
        }

    @property
    def coupon_info2(self):
        return self.coupon_info,

    @property
    def data(self):
        if self.submit_during_activity:
            try:
                act = ActivityInfo.objects.get(pk=self.activity_id)
            except ActivityInfo.DoesNotExist:
                act = None
        else:
            act = None
        return {
            'id': self.pk,
            'lat': self.lat,
            'lon': self.lon,
            'brand_id': self.brand_id,
            'brand_name': self.brand_name,
            'model_id': self.model_id,
            'model_name': self.model_name,
            'model_info': self.model_info,
            'serialNo': self.serialNo,
            'dupload': self.dupload,
            'verifyResult': self.verifyResult,
            'submit_during_activity': self.submit_during_activity,
            'coupon_info': act.coupon_info2(created_at=self.created_at) if act else self.coupon_info2,
            'final_coupon_info': act.coupon_info(created_at=self.created_at) if act else self.coupon_info,
            'has_used': self.has_used,
            'used_at': self.used_at,
            'created_at': tc.local_string(utc_dt=self.created_at, format='%Y-%m-%d %H:%M'),
        }

    @property
    def adminuserdata(self):
        return {
            'id': self.pk,
            'model_id': self.model_id,
            'model_name': self.model_name,
            'serialNo': self.serialNo,
            'integral': self.integral,
            'dupload': self.dupload,
            'unbound': self.unbound,
            'code_version': self.code_version,
            'created_at': tc.local_string(utc_dt=self.created_at, format='%Y-%m-%d'),
        }

    @property
    def admindata(self):
        return {
            'pk': self.pk,
            'user_id': self.user_id,
            'model_name': self.model_name,
            'model_uni_name': self.model_uni_name,
            'serialNo': self.serialNo,
            'phone': self.phone,
            'code_version': self.code_version,
            'dupload': self.dupload,
            'unbound': self.unbound,
            'created_at': tc.local_string(utc_dt=self.created_at),
        }


class ConsumeShotUnbindingInfo(BaseModelMixin, BrandInfoMixin):
    user_id = models.CharField(_(u'user_id'), max_length=32, blank=True, null=True, help_text=u'用户唯一标识', db_index=True)
    submit_pk = models.IntegerField(_(u'submit_pk'), default=0, blank=True, null=True, help_text=u'消费者提交ID')
    submit_at = models.DateTimeField(_(u'submit_at'), blank=True, null=True, help_text=u'消费者提交时间')
    model_id = models.CharField(_(u'model_id'), max_length=32, blank=True, null=True, help_text=u'型号唯一标识', db_index=True)
    sn = models.CharField(_(u'sn'), max_length=16, blank=True, null=True, help_text=u'序列号', db_index=True)
    reason = models.CharField(_(u'reason'), max_length=256, blank=True, null=True, help_text=u'解绑理由', db_index=True)

    class Meta:
        verbose_name = _(u'消费者镜头解绑记录')
        verbose_name_plural = _(u'消费者镜头解绑记录')

        unique_together = (
            ('user_id', 'submit_pk'),
        )

    def __unicode__(self):
        return '%d' % self.pk

    @property
    def admindata(self):
        from account.models import UserInfo

        model_info = ModelInfo.objects.get(model_id=self.model_id).fulldata
        user = UserInfo.objects.get(user_id=self.user_id)

        return {
            'user_id': self.user_id,
            'phone': user.phone,
            'model_info': model_info,
            'sn': self.sn,
            'reason': self.reason,
            'submit_pk': self.submit_pk,
            'submit_at': tc.local_string(utc_dt=self.submit_at, format='%Y-%m-%d %H:%M'),
            'created_at': tc.local_string(utc_dt=self.created_at, format='%Y-%m-%d %H:%M'),
        }


class ActivityInfo(BaseModelMixin):
    FIXED_EXPIRED_TIME = 0
    CHANGED_EXPIRED_TIME = 1

    COUPON_EXPIRED_TIME_TUPLE = (
        (FIXED_EXPIRED_TIME, u'固定结束时间'),
        (CHANGED_EXPIRED_TIME, u'可变结束时间'),
    )

    activity_id = ShortUUIDField(_(u'activity_id'), max_length=32, blank=True, null=True, help_text=u'活动唯一标识', db_index=True, unique=True)
    activity_name = models.CharField(_(u'activity_name'), max_length=255, blank=True, null=True, help_text=u'活动名称')

    model_uni_names = JSONField(_(u'model_uni_names'), default=[], blank=True, null=True, help_text=u'型号统一名称列表')

    start_at = models.DateTimeField(_(u'start_at'), help_text=u'start_at')
    end_at = models.DateTimeField(_(u'end_at'), help_text=u'end_at')

    coupon_id = models.CharField(_(u'coupon_id'), max_length=32, blank=True, null=True, help_text=u'券唯一标识', db_index=True)

    coupon_expire_type = models.IntegerField(_(u'coupon_expire_type'), choices=COUPON_EXPIRED_TIME_TUPLE, default=FIXED_EXPIRED_TIME, help_text=u'维修券类型')
    coupon_valid_period = models.IntegerField(_(u'coupon_valid_period'), default=0, help_text=u'维修券有效时间(单位:天)')
    coupon_expire_at = models.DateTimeField(_(u'coupon_expire_at'), blank=True, null=True, help_text=u'维修券过期时间')
    coupon_value = models.IntegerField(_(u'coupon_value'), default=0, help_text=u'维修券金额(单位:分)')

    class Meta:
        verbose_name = _(u'活动信息')
        verbose_name_plural = _(u'活动信息')

    def __unicode__(self):
        return '%d' % self.pk

    def final_expire_at(self, created_at=None):
        if self.coupon_expire_type == ActivityInfo.FIXED_EXPIRED_TIME:
            return self.coupon_expire_at
        return tc.utc_datetime(dt=created_at, days=self.coupon_valid_period)

    def final_coupon_expire_at(self, created_at=None):
        final_expire_at = self.final_expire_at(created_at=created_at)
        if not final_expire_at:
            return ''
        return tc.local_string(utc_dt=final_expire_at, format=u'%Y年%m月%d日', isuc=True)

    def has_unexpired_activity(self, model_name):
        return ((self.model_uni_names and model_name in self.model_uni_names) or not self.model_uni_names) and (self.start_at <= tc.utc_datetime() < self.end_at)

    def coupon_info(self, created_at=None):
        return {
            'coupon_expire_at': self.final_coupon_expire_at(created_at=created_at),
            'coupon_value': self.coupon_value,
            'coupon_has_expired': tc.utc_datetime() >= self.final_expire_at(created_at=created_at),
        }

    def coupon_info2(self, created_at=None):
        return self.coupon_info(created_at=created_at),

    @property
    def coupon_info3(self):
        try:
            coupon = CouponInfo.objects.get(coupon_id=self.coupon_id)
        except CouponInfo.DoesNotExist:
            coupon = None

        return {
            'coupon_image': coupon.coupon_image_url,
            'coupon_expire_at': coupon.coupon_expire_at,
            'coupon_value': coupon.coupon_value,
            'coupon_title': coupon.coupon_title,
            'coupon_valid_period': coupon.coupon_valid_period,
            'coupon_id': coupon.coupon_id,
            'activity_id': self.activity_id,
            'activity_name': self.activity_name,
        } if coupon else {}