from itertools import chain
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser
from django.db import models
from django.utils import six, timezone
from django.utils.encoding import python_2_unicode_compatible
from imagekit.models import ProcessedImageField
from imagekit.processors import ResizeToFill

from Ansjer.config import SERVER_DOMAIN
from django.utils import timezone


class PermissionsManager(models.Manager):
    use_in_migrations = True

    def get_by_natural_key(self, permName):
        return self.get(
            permName=permName
        )


class RoleManager(models.Manager):
    """
    The manager for the auth's Role model..
    """
    use_in_migrations = True

    def get_by_natural_key(self, roleName):
        return self.get(roleName=roleName)


class UserManager(BaseUserManager):

    def create_user(self, username, password, userID, is_active,
                    user_isValid, **extra_fields):
        user = self.model(
            username=username,
            userID=userID,
            is_active=is_active,
            user_isValid=user_isValid,
            **extra_fields
        )
        user.set_password(password)
        user.save(using=self._db)

        role = Role.objects.get(rid=1)
        user.role.add(role)
        return user

    def create_superuser(self, username, password, userID, is_active,
                         user_isValid, **extra_fields):
        # extra_fields.setdefault('is_superuser', is_superuser)
        is_superuser = extra_fields.get('is_superuser', None)
        if is_superuser != 100 and is_superuser != 1:
            raise ValueError('Superuser must have is_superuser=1 or 100.')

        return self.create_user(username, password, userID, is_active,
                                user_isValid, **extra_fields)


@python_2_unicode_compatible
class Permissions(models.Model):
    permName = models.CharField(blank=True, null=True, max_length=32, unique=True, verbose_name=u'权限名称')
    description = models.CharField(blank=True, null=True, max_length=128, verbose_name=u'描述信息', default='')
    objects = PermissionsManager()

    def __str__(self):
        return "%s" % (
            six.text_type(self.description))

    class Meta:
        ordering = ['permName']
        db_table = 'permissions'
        verbose_name = u'role permission'
        verbose_name_plural = verbose_name
        unique_together = (('permName'),)

    def natural_key(self):
        return (self.permName)


class Role(models.Model):
    rid = models.SmallIntegerField(primary_key=True, unique=True, verbose_name=u'用户角色组ID')
    roleName = models.CharField(max_length=32, unique=True,
                                default='User', verbose_name=u'角色名称')
    permission = models.ManyToManyField(to='Permissions', blank=True, verbose_name=u'权限', db_table='role_permissions')
    Description = models.TextField(blank=True, default='', verbose_name=u'描述信息')

    objects = RoleManager()

    class Meta:
        ordering = ['roleName']
        db_table = 'role'
        verbose_name = u'用户角色'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.roleName

    def natural_key(self):
        return (self.roleName,)

    def get_all_permission(self):
        perms = self.permission.all()

        permslist = []
        if self.rid == 100:
            perms = Permissions.objects.all()
            for perm in perms:
                permslist.append(perm.permName)
            return permslist
        else:
            permlist_tmp = []
            for perm in perms:
                permlist_tmp.append(perm.permName)
            permSet = set(permlist_tmp)
            permslist = list(permSet)
            permslist.sort()
            return permslist

class Device_User(AbstractBaseUser):
    userID = models.CharField(blank=True, max_length=32, primary_key=True,
                              verbose_name=u'用户ID', unique=True)
    role = models.ManyToManyField(to='Role', blank=True, verbose_name=u'用户角色', db_table='user_role')

    username = models.CharField(max_length=64, db_index=True, verbose_name=u'用户名', default='', blank=True)
    password = models.CharField(max_length=128, verbose_name=u'密码')
    userEmail = models.EmailField(max_length=64, verbose_name=u'邮箱', default='', blank=True)
    # 实际的路径就是 MEDIA_ROOT/Image/filename,所以可用upload_to来指定文件存放的前缀路径
    userIconPath = ProcessedImageField(blank=True, upload_to='User/Images/', default='static/User/default.png',
                                       verbose_name=u'头像',
                                       # 图片将处理成85x85的尺寸
                                       processors=[ResizeToFill(85, 85)], )
    userIconUrl = models.URLField(blank=True, max_length=128, default=SERVER_DOMAIN
                                                                      + 'account/getAvatar/User/defaultUser.png')
    NickName = models.CharField(blank=True, max_length=64, default='', verbose_name=u'用户昵称')
    is_superuser = models.IntegerField(blank=True, default=0, verbose_name=u'用户类型')
    is_active = models.BooleanField(blank=True, default=False, verbose_name=u'用户活动状态')
    data_joined = models.DateTimeField(blank=True, verbose_name=u'加入时间', auto_now_add=True)
    last_login = models.DateTimeField(blank=True, verbose_name='u最后登录时间', auto_now=True)
    user_isValid = models.BooleanField(blank=True, default=False, verbose_name=u'用户有效性', help_text=u'是否是认证通过的用户')
    online = models.BooleanField(blank=True, default=False, verbose_name=u'用户登录状态')
    machine_code = models.CharField(blank=True, max_length=128, default='', verbose_name=u'机械码')

    language = models.CharField(blank=True, max_length=16, default='en', verbose_name=u'语言地区')
    # 手机注册增加字段
    phone = models.CharField(max_length=16, verbose_name=u'手机号', default='', blank=True)
    fingerprint_enable = models.SmallIntegerField(default=0, verbose_name=u'是否开启了指纹登录') # 0：未开启，1：已开启
    fingerprint_key = models.CharField(max_length=128, default='', verbose_name=u'客户端用于解码的密钥等信息')
    is_local = models.BooleanField(blank=True, default=False, verbose_name=u'是否是本地登录用户') # False：账号登录，1：本地登录
    subscribe_email = models.SmallIntegerField(default=0, verbose_name=u'是否订阅营销邮件') # 0：未订阅，1：订阅，2：不订阅
    region_country = models.IntegerField(blank=True, default=0, verbose_name='地区表唯一标识')
    objects = UserManager()

    USERNAME_FIELD = 'userID'  # 必须有一个唯一标识
    REQUIRED_FIELDS = ['is_superuser']  # 创建superuser时的必须字段

    class Meta:
        ordering = ('-data_joined',)
        verbose_name = u'用户信息'
        db_table = 'device_user'
        get_latest_by = 'last_login'

    def __str__(self):
        return self.username

    def get_role_id(self):
        rids = []
        roles = self.role.all()
        for role in roles:
            rids.append(role.rid)
        return rids

    def get_all_permission(self):
        roles = self.role.all()
        perms = self.permission.all()

        permslist = []
        for role in roles:
            if role.rid == 100:
                perms = Permissions.objects.all()
                for perm in perms:
                    permslist.append(perm.permName)
                return permslist

        for perm in perms:
            permslist.append(perm.permName)

        permSet = set(permslist)
        for role in roles:
            permlist_tmp = []
            for perm in role.permission.all():
                permlist_tmp.append(perm.permName)

            permSet_tmp = set(permlist_tmp)
            permSet = permSet.union(permSet_tmp)
            permslist = list(permSet)
            permslist.sort()
            return permslist

    @property
    def is_staff(self):
        return self.is_superuser


# 设备表是建项目开发者设计的,自己看着办
# 谢谢提醒！我选择凉拌。
# 我选择狗带
class Device_Info(models.Model):
    id = models.CharField(blank=True, max_length=32, primary_key=True)
    userID = models.ForeignKey(Device_User, blank=True, to_field='userID', on_delete=models.CASCADE)
    NickName = models.CharField(blank=True, max_length=32, verbose_name=u'设备名称')
    UID = models.CharField(blank=True, max_length=32, verbose_name=u'设备UID', default='')
    SID = models.CharField(blank=True, max_length=32, verbose_name=u'服务器ID', default='')
    View_Account = models.CharField(blank=True, max_length=32, verbose_name=u'设备用户名称', default='')
    View_Password = models.CharField(blank=True, max_length=32, verbose_name=u'设备密码', default='')
    AudioFormat = models.IntegerField(blank=True, default=1, verbose_name=u'初始化影像')
    EventNotification = models.IntegerField(blank=True, verbose_name=u'报警类型', default=0)
    ChannelIndex = models.IntegerField(blank=True, verbose_name=u'通道号', default=0)
    Online = models.BooleanField(blank=True, default=False, verbose_name=u'设备是否在线')
    mMonitorIndex = models.IntegerField(blank=True, default=-1)
    Type = models.IntegerField(blank=True, verbose_name='设备类型')
    DebugMode = models.IntegerField(blank=True, verbose_name=u'调试模式', default=0)
    NotificationMode = models.IntegerField(blank=True, verbose_name=u'是否报警', default=0)
    qvgaLevel = models.BooleanField(blank=True, verbose_name=u'SD与HD画面', default=0,
                                    help_text=u'0代表：SD、1代表：HD')
    isShare = models.BooleanField(blank=True, verbose_name=u'共享设备',
                                  help_text=u'是否为共享获取的设备', default=False)
    primaryUserID = models.CharField(blank=True, verbose_name='主用户id', max_length=32, default='')
    primaryMaster = models.CharField(max_length=64, verbose_name=u'主用户名', default='')
    vodPrimaryUserID = models.CharField(blank=True, verbose_name='云存主用户id', max_length=32, default='')
    vodPrimaryMaster = models.CharField(max_length=64, verbose_name=u'云存主用户名', default='')
    data_joined = models.DateTimeField(blank=True, verbose_name=u'设备添加时间', auto_now_add=True)
    update_time = models.DateTimeField(blank=True, verbose_name=u'更新时间', auto_now=True, null=True)

    ip = models.CharField(blank=True, max_length=20, default='', verbose_name=u'设备区域ip')
    area = models.CharField(blank=True, max_length=100, default='', verbose_name=u'设备区域area')
    # ios要求新增字段
    version = models.CharField(blank=True, max_length=20, default='', verbose_name=u'版本号')
    iSNotification = models.BooleanField(blank=True, verbose_name=u'报警通知 0:关闭,1:开启)', default=False)
    isVod = models.SmallIntegerField(blank=True, default=0, verbose_name='是否支持云存')  # 是否支持云存设备
    isExist = models.SmallIntegerField(blank=True, default=1, verbose_name='是否被删除')  # 是否被删除了(需主用户交互) 1存在，0不存在，2设备被重置
    isCameraOpenCloud =  models.SmallIntegerField(blank=True, default=1, verbose_name='是否开启云存')  # 0：不开启  1：开启
    serial_number = models.CharField(blank=True, max_length=9, default='', verbose_name='关联序列号')
    ###
    REQUIRED_FIELDS = []

    def __str__(self):
        return self.NickName

    def model_to_dict(self, fields=None, exclude=None):
        opts = self._meta
        data = {}
        for f in chain(opts.concrete_fields, opts.private_fields, opts.many_to_many):
            if not getattr(f, 'editable', False):
                continue
            if fields and f.name not in fields:
                continue
            if exclude and f.name in exclude:
                continue
            data[f.name] = f.value_from_object(self)
        return data

    class Meta:
        db_table = 'device_info'
        ordering = ('-data_joined',)
        verbose_name = u'用户设备信息表'
        verbose_name_plural = verbose_name


