# coding=utf-8

from django.db import models

from utils.exceptions import CustomError


class Wechat(models.Model):
    appid = models.CharField(max_length=512, verbose_name=u'小程序appid', editable=False)
    secret = models.CharField(max_length=512, verbose_name=u'小程序秘钥', null=True, blank=True)
    agent_num = models.CharField(max_length=512, verbose_name=u'商户号', null=True, blank=True)
    agent_key = models.CharField(max_length=512, verbose_name=u'商户密钥', null=True, blank=True)
    sms_sign = models.CharField(verbose_name=u'短信SIGN', max_length=200, null=True, blank=True)
    sms_token = models.CharField(verbose_name=u'短信TOKEN', max_length=200, null=True, blank=True)
    privacy_statement = models.TextField(verbose_name=u'隐私声明', null=True, blank=True)

    class Meta:
        db_table = "wechat"
        ordering = ['-id']
        index_together = ()
        verbose_name = u"小程序"
        default_permissions = ()

    @staticmethod
    def getByAppid(appid):
        instance = Wechat.objects.filter(appid=appid).first()
        if not instance:
            raise CustomError(u'未找到相应的小程序！')
        return instance
