import django
from django.core.exceptions import ValidationError
from django.db import models
from django.utils import timezone
from datetime import timedelta,datetime

from django.utils.translation import gettext_lazy as _
from django.db.models import Q
# Create your models here.
DefaultStatus = [
    (5, "使用中"),
    (6, "已停用"),
    (4, "已删除"),
]

UseStatus = [
    (5, "已用中"),
    (6, "未使用"),
]

ParamsType = [
    (1, "变量"),
    (2, "常量"),
]
ParamsValueType = [
    (1, "输入框"),
    (2, "下拉选择"),
    (3, "小数"),
    (4, "百分比"),
    (5, "分数")
]
YESOrNO = [
    (0, "否"),
    (1, "是"),
]
def eight_hours_from_now():
    return timezone.now() + timedelta(hours=8)


# 管理员
class Admin(models.Model):
    id = models.AutoField(primary_key=True)
    username = models.CharField(max_length=60, verbose_name="登录用户名", unique=True)
    password = models.CharField(max_length=255, verbose_name="登录密码")
    mobile = models.CharField(max_length=20, verbose_name="手机号码")
    email = models.CharField(max_length=60, verbose_name="邮件地址",blank=True,null=True)
    real_name = models.CharField(max_length=255, verbose_name="真实姓名", blank=True,null=True)
    token = models.CharField(max_length=120, verbose_name="登录授权码", default="")
    role_id = models.IntegerField(verbose_name='所属角色', default=0)
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now)
    last_login_time = models.DateTimeField(verbose_name="最后登录时间", default=eight_hours_from_now)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态")
    header = models.CharField(max_length=255, verbose_name="头像", blank=True,null=True)
    code = models.CharField(max_length=255, verbose_name="工号", blank=True,null=True)
    job = models.CharField(max_length=255, verbose_name="职位", blank=True,null=True)
    contact = models.CharField(max_length=255, verbose_name="联系方式", blank=True,null=True)
    verification_code = models.CharField(max_length=255, verbose_name="验证码", blank=True,null=True)

    class Meta:
        db_table = 'dc_admin'
        verbose_name = "管理员信息"
        verbose_name_plural = "管理员信息"

    def __int__(self):
        return self.id


# 角色
class AdminRole(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=60, verbose_name="角色名称")
    intro = models.CharField(max_length=255, verbose_name="角色介绍",blank=True,null=True)
    is_admin = models.SmallIntegerField(verbose_name="超管标识", default=2)
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态")

    class Meta:
        db_table = 'dc_admin_role'
        verbose_name = "管理员角色"
        verbose_name_plural = "管理员角色信息"
    def clean(self):
        print("开始进行校验")
        super().clean()
        # 检查是否存在同名且状态不为4的记录
        if AdminRole.objects.filter(name=self.name).exclude(status=4).exclude(pk=self.pk).exists():
            print("校验返回")
            raise ValidationError({'name': _("已存在同名,请修改后重试。")})

    def save(self, *args, **kwargs):
        self.full_clean()
        super().save(*args, **kwargs)
    def __str__(self):
        return self.name




# 客户
class Customer(models.Model):
    id = models.AutoField(primary_key=True)
    code = models.CharField(max_length=60, verbose_name="客户编码", unique=True)
    name = models.CharField(max_length=255, verbose_name="客户名称")
    en_name = models.CharField(max_length=255, verbose_name="英文名称")
    mobile = models.CharField(max_length=255, verbose_name="手机号码")
    contact = models.CharField(max_length=60, verbose_name="联系人")
    address = models.CharField(max_length=255, verbose_name="联系地址")
    other = models.CharField(max_length=255, verbose_name="其它信息")
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态")

    class Meta:
        db_table = 'dc_customer'
        verbose_name = "客户信息"
        verbose_name_plural = "客户信息"
    def clean(self):
        super().clean()
        # 检查是否存在同名且状态不为4的记录
        if Customer.objects.filter(name=self.name).exclude(status=4).exclude(pk=self.pk).exists():
            raise ValidationError({'name': _("已存在同名,请修改后重试。")})

    def save(self, *args, **kwargs):
        self.full_clean()
        super().save(*args, **kwargs)

    def __str__(self):
        return self.name


# 项目分类
class ProjectCategory(models.Model):
    id = models.AutoField(primary_key=True)
    code = models.CharField(max_length=60, verbose_name="客户编码")
    name = models.CharField(max_length=255, verbose_name="客户名称")
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态")

    class Meta:
        db_table = 'dc_project_category'
        verbose_name = "项目分类"
        verbose_name_plural = "项目分类"

    def clean(self):
        super().clean()
        # 检查是否存在同名且状态不为4的记录
        if ProjectCategory.objects.filter(name=self.name).exclude(status=4).exclude(pk=self.pk).exists():
            raise ValidationError({'name': _("已存在同名,请修改后重试。")})

    def save(self, *args, **kwargs):
        self.full_clean()
        super().save(*args, **kwargs)

    def __str__(self):
        return self.name


