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 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
"""种子问题相关的API接口"""

import csv
import io
from datetime import datetime
from typing import List, Optional

from fastapi import APIRouter, Depends, File, HTTPException, UploadFile, status
from fastapi.responses import StreamingResponse
from sqlalchemy.orm import Session

from qa_annotate.api.auth import get_current_active_user, get_current_superuser
from qa_annotate.database.base import get_db
from qa_annotate.database.crud import QuestionTypeCRUD, SeedQuestionCRUD
from qa_annotate.schema.question_type import (
    QuestionType,
    QuestionTypeCreate,
    QuestionTypeUpdate,
)
from qa_annotate.schema.seed_question import (
    SeedQuestion,
    SeedQuestionCreate,
    SeedQuestionUpdate,
    SeedQuestionWithCreator,
)
from qa_annotate.schema.user import User

router = APIRouter(prefix="/seed-questions", tags=["seed-questions"])


# ==================== 普通用户接口 ====================


@router.post("/", response_model=SeedQuestion, status_code=status.HTTP_201_CREATED)
def create_seed_question(
    seed_question: SeedQuestionCreate,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_active_user),
):
    """创建种子问题(所有登录用户可访问)"""
    return SeedQuestionCRUD.create(
        db=db, seed_question=seed_question, creator_id=current_user.id
    )


@router.get("/", response_model=List[SeedQuestion])
def list_seed_questions(
    skip: int = 0,
    limit: int = 100,
    type: Optional[str] = None,
    subtype: Optional[str] = None,
    search: Optional[str] = None,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_active_user),
):
    """获取种子问题列表(只能看到自己创建的)"""
    return SeedQuestionCRUD.get_all(
        db=db,
        skip=skip,
        limit=limit,
        creator_id=current_user.id,  # 只返回当前用户创建的
        type=type,
        subtype=subtype,
        search=search,
    )


@router.get("/{question_id}", response_model=SeedQuestion)
def get_seed_question(
    question_id: int,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_active_user),
):
    """获取单个种子问题(只能获取自己创建的)"""
    seed_question = SeedQuestionCRUD.get_by_id(db, question_id=question_id)
    if not seed_question:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=f"种子问题 ID {question_id} 不存在",
        )

    # 检查权限:只能查看自己创建的
    if seed_question.creator_id != current_user.id:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="只能查看自己创建的种子问题",
        )

    return seed_question


@router.put("/{question_id}", response_model=SeedQuestion)
def update_seed_question(
    question_id: int,
    seed_question_update: SeedQuestionUpdate,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_active_user),
):
    """更新种子问题(只能更新自己创建的)"""
    # 检查种子问题是否存在
    existing = SeedQuestionCRUD.get_by_id(db, question_id=question_id)
    if not existing:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=f"种子问题 ID {question_id} 不存在",
        )

    # 检查权限:只能更新自己创建的
    if existing.creator_id != current_user.id:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="只能更新自己创建的种子问题",
        )

    updated = SeedQuestionCRUD.update(
        db=db, question_id=question_id, seed_question=seed_question_update
    )
    if not updated:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=f"种子问题 ID {question_id} 不存在",
        )

    return updated


@router.delete("/{question_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_seed_question(
    question_id: int,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_active_user),
):
    """删除种子问题(只能删除自己创建的)"""
    # 检查种子问题是否存在
    existing = SeedQuestionCRUD.get_by_id(db, question_id=question_id)
    if not existing:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=f"种子问题 ID {question_id} 不存在",
        )

    # 检查权限:只能删除自己创建的
    if existing.creator_id != current_user.id:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="只能删除自己创建的种子问题",
        )

    success = SeedQuestionCRUD.delete(db=db, question_id=question_id)
    if not success:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=f"种子问题 ID {question_id} 不存在",
        )

    return None