class Access_Log(models.Model):
    id = models.AutoField(primary_key=True, verbose_name=u'自增标记ID')
    user = models.CharField(max_length=100, default='', db_index=True, blank=True, verbose_name=u'用户')
    operation = models.CharField(max_length=100, db_index=True, default='', blank=True, verbose_name=u'操作')
    ip = models.CharField(max_length=100, default='', db_index=True, blank=True, verbose_name=u'访问ip地址')
    url = models.CharField(max_length=150, default='', blank=True, verbose_name=u'访问路径')
    status = models.IntegerField(default=0, blank=True, verbose_name=u'状态')
    time = models.DateTimeField(null=True, blank=True, db_index=True, verbose_name=u'访问时间')
    content = models.TextField(blank=True, default='', verbose_name=u'参数内容')

    class Meta:
        db_table = 'access_log'
        verbose_name = u'访问日志表'
        verbose_name_plural = verbose_name


class Equipment_Info(models.Model):
    id = models.AutoField(primary_key=True, verbose_name=u'自增标记ID')
    devUid = models.CharField(default='', db_index=True, blank=True, max_length=32, verbose_name=u'设备ID')
    devNickName = models.CharField(blank=True, max_length=32, default='', verbose_name=u'设备昵称')
    Channel = models.IntegerField(default=1, blank=True, verbose_name=u'设备通道')
    eventType = models.IntegerField(default=0, blank=True, verbose_name=u'事件类型')
    status = models.BooleanField(blank=True, default=False, verbose_name=u'事件状态')
    alarm = models.CharField(blank=True, max_length=256, verbose_name=u'报警信息')
    eventTime = models.CharField(blank=True, default='', max_length=16, verbose_name=u'设备报警时间')
    receiveTime = models.CharField(blank=True, default='', max_length=16, verbose_name=u'接收到报警时间')
    userID_id = models.CharField(default='',  db_index=True, blank=True, max_length=32, verbose_name=u'用户ID')
    is_st = models.SmallIntegerField(default=0, verbose_name='是否截图')  # 0 否,1 是图，2,视频
    storage_location = models.SmallIntegerField(default=1, db_index=True, verbose_name='数据信息存储位置。1：阿里云oss，2：aws')
    # message_id = models.CharField(blank=True, max_length=32, default='', verbose_name='第三方推送服务器返回的id')
    # push_type = models.SmallIntegerField(blank=True, default=0, verbose_name='第三方推送服务器标志。0：APNS推送，1：谷歌推送，2：极光推送')
    # push_server_status = models.IntegerField(blank=True, default=200, verbose_name='是否成功推送到第三方服务器。200：成功，other：失败')
    # push_device_status = models.SmallIntegerField(blank=True, default=-1, verbose_name='是否成功推送到目标设备。0：失败，1：成功')
    addTime = models.IntegerField(verbose_name='添加时间', default=0)

    def __str__(self):
        return self.id

    class Meta:
        db_table = 'equipment_info'
        verbose_name = u'设备信息推送表'
        verbose_name_plural = verbose_name
        ordering = ('-id',)
        app_label = "db2"

class StatResModel(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='自动ID')
    name = models.CharField(default='', max_length=120, verbose_name='图片名称', unique=True)
    addTime = models.IntegerField(verbose_name='添加时间', default=0)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'statres'
        verbose_name = '静态资源表'
        verbose_name_plural = verbose_name
        ordering = ('id',)


class FeedBackModel(models.Model):
    id = models.AutoField(primary_key=True, verbose_name=u'自增标记ID')
    userID = models.ForeignKey(Device_User, null=True, blank=True, to_field='userID', on_delete=models.CASCADE)
    type = models.SmallIntegerField(default=0, verbose_name='反馈类型')  # 【0:产品建议,1:功能异常,2:安全问题】
    status = models.SmallIntegerField(default=0, verbose_name='状态:0未解决/解决')
    content = models.TextField(blank=True, null=True, verbose_name=u'反馈描述')
    addTime = models.IntegerField(verbose_name='添加时间', default=0)
    updTime = models.IntegerField(verbose_name='更新时间', default=0)
    FS = models.ManyToManyField(to='StatResModel', blank=True, verbose_name=u'用户反馈关联资源',
                                db_table='fb_res')
    app = models.CharField(max_length=8, default='', verbose_name='类型')  # [ios,android]
    phone_model = models.CharField(max_length=64, default='', verbose_name='手机型号')  # [小米,android]
    os_version = models.CharField(max_length=16, default='', verbose_name='系统版本')
    uid = models.CharField(max_length=20, default='', verbose_name='设备uid')
    product_type = models.CharField(max_length=32, default='', verbose_name='设备uid')
    app_version = models.CharField(max_length=32, default='', verbose_name='app版本信息')

    appBundleId = models.CharField(max_length=32, default='', verbose_name='app版本appBundleId')
    email = models.CharField(max_length=32, default='', verbose_name='app版本email')
    score = models.CharField(max_length=32, default='0', verbose_name='评分--星星数')

    def __str__(self):
        return self.id

    class Meta:
        db_table = 'feedback'
        verbose_name = u'用户反馈信息表'
        verbose_name_plural = verbose_name
        ordering = ('-id',)


class User_Brand(models.Model):
    id = models.AutoField(primary_key=True, verbose_name=u'自增标记ID')
    # 关联用户
    userID = models.ForeignKey(Device_User, to_field='userID', on_delete=models.CASCADE)
    deviceSupplier = models.CharField(max_length=32, default='', verbose_name='（必填）设备供应商 string  （例：华为、小米...）')
    deviceModel = models.CharField(max_length=64, default='', verbose_name='（必填）设备型号 string @mock=HM NOTE 1TD')
    osType = models.CharField(max_length=32, default='', verbose_name='（必填）操作系统名称 string @mock=Android')
    osVersion = models.CharField(max_length=32, default='', verbose_name='（必填）操作系统版本号 string @mock=4.0')

    country = models.CharField(max_length=200, default='', verbose_name='国家')
    province = models.CharField(max_length=150, default='', verbose_name='省')
    city = models.CharField(max_length=132, default='', verbose_name='市')
    area = models.CharField(max_length=132, default='', verbose_name='区')
    street = models.CharField(max_length=132, default='', verbose_name='街道')

    longitude = models.CharField(max_length=30, default='', verbose_name='经度')
    latitude = models.CharField(max_length=30, default='', verbose_name='纬度')
    appId = models.CharField(max_length=50, default='', verbose_name='appid_值')
    status = models.IntegerField(default=0, verbose_name='传国家省市信息过来就为状态：1 ，不传就为状态：0')
    ip = models.CharField(blank=True, max_length=20, default='', verbose_name=u'区域ip')

    addTime = models.IntegerField(verbose_name='添加时间', default=0)

    def __str__(self):
        return self.id

    class Meta:
        db_table = 'user_brand'
        verbose_name = u'用户登录手机端手机品牌统计表'
        verbose_name_plural = verbose_name
        ordering = ('id',)


# 存储通表
class VodBucketModel(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='主键')
    bucket = models.CharField(max_length=60, verbose_name='存储桶名称')
    storeDay = models.IntegerField(default=0, verbose_name='存储生命周期(天)')
    content = models.TextField(verbose_name='描述', default='')
    endpoint = models.CharField(max_length=125, default='', verbose_name='存储节点')
    area = models.CharField(max_length=16, default='', verbose_name='区域')
    region = models.CharField(max_length=16, default='', verbose_name='regionID')
    is_free = models.SmallIntegerField(default=0, verbose_name='是否为免费存储桶[0:否,1:是]')

    addTime = models.IntegerField(verbose_name='添加时间', default=0)
    updTime = models.IntegerField(verbose_name='更新时间', default=0)

    mold = models.SmallIntegerField(default=0, verbose_name='存储区域类型')  # 0：国内阿里 1：国外aws
    region_id = models.IntegerField(default=1, verbose_name='大洲编号')

    def __str__(self):
        return self.id

    class Meta:
        db_table = 'vod_bucket'
        verbose_name = u'存储通信息'
        verbose_name_plural = verbose_name
        ordering = ('-id',)