# 项目信息
class Project(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, verbose_name="客户名称")
    intro = models.TextField(verbose_name="客户编码")
    customer_id = models.IntegerField(verbose_name="客户id")
    category_id = models.IntegerField(verbose_name="所属分类id")
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态")

    class Meta:
        db_table = 'dc_project'
        verbose_name = "项目信息"
        verbose_name_plural = "项目信息"

    def clean(self):
        super().clean()
        # 检查是否存在同名且状态不为4的记录
        if Project.objects.filter(name=self.name).exclude(status=4).exclude(pk=self.pk).exists():
            raise ValidationError({'name': _("已存在同名,请修改后重试。")})

    def save(self, *args, **kwargs):
        self.full_clean()
        super().save(*args, **kwargs)

    def __str__(self):
        return self.name


# 项目相关文档
class ProjectDocument(models.Model):
    id = models.AutoField(primary_key=True)
    project_id = models.IntegerField(verbose_name="项目id")
    doc_id = models.IntegerField(verbose_name="文档id")
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态")

    class Meta:
        db_table = 'dc_project_document'
        verbose_name = "项目相关文档"
        verbose_name_plural = "项目相关文档"

    def __int__(self):
        return self.id


# 文档
class Document(models.Model):
    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=255, verbose_name="文档标题")
    data = models.TextField(verbose_name="文档内容")
    user_id = models.IntegerField(verbose_name="用户id", default=0)
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now)
    update_time = models.DateTimeField(verbose_name="更新时间", default=eight_hours_from_now)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态")
    category_id = models.IntegerField(verbose_name="所属文档分类", default=0, blank=True,null=True)
    is_template = models.SmallIntegerField(choices=YESOrNO, verbose_name="是否为模板  0否  1是",default=1)

    class Meta:
        db_table = 'dc_document'
        verbose_name = "文档"
        verbose_name_plural = "文档"


    def __str__(self):
        return self.title


# 文档
class SourceData(models.Model):
    id = models.AutoField(primary_key=True)
    code = models.CharField(max_length=60, unique=True, verbose_name="数据代码")
    name = models.TextField(verbose_name="数据名称")
    file_name = models.TextField(verbose_name="文件名称", default="",blank=True, null=True)
    source_data = models.TextField(verbose_name="文件内容", default="")
    create_time = models.DateTimeField(verbose_name="创建日期", default=django.utils.timezone.now)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态")
    category_id = models.IntegerField(verbose_name="所属文档分类", default=0)

    class Meta:
        db_table = 'dc_source_data'
        verbose_name = "原始数据"
        verbose_name_plural = "原始数据"

    def clean(self):
        super().clean()
        # 检查是否存在同名且状态不为4的记录
        if SourceData.objects.filter(name=self.name).exclude(status=4).exclude(pk=self.pk).exists():
            raise ValidationError({'name': _("已存在同名,请修改后重试。")})

    def save(self, *args, **kwargs):
        self.full_clean()
        super().save(*args, **kwargs)

    def __str__(self):
        return self.name


class SourceCategory(models.Model):
    name = models.CharField(max_length=100, verbose_name="分类名称")
    status = models.CharField(max_length=20, choices=[
        ('active', '启用'),
        ('inactive', '禁用')
    ], default='active', verbose_name="分类状态")
    created_at = models.DateTimeField(default=eight_hours_from_now, verbose_name="创建时间")

    class Meta:
        db_table = 'dc_source_data_category'
        verbose_name = "原始数据分类"
        verbose_name_plural = "原始数据分类"

    def clean(self):
        super().clean()
        # 检查是否存在同名且状态不为4的记录
        if SourceCategory.objects.filter(name=self.name).exclude(status=4).exclude(pk=self.pk).exists():
            raise ValidationError({'name': _("已存在同名,请修改后重试。")})

    def save(self, *args, **kwargs):
        self.full_clean()
        super().save(*args, **kwargs)

    def __str__(self):
        return self.name

# 文档分类
class DocumentCategory(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, verbose_name="文档分类名称")
    intro = models.TextField(verbose_name="文档分类介绍")
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态")

    class Meta:
        db_table = 'dc_document_category'
        verbose_name = "文档分类"
        verbose_name_plural = "文档分类"

    def clean(self):
        super().clean()
        # 检查是否存在同名且状态不为4的记录
        if DocumentCategory.objects.filter(name=self.name).exclude(status=4).exclude(pk=self.pk).exists():
            raise ValidationError({'name': _("已存在同名,请修改后重试。")})

    def save(self, *args, **kwargs):
        self.full_clean()
        super().save(*args, **kwargs)

    def __str__(self):
        return self.name


# 品牌信息
class Brand(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, verbose_name="品牌名称")
    intro = models.TextField(verbose_name="品牌介绍")
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态")

    class Meta:
        db_table = 'dc_brand'
        verbose_name = "品牌信息"
        verbose_name_plural = "品牌信息"

    def clean(self):
        super().clean()
        # 检查是否存在同名且状态不为4的记录
        if Brand.objects.filter(name=self.name).exclude(status=4).exclude(pk=self.pk).exists():
            raise ValidationError({'name': _("已存在同名,请修改后重试。")})

    def save(self, *args, **kwargs):
        self.full_clean()
        super().save(*args, **kwargs)

    def __str__(self):
        return self.name


