# Generated by Django 5.2.3 on 2025-06-23 02:14

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, verbose_name='创建时间')),
                ('update_time', models.DateTimeField(auto_now=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')], default='TEMPORARY_120_MINUTE', verbose_name='资源类型')),
                ('source_id', models.CharField(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, verbose_name='创建时间')),
                ('update_time', models.DateTimeField(auto_now=True, verbose_name='修改时间')),
                ('id', models.UUIDField(default=uuid_utils.compat.uuid7, editable=False, primary_key=True, serialize=False, verbose_name='主键id')),
                ('name', models.CharField(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, '语雀类型')], default=0, verbose_name='类型')),
                ('scope', models.CharField(choices=[('SHARED', '共享'), ('WORKSPACE', '工作空间可用')], 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(on_delete=django.db.models.deletion.DO_NOTHING, to='models_provider.model', verbose_name='向量模型')),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='users.user', verbose_name='所属用户')),
            ],
            options={
                'db_table': 'knowledge',
            },
        ),
        migrations.CreateModel(
            name='Document',
            fields=[
                ('create_time', models.DateTimeField(auto_now_add=True, verbose_name='创建时间')),
                ('update_time', models.DateTimeField(auto_now=True, verbose_name='修改时间')),
                ('id', models.UUIDField(default=uuid_utils.compat.uuid7, editable=False, primary_key=True, serialize=False, verbose_name='主键id')),
                ('name', models.CharField(max_length=150, verbose_name='文档名称')),
                ('char_length', models.IntegerField(verbose_name='文档字符数 冗余字段')),
                ('status', models.CharField(default=knowledge.models.knowledge.Status.__str__, max_length=20, verbose_name='状态')),
                ('status_meta', models.JSONField(default=knowledge.models.knowledge.default_status_meta, verbose_name='状态统计数据')),
                ('is_active', models.BooleanField(default=True)),
                ('type', models.IntegerField(choices=[(0, '通用类型'), (1, 'web站点类型'), (2, '飞书类型'), (3, '语雀类型')], 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, verbose_name='创建时间')),
                ('update_time', models.DateTimeField(auto_now=True, verbose_name='修改时间')),
                ('id', models.CharField(editable=False, max_length=64, primary_key=True, serialize=False, verbose_name='主键id')),
                ('name', models.CharField(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(on_delete=django.db.models.deletion.DO_NOTHING, to='users.user', verbose_name='用户id')),
            ],
            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, verbose_name='创建时间')),
                ('update_time', models.DateTimeField(auto_now=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(default='', max_length=256, verbose_name='标题')),
                ('status', models.CharField(default=knowledge.models.knowledge.Status.__str__, 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(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(max_length=128, verbose_name='资源id')),
                ('source_type', models.CharField(choices=[(0, '问题'), (1, '段落'), (2, '标题')], 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, verbose_name='创建时间')),
                ('update_time', models.DateTimeField(auto_now=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=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, verbose_name='创建时间')),
                ('update_time', models.DateTimeField(auto_now=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),
    ]
