TopicService.php 5.4 KB

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