# 供应商
class Supplier(models.Model):
    id = models.AutoField(primary_key=True)
    code = models.CharField(max_length=255, verbose_name="供应商编码", unique=True)
    name = models.CharField(max_length=255, verbose_name="供应商名称")
    intro = models.TextField(verbose_name="供应商介绍")
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态")

    class Meta:
        db_table = 'dc_supplier'
        verbose_name = "供应商信息"
        verbose_name_plural = "供应商信息"

    def clean(self):
        super().clean()
        # 检查是否存在同名且状态不为4的记录
        if Supplier.objects.filter(name=self.name).exclude(status=4).exclude(pk=self.pk).exists():
            raise ValidationError({'name': _("已存在同名,请修改后重试。")})

    def save(self, *args, **kwargs):
        self.full_clean()
        super().save(*args, **kwargs)

    def __str__(self):
        return self.name


# 模板分类
class TemplateCategory(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, verbose_name="文档分类名称")
    intro = models.TextField(verbose_name="文档分类介绍",blank=True,null=True)
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态")
    parent_id = models.CharField(max_length=100,default="0", verbose_name="父类id")

    class Meta:
        db_table = 'dc_template_category'
        verbose_name = "模板分类"
        verbose_name_plural = "模板分类"

    def clean(self):
        super().clean()
        # 检查是否存在同名、状态不为4、parent_id不为0的记录
        if TemplateCategory.objects.filter(
            Q(name=self.name),
            Q(parent_id="0"),  # 确保 parent_id 不等于 "0"
            ~Q(status=4),
            ~Q(pk=self.pk)
        ).exists():
            raise ValidationError({'name': _("已存在同名且非顶级的分类,请修改后重试。")})

    def save(self, *args, **kwargs):
        self.full_clean()
        super().save(*args, **kwargs)

    def __str__(self):
        return self.name


# 模板信息
class Template(models.Model):
    id = models.AutoField(primary_key=True)
    code = models.CharField(max_length=255, verbose_name="模板编码",blank=True,null=True)
    name = models.CharField(max_length=255, verbose_name="模板分类名称")
    intro = models.TextField(verbose_name="介绍", default="",blank=True,null=True)
    lay_id = models.CharField(max_length=100, default="", verbose_name="图层id",blank=True,null=True)
    type = models.CharField(max_length=100, default="", verbose_name="类型",blank=True,null=True)
    content = models.TextField(verbose_name="模板内容", default="",blank=True,null=True)
    attrs = models.TextField(verbose_name="属性", default="[]",blank=True,null=True)
    category_id = models.IntegerField(verbose_name="所属分类")
    admin_id = models.IntegerField(verbose_name="管理员id", default=0)
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态")

    class Meta:
        db_table = 'dc_template'
        verbose_name = "模板信息"
        verbose_name_plural = "模板信息"

    def clean(self):
        super().clean()
        # 检查是否存在同名且状态不为4的记录
        if Template.objects.filter(name=self.name).exclude(status=4).exclude(pk=self.pk).exists():
            raise ValidationError({'name': _("已存在同名,请修改后重试。")})

    def save(self, *args, **kwargs):
        self.full_clean()
        super().save(*args, **kwargs)

    def __str__(self):
        return self.name


# 模板分类
class ImageCategory(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, verbose_name="图片分类名称")
    intro = models.TextField(verbose_name="图片分类介绍")
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态")

    class Meta:
        db_table = 'dc_image_category'
        verbose_name = "图片分类"
        verbose_name_plural = "图片分类"

    def clean(self):
        super().clean()
        # 检查是否存在同名且状态不为4的记录
        if ImageCategory.objects.filter(name=self.name).exclude(status=4).exclude(pk=self.pk).exists():
            raise ValidationError({'name': _("已存在同名,请修改后重试。")})

    def save(self, *args, **kwargs):
        self.full_clean()
        super().save(*args, **kwargs)

    def __str__(self):
        return self.name


# 模板分类
class Image(models.Model):
    id = models.AutoField(primary_key=True)
    category_id = models.IntegerField(verbose_name="图片分类")
    name = models.CharField(max_length=255, verbose_name="图片信息")
    width = models.CharField(max_length=60, verbose_name="图片宽度")
    height = models.CharField(max_length=255, verbose_name="图片高度")
    path = models.CharField(max_length=255, verbose_name="图片路径")
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态")

    class Meta:
        db_table = 'dc_image'
        verbose_name = "图片信息"
        verbose_name_plural = "图片信息"

    def clean(self):
        super().clean()
        # 检查是否存在同名且状态不为4的记录
        if Image.objects.filter(name=self.name).exclude(status=4).exclude(pk=self.pk).exists():
            raise ValidationError({'name': _("已存在同名,请修改后重试。")})

    def save(self, *args, **kwargs):
        self.full_clean()
        super().save(*args, **kwargs)

    def __str__(self):
        return self.name