class Store_Meal(models.Model):
    id = models.AutoField(primary_key=True, verbose_name=u'自增ID')
    # title = models.CharField(blank=True, max_length=32, verbose_name=u'标题')
    currency = models.CharField(blank=True, default='$', max_length=32, verbose_name=u'货币符号')
    symbol = models.CharField(blank=True, default='$', max_length=32, verbose_name=u'符号')
    price = models.CharField(blank=True, max_length=32, verbose_name=u'价格')
    virtual_price = models.CharField(blank=True, max_length=32, verbose_name=u'虚拟价格')
    is_discounts = models.SmallIntegerField(default=0, verbose_name=u'该套餐是否有优惠 [0=否，1是]')   # 0没有；1有
    discount_price = models.CharField(blank=True, max_length=32, verbose_name=u'第二年优惠价格')
    day = models.IntegerField(default=0, blank=True, verbose_name=u'云存录像保存天数（循环）')   # 7,30,180,360
    expire = models.IntegerField(default=0, blank=True, verbose_name=u'有效期') #单位月
    # content = models.TextField(blank=True, null=True, verbose_name=u'描述')
    add_time = models.DateTimeField(blank=True, null=True, verbose_name=u'加入时间', auto_now_add=True)
    # type = models.SmallIntegerField(default=0, verbose_name='付款类型')  # 0是paypal,1为支付宝
    pay_type = models.ManyToManyField(to='Pay_Type', verbose_name='付款类型', db_table='store_meal_pay')
    update_time = models.DateTimeField(blank=True, verbose_name=u'更新时间', auto_now=True)
    bucket = models.ForeignKey(VodBucketModel, blank=True, to_field='id', on_delete=models.CASCADE,
                               default=1, verbose_name='存储空间')
    commodity_type = models.SmallIntegerField(default=0, verbose_name='云存储套餐类型')  # 0：事件型 1：连续型
    commodity_code = models.CharField(default='', max_length=32, verbose_name='套餐规格码')
    is_show = models.SmallIntegerField(default=0, verbose_name=u'该套餐是否隐藏 [0=否，1是]')
    # lang = models.CharField(default='', max_length=20, verbose_name='语言/国家')
    lang = models.ManyToManyField(to='Lang', verbose_name='套餐语言', db_table='store_meal_lang')

    # 备用字段
    spare_1 = models.CharField(default='', blank=True, max_length=64, verbose_name=u'备用字段1')
    spare_2 = models.CharField(default='', blank=True, max_length=64, verbose_name=u'备用字段2')
    spare_3 = models.CharField(default='', blank=True, max_length=64, db_index=True, verbose_name=u'备用字段3')
    spare_4 = models.CharField(default='', blank=True, max_length=64, db_index=True, verbose_name=u'备用字段4')

    def __str__(self):
        return self.id

    class Meta:
        db_table = 'store_meal'
        verbose_name = u'存储套餐'
        verbose_name_plural = verbose_name
        ordering = ('id',)


class Pay_Type(models.Model):
    id = models.AutoField(primary_key=True, verbose_name=u'自增ID')
    payment = models.CharField(max_length=32, verbose_name=u'支付方式')

    def __str__(self):
        return self.id

    class Meta:
        db_table = 'pay_type'
        verbose_name = u'付款类型'
        verbose_name_plural = verbose_name
        ordering = ('id',)


# 套餐语言表
class Lang(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='自增ID')
    lang = models.CharField(default='', max_length=20, db_index=True, verbose_name='语言/国家')
    title = models.CharField(blank=True, max_length=320, verbose_name='标题')
    content = models.TextField(blank=True, null=True, verbose_name='描述')
    discount_content = models.CharField(blank=True, max_length=320, verbose_name=u'优惠信息描述')

    def __str__(self):
        return self.id

    class Meta:
        db_table = 'lang'
        verbose_name = '套餐语言'
        verbose_name_plural = verbose_name
        ordering = ('id',)


class Equipment_Version(models.Model):
    eid = models.CharField(blank=True, max_length=32, primary_key=True)
    ESN = models.CharField(blank=True, max_length=32, verbose_name=u'设备规格名称')
    code = models.CharField(blank=True, max_length=64, verbose_name=u'设备规格代码')
    version = models.CharField(blank=True, max_length=64, verbose_name=u'设备版本')
    softwareVersion = models.CharField(blank=True, max_length=32, verbose_name=u'软件版本')
    chipModelList2Code = models.CharField(max_length=32, blank=True, verbose_name=u'主芯片码')
    channel = models.IntegerField(blank=True, verbose_name=u'通道数')
    resolutionRatio = models.IntegerField(blank=True, verbose_name=u'分辨率')
    type = models.CharField(blank=True, max_length=16, verbose_name=u'设备机型')
    companyCode = models.CharField(blank=True, max_length=128, verbose_name=u'用户公司名称')
    data_joined = models.DateTimeField(blank=True, verbose_name=u'加入时间', auto_now_add=True)
    fileSize = models.IntegerField(blank=True, verbose_name=u'文件总大小')
    filePath = models.CharField(blank=True, max_length=256, verbose_name=u'升级文件路径')
    Description = models.TextField(blank=True, default='', verbose_name=u'描述信息')
    fileMd5 = models.CharField(blank=True, max_length=32, default='', verbose_name=u'上传文件MD5加密')
    status = models.BooleanField(blank=True, default=True, verbose_name=u'是否开启更新状态')
    update_time = models.DateTimeField(blank=True, verbose_name=u'更新时间', auto_now=True)
    lang = models.CharField(blank=True, default='en', max_length=32, verbose_name=u'ota包内置语言')
    mci = models.CharField(default='', blank=True, max_length=10, verbose_name='设备类型')
    img = models.CharField(max_length=300, blank=True, default='', verbose_name=u'图片路径')
    max_ver = models.CharField(default='', max_length=16, verbose_name='最大版本号')

    def __str__(self):
        return self.eid

    class Meta:
        verbose_name = '设备版本信息'
        ordering = ('-data_joined',)
        db_table = 'equipment_version'


class App_Info(models.Model):
    id = models.AutoField(primary_key=True, verbose_name=u'自增标记ID')
    appBundleId = models.CharField(blank=True, max_length=32, verbose_name=u'appID')
    appName = models.CharField(blank=True, max_length=32, verbose_name=u'app名称')
    systemLanguage = models.CharField(blank=True, max_length=32, verbose_name=u'系统语言')
    newAppversion = models.CharField(blank=True, max_length=12, verbose_name=u'系统版本')
    minAppversion = models.CharField(default='', blank=True, max_length=12, verbose_name=u'最小系统版本')
    bundleVersion = models.CharField(blank=True, max_length=12, verbose_name=u'项目版本')
    content = models.TextField(blank=True, default='', verbose_name=u'更新内容')
    app_type = models.IntegerField(default=0, blank=True, verbose_name=u'app类型')  # ios 1,android 2
    add_time = models.DateTimeField(blank=True, null=True, verbose_name=u'加入时间', auto_now_add=True)
    update_time = models.DateTimeField(blank=True, verbose_name=u'更新时间', auto_now=True)
    downloadLink = models.CharField(max_length=300, blank=True, default='', verbose_name='升级')
    img = models.CharField(default='', max_length=128, verbose_name='图片路径')

    def __str__(self):
        return self.id

    class Meta:
        db_table = 'app_info'
        verbose_name = u'app信息表'
        verbose_name_plural = verbose_name
        ordering = ('id',)


class App_Colophon(models.Model):
    id = models.AutoField(primary_key=True, verbose_name=u'自增标记ID')
    app_id = models.ForeignKey(App_Info, to_field='id', on_delete=models.CASCADE, verbose_name='appid编号', blank=True,
                               default=1)
    lang = models.CharField(blank=True, max_length=32, verbose_name=u'系统语言')
    newApp_version = models.CharField(blank=True, max_length=12, verbose_name=u'系统版本')
    content = models.TextField(blank=True, default='', verbose_name=u'更新内容')
    version_time = models.IntegerField(default=0, blank=True, verbose_name=u'版本更新的时间')

    def __str__(self):
        return self.id

    class Meta:
        db_table = 'app_colophon'
        verbose_name = u'app信息表的记录关联到App_Info'
        verbose_name_plural = verbose_name
        ordering = ('id',)


class Order_Model(models.Model):
    orderID = models.CharField(blank=True, max_length=20, primary_key=True, verbose_name=u'订单id')
    trade_no = models.CharField(blank=True, max_length=32, default='', verbose_name='第三方订单号')
    # 订单关联用户
    userID = models.ForeignKey(Device_User, to_field='userID', on_delete=models.CASCADE)
    UID = models.CharField(max_length=20, verbose_name='设备UID')
    channel = models.SmallIntegerField(default=0, verbose_name=u'通道数')
    desc = models.CharField(max_length=50, default='', verbose_name='商品描述')
    price = models.CharField(default='', max_length=16, verbose_name='价格')
    currency = models.CharField(blank=True, default='$', max_length=32, verbose_name=u'货币')
    addTime = models.IntegerField(verbose_name='添加时间', default=0)
    updTime = models.IntegerField(verbose_name='更新时间', default=0)
    # endTime = models.IntegerField(verbose_name='结束时间', default=0)
    isSelectDiscounts = models.SmallIntegerField(default=0, verbose_name=u'用户是否选择了第二年优惠 [0=否，1是]')
    status = models.SmallIntegerField(default=0, verbose_name='付款状态')  # 0:待支付，1：成功，2：取消，3：已退款, 9：处理中，10：付款失败
    payType = models.SmallIntegerField(default=0, verbose_name='付款类型0:paypal,1:alipay')
    payTime = models.IntegerField(verbose_name='支付成功时间', default=0)
    rank = models.ForeignKey(Store_Meal, to_field='id', default='', on_delete=models.CASCADE, verbose_name='套餐类型')
    nickname = models.CharField(default='', max_length=64, verbose_name='设备昵称')
    uid_bucket_id = models.IntegerField(default=0, verbose_name='关联uid_bucket的字段')
    commodity_type = models.SmallIntegerField(default=0, verbose_name='云存储套餐类型')
    commodity_code = models.CharField(default='', max_length=32, verbose_name='套餐规格码')
    pay_url = models.CharField(max_length=2000, default='', verbose_name='支付url')
    paypal = models.CharField(max_length=500, null=True, blank=True, verbose_name='支付批准url')

    # 备用字段
    spare_1 = models.CharField(default='', blank=True, max_length=64, verbose_name=u'备用字段1')
    spare_2 = models.CharField(default='', blank=True, max_length=64, verbose_name=u'备用字段2')
    spare_3 = models.CharField(default='', blank=True, max_length=64, db_index=True, verbose_name=u'备用字段3')
    spare_4 = models.CharField(default='', blank=True, max_length=64, db_index=True, verbose_name=u'备用字段4')

    def __str__(self):
        return self.orderID

    class Meta:
        db_table = 'orders'
        verbose_name = u'订单信息表'
        verbose_name_plural = verbose_name
        ordering = ('-orderID',)


