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

from django.db import models
from django.utils.translation import ugettext_lazy as _
from django_models_ext import BaseModelMixin, upload_file_path, upload_file_url, upload_path
from shortuuidfield import ShortUUIDField
from TimeConvert import TimeConvert as tc

from mch.models import ModelInfo, OperatorInfo
from utils.qiniucdn import qiniu_file_url


class MchInfoEncryptLogInfo(BaseModelMixin):
    plaintext = models.CharField(_(u'plaintext'), max_length=64, blank=True, null=True, help_text=u'待加密字符串', db_index=True, unique=True)

    alg = models.CharField(_(u'alg'), max_length=16, blank=True, null=True, help_text=u'加密算法')
    ciphertext = models.CharField(_(u'ciphertext'), max_length=64, blank=True, null=True, help_text=u'加密字符串')

    brand_pk = models.IntegerField(_(u'brand_pk'), default=0, help_text=u'品牌PK', db_index=True)
    model_pk = models.IntegerField(_(u'model_pk'), default=0, help_text=u'型号PK', db_index=True)
    distributor_pk = models.IntegerField(_(u'distributor_pk'), default=0, help_text=u'经销商PK', db_index=True)

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

    # 二维码版本
    version = models.IntegerField(_(u'version'), default=2, help_text=u'二维码版本', db_index=True)

    # 一物一码
    application_id = models.IntegerField(_(u'application_id'), default=0, help_text=u'申请单号', db_index=True)
    code = models.CharField(_(u'code'), max_length=16, blank=True, null=True, help_text=u'九位的字符串原始码', db_index=True)
    code_url = models.CharField(_(u'code_url'), max_length=128, blank=True, null=True, help_text=u'28位普通码字符	', db_index=True)

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

    class Meta:
        verbose_name = _(u'mchinfoencryptloginfo')
        verbose_name_plural = _(u'mchinfoencryptloginfo')

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

    @property
    def admindata(self):
        model = ModelInfo.objects.get(pk=self.model_pk, status=True)
        try:
            operator_name = OperatorInfo.objects.get(operator_id=self.operator_id).name
        except OperatorInfo.DoesNotExist:
            operator_name = u'深圳捷成'

        return {
            'sn': self.sn,
            'model_pk': self.model_pk,
            'model_uni_name': model.model_uni_name,
            'model_name': model.model_name,
            'operator_name': operator_name,
            'created_at': tc.local_string(utc_dt=self.created_at)
        }


class MchInfoDecryptLogInfo(BaseModelMixin):
    ciphertext = models.CharField(_(u'ciphertext'), max_length=64, blank=True, null=True, help_text=u'待解密字符串', db_index=True)

    brand_pk = models.IntegerField(_(u'brand_pk'), default=0, help_text=u'品牌PK', db_index=True)
    model_pk = models.IntegerField(_(u'model_pk'), default=0, help_text=u'型号PK', db_index=True)
    distributor_pk = models.IntegerField(_(u'distributor_pk'), default=0, help_text=u'经销商PK', db_index=True)

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

    # 一物一码
    application_id = models.IntegerField(_(u'application_id'), default=0, help_text=u'申请单号', db_index=True)
    code = models.CharField(_(u'code'), max_length=16, blank=True, null=True, help_text=u'九位的字符串原始码', db_index=True)
    code_url = models.CharField(_(u'code_url'), max_length=128, blank=True, null=True, help_text=u'28位普通码字符	', db_index=True)

    decrypt_count = models.IntegerField(_(u'decrypt_count'), default=1, help_text=u'解密次数')

    class Meta:
        verbose_name = _(u'mchinfodecryptloginfo')
        verbose_name_plural = _(u'mchinfodecryptloginfo')

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


class MchLogInfo(BaseModelMixin):
    log_id = ShortUUIDField(_(u'log_id'), max_length=32, help_text=u'日志唯一标识', db_index=True)
    log_file = models.FileField(_(u'log_file'), upload_to=upload_path, blank=True, null=True, help_text=u'日志文件')

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

    app_version = models.IntegerField(_(u'app_version'), default=0, help_text=u'APP 版本号', db_index=True)

    class Meta:
        verbose_name = _(u'mchloginfo')
        verbose_name_plural = _(u'mchloginfo')

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


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

    log_id = ShortUUIDField(_(u'log_id'), max_length=32, help_text=u'日志唯一标识', db_index=True)

    is_search_model = models.IntegerField(_(u'is_search_model'), default=0, help_text=u'搜索镜头型号', db_index=True)
    is_selected_model = models.IntegerField(_(u'is_selected_model'), default=0, help_text=u'搜索相机型号', db_index=True)
    is_search_camera = models.IntegerField(_(u'is_search_model'), default=0, help_text=u'搜索相机型号', db_index=True)
    is_search_model_camera = models.IntegerField(_(u'is_search_model'), default=0, help_text=u'搜索相机型号和镜头型号', db_index=True)
    is_search_camera_after_model = models.IntegerField(_(u'is_search_camera_after_model'), default=0, help_text=u'选择镜头后搜索相机型号', db_index=True)

    class Meta:
        verbose_name = _(u'mchsearchmodelandcameraloginfo')
        verbose_name_plural = _(u'mchsearchmodelandcameraloginfo')

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