# 系统参数配置
class Params(models.Model):
    id = models.AutoField(primary_key=True)
    code = models.CharField(max_length=255, verbose_name="参数代码", unique=True)
    name = models.CharField(max_length=255, verbose_name="参数名称")
    value = models.CharField(max_length=255, verbose_name="参数值",blank=True,null=True)
    value_type = models.SmallIntegerField(verbose_name="变量类型", choices=ParamsValueType, default=1)
    value_item = models.CharField(max_length=255, verbose_name="取值范围",blank=True,null=True)
    intro = models.CharField(max_length=255, verbose_name="参数介绍",blank=True,null=True)
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now)
    type = models.SmallIntegerField(verbose_name="状态", choices=ParamsType, default=1)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态")

    class Meta:
        db_table = 'dc_params'
        verbose_name = "参数配置"
        verbose_name_plural = "参数配置"

    def clean(self):
        super().clean()
        # 检查是否存在同名且状态不为4的记录
        if Params.objects.filter(name=self.name).exclude(status=4).exclude(pk=self.pk).exists():
            raise ValidationError({'name': _("已存在同名,请修改后重试。")})

    def save(self, *args, **kwargs):
        self.full_clean()
        super().save(*args, **kwargs)

    def __str__(self):
        return self.name


# 公式管理
class Formula(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, verbose_name="公式名称")
    formula = models.TextField(max_length=255, verbose_name="公式内容")
    intro = models.CharField(max_length=255, verbose_name="公式介绍",null=True, blank=True)
    params = models.CharField(max_length=255, verbose_name="公式参数")
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now)
    use_status = models.SmallIntegerField(choices=UseStatus, verbose_name="用户状态", default=5)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态", default=5)
    point = models.SmallIntegerField(verbose_name="小数点标识",default=0)


    class Meta:
        db_table = 'dc_formula'
        verbose_name = "公式管理"
        verbose_name_plural = "公式管理"

    def clean(self):
        super().clean()
        # 检查是否存在同名且状态不为4的记录
        if Formula.objects.filter(name=self.name).exclude(status=4).exclude(pk=self.pk).exists():
            raise ValidationError({'name': _("已存在同名,请修改后重试。")})

    def save(self, *args, **kwargs):
        self.full_clean()
        super().save(*args, **kwargs)

    def __str__(self):
        return self.name


class Product(models.Model):
    id = models.AutoField(primary_key=True)
    category_id = models.IntegerField(verbose_name="分类信息")
    brand_id = models.IntegerField(verbose_name="所属品牌")
    supplier_id = models.IntegerField(verbose_name="所属供应商")
    name = models.CharField(max_length=255, verbose_name="产品名称")
    intro = models.CharField(max_length=255, verbose_name="产品介绍")
    price = models.CharField(max_length=255, verbose_name="价格")
    product_area = models.CharField(max_length=255, verbose_name="产地")
    is_safe = models.CharField(max_length=255, verbose_name="是否质保")
    safe_time = models.CharField(max_length=255, verbose_name="质保期限", default="")
    is_package = models.CharField(max_length=255, verbose_name="是否为套餐商品")
    package_intro = models.CharField(max_length=255, verbose_name="套餐说明", default="")
    qualifications = models.CharField(max_length=255, verbose_name="产品资质")
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态", default=5)
    attrs = models.TextField(verbose_name="产品资质", default="")

    class Meta:
        db_table = 'dc_product'
        verbose_name = "产品信息"
        verbose_name_plural = "产品信息"

    def clean(self):
        super().clean()
        # 检查是否存在同名且状态不为4的记录
        if Product.objects.filter(name=self.name).exclude(status=4).exclude(pk=self.pk).exists():
            raise ValidationError({'name': _("已存在同名,请修改后重试。")})

    def save(self, *args, **kwargs):
        self.full_clean()
        super().save(*args, **kwargs)

    def __str__(self):
        return self.name


# 模板分类
class ProductCategory(models.Model):
    id = models.AutoField(primary_key=True)
    parent_id = models.IntegerField(verbose_name="所属父级")
    name = models.CharField(max_length=255, verbose_name="分类名称")
    attrs = models.TextField(verbose_name="属性配置")
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态")

    class Meta:
        db_table = 'dc_product_category'
        verbose_name = "产品分类"
        verbose_name_plural = "产品分类"

    def clean(self):
        super().clean()
        # 检查是否存在同名且状态不为4的记录
        if ProductCategory.objects.filter(name=self.name).exclude(status=4).exclude(pk=self.pk).exists():
            raise ValidationError({'name': _("已存在同名,请修改后重试。")})

    def save(self, *args, **kwargs):
        self.full_clean()
        super().save(*args, **kwargs)

    def __str__(self):
        return self.name


# 模板分类
class ProductDocument(models.Model):
    id = models.AutoField(primary_key=True)
    product_id = models.IntegerField(verbose_name="相关产品")
    document_id = models.IntegerField(verbose_name="文档id")
    create_time = models.DateTimeField(verbose_name="创建日期", default=django.utils.timezone.now)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态")

    class Meta:
        db_table = 'dc_product_document'
        verbose_name = "产品相关文档"
        verbose_name_plural = "产品相关文档"


    def __int__(self):
        return self.id