class VodHlsModel(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='回放列表主键')
    uid = models.CharField(max_length=20, verbose_name='设备UID', db_index=True)
    channel = models.SmallIntegerField(default=0, verbose_name='通道')
    time = models.IntegerField(verbose_name='播放列表名字时间戳', default=0, db_index=True)
    endTime = models.IntegerField(verbose_name='云存储删除时间（周期）', default=0)
    sec = models.IntegerField(verbose_name='秒数', default=0)
    bucket = models.ForeignKey(VodBucketModel, blank=True, to_field='id', on_delete=models.CASCADE, default=1,
                               verbose_name='存储空间')
    fg = models.CharField(max_length=20,verbose_name='ts个数,时间描述片段数') # 阿里为时间片段数,亚马逊为一个32bit整型，前28bit代表ts文件的时长


    def __str__(self):
        return self.id

    class Meta:
        db_table = 'vod_hls'
        verbose_name = u'云存回放信息表'
        verbose_name_plural = verbose_name
        ordering = ('-id',)


class OssCrdModel(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='主键')
    uid = models.CharField(max_length=20, verbose_name='设备UID')
    channel = models.SmallIntegerField(default=0, verbose_name='通道')
    data = models.TextField(verbose_name='设备sts')
    addTime = models.IntegerField(verbose_name='添加时间', default=0)
    bucket = models.ForeignKey(VodBucketModel, blank=True, to_field='id', on_delete=models.CASCADE, default=1,
                               verbose_name='存储空间')

    def __str__(self):
        return self.id

    class Meta:
        db_table = 'osscrd'
        verbose_name = u'设备证书'
        verbose_name_plural = verbose_name
        ordering = ('-id',)


class StsCrdModel(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='主键')
    uid = models.CharField(max_length=20, verbose_name='设备UID', db_index=True)
    channel = models.SmallIntegerField(default=0, verbose_name='通道')
    data = models.TextField(verbose_name='设备sts')
    addTime = models.IntegerField(verbose_name='添加时间', default=0)
    bucket = models.ForeignKey(VodBucketModel, blank=True, to_field='id', on_delete=models.CASCADE, default=1,
                               verbose_name='存储空间')
    type = models.SmallIntegerField(default=0,verbose_name='sts类型') # 0:阿里云,1:s3

    def __str__(self):
        return self.id

    class Meta:
        db_table = 'stscrd'
        verbose_name = u'设备证书'
        verbose_name_plural = verbose_name
        ordering = ('-id',)


class UID_Bucket(models.Model):
    id = models.AutoField(primary_key=True, verbose_name=u'自增标记ID')
    uid = models.CharField(max_length=20, verbose_name='设备UID', db_index=True)
    channel = models.SmallIntegerField(default=0, verbose_name='通道')
    bucket = models.ForeignKey(VodBucketModel, blank=True, to_field='id', on_delete=models.CASCADE, verbose_name='存储空间')
    status = models.SmallIntegerField(default=0, verbose_name='状态[0:开启,1:关闭]')
    endTime = models.BigIntegerField(verbose_name='套餐结束时间', db_index=True, default=0)
    addTime = models.IntegerField(verbose_name='添加时间', default=0)
    updateTime = models.BigIntegerField(verbose_name='更新时间', default=0)
    use_status = models.IntegerField(verbose_name='使用状态[1:使用中,2已过期]', default=0)
    has_unused = models.SmallIntegerField(default=0, verbose_name='是否拥有其它未使用的套餐[0:否,1:是]')

    class Meta:
        db_table = 'vod_uid_bucket'
        verbose_name = '设备关联套餐表'
        verbose_name_plural = verbose_name
        ordering = ('id',)

class Unused_Uid_Meal(models.Model):
    id = models.AutoField(primary_key=True, verbose_name=u'自增标记ID')
    uid = models.CharField(max_length=20, verbose_name='设备UID', db_index=True)
    channel = models.SmallIntegerField(default=0, verbose_name='通道')
    bucket = models.ForeignKey(VodBucketModel, blank=True, to_field='id', on_delete=models.CASCADE, verbose_name='存储空间')
    addTime = models.IntegerField(verbose_name='添加时间', default=0)
    expire = models.IntegerField(verbose_name='存储桶存储时长[月份]', default=0)
    num = models.IntegerField(verbose_name='个数', default=0)
    # effect_time = models.BigIntegerField(verbose_name='生效时间', db_index=True, default=0)
    # uid_bucket = models.ForeignKey(UID_Bucket, blank=True, to_field='id', on_delete=models.CASCADE, default=0,
    #                            verbose_name='uid_bucket关联')
    class Meta:
        db_table = 'unused_uid_Meal'
        verbose_name = '未使用的设备关联套餐表'
        verbose_name_plural = verbose_name
        ordering = ('id',)


class UID_Preview(models.Model):
    id = models.AutoField(primary_key=True, verbose_name=u'自增标记ID')
    uid = models.CharField(default='', max_length=20, db_index=True, verbose_name='设备UID')
    channel = models.SmallIntegerField(verbose_name=u'通道号', default=0)
    addTime = models.IntegerField(verbose_name='添加时间', default=0)
    updTime = models.IntegerField(verbose_name='更新时间', default=0)

    class Meta:
        db_table = 'uid_preview'
        verbose_name = '设备预览图'
        verbose_name_plural = verbose_name
        ordering = ('id',)


# 系统发送信息新到用户
class SysMsgModel(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='自增id')
    userID_id = models.CharField(default='', db_index=True, blank=True, max_length=32, verbose_name=u'用户ID')
    msg = models.TextField(blank=True, default='', verbose_name=u'发送内容')
    status = models.SmallIntegerField(verbose_name='是否已读', default=0)  # 0:否,1:是
    addTime = models.IntegerField(verbose_name='添加时间', default=0)
    updTime = models.IntegerField(verbose_name='更新时间', default=0)
    eventType = models.IntegerField(verbose_name='消息类型', default=0)  # 默认系统消息类型,0系统消息，1 ipcamera消息
    uid = models.CharField(default='', max_length=20, db_index=True, verbose_name='设备UID')

    class Meta:
        db_table = 'sys_msg'
        verbose_name = '系统消息'
        verbose_name_plural = verbose_name
        ordering = ('-id',)
        app_label = "db2"


# 设备推送重构
# 设备配置表，新
class UidSetModel(models.Model):
    id = models.AutoField(primary_key=True, verbose_name=u'自增标记ID')
    uid = models.CharField(max_length=20, db_index=True, unique=True, verbose_name='设备UID')
    channel = models.SmallIntegerField(default=0, verbose_name='通道数量')  #
    detect_status = models.SmallIntegerField(default=0, verbose_name='推送开关')  # 状态[0:关闭,1:开启,2:用户解绑]
    detect_interval = models.IntegerField(verbose_name='推送间隔', default=60)  # 秒
    addTime = models.IntegerField(verbose_name='添加时间', default=0)
    updTime = models.IntegerField(verbose_name='更新时间', default=0)
    ucode = models.CharField(max_length=32, verbose_name='设备产品码', default='')
    version = models.CharField(max_length=32, verbose_name='设备版本', default='')
    p2p_region = models.CharField(max_length=16, verbose_name='设备p2p区域', default='ALL')  # ALL CN EU US
    cloud_vod = models.SmallIntegerField(default=2, verbose_name='云存开关')  # 0,关闭，1开启，2,不支持
    tz = models.CharField(default='', max_length=16, verbose_name ='设备时区')  # +8
    video_code = models.SmallIntegerField(default=0, verbose_name='编码类型')  # 0:264,1:265
    nickname = models.CharField(default='', max_length=64, verbose_name='设备昵称')
    ip = models.CharField(max_length=20, default='', verbose_name=u'设备ip')
    # 设备重置后第一次启动时间
    is_alexa = models.IntegerField(default=0, verbose_name='是否支持alexa')  # 0为不支持，1为支持，2为开启alexa发现
    detect_group = models.CharField(default='', max_length=32, verbose_name=u'检测类型')
    pwd = models.CharField(max_length=32, default='', verbose_name=u'设备密码') # 暂时是预留字段
    resetTime = models.IntegerField(default=0, verbose_name='设备重置时间')
    region_alexa = models.CharField(max_length=8, verbose_name='设备alexa区域', default='ALL')  # ALL CN EU US
    deviceModel = models.CharField(blank=True, max_length=64, default='', verbose_name=u'设备型号')
    TimeZone = models.CharField(blank=True, max_length=50, default='', verbose_name=u'时区')
    TimeStatus = models.SmallIntegerField(default=0, verbose_name='同步手机时间开关。0：关闭，1：开启')
    SpaceUsable = models.CharField(blank=True, max_length=20, default='', verbose_name=u'可用空间')
    SpaceSum = models.CharField(blank=True, max_length=20, default='', verbose_name=u'总空间')
    MirrorType = models.IntegerField(blank=True, default=0, verbose_name=u'镜像类型 0:关闭镜像,1：上下镜像，2:左右镜像，3：上下左右镜像')
    RecordType = models.IntegerField(blank=True, default=0, verbose_name=u'录像模式(0:关闭,1:全是录像,3:报警录像)')
    OutdoorModel = models.IntegerField(blank=True, default=0, verbose_name=u'室外模式 0：关闭，1：开启')
    WIFIName = models.CharField(blank=True, max_length=50, default='', verbose_name=u'无线名称')
    isDetector = models.SmallIntegerField(default=0, verbose_name=u'侦测开关0:关闭,1:开启')
    DetectorRank = models.IntegerField(default=0, verbose_name=u'侦测灵敏度 0:低,1:中,2:高,3:最高')
    is_human = models.IntegerField(default=0, verbose_name='是否支持人形追踪。0：不支持，1：支持')
    is_custom_voice = models.IntegerField(default=0, verbose_name='是否支持自定义语音。0：不支持，1：支持')
    double_wifi = models.IntegerField(default=0, verbose_name='是否支持双频wifi。0：不支持，1：支持')

    class Meta:
        db_table = 'uid_set'
        verbose_name = u'设备配置表'
        verbose_name_plural = verbose_name
        ordering = ('id',)