class ComplementCodeLogInfo(BaseModelMixin):
    AUDIT_TODO = 0
    AUDIT_PASS = 1
    AUDIT_REFUSED = -1

    AUDIT_STATUS_TUPLE = (
        (AUDIT_TODO, u'待审核'),
        (AUDIT_PASS, u'审核通过'),
        (AUDIT_REFUSED, u'审核不通过'),
    )

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

    log_id = ShortUUIDField(_(u'log_id'), max_length=32, help_text=u'日志唯一标识', db_index=True)

    name = models.CharField(_(u'name'), max_length=255, blank=True, null=True, help_text=u'用户姓名')
    phone = models.CharField(_(u'phone'), max_length=11, blank=True, null=True, help_text=u'用户电话', db_index=True)

    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'型号名称')

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

    shot_image = models.ImageField(_(u'shot_image'), upload_to=upload_path, blank=True, null=True, help_text=u'镜头机身照片')
    invoice_image = models.ImageField(_(u'invoice_image'), upload_to=upload_path, blank=True, null=True, help_text=u'购买凭证照片')

    audit_status = models.IntegerField(_(u'audit_status'), choices=AUDIT_STATUS_TUPLE, default=AUDIT_TODO, help_text=u'审核状态')

    ciphertext = models.CharField(_(u'ciphertext'), max_length=64, blank=True, null=True, help_text=u'待解密字符串', db_index=True)

    is_contacted = models.BooleanField(_(u'is_contacted'), default=False, help_text=u'是否已联系用户')

    is_upload_qiniu = models.BooleanField(_(u'is_upload_qiniu'), default=False, help_text=u'是否已上传七牛')

    class Meta:
        verbose_name = _(u'补码记录')
        verbose_name_plural = _(u'补码记录')

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

    @property
    def shot_image_url(self):
        return qiniu_file_url(self.shot_image.name, bucket='tamron') if self.is_upload_qiniu else upload_file_url(self.shot_image)

    @property
    def shot_image_path(self):
        return upload_file_path(self.shot_image)

    @property
    def invoice_image_url(self):
        return qiniu_file_url(self.invoice_image.name, bucket='tamron') if self.is_upload_qiniu else upload_file_url(self.invoice_image)

    @property
    def invoice_image_path(self):
        return upload_file_path(self.invoice_image)

    @property
    def data(self):
        model = ModelInfo.objects.get(model_id=self.model_id)
        return {
            'user_id': self.user_id,
            'log_id': self.log_id,
            'name': self.name,
            'phone': self.phone,
            'model_id': self.model_id,
            'model_name': model.model_name,
            'sn': self.sn,
            'shot_image': self.shot_image_path,
            'shot_image_url': self.shot_image_url,
            'invoice_image': self.invoice_image_path,
            'invoice_image_url': self.invoice_image_url,
            'audit_status': self.audit_status,
            'is_contacted': self.is_contacted,
            'ciphertext': self.ciphertext,
            'created_at': tc.local_string(utc_dt=self.created_at)
        }


class AdministratorLoginLogInfo(BaseModelMixin):
    admin_id = models.CharField(_(u'admin_id'), max_length=32, blank=True, null=True, help_text=u'管理员唯一标识')
    admin_name = models.CharField(_(u'name'), max_length=255, blank=True, null=True, help_text=u'管理员姓名')
    login_ip = models.CharField(_(u'login_ip'), max_length=32, blank=True, null=True, help_text=u'登录IP')
    login_at = models.DateTimeField(_(u'login_at'), blank=True, null=True, help_text=u'登录时间')

    @property
    def admindata(self):
        return {
            'admin_id': self.admin_id,
            'admin_name': self.admin_name,
            'login_ip': self.login_ip,
            'login_at': tc.local_string(utc_dt=self.login_at),
        }