TopicService.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace App\Services\Exam;
  3. use App\Models\ExamTopicModel;
  4. use App\Models\ActionLogModel;
  5. use App\Services\BaseService;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Log;
  8. /**
  9. * 题目管理-服务类
  10. * Class TopicService
  11. */
  12. class TopicService extends BaseService
  13. {
  14. protected static $instance = null;
  15. protected $model = null;
  16. public function __construct()
  17. {
  18. $this->model = new ExamTopicModel();
  19. }
  20. public static function make()
  21. {
  22. if (!self::$instance) {
  23. self::$instance = new TopicService();
  24. }
  25. return self::$instance;
  26. }
  27. public function customList($param, $pageSize = 15)
  28. {
  29. $query = $this->model->where('mark', 1);
  30. if (!empty($param['paper_id'])) {
  31. $query->where('paper_id', $param['paper_id']);
  32. }
  33. if (!empty($param['keyword'])) {
  34. $query->where(function ($q) use ($param) {
  35. $q->where('id', $param['keyword'])
  36. ->orWhere('topic_name', 'like', "%{$param['keyword']}%");
  37. });
  38. }
  39. $list = $query->orderBy('sort', 'desc')->paginate($pageSize > 0 ? $pageSize : 9999999);
  40. $list = $list ? $list->toArray() : [];
  41. // 要处理的字段
  42. $fields = ['poster', 'topic_analysis', 'topic_name'];
  43. $items = isset($list['data']) ? $list['data'] : [];
  44. foreach ($items as &$item) {
  45. // 1-图片 2-文字
  46. if ($item['answer_type'] == 1 && in_array($item['topic_type'], ['填空题', '简单题'])) {
  47. $fields[] = 'correct_answer';
  48. }
  49. if (!empty($item['show_type']) && (int) $item['show_type'] === 2) {
  50. // 图片型 → 转为完整图片路径(支持多图)
  51. foreach ($fields as $field) {
  52. if (!empty($item[$field])) {
  53. $item[$field] = format_image_field($item[$field]);
  54. }
  55. }
  56. } else {
  57. // 文本型 → 保持原样(如果你想裁剪空格可以加 trim)
  58. foreach ($fields as $field) {
  59. if (!empty($item[$field])) {
  60. $item[$field] = trim($item[$field]);
  61. }
  62. }
  63. }
  64. // $topicType = isset($item['topic_type'])?$item['topic_type'] : 0;
  65. // if($topicType=='多选题'){
  66. //
  67. // }
  68. }
  69. return [
  70. 'pageSize' => $pageSize,
  71. 'total' => isset($list['total']) ? $list['total'] : 0,
  72. 'list' => $items
  73. ];
  74. }
  75. /**
  76. * 排序更新
  77. * @param array $param
  78. * - paper_id
  79. * - sort_data = [{id:xx, sort:xx}, ...]
  80. * @return array
  81. */
  82. public function sort($param)
  83. {
  84. $paperId = $param['paper_id'] ?? 0;
  85. $sortData = $param['sort_data'] ?? [];
  86. if (empty($paperId) || empty($sortData)) {
  87. return message("参数错误", false);
  88. }
  89. DB::beginTransaction();
  90. try {
  91. // 先更新前端传来的顺序
  92. foreach ($sortData as $item) {
  93. $this->model
  94. ->where('id', $item['id'])
  95. ->where('paper_id', $paperId)
  96. ->update([
  97. 'sort' => $item['sort'],
  98. 'update_time' => time()
  99. ]);
  100. }
  101. // 再统一重新编号,保证唯一性 + 倒序
  102. $topics = $this->model
  103. ->where('paper_id', $paperId)
  104. ->orderByDesc('sort')
  105. ->get();
  106. $newSort = count($topics);
  107. foreach ($topics as $topic) {
  108. $this->model
  109. ->where('id', $topic->id)
  110. ->update(['sort' => $newSort--]);
  111. }
  112. DB::commit();
  113. return message("排序更新成功", true);
  114. } catch (\Exception $e) {
  115. DB::rollBack();
  116. return message("排序更新失败:" . $e->getMessage(), false);
  117. }
  118. }
  119. /**
  120. * 编辑或新增题目
  121. */
  122. public function edit()
  123. {
  124. $data = request()->all();
  125. // 定义需要处理的字段
  126. $fields = ['poster', 'topic_analysis', 'topic_name', 'correct_answer'];
  127. foreach ($fields as $field) {
  128. if (!empty($data[$field])) {
  129. // 如果 show_type == 2 → 处理为图片路径
  130. if (!empty($data['show_type']) && (int) $data['show_type'] === 2) {
  131. if (is_array($data[$field])) {
  132. $data[$field] = array_map(function ($img) {
  133. return get_image_path(trim($img));
  134. }, $data[$field]);
  135. $data[$field] = implode(',', $data[$field]); // 多图用逗号拼接
  136. } else {
  137. $data[$field] = get_image_path(trim($data[$field]));
  138. }
  139. } else {
  140. // show_type != 2 → 文本,直接 trim
  141. if (is_array($data[$field])) {
  142. $data[$field] = implode(',', array_map('trim', $data[$field]));
  143. } else {
  144. $data[$field] = trim($data[$field]);
  145. }
  146. }
  147. }
  148. }
  149. $id = $data['id'] ?? 0;
  150. $paperId = $data['paper_id'] ?? 0;
  151. // 默认字段处理
  152. $data['update_time'] = time();
  153. if (!$id) {
  154. $data['create_time'] = time();
  155. }
  156. $data['answer_A'] = $data['answer_A']? $data['answer_A'] : '';
  157. $data['answer_B'] = $data['answer_B']? $data['answer_B'] : '';
  158. $data['answer_C'] = $data['answer_C']? $data['answer_C'] : '';
  159. $data['answer_D'] = $data['answer_D']? $data['answer_D'] : '';
  160. $data['answer_E'] = $data['answer_E']? $data['answer_E'] : '';
  161. $data['answer_F'] = $data['answer_F']? $data['answer_F'] : '';
  162. $data['topic_analysis'] = $data['topic_analysis']? $data['topic_analysis'] : '';
  163. if($data['topic_type'] == '判断题'){
  164. $data['answer_A'] = $data['answer_A']? $data['answer_A'] : '正确';
  165. $data['answer_B'] = $data['answer_B']? $data['answer_B'] : '错误';
  166. $data['answer_C'] = $data['answer_C']? $data['answer_C'] : '';
  167. $data['answer_D'] = $data['answer_D']? $data['answer_D'] : '';
  168. $data['answer_E'] = $data['answer_E']? $data['answer_E'] : '';
  169. }
  170. if($data['sort']<=0){
  171. $data['sort'] = time();
  172. }
  173. // 调用父类的 edit 方法
  174. $result = parent::edit($data);
  175. // 如果保存成功,更新试卷统计信息
  176. if ($result['code'] == 0 && $paperId) {
  177. $this->updatePaperStatistics($paperId);
  178. }
  179. return $result;
  180. }
  181. /**
  182. * 更新试卷统计信息(总分和题目数量)
  183. */
  184. private function updatePaperStatistics($paperId)
  185. {
  186. try {
  187. // 获取该试卷下所有有效题目的统计信息
  188. $statistics = $this->model
  189. ->where('paper_id', $paperId)
  190. ->where('mark', 1)
  191. ->selectRaw('COUNT(*) as topic_count, SUM(score) as score_total')
  192. ->first();
  193. if ($statistics) {
  194. // 更新试卷表
  195. DB::table('exam_papers')
  196. ->where('id', $paperId)
  197. ->update([
  198. 'topic_count' => $statistics->topic_count ?? 0,
  199. 'score_total' => $statistics->score_total ?? 0,
  200. 'update_time' => time()
  201. ]);
  202. }
  203. } catch (\Exception $e) {
  204. // 记录错误但不影响题目保存
  205. Log::error('更新试卷统计信息失败: ' . $e->getMessage());
  206. }
  207. }
  208. /**
  209. * 删除七天之前标记软删除的数据
  210. */
  211. public function delete()
  212. {
  213. // 设置日志标题
  214. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除题目信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  215. ActionLogModel::record();
  216. // 获取要删除的题目信息
  217. $id = request()->input('id', 0);
  218. $topic = $this->model->find($id);
  219. $paperId = $topic ? $topic->paper_id : 0;
  220. // 执行删除操作
  221. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  222. $result = parent::delete();
  223. // 如果删除成功,更新试卷统计信息
  224. if ($result['code'] == 0 && $paperId) {
  225. $this->updatePaperStatistics($paperId);
  226. }
  227. return $result;
  228. }
  229. }