# 设备关联用户推送
class   UidPushModel(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='自增id')
    userID = models.ForeignKey(Device_User, to_field='userID', on_delete=models.CASCADE)
    uid_set = models.ForeignKey(UidSetModel, to_field='id', on_delete=models.CASCADE)
    appBundleId = models.CharField(blank=True, max_length=32, verbose_name=u'appID')
    app_type = models.IntegerField(default=0, verbose_name=u'app类型 1:ios,2:安卓')
    push_type = models.IntegerField(default=0, verbose_name=u'推送类型')  # 0,apns 1,安卓gcm 2,极光
    token_val = models.CharField(default='', max_length=160, verbose_name=u'设备验证令牌')
    m_code = models.CharField(default='', max_length=64, verbose_name='手机唯一标识')
    addTime = models.IntegerField(verbose_name='添加时间', default=0)
    updTime = models.IntegerField(verbose_name='更新时间', default=0)
    lang = models.CharField(max_length=8, verbose_name='推送语言类型', default='en')  # en英文 cn中文
    tz = models.CharField(max_length=8, verbose_name='utc时区', default='0')

    class Meta:
        db_table = 'uid_push'
        verbose_name = '设备绑定'
        verbose_name_plural = verbose_name
        ordering = ('-id',)


# 设备通道配置
class UidChannelSetModel(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='自增id')
    uid = models.ForeignKey(UidSetModel, to_field='id', on_delete=models.CASCADE)
    channel = models.IntegerField(default=1, verbose_name='通道号')
    channel_name = models.CharField(blank=True, default='', max_length=20, verbose_name=u'通道名')
    pir_audio = models.SmallIntegerField(default=0, verbose_name='pir声音。0：关闭，1：开启')
    mic_audio = models.SmallIntegerField(default=0, verbose_name='mic声音。0：关闭，1：开启')
    battery_status = models.SmallIntegerField(default=0, verbose_name='低电量提醒状态。0：关闭，1：开启')
    battery_level = models.SmallIntegerField(default=0, verbose_name='低电量提醒级别。0: 低于10%；1：低于25%；2：低于50%；3：低于75%')
    sleep_status = models.SmallIntegerField(default=0, verbose_name='是否开启【休眠通知】。0：关闭；1：开启')
    sleep_time = models.SmallIntegerField(default=0, verbose_name='摄像机进入休眠时间。0：不休眠；1：10秒；2：20秒；3：30秒')
    light_night_model = models.IntegerField(default=0, verbose_name='夜视模式')  # 0：全彩模式，1：黑白模式，2：智能夜视模式
    light_alarm_type = models.IntegerField(default=0, verbose_name='警报人形类型')  # 0为人形，1为移动，3是人形和移动侦测0x0f
    # 声光报警级别: 0: 关闭，1：强烈声光告警，2：轻微声光告警，3: 强烈声告警，4:轻微声告警，5:强烈光告警
    light_alarm_level = models.IntegerField(default=0, verbose_name='声光报警级别')
    light_alarm_man_en = models.IntegerField(default=0, verbose_name='人为告警状态')  # 0：关，1：开
    light_alarm_vol = models.IntegerField(default=0, verbose_name='报警音量')  # 音量值0-100
    light_long_light = models.IntegerField(default=0, verbose_name='长亮')
    voice_prompt_status = models.SmallIntegerField(default=0, verbose_name='个性语音提示快关。0：关闭，1：开启')
    voice_prompt_enter = models.IntegerField(default=0, verbose_name='进入铃声id')
    voice_prompt_leave = models.IntegerField(default=0, verbose_name='离开铃声id')
    voice_prompt_intelligent_mute = models.SmallIntegerField(default=0, verbose_name='智能静音。0：关闭，1：开启')
    voice_start_x = models.FloatField(default=0, verbose_name='起始坐标的x')
    voice_start_y = models.FloatField(default=18, verbose_name='起始坐标的y')
    voice_end_x = models.FloatField(default=44, verbose_name='结束坐标的x')
    voice_end_y = models.FloatField(default=18, verbose_name='结束坐标的y')
    voice_start_time = models.IntegerField(default=0, verbose_name='语音执行的起始时间')
    voice_end_time = models.IntegerField(default=0, verbose_name='语音执行的结束时间')
    voice_repeat_day = models.IntegerField(default=127, verbose_name='语音执行的日期，周几')
    voice_direction = models.IntegerField(default=0, verbose_name='语音方向。')

    class Meta:
        db_table = 'uid_channel'
        verbose_name = '设备通道设置'
        verbose_name_plural = verbose_name


# oauth2 第三方登录补全信息
class UserExModel(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='自增id')
    userID = models.ForeignKey(Device_User, to_field='userID', on_delete=models.CASCADE)
    appBundleId = models.CharField(default='', max_length=32, verbose_name=u'appID')
    region = models.CharField(default='', max_length=16, verbose_name='区域语言')
    addTime = models.IntegerField(verbose_name='添加时间', default=0)
    updTime = models.IntegerField(verbose_name='更新时间', default=0)

    # {'cn': '简体中文', 'tc': '繁体中文', 'fr': '法文', 'ru': '俄文', 'es': '西班牙文', 'pl': '波兰文', 'ja': '日文', 'de': '德文', 'en': '英文'}

    class Meta:
        db_table = 'user_ex'
        verbose_name = '用户扩展信息表'
        verbose_name_plural = verbose_name
        ordering = ('-id',)


# oauth2 第三方登录补全信息
class UserOauth2Model(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='自增id')
    userID = models.ForeignKey(Device_User, to_field='userID', on_delete=models.CASCADE)
    authType = models.SmallIntegerField(default=0, verbose_name=0)  # 授权类型 0 非授权，1 微信授权，2 苹果授权
    unionID = models.CharField(default='', max_length=64, verbose_name='唯一标记')  # 绑定唯一标记 unionID
    addTime = models.IntegerField(verbose_name='添加时间', default=0)
    updTime = models.IntegerField(verbose_name='更新时间', default=0)

    # {'cn': '简体中文', 'tc': '繁体中文', 'fr': '法文', 'ru': '俄文', 'es': '西班牙文', 'pl': '波兰文', 'ja': '日文', 'de': '德文', 'en': '英文'}

    class Meta:
        db_table = 'user_oauth2'
        verbose_name = '用户oauth2关联'
        verbose_name_plural = verbose_name
        ordering = ('-id',)


class SysMassModel(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='主键自增id')
    sender = models.ForeignKey(Device_User, to_field='userID', on_delete=models.CASCADE)  # 发送人
    platform = models.CharField(default='', verbose_name='平台', max_length=16)
    lang = models.TextField(blank=True, default='', verbose_name=u'语种')
    recever = models.TextField(blank=True, default='', verbose_name=u'接受者')
    msg = models.TextField(blank=True, default='', verbose_name=u'发送内容')
    addTime = models.IntegerField(verbose_name='添加时间', default=0)
    updTime = models.IntegerField(verbose_name='修改时间', default=0)

    class Meta:
        db_table = 'sys_mass'
        verbose_name = '群发消息记录表'
        verbose_name_plural = verbose_name
        ordering = ('-id',)


class UidUserModel(models.Model):
    id = models.CharField(max_length=32, primary_key=True)
    userID = models.ForeignKey(Device_User, to_field='userID', on_delete=models.CASCADE)
    NickName = models.CharField(max_length=32, verbose_name=u'设备名称')
    UID = models.CharField(max_length=32, verbose_name=u'设备UID', default='')
    View_Account = models.CharField(max_length=32, verbose_name=u'设备用户名称', default='')
    View_Password = models.CharField(max_length=32, verbose_name=u'设备密码', default='')
    ChannelIndex = models.IntegerField(verbose_name=u'通道号', default=0)
    Type = models.IntegerField(verbose_name='设备类型')
    NotificationMode = models.IntegerField(verbose_name=u'是否报警', default=0)
    isShare = models.BooleanField(verbose_name=u'共享设备', default=False)
    primaryUserID = models.CharField(verbose_name='主用户id', max_length=32, default='')
    primaryMaster = models.CharField(max_length=64, verbose_name=u'主用户名', default='')
    data_joined = models.DateTimeField(verbose_name=u'设备添加时间', auto_now_add=True)
    version = models.CharField(max_length=20, default='', verbose_name=u'版本号')
    isVod = models.SmallIntegerField(default=0, verbose_name='是否支持云存')  # 是否支持云存设备
    isExist = models.SmallIntegerField(default=1, verbose_name='是否被删除')  # 是否被删除了(需主用户交互) 1存在，0不存在
    is_ap = models.SmallIntegerField(default=1, verbose_name='是否为ap模式添加')  # 1为ap模式添加，0为非ap模式添加

    class Meta:
        db_table = 'uid_user'
        ordering = ('-data_joined',)
        verbose_name = u'用户设备ap表'
        verbose_name_plural = verbose_name


class pushDeployModel(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='主键自增id')
    name = models.CharField(max_length=32, default='', verbose_name=u'推送配置名称')
    key = models.CharField(max_length=32, default='', verbose_name=u'推送配置key')
    secret = models.CharField(max_length=32, default='', verbose_name=u'推送配置密钥')
    pem = models.CharField(max_length=32, default='', verbose_name=u'推送配置路径')
    type = models.CharField(max_length=32, default='', verbose_name=u'推送配置类型')  # ios 1,android 2
    addTime = models.IntegerField(verbose_name='添加时间', default=0)
    updTime = models.IntegerField(verbose_name='修改时间', default=0)

    class Meta:
        db_table = 'push_deploy'
        verbose_name = '推送配置表'
        verbose_name_plural = verbose_name
        ordering = ('-id',)


class AppSetModel(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='主键自增id')
    appBundleId = models.CharField(max_length=64, default='', verbose_name=u'APPid')
    content = models.TextField(default='', verbose_name=u'里面包含评分，用户帮助，ap添加方式，ap工具，广告模块等json字段值')
    addTime = models.IntegerField(verbose_name='添加时间', default=0)
    updTime = models.IntegerField(verbose_name='修改时间', default=0)

    class Meta:
        db_table = 'app_set'
        verbose_name = 'app的各个版本控制表'
        verbose_name_plural = verbose_name
        ordering = ('-id',)