#用户菜单表
class AuthMenu(models.Model):
    id = models.AutoField(primary_key=True)
    code = models.CharField(max_length=255, verbose_name="菜单code")
    name = models.CharField(max_length=255, verbose_name="菜单名称")
    parent_id = models.IntegerField(verbose_name="父级id",null=True, blank=True,default=0)
    path = models.CharField(max_length=255, verbose_name="权限路径",default='')
    type = models.CharField(max_length=255, verbose_name="type",default='')
    sort = models.IntegerField(verbose_name="排序",default=0)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态",default=5)
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now,null=True, blank=True)

    class Meta:
        db_table = 'auth_menu'
        verbose_name = "用户菜单表"
        verbose_name_plural = "用户菜单表"

    def clean(self):
        super().clean()
        # 检查是否存在同名且状态不为4的记录
        if AuthMenu.objects.filter(name=self.name).exclude(status=4).exclude(pk=self.pk).exists():
            raise ValidationError({'name': _("已存在同名,请修改后重试。")})

    def save(self, *args, **kwargs):
        self.full_clean()
        super().save(*args, **kwargs)

    def __str__(self):
        return self.name
#用户菜单关联表
class AuthUserMenu(models.Model):
    id = models.AutoField(primary_key=True)
    rola_id = models.IntegerField(verbose_name="角色id",default=0)
    menu_id = models.IntegerField(verbose_name="菜单id")

    class Meta:
        db_table = 'auth_user_menu'
        verbose_name = "用户菜单关联表"
        verbose_name_plural = "用户菜单关联表"


    def __str__(self):
        return self.name

    # 模块分类用户关联表
class TemplateCateGoryUser(models.Model):
    id = models.AutoField(primary_key=True)
    rola_id = models.IntegerField(verbose_name="角色id",default=0)
    category_id = models.IntegerField(verbose_name="模块分类id")
    action = models.CharField(max_length=255, verbose_name="权限类型",default=0)
    value = models.CharField(max_length=255, verbose_name="权限code",default=0)
    type = models.CharField(max_length=255, verbose_name="归属分类",default=0)
    name = models.CharField(max_length=255, verbose_name="权限名称",default=0)
    label = models.CharField(max_length=255, verbose_name="权限label",default=0)
    isMain = models.CharField(max_length=255, verbose_name="权限label",default=0)

    class Meta:
        db_table = 'dc_template_category_user'
        verbose_name = "模块分类用户关联表"
        verbose_name_plural = "模块分类用户关联表"


    def __str__(self):
            return self.name


    # 模块分类用户关联表
class DocumentCategoryUser(models.Model):
    id = models.AutoField(primary_key=True)
    rola_id = models.IntegerField(verbose_name="角色id",default=0)
    category_id = models.IntegerField(verbose_name="文档分类id")
    label = models.CharField(max_length=255, verbose_name="权限label", default=0)
    name = models.CharField(max_length=255, verbose_name="权限名称", default=0)
    action = models.CharField(max_length=255, verbose_name="权限类型", default=0)
    type = models.CharField(max_length=255, verbose_name="归属分类", default=0)
    value = models.CharField(max_length=255, verbose_name="权限code", default=0)
    isMain = models.CharField(max_length=255, verbose_name="是否选择", default=0)

    class Meta:
        db_table = 'dc_document_category_user'
        verbose_name = "文档分类角色关联表"
        verbose_name_plural = "文档分类角色关联表"


    def __str__(self):
            return self.name

    # 原始数据分类关联表
class SourceDataCategoryUser(models.Model):
    id = models.AutoField(primary_key=True)
    rola_id = models.IntegerField(verbose_name="角色id",default=0)
    category_id = models.IntegerField(verbose_name="文档分类id")
    label = models.CharField(max_length=255, verbose_name="权限label", default=0)
    name = models.CharField(max_length=255, verbose_name="权限名称", default=0)
    action = models.CharField(max_length=255, verbose_name="权限类型", default=0)
    type = models.CharField(max_length=255, verbose_name="归属分类", default=0)
    value = models.CharField(max_length=255, verbose_name="权限code", default=0)
    isMain = models.CharField(max_length=255, verbose_name="是否选择", default=0)

    class Meta:
        db_table = 'dc_source_data_category_user'
        verbose_name = "原始数据分类关联表"
        verbose_name_plural = "原始数据分类关联表"



    def __str__(self):
            return self.name

