# Generated by Django 5.2.4 on 2025-07-14 03:50

import django.contrib.postgres.search
import django.db.models.deletion
import knowledge.models.knowledge
import mptt.fields
import uuid_utils.compat
from django.db import migrations, models

from knowledge.models import KnowledgeFolder


def insert_default_data(apps, schema_editor):
    # 创建一个根模块（没有父节点）
    KnowledgeFolder.objects.create(id='default', name='根目录', user_id='f0dd8f71-e4ee-11ee-8c84-a8a1595801ab', workspace_id='default')


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('models_provider', '0001_initial'),
        ('users', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='File',
            fields=[
                ('create_time', models.DateTimeField(auto_now_add=True, db_index=True, verbose_name='创建时间')),
                ('update_time', models.DateTimeField(auto_now=True, db_index=True, verbose_name='修改时间')),
                ('id', models.UUIDField(default=uuid_utils.compat.uuid7, editable=False, primary_key=True, serialize=False, verbose_name='主键id')),
                ('file_name', models.CharField(default='', max_length=256, verbose_name='文件名称')),
                ('file_size', models.IntegerField(default=0, verbose_name='文件大小')),
                ('sha256_hash', models.CharField(default='', verbose_name='文件sha256_hash标识')),
                ('source_type', models.CharField(choices=[('KNOWLEDGE', 'Knowledge'), ('APPLICATION', 'Application'), ('TOOL', 'Tool'), ('DOCUMENT', 'Document'), ('CHAT', 'Chat'), ('TEMPORARY_30_MINUTE', 'Temporary 30 Minute'), ('TEMPORARY_120_MINUTE', 'Temporary 120 Minute'), ('TEMPORARY_1_DAY', 'Temporary 1 Day')], db_index=True, default='TEMPORARY_120_MINUTE', verbose_name='资源类型')),
                ('source_id', models.CharField(db_index=True, default='TEMPORARY_120_MINUTE', verbose_name='资源id')),
                ('loid', models.IntegerField(verbose_name='loid')),
                ('meta', models.JSONField(default=dict, verbose_name='文件关联数据')),
            ],
            options={
                'db_table': 'file',
            },
        ),
        migrations.CreateModel(
            name='Knowledge',
            fields=[
                ('create_time', models.DateTimeField(auto_now_add=True, db_index=True, verbose_name='创建时间')),
                ('update_time', models.DateTimeField(auto_now=True, db_index=True, verbose_name='修改时间')),
                ('id', models.UUIDField(default=uuid_utils.compat.uuid7, editable=False, primary_key=True, serialize=False, verbose_name='主键id')),
                ('name', models.CharField(db_index=True, max_length=150, verbose_name='知识库名称')),
                ('workspace_id', models.CharField(db_index=True, default='default', max_length=64, verbose_name='工作空间id')),
                ('desc', models.CharField(max_length=256, verbose_name='描述')),
                ('type', models.IntegerField(choices=[(0, '通用类型'), (1, 'web站点类型'), (2, '飞书类型'), (3, '语雀类型')], db_index=True, default=0, verbose_name='类型')),
                ('scope', models.CharField(choices=[('SHARED', '共享'), ('WORKSPACE', '工作空间可用')], db_index=True, default='WORKSPACE', max_length=20, verbose_name='可用范围')),
                ('file_size_limit', models.IntegerField(default=100, verbose_name='文件大小限制')),
                ('file_count_limit', models.IntegerField(default=50, verbose_name='文件数量限制')),
                ('meta', models.JSONField(default=dict, verbose_name='元数据')),
                ('embedding_model', models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.SET_NULL, to='models_provider.model')),
                ('user', models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.SET_NULL, to='users.user')),
            ],
            options={
                'db_table': 'knowledge',
            },
        ),
        migrations.CreateModel(
            name='Document',
            fields=[
                ('create_time', models.DateTimeField(auto_now_add=True, db_index=True, verbose_name='创建时间')),
                ('update_time', models.DateTimeField(auto_now=True, db_index=True, verbose_name='修改时间')),
                ('id', models.UUIDField(default=uuid_utils.compat.uuid7, editable=False, primary_key=True, serialize=False, verbose_name='主键id')),
                ('name', models.CharField(db_index=True, max_length=150, verbose_name='文档名称')),
                ('char_length', models.IntegerField(verbose_name='文档字符数 冗余字段')),
                ('status', models.CharField(db_index=True, default=knowledge.models.knowledge.get_default_status, max_length=20, verbose_name='状态')),
                ('status_meta', models.JSONField(default=knowledge.models.knowledge.default_status_meta, verbose_name='状态统计数据')),
                ('is_active', models.BooleanField(db_index=True, default=True)),
                ('type', models.IntegerField(choices=[(0, '通用类型'), (1, 'web站点类型'), (2, '飞书类型'), (3, '语雀类型')], db_index=True, default=0, verbose_name='类型')),
                ('hit_handling_method', models.CharField(choices=[('optimization', '模型优化'), ('directly_return', '直接返回')], default='optimization', max_length=20, verbose_name='命中处理方式')),
                ('directly_return_similarity', models.FloatField(default=0.9, verbose_name='直接回答相似度')),
                ('meta', models.JSONField(default=dict, verbose_name='元数据')),
                ('knowledge', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='knowledge.knowledge', verbose_name='知识库id')),
            ],
            options={
                'db_table': 'document',
            },
        ),
        migrations.CreateModel(
            name='KnowledgeFolder',
            fields=[
                ('create_time', models.DateTimeField(auto_now_add=True, db_index=True, verbose_name='创建时间')),
                ('update_time', models.DateTimeField(auto_now=True, db_index=True, verbose_name='修改时间')),
                ('id', models.CharField(editable=False, max_length=64, primary_key=True, serialize=False, verbose_name='主键id')),
                ('name', models.CharField(db_index=True, max_length=64, verbose_name='文件夹名称')),
                ('desc', models.CharField(blank=True, max_length=200, null=True, verbose_name='描述')),
                ('workspace_id', models.CharField(db_index=True, default='default', max_length=64, verbose_name='工作空间id')),
                ('lft', models.PositiveIntegerField(editable=False)),
                ('rght', models.PositiveIntegerField(editable=False)),
                ('tree_id', models.PositiveIntegerField(db_index=True, editable=False)),
                ('level', models.PositiveIntegerField(editable=False)),
                ('parent', mptt.fields.TreeForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='children', to='knowledge.knowledgefolder')),
                ('user', models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.SET_NULL, to='users.user')),
            ],
            options={
                'db_table': 'knowledge_folder',
            },
        ),
        migrations.AddField(
            model_name='knowledge',
            name='folder',
            field=models.ForeignKey(default='default', on_delete=django.db.models.deletion.DO_NOTHING, to='knowledge.knowledgefolder', verbose_name='文件夹id'),
        ),
        migrations.CreateModel(
            name='Paragraph',
            fields=[
                ('create_time', models.DateTimeField(auto_now_add=True, db_index=True, verbose_name='创建时间')),
                ('update_time', models.DateTimeField(auto_now=True, db_index=True, verbose_name='修改时间')),
                ('id', models.UUIDField(default=uuid_utils.compat.uuid7, editable=False, primary_key=True, serialize=False, verbose_name='主键id')),
                ('content', models.CharField(max_length=102400, verbose_name='段落内容')),
                ('title', models.CharField(db_index=True, default='', max_length=256, verbose_name='标题')),
                ('status', models.CharField(db_index=True, default=knowledge.models.knowledge.get_default_status, max_length=20, verbose_name='状态')),
                ('status_meta', models.JSONField(default=knowledge.models.knowledge.default_status_meta, verbose_name='状态数据')),
                ('hit_num', models.IntegerField(default=0, verbose_name='命中次数')),
                ('is_active', models.BooleanField(db_index=True, default=True)),
                ('position', models.IntegerField(db_index=True, default=0, verbose_name='段落顺序')),
                ('document', models.ForeignKey(db_constraint=False, on_delete=django.db.models.deletion.DO_NOTHING, to='knowledge.document')),
                ('knowledge', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='knowledge.knowledge')),
            ],
            options={
                'db_table': 'paragraph',
            },
        ),
        migrations.CreateModel(
            name='Embedding',
            fields=[
                ('id', models.CharField(max_length=128, primary_key=True, serialize=False, verbose_name='主键id')),
                ('source_id', models.CharField(db_index=True, max_length=128, verbose_name='资源id')),
                ('source_type', models.CharField(choices=[(0, '问题'), (1, '段落'), (2, '标题')], db_index=True, default=0, max_length=5, verbose_name='资源类型')),
                ('is_active', models.BooleanField(default=True, max_length=1, verbose_name='是否可用')),
                ('embedding', knowledge.models.knowledge.VectorField(verbose_name='向量')),
                ('search_vector', django.contrib.postgres.search.SearchVectorField(default='', verbose_name='分词')),
                ('meta', models.JSONField(default=dict, verbose_name='元数据')),
                ('document', models.ForeignKey(db_constraint=False, on_delete=django.db.models.deletion.DO_NOTHING, to='knowledge.document', verbose_name='文档关联')),
                ('knowledge', models.ForeignKey(db_constraint=False, on_delete=django.db.models.deletion.DO_NOTHING, to='knowledge.knowledge', verbose_name='文档关联')),
                ('paragraph', models.ForeignKey(db_constraint=False, on_delete=django.db.models.deletion.DO_NOTHING, to='knowledge.paragraph', verbose_name='段落关联')),
            ],
            options={
                'db_table': 'embedding',
            },
        ),
        migrations.CreateModel(
            name='Problem',
            fields=[
                ('create_time', models.DateTimeField(auto_now_add=True, db_index=True, verbose_name='创建时间')),
                ('update_time', models.DateTimeField(auto_now=True, db_index=True, verbose_name='修改时间')),
                ('id', models.UUIDField(default=uuid_utils.compat.uuid7, editable=False, primary_key=True, serialize=False, verbose_name='主键id')),
                ('content', models.CharField(db_index=True, max_length=256, verbose_name='问题内容')),
                ('hit_num', models.IntegerField(default=0, verbose_name='命中次数')),
                ('knowledge', models.ForeignKey(db_constraint=False, on_delete=django.db.models.deletion.DO_NOTHING, to='knowledge.knowledge')),
            ],
            options={
                'db_table': 'problem',
            },
        ),
        migrations.CreateModel(
            name='ProblemParagraphMapping',
            fields=[
                ('create_time', models.DateTimeField(auto_now_add=True, db_index=True, verbose_name='创建时间')),
                ('update_time', models.DateTimeField(auto_now=True, db_index=True, verbose_name='修改时间')),
                ('id', models.UUIDField(default=uuid_utils.compat.uuid7, editable=False, primary_key=True, serialize=False, verbose_name='主键id')),
                ('document', models.ForeignKey(db_constraint=False, on_delete=django.db.models.deletion.DO_NOTHING, to='knowledge.document')),
                ('knowledge', models.ForeignKey(db_constraint=False, on_delete=django.db.models.deletion.DO_NOTHING, to='knowledge.knowledge')),
                ('paragraph', models.ForeignKey(db_constraint=False, on_delete=django.db.models.deletion.DO_NOTHING, to='knowledge.paragraph')),
                ('problem', models.ForeignKey(db_constraint=False, on_delete=django.db.models.deletion.DO_NOTHING, to='knowledge.problem')),
            ],
            options={
                'db_table': 'problem_paragraph_mapping',
            },
        ),
        migrations.RunPython(insert_default_data),
    ]