# 认证客户端类型表
class ApplicationModel(models.Model):
    CLIENT_TYPES = (
        ('confidential', 'confidential'),
        ('public', 'public'),
    )
    # authorization_code
    # 授权码模式(即先登录获取code, 再获取token)
    # password
    # 密码模式(将用户名, 密码传过去, 直接获取token)
    # client_credentials
    # 客户端模式(无用户, 用户向客户端注册, 然后客户端以自己的名义向’服务端’获取资源)
    # implicit
    # 简化模式(在redirect_uri
    # 的Hash传递token;
    # Auth客户端运行在浏览器中, 如JS, Flash)
    # refresh_token
    # 刷新access_token
    GRANT_TYPES = (
        ('authorization_code', "authorization_code"),
        ('password', "password"),
        ('client_credentials', "client_credentials"),
        ('implicit', "implicit"),
        ('refresh_token', "refresh_token"),
    )
    id = models.BigAutoField(primary_key=True)
    name = models.CharField(max_length=128, blank=True, verbose_name='client名字描述')
    client_id = models.CharField(max_length=50, unique=True, verbose_name='客户端id')
    client_secret = models.CharField(max_length=50, unique=True, verbose_name='客户端secret')
    client_type = models.CharField(max_length=32, choices=CLIENT_TYPES, verbose_name='客户端类型')
    grant_type = models.CharField(max_length=32, choices=GRANT_TYPES, blank=True, null=True)
    redirect_uri = models.TextField(blank=True, null=True, verbose_name='重定向url')
    skip_auth = models.BooleanField(default=False, verbose_name='是否跳过点击授权')
    add_time = models.IntegerField(verbose_name='添加时间', default=0)
    update_time = models.IntegerField(verbose_name='更新时间', default=0)

    class Meta:
        ordering = ('-add_time',)
        verbose_name = u'可授权客户端表'
        db_table = 'oauth_application'


# 授权码信息表
class GrantCodeModel(models.Model):
    id = models.BigAutoField(primary_key=True)
    userID = models.ForeignKey(Device_User, verbose_name="用户表userID", to_field='userID', on_delete=models.CASCADE)
    code = models.CharField(max_length=32, unique=True)
    app_id = models.CharField(max_length=100, default='', verbose_name="appBundleId")
    application = models.ForeignKey(ApplicationModel, verbose_name="用户表id", to_field='id', on_delete=models.CASCADE)
    expire_time = models.IntegerField(verbose_name='过期时间', default=0)
    add_time = models.IntegerField(verbose_name='添加时间', default=0)
    update_time = models.IntegerField(verbose_name='更新时间', default=0)

    # 输出的永远是本地时间输出的永远是本地时间
    def is_expired(self):
        return timezone.now() >= self.expire_time

    class Meta:
        ordering = ('-add_time',)
        verbose_name = u'授权码表'
        db_table = 'oauth_grant_code'


class UserAppFrequencyModel(models.Model):
    id = models.AutoField(primary_key=True)
    user = models.ForeignKey(Device_User, to_field='userID', on_delete=models.CASCADE, verbose_name='关联设备用户表')
    type = models.SmallIntegerField(default=0, verbose_name='使用频率类型') # 1：每天，2：三天，3：一周，4：两周，5：一个月，6：一个月以上
    data_time = models.IntegerField(default=0, verbose_name='数据时间')
    add_time = models.IntegerField(default=0, verbose_name='添加时间')
    update_time = models.IntegerField(default=0, verbose_name='更新时间')

    class Meta:
        verbose_name = '用户使用APP频率表'
        verbose_name_plural = verbose_name
        db_table = 'user_app_frequency'
        ordering = ('-add_time',)


class AppFrequencyStatisticsModel(models.Model):
    id = models.AutoField(primary_key=True)
    data = models.TextField(default='', verbose_name='统计好的数据')
    month = models.IntegerField(default=0, verbose_name='月份')
    year = models.IntegerField(default=0, verbose_name='年份')

    class Meta:
        verbose_name = 'app月使用频率统计表'
        verbose_name_plural = verbose_name
        db_table = 'app_frequency_statistics'


class AppFrequencyYearStatisticsModel(models.Model):
    id = models.AutoField(primary_key=True)
    data = models.TextField(default='', verbose_name='统计好的数据')
    year = models.IntegerField(default=0, verbose_name='年份')
    num = models.IntegerField(default=0, verbose_name='总人数')

    class Meta:
        verbose_name = 'app使用频率统计表，年度统计'
        verbose_name_plural = verbose_name
        db_table = 'app_frequency_year_statistics'

# alexa连接数统计表
class AlexaConnectStatisticsModel(models.Model):
    id = models.AutoField(primary_key=True)
    num = models.IntegerField(default=0, verbose_name='总人数')
    data_time = models.IntegerField(default=0, verbose_name='数据时间')

    class Meta:
        verbose_name = 'alexa连接数统计表'
        verbose_name_plural = verbose_name
        db_table = 'alexa_statistics'


class FAQModel(models.Model):
    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=64, default='', unique=True, verbose_name='标题')
    content = models.TextField(default='', verbose_name='内容')
    add_time = models.IntegerField(default=0, verbose_name='添加时间')
    update_time = models.IntegerField(default=0, verbose_name='更新时间')

    class Meta:
        db_table = 'tb_faq'
        verbose_name = '问题帮助表'
        verbose_name_plural = verbose_name


class AppLogModel(models.Model):
    id = models.AutoField(primary_key=True)
    user = models.ForeignKey(Device_User, to_field='userID', on_delete=models.CASCADE, verbose_name='关联设备用户表')
    uid = models.CharField(max_length=20, default='', verbose_name='设备uid')
    average_delay = models.CharField(max_length=32, default='', verbose_name='最高平均延时')
    status = models.SmallIntegerField(default=0, verbose_name='失败状态')
    filename = models.CharField(max_length=120, default='', verbose_name='文件名')
    add_time = models.IntegerField(default=0, verbose_name='日期')

    class Meta:
        db_table = 'app_log'
        verbose_name = 'APP日志表'
        verbose_name_plural = verbose_name


class EquipmentInfoExStatisticsModel(models.Model):
    id = models.AutoField(primary_key=True)
    push_type = models.SmallIntegerField(default=0, verbose_name='第三方推送服务器标志。0：APNS推送，1：谷歌推送，2：极光推送')
    number_of_successes = models.IntegerField(default=0, verbose_name='推送成功数量')
    number_of_failures = models.IntegerField(default=0, verbose_name='推送失败数量')
    number_of_arrival = models.IntegerField(default=0, verbose_name='推送到达数量')
    statistics_date = models.IntegerField(default=0, verbose_name='属于哪天、哪月的统计')
    date_type = models.SmallIntegerField(default=0, verbose_name='统计日期的类型。0：天，1：月')
    add_time = models.IntegerField(default=0, verbose_name='添加时间')

    class Meta:
        db_table = 'equipment_info_ex_statistics'
        verbose_name = '推送消息统计表'
        verbose_name_plural = verbose_name


class CountryIPModel(models.Model):
    id = models.AutoField(primary_key=True)
    ip = models.CharField(default='', max_length=32, verbose_name='ip')
    user_ex = models.ForeignKey(UserExModel, to_field='id', on_delete=models.CASCADE, verbose_name='关联用户扩展信息表id')
    country = models.CharField(default='', max_length=100, verbose_name='国家')
    status = models.SmallIntegerField(default=0, verbose_name='是否已经查找，0：否，1：是')
    add_time = models.IntegerField(default=0, verbose_name='添加时间')

    class Meta:
        db_table = 'country_ip'
        verbose_name = 'ip-国家统计表'
        verbose_name_plural = verbose_name


class ZositechHelpModel(models.Model):
    id = models.AutoField(primary_key=True)
    locale = models.CharField(default='', db_index=True, max_length=50, verbose_name='语言')
    label_names =  models.CharField(default='', db_index=True, max_length=50, verbose_name='标签')
    origin  =  models.CharField(default='', db_index=True ,max_length=50, verbose_name='来源')
    # author_id = models.CharField(default='', max_length=50, verbose_name='管理员id')
    # body = models.TextField(default='', blank=True, verbose_name='内容')
    # comments_disabled = models.CharField(default='', max_length=10, verbose_name='comments')
    # created_at = models.CharField(default='', max_length=50, verbose_name='创建时间')
    # draft = models.CharField(default='', max_length=10, verbose_name='草稿')
    # edited_at = models.CharField(default='',max_length=50, verbose_name='修改时间')
    # html_url = models.CharField(default='', blank=True, verbose_name='跳转路径')
    # body = models.CharField(default='', blank=True, verbose_name='标签')
    # body = models.CharField(default='', blank=True, verbose_name='标签')
    # body = models.TextField(default='', blank=True, verbose_name='标签')
    # body = models.TextField(default='', blank=True, verbose_name='标签')
    # body = models.CharField(default='', blank=True, verbose_name='标签')
    # body = models.CharField(default='', blank=True, verbose_name='标签')

    content = models.TextField(blank=True, default='', verbose_name=u'内容')

    class Meta:
        db_table = 'zositech_help'
        verbose_name = '周视使用帮助表'
        verbose_name_plural = verbose_name


class EquipmentVersionLimitModel(models.Model):
    id = models.AutoField(primary_key=True)
    type = models.SmallIntegerField(default=0, verbose_name='限制类型')
    content = models.TextField(default='', verbose_name='限制内容')
    equipment_version = models.ForeignKey(Equipment_Version, to_field='eid', on_delete=models.CASCADE, verbose_name='关联设备版本信息id')
    status = models.SmallIntegerField(default=0, verbose_name='是否启用。0：不启用，1：启用')
    add_time = models.IntegerField(default=0, verbose_name='添加时间')
    update_time = models.IntegerField(default=0, verbose_name='更新时间')

    class Meta:
        db_table = 'equipment_version_limit'
        verbose_name = '设备版本信息限制表'
        verbose_name_plural = verbose_name