@router.post("/import", status_code=status.HTTP_200_OK)
async def import_seed_questions_from_csv(
    file: UploadFile = File(..., description="CSV文件"),
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_active_user),
):
    """批量导入种子问题(从CSV文件)"""
    # 检查文件类型
    if not file.filename or not file.filename.endswith(".csv"):
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST, detail="只支持.csv格式的文件"
        )

    # 读取文件内容
    try:
        content = await file.read()
        # 尝试使用utf-8-sig解码(支持Excel导出的CSV)
        try:
            text_content = content.decode("utf-8-sig")
        except UnicodeDecodeError:
            text_content = content.decode("utf-8")
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST, detail=f"读取文件失败: {str(e)}"
        )

    # 解析CSV文件
    try:
        csv_reader = csv.DictReader(io.StringIO(text_content))
        rows = list(csv_reader)
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST, detail=f"解析CSV文件失败: {str(e)}"
        )

    if not rows:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST, detail="CSV文件为空或格式不正确"
        )

    # 验证必需的列
    required_columns = [
        "种子问题",
        "类型",
        "亚类",
        "物种/领域",
        "模型",
        "日期",
        "是否核验",
    ]
    for col in required_columns:
        if col not in csv_reader.fieldnames:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail=f"CSV文件缺少必需的列: {col}",
            )

    imported_count = 0
    failed_count = 0
    errors = []
    seed_questions_to_create = []

    for row_num, row in enumerate(rows, start=2):  # 从第2行开始(第1行是标题)
        try:
            # 提取必需字段
            question = row.get("种子问题", "").strip()
            type_name = row.get("类型", "").strip()
            subtype = row.get("亚类", "").strip()
            species_or_domain = row.get("物种/领域", "").strip()
            model = row.get("模型", "").strip()
            date_str = row.get("日期", "").strip()
            verified_str = row.get("是否核验", "").strip()

            # 验证必需字段
            if not question:
                errors.append(f"第{row_num}行: 种子问题不能为空")
                failed_count += 1
                continue

            if not type_name:
                errors.append(f"第{row_num}行: 类型不能为空")
                failed_count += 1
                continue

            if not subtype:
                errors.append(f"第{row_num}行: 亚类不能为空")
                failed_count += 1
                continue

            if not species_or_domain:
                errors.append(f"第{row_num}行: 物种/领域不能为空")
                failed_count += 1
                continue

            if not model:
                errors.append(f"第{row_num}行: 模型不能为空")
                failed_count += 1
                continue

            if not date_str:
                errors.append(f"第{row_num}行: 日期不能为空")
                failed_count += 1
                continue

            # 处理日期字段(格式:YYYYMMDD或YYYY-MM-DD)
            date_value = None
            try:
                # 尝试解析YYYYMMDD格式
                if len(date_str) == 8 and date_str.isdigit():
                    date_value = datetime.strptime(date_str, "%Y%m%d").date()
                else:
                    # 尝试其他常见格式
                    date_value = datetime.strptime(date_str, "%Y-%m-%d").date()
            except ValueError:
                errors.append(
                    f"第{row_num}行: 日期格式不正确,应为YYYYMMDD或YYYY-MM-DD"
                )
                failed_count += 1
                continue

            # 处理是否核验字段(是/否 -> True/False)
            if not verified_str:
                errors.append(f"第{row_num}行: 是否核验不能为空")
                failed_count += 1
                continue

            verified_str_lower = verified_str.lower()
            if verified_str_lower in ["是", "yes", "true", "1"]:
                is_verified = True
            elif verified_str_lower in ["否", "no", "false", "0"]:
                is_verified = False
            else:
                errors.append(
                    f"第{row_num}行: 是否核验格式不正确,应为:是/否/yes/no/true/false/1/0"
                )
                failed_count += 1
                continue

            # 创建SeedQuestionCreate对象
            seed_question = SeedQuestionCreate(
                question=question,
                type=type_name,
                subtype=subtype,
                species_or_domain=species_or_domain,
                model=model,
                date=date_value,
                is_verified=is_verified,
            )

            seed_questions_to_create.append(seed_question)

        except Exception as e:
            errors.append(f"第{row_num}行: 处理失败 - {str(e)}")
            failed_count += 1
            continue

    # 批量创建种子问题
    if seed_questions_to_create:
        try:
            created_questions = SeedQuestionCRUD.create_batch(
                db=db,
                seed_questions=seed_questions_to_create,
                creator_id=current_user.id,
            )
            imported_count = len(created_questions)
        except Exception as e:
            raise HTTPException(
                status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
                detail=f"批量创建种子问题失败: {str(e)}",
            )

    return {
        "imported_count": imported_count,
        "failed_count": failed_count,
        "total_rows": len(rows),
        "errors": errors[:50],  # 最多返回50个错误
    }


@router.get("/options/types")
def get_question_type_options(
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_active_user),
):
    """获取类型和亚类选项(从数据库读取)"""
    grouped = QuestionTypeCRUD.get_all_grouped(db=db)
    return grouped


# ==================== 管理员接口 ====================


