problem.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from typing import Any, Dict, List
  4. from sqlalchemy import update
  5. from crud.paper import crud_question
  6. from crud.problem import crud_class_error_statistic, crud_student_error_statistic
  7. from db.asyncsession import LocalAsyncSession
  8. from models.paper import PaperQuestion
  9. from models.problem import StudentErrorQuestion
  10. from schemas.problem import ClassErrorQuestionInDB, StudentErrorQuestionInDB
  11. # 创建班级错题统计
  12. async def bgtask_create_class_error_statistic(school_id: int, grade_id: int, task_id: int,
  13. task_name: str, task_type: str, class_id: int,
  14. student_count: int, paper_id: int, school_name: str,
  15. grade_name: str, class_name: str):
  16. print("[bgTask] Create Class-Error-Statistics Starting!")
  17. db = LocalAsyncSession()
  18. # 根据试卷ID获取所有的客观题
  19. total, db_questions = await crud_question.find_all(db,
  20. filters=[PaperQuestion.pid == paper_id],
  21. return_fields=["id"])
  22. errors = []
  23. for x in db_questions:
  24. temp = ClassErrorQuestionInDB(qid=x.id,
  25. school_id=school_id,
  26. grade_id=grade_id,
  27. class_id=class_id,
  28. student_count=student_count,
  29. task_id=task_id,
  30. task_type=task_type,
  31. task_name=task_name,
  32. school_name=school_name,
  33. grade_name=grade_name,
  34. class_name=class_name)
  35. errors.append(temp)
  36. try:
  37. await crud_class_error_statistic.insert_many(db, errors)
  38. except Exception as ex:
  39. print(f"[ERROR]-[bgTask] 创建错题失败!原因:{str(ex)}")
  40. print("[bgTask] Create Class-Error-Statistics Successfully!")
  41. async def bgtask_create_student_error_statistic(student_errors: List[Dict[str, Any]] = None):
  42. """
  43. 创建每个学生的错题统计记录
  44. """
  45. db = LocalAsyncSession()
  46. print("[bgTask] Create Student-Error-Statistics Starting!")
  47. for stu in student_errors:
  48. # 查询学生错题统计是否存在
  49. db_error = await crud_student_error_statistic.find_one(
  50. db, filters={"student_id": stu["student_id"]})
  51. # 如果不存在就新建,否则就更新
  52. if not db_error:
  53. temp = StudentErrorQuestionInDB(**stu)
  54. try:
  55. await crud_student_error_statistic.insert_one(db, temp)
  56. except Exception as ex:
  57. print(f"[ERROR]-[bgTask] 创建学生错题统计失败!原因:{str(ex)}")
  58. else:
  59. total_questions = db_error.total_questions + stu["total_questions"]
  60. error_ratio = round(db_error.total_errors / total_questions * 100, 2)
  61. stmt = (update(StudentErrorQuestion).where(
  62. StudentErrorQuestion.student_id == stu["student_id"]).values(
  63. total_questions=total_questions, error_ratio=error_ratio))
  64. await crud_student_error_statistic.increase(db, stmt)
  65. print("[bgTask] Create Student-Error-Statistics Successfully!")