class ExperienceContextModel(models.Model):
    id = models.AutoField(primary_key=True)
    experience_type = models.SmallIntegerField(default=0,verbose_name='体验类型') # 0: 免费体验套餐， 1： 激活码
    uid = models.CharField(max_length=20, default='', verbose_name='设备uid')
    do_time = models.IntegerField(default=0, verbose_name='激活时间')
    # is_experience = models.SmallIntegerField(default=0, verbose_name=u'是否云存体验用户')  # 0：不是体验用户，1：是体验用户

    class Meta:
        db_table = 'experience_context'
        verbose_name = '设备体验表'
        verbose_name_plural = verbose_name

class CDKcontextModel(models.Model):
    id = models.AutoField(primary_key=True)
    cdk = models.CharField(max_length=50,unique=True,verbose_name='激活码')
    create_time = models.IntegerField(default=0, verbose_name='创建时间')
    valid_time = models.IntegerField(default=0, verbose_name='有效期间')  #超过有效期激活码不可在激活 ,0：永久
    is_activate = models.SmallIntegerField(default=0, verbose_name='是否已激活') #0 未激活  1 已激活
    rank = models.ForeignKey(Store_Meal, to_field='id', default='', on_delete=models.CASCADE, verbose_name='套餐类型')
    # order = models.ForeignKey(Order_Model, blank=True, max_length=20, to_field='orderID', on_delete=models.CASCADE, verbose_name='订单id', unique=True)
    order = models.CharField(max_length=20, blank=True, unique=True, verbose_name='订单id')

    # 备用字段
    spare_1 = models.CharField(default='', blank=True, max_length=64, verbose_name=u'备用字段1')
    spare_2 = models.CharField(default='', blank=True, max_length=64, verbose_name=u'备用字段2')
    spare_3 = models.CharField(default='', blank=True, max_length=64, db_index=True, verbose_name=u'备用字段3')
    spare_4 = models.CharField(default='', blank=True, max_length=64, db_index=True, verbose_name=u'备用字段4')

    class Meta:
        db_table = 'cdk_context'
        verbose_name = '激活码表'
        verbose_name_plural = verbose_name


class VoicePromptModel(models.Model):
    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=128, default='', verbose_name='语音标题')
    type = models.SmallIntegerField(default=0, verbose_name='语音类型。0：进入语音，1：离开语音')
    filename = models.CharField(max_length=120, default='', verbose_name='文件名')
    language = models.CharField(max_length=16, default='', verbose_name='语言类型')
    classification = models.SmallIntegerField(default=1, verbose_name='语音分类。0：系统，1：自定义')
    add_time = models.IntegerField(default=0, verbose_name='添加时间')
    uid = models.CharField(max_length=20, default='0', verbose_name='关联设备UID')
    channel = models.IntegerField(default=0, verbose_name='通道号')
    status = models.SmallIntegerField(default=1, verbose_name='是否启用。0：不启用，1：启用')

    class Meta:
        db_table = 'voice_prompt'
        verbose_name = '语音提示表'
        verbose_name_plural = verbose_name


class DeviceTypeModel(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=64, default='', verbose_name='设备名称')
    model = models.SmallIntegerField(default=0, verbose_name='设备大类。1：dvr，2：ipc')
    type = models.IntegerField(default=0, verbose_name='设备类型')
    ptz_type = models.IntegerField(default=0, verbose_name='ptz类型。1：dvr球机，2：IPCPTZ，3：同轴菜单')
    icon = models.CharField(max_length=120, default='', verbose_name='文件名')
    add_time = models.IntegerField(default=0, verbose_name='添加时间')
    update_time = models.IntegerField(default=0, verbose_name='更新时间')

    class Meta:
        db_table = 'device_type'
        verbose_name = '设备类型表'
        verbose_name_plural = verbose_name


class OperatingLogsModel(models.Model):
    id = models.AutoField(primary_key=True)
    userId = models.CharField(blank=False, max_length=32, db_index=True, verbose_name=u'操作人')
    operatingtime = models.DateTimeField(blank=True, verbose_name=u'操作时间', auto_now=True)
    operatingcontent = models.TextField(blank=True, default='', verbose_name=u'操作内容')
    operatingtype = models.CharField(blank=True, verbose_name=u'操作类型', max_length=32)
    is_saveprocessinfo = models.SmallIntegerField(default=1, verbose_name=u'是否保存过程信息。1：保存，2：不保存')
    devUid = models.CharField(default='', blank=True, max_length=32, verbose_name=u'设备ID')
    userIp = models.CharField(blank=True, max_length=20, default='', verbose_name=u'用户ip')

    class Meta:
        db_table = 'operating_logs'
        verbose_name = '操作数据表'
        verbose_name_plural = verbose_name


class ProcessInfoLogsModel(models.Model):
    id = models.AutoField(primary_key=True)
    userId = models.CharField(blank=False, max_length=32, db_index=True, verbose_name=u'操作人')
    devUid = models.CharField(default='', blank=True, max_length=32, verbose_name=u'设备ID')
    operatingcontent = models.TextField(blank=True, default='', verbose_name=u'操作内容')
    operatingtime = models.DateTimeField(blank=True, verbose_name=u'操作时间', auto_now=True)

    class Meta:
        db_table = 'processinfo_logs'
        verbose_name = '过程信息表'
        verbose_name_plural = verbose_name

class EquipmentLogModel(models.Model):
    id = models.AutoField(primary_key=True)
    user = models.CharField(blank=False, max_length=32, db_index=True, verbose_name=u'操作用户')
    uid = models.CharField(default='', blank=True, max_length=32, verbose_name=u'设备uid')
    equipmentid = models.CharField(blank=False, max_length=100, db_index=True, verbose_name=u'设备主键id')
    NickName = models.CharField(blank=True, max_length=32, verbose_name=u'设备名称')
    ip = models.CharField(max_length=100, default='', db_index=True, blank=True, verbose_name=u'访问ip地址')
    time = models.DateTimeField(null=True, blank=True, db_index=True, verbose_name=u'访问时间')
    operatingcontent = models.TextField(blank=True, default='', verbose_name=u'操作内容')
    url = models.CharField(max_length=150, default='', blank=True, verbose_name=u'访问路径')
    class Meta:
        db_table = 'equipment_log'
        verbose_name = '设备日志表'
        verbose_name_plural = verbose_name


class UserModel(models.Model):
    id = models.AutoField(primary_key=True)
    username = models.CharField(unique=True, max_length=128, null=False, verbose_name='用户名')
    password = models.CharField(max_length=128, null=False, verbose_name='密码')
    permission = models.CharField(max_length=24, null=False, verbose_name='权限')
    login_time = models.IntegerField(blank=True, default=0, verbose_name='登录时间')
    online = models.SmallIntegerField(default=0, verbose_name='是否在线')
    phone = models.CharField(max_length=11, default='', verbose_name='手机号')
    add_time = models.IntegerField(default=0, verbose_name='添加时间')

    class Meta:
        verbose_name = '用户表'
        verbose_name_plural = verbose_name
        db_table = 'user'


class CompanyModel(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=64, unique=True, default='', verbose_name='企业名称')
    mark = models.CharField(max_length=3, unique=True, default='', verbose_name='企业标志，用于序列号的后三位')
    secret = models.CharField(max_length=10, db_index=True, default='', verbose_name='企业秘钥')
    quantity = models.IntegerField(default=0, verbose_name='已购买的序列号的数量')
    add_time = models.IntegerField(default=0, verbose_name='添加时间')
    update_time = models.IntegerField(default=0, verbose_name='更新时间')

    class Meta:
        db_table = 'tb_company'
        verbose_name = '企业表'
        verbose_name_plural = verbose_name


class RegionModel(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=32, default='', verbose_name='区域名称')
    continent_code = models.CharField(max_length=3, default='', verbose_name='洲代码')
    api = models.CharField(max_length=50, default='', verbose_name='请求地址')


    class Meta:
        db_table = 'tb_region'
        verbose_name = '区域表'
        verbose_name_plural = verbose_name


class VPGModel(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=32, default='', verbose_name='群组名称')
    uid_count = models.IntegerField(default=0, verbose_name='uid数量')
    region = models.ForeignKey(RegionModel, to_field='id', on_delete=models.DO_NOTHING, verbose_name='群组用于的地区')
    company = models.ForeignKey(CompanyModel, to_field='id', on_delete=models.DO_NOTHING, verbose_name='关联企业表')
    add_time = models.IntegerField(default=0, verbose_name='添加时间')
    update_time = models.IntegerField(default=0, verbose_name='更新时间')

    class Meta:
        db_table = 'tb_vpg'
        verbose_name = 'vpg表'
        verbose_name_plural = verbose_name


class UIDModel(models.Model):
    id = models.AutoField(primary_key=True)
    uid = models.CharField(max_length=20, null=False, db_index=True, unique=True, verbose_name='设备id')
    mac = models.CharField(max_length=17, null=False, default='', unique=True, verbose_name='设备id对应的mac地址')
    uid_extra = models.TextField(default='', verbose_name='uid的额外描述')
    status = models.SmallIntegerField(default=0, verbose_name='使用状态')   # 0:未分配,1:已分配,2:已使用
    add_time = models.IntegerField(default=0, verbose_name='添加时间')
    update_time = models.IntegerField(default=0, verbose_name='更新时间')
    area = models.SmallIntegerField(default=0, verbose_name='区域')  #0：国内；1：国外
    vpg = models.ForeignKey(VPGModel, to_field='id', default=1, on_delete=models.DO_NOTHING, verbose_name='关联VPG表的id')
    p2p_type = models.IntegerField(default=1, verbose_name='p2p类型。1：宸云，2：tutk')
    full_uid_code = models.CharField(max_length=256, default='', verbose_name='宸云完整uid')

    class Meta:
        ordering = ('-add_time',)
        verbose_name = 'uid表'
        verbose_name_plural = verbose_name
        db_table = 'uid'