class Knowledgebase(models.Model):
    id = models.AutoField(primary_key=True)
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now,null=True, blank=True)
    update_time = models.DateTimeField(verbose_name="更新时间", default=eight_hours_from_now)
    role_id= models.IntegerField(verbose_name="角色id", default=2)
    location=models.CharField(max_length=255, verbose_name="buceket内名称",default='')
    name = models.CharField(max_length=255, verbose_name="展示名称")
    description = models.CharField(max_length=255, verbose_name="描述备注",null=True, blank=True)
    language = models.CharField(max_length=255, verbose_name="语言",null=True, blank=True)
    embd_id = models.CharField(max_length=255, verbose_name="嵌入模型",null=True, blank=True)
    permission = models.CharField(max_length=255, verbose_name="权限:如果权限是“团队”，则所有团队成员都可以操作知识库。",null=True, blank=True)
    created_by = models.CharField(max_length=255, verbose_name="创建人id",null=True, blank=True)
    doc_num = models.CharField(max_length=255, verbose_name="",null=True, blank=True,default=0)
    token_num = models.CharField(max_length=255, verbose_name="块Token数",null=True, blank=True,default=0)
    chunk_num = models.CharField(max_length=255, verbose_name="",null=True, blank=True,default=0)
    similarity_threshold = models.CharField(max_length=255, verbose_name="相似度阈值",null=True, blank=True)
    vector_similarity_weight = models.CharField(max_length=255, verbose_name="关键字相似度权重",null=True, blank=True)
    parser_id = models.CharField(max_length=255, verbose_name="",null=True, blank=True)
    parser_config = models.CharField(max_length=255, verbose_name="",null=True, blank=True)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态",default=5)

    def clean(self):
        super().clean()
        # 检查是否存在同名且状态不为4的记录
        if Knowledgebase.objects.filter(name=self.name).exclude(status=4).exclude(pk=self.pk).exists():
            raise ValidationError({'name': _("已存在同名,请修改后重试。")})

    def save(self, *args, **kwargs):
        self.update_time = eight_hours_from_now()
        self.full_clean()
        super().save(*args, **kwargs)


    class Meta:
        db_table = 'kbm_knowledgebase'
        verbose_name = "bucket信息表"
        verbose_name_plural = "bucket信息表"


    def __str__(self):
            return self.name


class File(models.Model):
    id = models.AutoField(primary_key=True)
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now,null=True, blank=True)
    update_time = models.DateTimeField(verbose_name="更新时间", default=eight_hours_from_now)
    parent_id = models.CharField(max_length=255, verbose_name="父级id",null=True, blank=True)
    role_id = models.IntegerField(verbose_name="角色id", default=2)
    created_by = models.CharField(max_length=255, verbose_name="创建人id", null=True, blank=True)
    name = models.CharField(max_length=255, verbose_name="展示文件名称")
    location =models.CharField(max_length=255, verbose_name="原文件名称",null=True, blank=True)
    size = models.IntegerField(verbose_name="文件大小", default=0)
    type = models.CharField(max_length=255, verbose_name="文件类型")
    source_type = models.CharField(max_length=255, verbose_name="属于分类",default="knowledgebase",null=True, blank=True)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态",default=5)

    def clean(self):
        super().clean()
        # 检查是否存在同名且状态不为4的记录
        if File.objects.filter(name=self.name,parent_id=self.parent_id).exclude(status=4).exclude(pk=self.pk).exists():
            raise ValidationError({'name': _("已存在同名,请修改后重试。")})

    def save(self, *args, **kwargs):
        self.update_time = eight_hours_from_now()
        self.full_clean()
        super().save(*args, **kwargs)


    class Meta:
        db_table = 'kbm_file'
        verbose_name = "知识库文件层级表"
        verbose_name_plural = "知识库文件层级表"


    def __str__(self):
            return self.name

class DocumentKbm(models.Model):
    id = models.AutoField(primary_key=True)
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now,null=True, blank=True)
    update_time = models.DateTimeField(verbose_name="更新时间", default=eight_hours_from_now)
    thumbnail = models.TextField(verbose_name="图片解析base64",null=True, blank=True)
    kb_id =models.CharField(max_length=255, verbose_name="知识库id")
    parser_id =models.CharField(max_length=255, verbose_name="分析器id")
    parser_config =models.CharField(max_length=255, verbose_name="分析器配置信息")
    source_type = models.CharField(max_length=255, verbose_name="属于分类", default="local", null=True,blank=True)
    type = models.CharField(max_length=255, verbose_name="文件类型")
    created_by = models.CharField(max_length=255, verbose_name="创建人id", null=True, blank=True)
    name = models.CharField(max_length=255, verbose_name="展示文件名称")
    location = models.CharField(max_length=255, verbose_name="原文件名称", null=True, blank=True)
    size = models.IntegerField(verbose_name="文件大小", default=0)
    token_num = models.IntegerField(verbose_name="token数量", default=0)
    chunk_num = models.IntegerField(verbose_name="分块数量", default=0)
    progress = models.FloatField(verbose_name="解析数", default=0)
    progress_msg = models.TextField(verbose_name="解析详情信息",default='',null=True, blank=True)
    process_begin_at = models.DateTimeField(verbose_name="解析开始时间",null=True, blank=True)
    process_duation = models.FloatField(verbose_name="解析时间记录", default=0)
    run = models.CharField(max_length=255, verbose_name="解析标识",default=0)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态",default=5)
    doc_type_id = models.CharField(max_length=255, verbose_name="文件所属分类id", null=True, blank=True,default=0)

    def clean(self):
        super().clean()
        # 检查是否存在同名且状态不为4的记录
        if DocumentKbm.objects.filter(name=self.name,kb_id=self.kb_id).exclude(status=4).exclude(pk=self.pk).exists():
            raise ValidationError({'name': _("已存在同名,请修改后重试。")})

    def save(self, *args, **kwargs):
        self.update_time = eight_hours_from_now()
        self.full_clean()
        super().save(*args, **kwargs)


    class Meta:
        db_table = 'kbm_document'
        verbose_name = "上传文件详情表"
        verbose_name_plural = "上传文件详情表"


    def __str__(self):
            return self.name

