# coding=utf-8

from apps.WechatApplet.models import WechatApplet
from apps.WechatTp.models import WechatTp
from utils.exceptions import CustomError

class TenantBiz():

    @staticmethod
    def bindWechatApplet(tenant, auth_code):
        tp = WechatTp.getDefault()
        if tenant.delete:
            raise CustomError(u'该租户已被删除！')
        if tenant.is_bind_app:
            raise CustomError(u'该租户已绑定小程序！')
        app = WechatApplet.addAuthorizer(tp, auth_code, tenant)
        tenant.is_bind_app = True
        tenant.save()
        return app