PaperService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App\Services\Exam;
  3. use App\Models\ExamPaperModel;
  4. use App\Models\ExamTopicModel;
  5. use App\Services\BaseService;
  6. use Illuminate\Support\Facades\DB;
  7. /**
  8. * 试卷管理-服务类
  9. * Class PaperService
  10. */
  11. class PaperService extends BaseService
  12. {
  13. protected static $instance = null;
  14. protected $model = null;
  15. public function __construct()
  16. {
  17. $this->model = new ExamPaperModel();
  18. }
  19. public static function make()
  20. {
  21. if (!self::$instance) {
  22. self::$instance = new PaperService();
  23. }
  24. return self::$instance;
  25. }
  26. public function customList($params, $pageSize = 15)
  27. {
  28. $query = $this->model->where('mark', operator: 1);
  29. if (!empty($params['scene_type'])) {
  30. $query->where('scene_type', $params['scene_type']);
  31. }
  32. if (!empty($params['type'])) {
  33. $query->where('type', $params['type']);
  34. }
  35. if (!empty($params['subject_id'])) {
  36. $query->where('subject_id', $params['subject_id']);
  37. }
  38. if (!empty($params['is_charge'])) {
  39. $query->where('is_charge', $params['is_charge']);
  40. }
  41. if (!empty($params['keyword'])) {
  42. $query->where(function ($q) use ($params) {
  43. $q->where('id', $params['keyword'])
  44. ->orWhere('name', 'like', "%{$params['keyword']}%");
  45. });
  46. }
  47. $list = $query->orderBy('sort', 'desc')->paginate($pageSize > 0 ? $pageSize : 9999999);
  48. $list = $list ? $list->toArray() : [];
  49. return [
  50. 'pageSize' => $pageSize,
  51. 'total' => isset($list['total']) ? $list['total'] : 0,
  52. 'list' => isset($list['data']) ? $list['data'] : []
  53. ];
  54. return message("操作成功", true, $list);
  55. }
  56. public function createPaperWithTopics(array $formData, array $topicsData)
  57. {
  58. if (empty($formData) || empty($topicsData)) {
  59. return ['code' => 1, 'msg' => '表单数据或题目数据不能为空'];
  60. }
  61. DB::beginTransaction();
  62. try {
  63. $paperData = array_merge($formData, [
  64. 'mark' => 1,
  65. 'create_time' => time(),
  66. 'score_total' => 0,
  67. 'topic_count' => 0,
  68. ]);
  69. // 创建试卷
  70. $paperId = $this->model->edit($paperData);
  71. // 重新加载完整字段
  72. $paper = $this->model->find($paperId);
  73. $totalScore = 0;
  74. $topicCount = 0;
  75. foreach ($topicsData as $index => &$topic) {
  76. $score = intval($topic['score'] ?? 0);
  77. $totalScore += $score;
  78. $topicCount++;
  79. $topic = array_merge($topic, [
  80. 'paper_id' => $paper->id,
  81. 'sort' => count($topicsData) - $index,
  82. 'status' => 1,
  83. 'mark' => 1,
  84. 'create_time' => time(),
  85. ]);
  86. unset($topic['_id']);
  87. }
  88. // 批量插入题目
  89. ExamTopicModel::insert($topicsData);
  90. // 更新试卷总分和题目数量
  91. $paper->score_total = $totalScore;
  92. $paper->topic_count = $topicCount;
  93. $paper->save();
  94. DB::commit();
  95. return ['code' => 0, 'msg' => '创建试卷成功', 'data' => $paper];
  96. } catch (\Exception $e) {
  97. DB::rollBack();
  98. return ['code' => 1, 'msg' => '创建失败:' . $e->getMessage()];
  99. }
  100. }
  101. }