class File2document(models.Model):
    id = models.AutoField(primary_key=True)
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now,null=True, blank=True)
    update_time = models.DateTimeField(verbose_name="更新时间", default=eight_hours_from_now)
    file_id =models.CharField(max_length=255, verbose_name="文件fileid")
    document_id =models.CharField(max_length=255, verbose_name="文件详情id")

    def save(self, *args, **kwargs):
        self.update_time = eight_hours_from_now()
        self.full_clean()
        super().save(*args, **kwargs)


    class Meta:
        db_table = 'kbm_file2document'
        verbose_name = "文件位置关联表"
        verbose_name_plural = "文件位置关联表"


    def __str__(self):
            return self.name

class Task(models.Model):
    id = models.AutoField(primary_key=True)
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now,null=True, blank=True)
    update_time = models.DateTimeField(verbose_name="更新时间", default=eight_hours_from_now)
    doc_id = models.CharField(max_length=255, verbose_name="文件详情id")
    from_page = models.IntegerField(verbose_name="开始页数", default=0)
    to_page = models.IntegerField(verbose_name="结束页数", default=-1)
    begin_at = models.CharField(max_length=255, verbose_name="begin_at",null=True, blank=True)
    process_duation = models.FloatField(max_length=255, verbose_name="解析时间",default=0)
    progress = models.FloatField(max_length=255, verbose_name="解析进度",default=0)
    progress_msg = models.TextField(max_length=255, verbose_name="解析内容",default='',null=True, blank=True)


    def save(self, *args, **kwargs):
        self.update_time = eight_hours_from_now()
        self.full_clean()
        super().save(*args, **kwargs)


    class Meta:
        db_table = 'kbm_task'
        verbose_name = "文件解析信息表"
        verbose_name_plural = "文件解析信息表"


    def __str__(self):
            return self.name

class TaskSublist(models.Model):
    id = models.AutoField(primary_key=True)
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now, null=True, blank=True)
    update_time = models.DateTimeField(verbose_name="更新时间", default=eight_hours_from_now)
    doc_id = models.CharField(max_length=255, verbose_name="文件详情id")
    name = models.CharField(max_length=255, verbose_name="展示文件名称")
    page_number = models.CharField(max_length=255, verbose_name="第几页",default=0)
    chunk_number = models.CharField(max_length=255, verbose_name="分片数量",default=0)
    content = models.TextField(max_length=255, verbose_name="解析文本",default='',null=True, blank=True)
    milvus_id = models.CharField(max_length=255, verbose_name="milvus向量库id", default=0,null=True, blank=True)


    def save(self, *args, **kwargs):
        self.update_time = eight_hours_from_now()
        self.full_clean()
        super().save(*args, **kwargs)


    class Meta:
        db_table = 'kbm_task_sublist'
        verbose_name = "文件解析子表"
        verbose_name_plural = "文件解析子表"

    def __str__(self):
            return self.name

class UserOperation(models.Model):
    id = models.AutoField(primary_key=True)
    role_id = models.IntegerField(verbose_name="角色id")
    operation_id = models.IntegerField(verbose_name="操作权限id")

    class Meta:
        db_table = 'auth_user_operation'
        verbose_name = "用户模块权限表"
        verbose_name_plural = "用户模块权限表"

    def __str__(self):
        return self.name


class Operation(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, verbose_name="对应模块名称")
    content_type_id = models.IntegerField(verbose_name="所属分类id", default=0)
    codename = models.CharField(max_length=100, verbose_name="权限code码")

    class Meta:
        db_table = 'auth_operation'
        verbose_name = "菜单操作权限表"
        verbose_name_plural = "菜单操作权限表"

    def __str__(self):
        return self.name


class WechatUser(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, verbose_name="姓名",null=True, blank=True)
    nikeName = models.CharField(max_length=255, verbose_name="昵称",null=True, blank=True)
    phone = models.CharField(max_length=255, verbose_name="手机号",null=True, blank=True)
    openid = models.CharField(max_length=255, verbose_name="微信唯一标识",null=True, blank=True)
    age = models.CharField(max_length=255, verbose_name="年龄",null=True, blank=True)
    level = models.CharField(max_length=255, verbose_name="等级",null=True, blank=True,default=1)
    user_id = models.CharField(max_length=255, verbose_name="用户id",null=True, blank=True)
    id_card = models.CharField(max_length=255, verbose_name="身份证号",null=True, blank=True)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态", default=5)
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now, null=True, blank=True)
    update_time = models.DateTimeField(verbose_name="更新时间", default=eight_hours_from_now)
    mini_url = models.CharField(max_length=255, verbose_name="小程序头像url",null=True, blank=True)
    examine = models.CharField(max_length=255, verbose_name="审核状态 0未审核 1审核",null=True, blank=True,default=0)
    source = models.CharField(max_length=255, verbose_name="来源标识 pc:pc  小程序：mini", null=True, blank=True)

    def save(self, *args, **kwargs):
        self.update_time = eight_hours_from_now()
        self.full_clean()
        super().save(*args, **kwargs)

    class Meta:
        db_table = 'wechat_user'
        verbose_name = "微信用户表"
        verbose_name_plural = "微信用户表"

    def __str__(self):
        return self.name


