TopicService.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. /**
  8. * 题目管理-服务类
  9. * Class TopicService
  10. */
  11. class TopicService extends BaseService
  12. {
  13. protected static $instance = null;
  14. protected $model = null;
  15. public function __construct()
  16. {
  17. $this->model = new ExamTopicModel();
  18. }
  19. public static function make()
  20. {
  21. if (!self::$instance) {
  22. self::$instance = new TopicService();
  23. }
  24. return self::$instance;
  25. }
  26. public function customList($param, $pageSize = 15)
  27. {
  28. $query = $this->model->where('mark', 1);
  29. if (!empty($param['paper_id'])) {
  30. $query->where('paper_id', $param['paper_id']);
  31. }
  32. if (!empty($param['keyword'])) {
  33. $query->where(function ($q) use ($param) {
  34. $q->where('id', $param['keyword'])
  35. ->orWhere('topic_name', 'like', "%{$param['keyword']}%");
  36. });
  37. }
  38. $list = $query->orderBy('sort', 'desc')->paginate($pageSize > 0 ? $pageSize : 9999999);
  39. $list = $list ? $list->toArray() : [];
  40. // 要处理的字段
  41. $fields = ['poster', 'topic_analysis', 'topic_name'];
  42. $items = isset($list['data']) ? $list['data'] : [];
  43. foreach ($items as &$item) {
  44. // 1-图片 2-文字
  45. if ($item['answer_type'] == 1 && in_array($item['topic_type'], ['填空题', '简单题'])) {
  46. $fields[] = 'correct_answer';
  47. }
  48. if (!empty($item['show_type']) && (int) $item['show_type'] === 2) {
  49. // 图片型 → 转为完整图片路径(支持多图)
  50. foreach ($fields as $field) {
  51. if (!empty($item[$field])) {
  52. $item[$field] = format_image_field($item[$field]);
  53. }
  54. }
  55. } else {
  56. // 文本型 → 保持原样(如果你想裁剪空格可以加 trim)
  57. foreach ($fields as $field) {
  58. if (!empty($item[$field])) {
  59. $item[$field] = trim($item[$field]);
  60. }
  61. }
  62. }
  63. }
  64. return [
  65. 'pageSize' => $pageSize,
  66. 'total' => isset($list['total']) ? $list['total'] : 0,
  67. 'list' => $items
  68. ];
  69. }
  70. /**
  71. * 排序更新
  72. * @param array $param
  73. * - paper_id
  74. * - sort_data = [{id:xx, sort:xx}, ...]
  75. * @return array
  76. */
  77. public function sort($param)
  78. {
  79. $paperId = $param['paper_id'] ?? 0;
  80. $sortData = $param['sort_data'] ?? [];
  81. if (empty($paperId) || empty($sortData)) {
  82. return message("参数错误", false);
  83. }
  84. DB::beginTransaction();
  85. try {
  86. // 先更新前端传来的顺序
  87. foreach ($sortData as $item) {
  88. $this->model
  89. ->where('id', $item['id'])
  90. ->where('paper_id', $paperId)
  91. ->update([
  92. 'sort' => $item['sort'],
  93. 'update_time' => time()
  94. ]);
  95. }
  96. // 再统一重新编号,保证唯一性 + 倒序
  97. $topics = $this->model
  98. ->where('paper_id', $paperId)
  99. ->orderByDesc('sort')
  100. ->get();
  101. $newSort = count($topics);
  102. foreach ($topics as $topic) {
  103. $this->model
  104. ->where('id', $topic->id)
  105. ->update(['sort' => $newSort--]);
  106. }
  107. DB::commit();
  108. return message("排序更新成功", true);
  109. } catch (\Exception $e) {
  110. DB::rollBack();
  111. return message("排序更新失败:" . $e->getMessage(), false);
  112. }
  113. }
  114. /**
  115. * 编辑或新增视频集
  116. */
  117. public function edit()
  118. {
  119. $data = request()->all();
  120. // 定义需要处理的字段
  121. $fields = ['poster', 'topic_analysis', 'topic_name', 'correct_answer'];
  122. foreach ($fields as $field) {
  123. if (!empty($data[$field])) {
  124. // 如果 show_type == 2 → 处理为图片路径
  125. if (!empty($data['show_type']) && (int) $data['show_type'] === 2) {
  126. if (is_array($data[$field])) {
  127. $data[$field] = array_map(function ($img) {
  128. return get_image_path(trim($img));
  129. }, $data[$field]);
  130. $data[$field] = implode(',', $data[$field]); // 多图用逗号拼接
  131. } else {
  132. $data[$field] = get_image_path(trim($data[$field]));
  133. }
  134. } else {
  135. // show_type != 2 → 文本,直接 trim
  136. if (is_array($data[$field])) {
  137. $data[$field] = implode(',', array_map('trim', $data[$field]));
  138. } else {
  139. $data[$field] = trim($data[$field]);
  140. }
  141. }
  142. }
  143. }
  144. $id = $data['id'] ?? 0;
  145. // 默认字段处理
  146. $data['update_time'] = time();
  147. if (!$id) {
  148. $data['create_time'] = time();
  149. }
  150. return parent::edit($data); // 调用父类的 edit 方法
  151. }
  152. /**
  153. * 删除七天之前标记软删除的数据
  154. */
  155. public function delete()
  156. {
  157. // 设置日志标题
  158. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除题目信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  159. ActionLogModel::record();
  160. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  161. return parent::delete();
  162. }
  163. }