class UserUIDModel(models.Model):
    id = models.AutoField(primary_key=True)
    user = models.ForeignKey(UserModel, to_field='id', on_delete=models.CASCADE, verbose_name='关联用户表id')
    uid = models.ForeignKey(UIDModel, to_field='id', on_delete=models.CASCADE, verbose_name='关联uid表id')
    add_time = models.IntegerField(default=0, verbose_name='添加时间')
    update_time = models.IntegerField(default=0, verbose_name='更新时间')

    class Meta:
        verbose_name = '用户与UID的关联表'
        verbose_name_plural = verbose_name
        db_table = 'user_uid'


class PermissionModel(models.Model):
    id = models.AutoField(primary_key=True)
    type = models.SmallIntegerField(null=False, verbose_name='权限枚举')
    description = models.TextField(default='', verbose_name='权限描述')

    class Meta:
        verbose_name = '权限表'
        verbose_name_plural = verbose_name
        db_table = 'permission'


class LogModel(models.Model):
    id = models.AutoField(primary_key=True)
    operation = models.CharField(max_length=100, default='', verbose_name='操作描述')
    time = models.IntegerField(default=0, verbose_name='操作时间')
    ip = models.CharField(default='', max_length=24, verbose_name='用户ip')
    user = models.ForeignKey(UserModel, to_field='id', on_delete=models.CASCADE, verbose_name='关联用户id')
    content = models.TextField(default='', verbose_name='请求参数')
    status = models.IntegerField(default=0, verbose_name='请求状态')
    url = models.CharField(max_length=150, default='', verbose_name='请求路径')

    class Meta:
        ordering = ('-time',)
        verbose_name = '日志表'
        verbose_name_plural = verbose_name
        db_table = 'log'


class HistoryUIDModel(models.Model):
    id = models.AutoField(primary_key=True)
    uid = models.CharField(max_length=20, null=False, db_index=True, verbose_name='设备id')
    uid_extra = models.TextField(default='', verbose_name='uid的额外描述')
    status = models.SmallIntegerField(default=0, verbose_name='使用状态')
    add_time = models.IntegerField(default=0, verbose_name='添加时间')
    update_time = models.IntegerField(default=0, verbose_name='更新时间')
    area = models.SmallIntegerField(default=0, verbose_name='区域')  # 0：国内；1：国外

    class Meta:
        verbose_name = '历史UID表'
        verbose_name_plural = verbose_name
        db_table = 'history_uid'


class MacModel(models.Model):
    id = models.AutoField(primary_key=True)
    value = models.CharField(max_length=17, null=False, verbose_name='最新的mac地址')
    is_active = models.BooleanField(default=True, verbose_name='当前mac地址是否可用')
    add_time = models.IntegerField(default=0, verbose_name='添加时间')
    update_time = models.IntegerField(default=0, verbose_name='更新时间')

    class Meta:
        verbose_name = '最新的mac地址表'
        verbose_name_plural = verbose_name
        db_table = 'tb_mac'


class OrderTaskModel(models.Model):
    id = models.AutoField(primary_key=True)
    order_number = models.CharField(max_length=32, verbose_name='订单号')
    board = models.CharField(max_length=32, verbose_name='主板')
    plan = models.CharField(max_length=32, verbose_name='配置方案')
    checksum = models.CharField(max_length=32, verbose_name='校验和')
    ic_model = models.CharField(max_length=32, verbose_name='ic型号')
    quantity = models.IntegerField(default=0, verbose_name='通知单上的生成数量')
    add_time = models.IntegerField(default=0, verbose_name='添加时间')

    class Meta:
        db_table = 'order_task'
        verbose_name = '订单任务表'
        verbose_name_plural = verbose_name


class OrderUIDModel(models.Model):
    id = models.AutoField(primary_key=True)
    uid = models.ForeignKey(UIDModel, to_field='id', on_delete=models.CASCADE, verbose_name='关联uid表')
    order = models.ForeignKey(OrderTaskModel, to_field='id', on_delete=models.CASCADE, verbose_name='关联订单任务表')
    add_time = models.IntegerField(default=0, verbose_name='添加时间')
    update_time = models.IntegerField(default=0, verbose_name='更新时间')

    class Meta:
        db_table = 'order_uid'
        verbose_name = '订单与uid关联表'
        verbose_name_plural = verbose_name


class CountryModel(models.Model):
    id = models.AutoField(primary_key=True)
    number = models.IntegerField(unique=True, verbose_name='区号')
    add_time = models.IntegerField(default=0, verbose_name='添加时间')
    update_time = models.IntegerField(default=0, verbose_name='更新时间')
    region = models.ForeignKey(RegionModel, to_field='id', on_delete=models.DO_NOTHING, verbose_name='关联区域表')
    country_code = models.CharField(max_length=2, default='', verbose_name='国家iso2代码')
    country_name = models.CharField(max_length=20, default='', verbose_name='国家名')

    class Meta:
        db_table = 'tb_country'
        verbose_name = '地区表'
        verbose_name_plural = verbose_name


class LanguageModel(models.Model):
    id = models.AutoField(primary_key=True)
    lang = models.CharField(max_length=16, default='', verbose_name='语种')
    lang_youdao = models.CharField(max_length=16, default='', verbose_name='有道的语种')
    add_time = models.IntegerField(default=0, verbose_name='添加时间')
    update_time = models.IntegerField(default=0, verbose_name='更新时间')

    class Meta:
        db_table = 'tb_language'
        verbose_name = '语言表'
        verbose_name_plural = verbose_name


class CountryLanguageModel(models.Model):
    id = models.AutoField(primary_key=True)
    country_name = models.CharField(max_length=64, default='', verbose_name='国家名称')
    language = models.ForeignKey(LanguageModel, to_field='id', on_delete=models.CASCADE, verbose_name='关联语言表的id')
    country = models.ForeignKey(CountryModel, to_field='id', on_delete=models.CASCADE, verbose_name='关联地区表的id')
    add_time = models.IntegerField(default=0, verbose_name='添加时间')
    update_time = models.IntegerField(default=0, verbose_name='更新时间')

    class Meta:
        db_table = 'tb_country_language'
        verbose_name = '不同语言的地区名称表'
        verbose_name_plural = verbose_name


class SerialNumberModel(models.Model):
    id = models.AutoField(primary_key=True)
    serial_number = models.CharField(max_length=9, db_index=True, unique=True, verbose_name='序列号')
    status = models.SmallIntegerField(default=1, verbose_name='可用状态。0：不可用，1：可用')
    use_status = models.SmallIntegerField(default=0, db_index=True, verbose_name='使用状态, 0: 未使用, 1: 已分配')
    p2p = models.SmallIntegerField(default=0, db_index=True, verbose_name='p2p类型。0：无，1：宸云，2：tutk')
    add_time = models.IntegerField(default=0, verbose_name='添加时间')

    class Meta:
        db_table = 'tb_serial_number'
        verbose_name = '序列号表'
        verbose_name_plural = verbose_name


class CompanySerialModel(models.Model):
    id = models.AutoField(primary_key=True)
    company = models.ForeignKey(CompanyModel, to_field='id', on_delete=models.CASCADE, verbose_name='关联企业表的id')
    serial_number = models.CharField(max_length=11, db_index=True, blank=True, default='', verbose_name=u'6位数序列号')
    status = models.SmallIntegerField(default=0, verbose_name='序列号的状态。0：未使用，1：已使用，2：已和UID关联')
    p2p = models.SmallIntegerField(default=0, verbose_name='p2p类型。0：无，1：宸云，2：tutk')
    add_time = models.IntegerField(default=0, verbose_name='添加时间')
    update_time = models.IntegerField(default=0, verbose_name='更新时间')

    class Meta:
        db_table = 'tb_company_serial'
        verbose_name = '企业关联序列号表'
        verbose_name_plural = verbose_name


class UIDCompanySerialModel(models.Model):
    id = models.AutoField(primary_key=True)
    uid = models.ForeignKey(UIDModel, to_field='id', on_delete=models.CASCADE, verbose_name='关联UID表')
    company_serial = models.ForeignKey(CompanySerialModel, to_field='id', on_delete=models.CASCADE, verbose_name='6位数序列号')
    add_time = models.IntegerField(default=0, verbose_name='添加时间')
    update_time = models.IntegerField(default=0, verbose_name='更新时间')

    class Meta:
        db_table = 'tb_uid_company_serial'
        verbose_name = 'UID关联【企业关联序列号】表'
        verbose_name_plural = verbose_name


class iotdeviceInfoModel(models.Model):
    id = models.AutoField(primary_key=True)
    serial_number = models.CharField(max_length=11, blank=True, default='', db_index=True, verbose_name=u'关联Device_Info表的序列号')
    uid = models.CharField(blank=True, max_length=32, default='', db_index=True, verbose_name=u'设备UID')
    certificate_id = models.CharField(blank=True, max_length=256, default='', verbose_name=u'证书id')
    certificate_pem = models.TextField(blank=True, default='', verbose_name=u'证书项目')
    public_key = models.TextField(blank=True, default='', verbose_name=u'公有密钥')
    private_key = models.TextField(blank=True, default='', verbose_name=u'私有密钥')
    thing_name = models.CharField(blank=True, max_length=256, default='', verbose_name=u'IoT Thing Name')
    thing_groups = models.CharField(blank=True, max_length=256, default='', verbose_name=u'IoT Thing Groups')
    endpoint = models.CharField(blank=True, max_length=256, db_index=True, default='', verbose_name=u'iot端点')
    token_iot_number = models.CharField(blank=True,  db_index=True ,default='', max_length=50, verbose_name='连接iot令牌')
    class Meta:
        db_table = 'iot_deviceInfo'
        verbose_name = 'iot设备信息表'
        verbose_name_plural = verbose_name


class UIDMainUser(models.Model):
    id = models.AutoField(primary_key=True)
    UID = models.CharField(blank=True, max_length=32, verbose_name=u'设备UID', default='')
    user_id = models.CharField(blank=True, max_length=32, verbose_name=u'用户ID', default='')
    class Meta:
        db_table = 'uid_mainuser'
        verbose_name = '设备主用户表'
        verbose_name_plural = verbose_name