class WechatComplaint(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, verbose_name="姓名",null=True, blank=True)
    phone = models.CharField(max_length=255, verbose_name="手机号",null=True, blank=True)
    corporate_name = models.CharField(max_length=255, verbose_name="公司名称",null=True, blank=True)
    content = models.CharField(max_length=255, verbose_name="内容",null=True, blank=True)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态", default=5)
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now, null=True, blank=True)
    update_time = models.DateTimeField(verbose_name="更新时间", default=eight_hours_from_now)


    def save(self, *args, **kwargs):
        if self.status is None:
            self.status = 5
        self.update_time = eight_hours_from_now()
        self.full_clean()
        super().save(*args, **kwargs)

    class Meta:
        db_table = 'wechat_complaint'
        verbose_name = "微信投诉建议信息表"
        verbose_name_plural = "微信投诉建议信息表"

    def __str__(self):
        return self.name

class KbmDocumentType(models.Model):
    id = models.AutoField(primary_key=True)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态", default=5)
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now, null=True, blank=True)
    update_time = models.DateTimeField(verbose_name="更新时间", default=eight_hours_from_now)
    name = models.CharField(max_length=255, verbose_name="姓名", null=True, blank=True)
    kb_id = models.CharField(max_length=255, verbose_name="所属知识库id", null=True, blank=True)
    parent_id = models.CharField(max_length=255, verbose_name="层级id", null=True, blank=True,default=0)


    def clean(self):
        super().clean()
        # 检查是否存在同名且状态不为4的记录
        if KbmDocumentType.objects.filter(name=self.name,kb_id=self.kb_id).exclude(status=4).exclude(pk=self.pk).exists():
            raise ValidationError({'name': _("已存在同名,请修改后重试。")})


    def save(self, *args, **kwargs):
        if self.status is None:
            self.status = 5
        self.update_time = eight_hours_from_now()
        self.full_clean()
        super().save(*args, **kwargs)

    class Meta:
        db_table = 'kbm_document_type'
        verbose_name = "知识库文件分类表"
        verbose_name_plural = "知识库文件分类表"

    def __str__(self):
        return self.name

# 超级管理员
class SuperAdmin(models.Model):
    id = models.AutoField(primary_key=True)
    username = models.CharField(max_length=60, verbose_name="登录用户名", unique=True)
    password = models.CharField(max_length=255, verbose_name="登录密码")
    mobile = models.CharField(max_length=20, verbose_name="手机号码")
    email = models.CharField(max_length=60, verbose_name="邮件地址",blank=True,null=True)
    real_name = models.CharField(max_length=255, verbose_name="真实姓名", blank=True,null=True)
    token = models.CharField(max_length=120, verbose_name="登录授权码", default="", blank=True,null=True)
    role_id = models.IntegerField(verbose_name='所属角色', default=0, blank=True,null=True)
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now)
    last_login_time = models.DateTimeField(verbose_name="最后登录时间", default=eight_hours_from_now)
    status = models.SmallIntegerField(choices=DefaultStatus, verbose_name="状态", blank=True,null=True)
    header = models.CharField(max_length=255, verbose_name="头像", blank=True,null=True)
    code = models.CharField(max_length=255, verbose_name="工号", blank=True,null=True)
    job = models.CharField(max_length=255, verbose_name="职位", blank=True,null=True)
    contact = models.CharField(max_length=255, verbose_name="联系方式", blank=True,null=True)
    verification_code = models.CharField(max_length=255, verbose_name="验证码", blank=True,null=True)

    class Meta:
        db_table = 'super_admin'
        verbose_name = "超级管理员信息表"
        verbose_name_plural = "超级管理员信息表"

    def __int__(self):
        return self.id

# 超级管理员配置
class SuperSettings(models.Model):
    id = models.AutoField(primary_key=True)
    create_time = models.DateTimeField(verbose_name="创建日期", default=eight_hours_from_now)
    logo = models.CharField(max_length=255, verbose_name="logo", blank=True,null=True)
    name = models.CharField(max_length=255, verbose_name="公司名称", blank=True,null=True)
    sidebar_color = models.CharField(max_length=255, verbose_name="侧边栏颜色", blank=True,null=True)
    internal_color = models.CharField(max_length=255, verbose_name="内部颜色", blank=True,null=True)
    button_color = models.CharField(max_length=255, verbose_name="按钮颜色", blank=True,null=True)
    head_color = models.CharField(max_length=255, verbose_name="头部颜色", blank=True,null=True)
    log_color = models.CharField(max_length=255, verbose_name="登录页背景颜色", blank=True,null=True)
    log_color_url = models.CharField(max_length=255, verbose_name="登录页背景URL", blank=True,null=True)


    class Meta:
        db_table = 'super_settings'
        verbose_name = "超级管理员设置表"
        verbose_name_plural = "超级管理员设置表"

    def __int__(self):
        return self.id
















