from django.db import models

# Create your models here.


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 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='', verbose_name='设备id对应的mac地址')
    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:
        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 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: 已分配  2: 已下载')
    p2p = models.SmallIntegerField(default=0, 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 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 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 OrderSerialNumberModel(models.Model):
    id = models.AutoField(primary_key=True)
    serial_number = models.ForeignKey(SerialNumberModel, to_field='id', on_delete=models.CASCADE, verbose_name='关联序列号表')
    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_serial_number'
        verbose_name = '订单与序列号关联表'
        verbose_name_plural = verbose_name


class UserSerialNumberModel(models.Model):
    id = models.AutoField(primary_key=True)
    user = models.ForeignKey(UserModel, to_field='id', on_delete=models.CASCADE, verbose_name='关联用户表id')
    serial_number = models.ForeignKey(SerialNumberModel, 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:
        verbose_name = '用户与序列号的关联表'
        verbose_name_plural = verbose_name
        db_table = 'user_serial_number'