PaperService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace App\Services\Exam;
  3. use App\Models\ExamPaperModel;
  4. use App\Models\ExamTopicModel;
  5. use App\Models\ActionLogModel;
  6. use App\Services\BaseService;
  7. use Illuminate\Support\Facades\DB;
  8. /**
  9. * 试卷管理-服务类
  10. * Class PaperService
  11. */
  12. class PaperService extends BaseService
  13. {
  14. protected static $instance = null;
  15. protected $model = null;
  16. public function __construct()
  17. {
  18. $this->model = new ExamPaperModel();
  19. }
  20. public static function make()
  21. {
  22. if (!self::$instance) {
  23. self::$instance = new PaperService();
  24. }
  25. return self::$instance;
  26. }
  27. public function customList($params, $pageSize = 15)
  28. {
  29. $query = $this->model->where('mark', 1);
  30. if (!empty($params['scene_type'])) {
  31. $query->where('scene_type', $params['scene_type']);
  32. }
  33. if (!empty($params['type'])) {
  34. $query->where('type', $params['type']);
  35. }
  36. if (!empty($params['subject_id'])) {
  37. $query->where('subject_id', $params['subject_id']);
  38. }
  39. if (!empty($params['is_charge'])) {
  40. $query->where('is_charge', $params['is_charge']);
  41. }
  42. if (!empty($params['keyword'])) {
  43. $query->where(function ($q) use ($params) {
  44. $q->where('id', $params['keyword'])
  45. ->orWhere('name', 'like', "%{$params['keyword']}%");
  46. });
  47. }
  48. $list = $query->orderBy('sort', 'desc')->paginate($pageSize > 0 ? $pageSize : 9999999);
  49. $list = $list ? $list->toArray() : [];
  50. return [
  51. 'pageSize' => $pageSize,
  52. 'total' => isset($list['total']) ? $list['total'] : 0,
  53. 'list' => isset($list['data']) ? $list['data'] : []
  54. ];
  55. return message("操作成功", true, $list);
  56. }
  57. public function createPaperWithTopics(array $formData, array $topicsData)
  58. {
  59. if (empty($formData) || empty($topicsData)) {
  60. return ['code' => 1, 'msg' => '表单数据或题目数据不能为空'];
  61. }
  62. DB::beginTransaction();
  63. try {
  64. $paperData = array_merge($formData, [
  65. 'mark' => 1,
  66. 'create_time' => time(),
  67. 'score_total' => 0,
  68. 'topic_count' => 0,
  69. ]);
  70. // 创建试卷
  71. $paperId = $this->model->edit($paperData);
  72. // 重新加载完整字段
  73. $paper = $this->model->find($paperId);
  74. $totalScore = 0;
  75. $topicCount = 0;
  76. foreach ($topicsData as $index => &$topic) {
  77. $score = intval($topic['score'] ?? 0);
  78. $totalScore += $score;
  79. $topicCount++;
  80. $topic = array_merge($topic, [
  81. 'paper_id' => $paper->id,
  82. 'sort' => count($topicsData) - $index,
  83. 'status' => 1,
  84. 'mark' => 1,
  85. 'create_time' => time(),
  86. ]);
  87. unset($topic['_id']);
  88. }
  89. // 批量插入题目
  90. ExamTopicModel::insert($topicsData);
  91. // 更新试卷总分和题目数量
  92. $paper->score_total = $totalScore;
  93. $paper->topic_count = $topicCount;
  94. $paper->save();
  95. DB::commit();
  96. return ['code' => 0, 'msg' => '创建试卷成功', 'data' => $paper];
  97. } catch (\Exception $e) {
  98. DB::rollBack();
  99. return ['code' => 1, 'msg' => '创建失败:' . $e->getMessage()];
  100. }
  101. }
  102. /**
  103. * 删除七天之前标记软删除的数据
  104. */
  105. public function delete()
  106. {
  107. // 设置日志标题
  108. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除试卷信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  109. ActionLogModel::record();
  110. // 获取要删除的试卷ID
  111. $ids = request()->post('id', []);
  112. if (!is_array($ids)) {
  113. $ids = [$ids];
  114. }
  115. // 删除该试卷下关联的所有试题
  116. if (!empty($ids)) {
  117. $topicModel = new ExamTopicModel();
  118. // 查询该试卷下的所有试题
  119. $topicIds = $topicModel->whereIn('paper_id', $ids)
  120. ->where('mark', 1)
  121. ->pluck('id')
  122. ->toArray();
  123. if (!empty($topicIds)) {
  124. // 软删除试题(标记mark=0)
  125. $topicModel->whereIn('id', $topicIds)->update([
  126. 'mark' => 0,
  127. 'update_time' => time()
  128. ]);
  129. }
  130. }
  131. // 清理七天前标记软删除的数据
  132. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  133. $topicModel = new ExamTopicModel();
  134. $topicModel->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  135. return parent::delete();
  136. }
  137. }