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

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


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)

    # 一物一码
    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)

    is_send_redpack = models.BooleanField(_(u'is_send_redpack'), default=False, help_text=u'是否发放红包', db_index=True)
    redpack_amount = models.IntegerField(_(u'redpack_amount'), default=0, help_text=u'红包固定金额，单位：分')
    redpack_max_amount = models.IntegerField(_(u'redpack_max_amount'), default=0, help_text=u'红包金额范围，单位：分）')
    has_send_redpack = models.BooleanField(_(u'has_send_redpack'), default=False, help_text=u'是否已发放红包', db_index=True)
    redpack_send_amount = models.IntegerField(_(u'redpack_send_amount'), default=0, help_text=u'红包发放金额，单位：分）')

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

    is_clerk_send_redpack = models.BooleanField(_(u'is_clerk_send_redpack'), default=False, help_text=u'销售员是否发放红包', db_index=True)
    clerk_redpack_amount = models.IntegerField(_(u'clerk_redpack_amount'), default=0, help_text=u'销售员红包固定金额，单位：分')
    clerk_redpack_max_amount = models.IntegerField(_(u'clerk_redpack_max_amount'), default=0, help_text=u'销售员红包金额范围，单位：分）')
    has_clerk_send_redpack = models.BooleanField(_(u'has_clerk_send_redpack'), default=False, help_text=u'销售员是否已发放红包', db_index=True)
    clerk_redpack_send_amount = models.IntegerField(_(u'clerk_redpack_send_amount'), default=0, help_text=u'销售员红包发放金额，单位：分）')

    clerk_user_id = models.CharField(_(u'clerk_user_id'), max_length=32, blank=True, null=True, help_text=u'销售员用户唯一标识(红包)', db_index=True)
    clerk_nickname = models.CharField(_(u'clerk_nickname'), max_length=255, blank=True, null=True, help_text=u'销售员用户昵称')

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

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

    @property
    def redpack_info(self):
        return {
            'is_send_redpack': self.is_send_redpack,
            'has_send_redpack': self.has_send_redpack,
            'redpack_send_amount': self.redpack_send_amount,
            'user_id': self.user_id,
            'nickname': self.nickname,
            'is_clerk_send_redpack': self.is_clerk_send_redpack,
            'has_clerk_send_redpack': self.has_clerk_send_redpack,
            'clerk_redpack_send_amount': self.clerk_redpack_send_amount,
            'clerk_user_id': self.clerk_user_id,
            'clerk_nickname': self.clerk_nickname,
        }


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 unicode(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 unicode(self.pk)


class RedpackSendLogInfo(BaseModelMixin):
    WX_REDPACK = 0
    QY_TRANSFER = 1

    REDPACK_TYPE_TUPLE = (
        (WX_REDPACK, u'微信红包'),
        (QY_TRANSFER, u'企业付款'),
    )

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

    user_id = models.CharField(_(u'user_id'), max_length=32, blank=True, null=True, help_text=u'用户唯一标识', db_index=True)
    nickname = models.CharField(_(u'nickname'), 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'用户电话')

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

    redpack_type = models.IntegerField(_(u'redpack_type'), default=WX_REDPACK, choices=REDPACK_TYPE_TUPLE, help_text=u'红包发放方式', db_index=True)
    redpack_amount = models.IntegerField(_(u'redpack_amount'), default=0, help_text=u'红包发放金额')

    is_clerk_redpack = models.BooleanField(_(u'is_clerk_redpack'), default=False, help_text=u'是否为销售员红包', db_index=True)

    class Meta:
        verbose_name = _(u'红包发放记录')
        verbose_name_plural = _(u'红包发放记录')

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