@router.get("/admin/all", response_model=List[SeedQuestionWithCreator])
def list_all_seed_questions(
    skip: int = 0,
    limit: int = 100,
    type: Optional[str] = None,
    subtype: Optional[str] = None,
    search: Optional[str] = None,
    creator_id: Optional[int] = None,
    db: Session = Depends(get_db),
    _: User = Depends(get_current_superuser),
):
    """获取所有种子问题(管理员,包含创建者全名)"""
    return SeedQuestionCRUD.get_all_with_creator(
        db=db,
        skip=skip,
        limit=limit,
        creator_id=creator_id,
        type=type,
        subtype=subtype,
        search=search,
    )


@router.get("/admin/export")
def export_all_seed_questions(
    db: Session = Depends(get_db),
    _: User = Depends(get_current_superuser),
):
    """导出所有种子问题(CSV格式,管理员)"""
    # 获取所有种子问题
    all_questions = SeedQuestionCRUD.export_all(db=db)

    # 创建CSV内容
    output = io.StringIO()
    writer = csv.writer(output)

    # 写入标题行
    writer.writerow(
        [
            "ID",
            "种子问题",
            "类型",
            "亚类",
            "物种/领域",
            "模型",
            "日期",
            "是否核验",
            "创建者全名",
            "创建时间",
            "更新时间",
        ]
    )

    # 写入数据行
    for question in all_questions:
        writer.writerow(
            [
                question.id,
                question.question,
                question.type,
                question.subtype,
                question.species_or_domain or "",
                question.model or "",
                question.date.strftime("%Y-%m-%d") if question.date else "",
                "是" if question.is_verified else "否",
                question.creator_full_name or f"用户ID:{question.creator_id}",
                question.created_at.strftime("%Y-%m-%d %H:%M:%S"),
                question.updated_at.strftime("%Y-%m-%d %H:%M:%S"),
            ]
        )

    # 设置响应头
    output.seek(0)
    csv_content = output.getvalue()
    csv_bytes = csv_content.encode("utf-8-sig")  # 使用utf-8-sig以支持Excel打开
    return StreamingResponse(
        io.BytesIO(csv_bytes),
        media_type="text/csv",
        headers={
            "Content-Disposition": "attachment; filename=seed_questions_export.csv"
        },
    )


@router.post(
    "/admin/types", response_model=QuestionType, status_code=status.HTTP_201_CREATED
)
def create_question_type(
    question_type: QuestionTypeCreate,
    db: Session = Depends(get_db),
    _: User = Depends(get_current_superuser),
):
    """创建类型/亚类(管理员)"""
    # 检查是否已存在
    existing = QuestionTypeCRUD.get_by_type_subtype(
        db=db, type=question_type.type, subtype=question_type.subtype
    )
    if existing:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=f"类型 '{question_type.type}' 和亚类 '{question_type.subtype}' 已存在",
        )

    return QuestionTypeCRUD.create(db=db, question_type=question_type)


@router.get("/admin/types", response_model=List[QuestionType])
def list_question_types(
    skip: int = 0,
    limit: int = 1000,
    db: Session = Depends(get_db),
    _: User = Depends(get_current_superuser),
):
    """获取所有类型/亚类(管理员)"""
    return QuestionTypeCRUD.get_all(db=db, skip=skip, limit=limit)


@router.put("/admin/types/{type_id}", response_model=QuestionType)
def update_question_type(
    type_id: int,
    question_type_update: QuestionTypeUpdate,
    db: Session = Depends(get_db),
    _: User = Depends(get_current_superuser),
):
    """更新类型/亚类(管理员)"""
    # 检查是否存在
    existing = QuestionTypeCRUD.get_by_id(db, type_id=type_id)
    if not existing:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=f"类型 ID {type_id} 不存在",
        )

    updated = QuestionTypeCRUD.update(
        db=db, type_id=type_id, question_type_update=question_type_update
    )
    if not updated:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="更新失败,可能是类型和亚类组合已存在",
        )

    return updated


@router.delete("/admin/types/{type_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_question_type(
    type_id: int,
    db: Session = Depends(get_db),
    _: User = Depends(get_current_superuser),
):
    """删除类型/亚类(管理员)"""
    # 检查是否存在
    existing = QuestionTypeCRUD.get_by_id(db, type_id=type_id)
    if not existing:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=f"类型 ID {type_id} 不存在",
        )

    success = QuestionTypeCRUD.delete(db=db, type_id=type_id)
    if not success:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=f"类型 ID {type_id} 不存在",
        )

    return None