TopicService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace App\Services\Exam;
  3. use App\Models\ExamTopic;
  4. use App\Models\ExamTopicModel;
  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', 'correct_answer'];
  42. $items = isset($list['data']) ? $list['data'] : [];
  43. foreach ($items as &$item) {
  44. if (!empty($item['show_type']) && (int) $item['show_type'] === 2) {
  45. // 图片型 → 转为完整图片路径(支持多图)
  46. foreach ($fields as $field) {
  47. if (!empty($item[$field])) {
  48. $item[$field] = format_image_field($item[$field]);
  49. }
  50. }
  51. } else {
  52. // 文本型 → 保持原样(如果你想裁剪空格可以加 trim)
  53. foreach ($fields as $field) {
  54. if (!empty($item[$field])) {
  55. $item[$field] = trim($item[$field]);
  56. }
  57. }
  58. }
  59. }
  60. return [
  61. 'pageSize' => $pageSize,
  62. 'total' => isset($list['total']) ? $list['total'] : 0,
  63. 'list' => $items
  64. ];
  65. }
  66. /**
  67. * 排序更新
  68. * @param array $param
  69. * - paper_id
  70. * - sort_data = [{id:xx, sort:xx}, ...]
  71. * @return array
  72. */
  73. public function sort($param)
  74. {
  75. $paperId = $param['paper_id'] ?? 0;
  76. $sortData = $param['sort_data'] ?? [];
  77. if (empty($paperId) || empty($sortData)) {
  78. return message("参数错误", false);
  79. }
  80. DB::beginTransaction();
  81. try {
  82. // 先更新前端传来的顺序
  83. foreach ($sortData as $item) {
  84. $this->model
  85. ->where('id', $item['id'])
  86. ->where('paper_id', $paperId)
  87. ->update([
  88. 'sort' => $item['sort'],
  89. 'update_time' => time()
  90. ]);
  91. }
  92. // 再统一重新编号,保证唯一性 + 倒序
  93. $topics = $this->model
  94. ->where('paper_id', $paperId)
  95. ->orderByDesc('sort')
  96. ->get();
  97. $newSort = count($topics);
  98. foreach ($topics as $topic) {
  99. $this->model
  100. ->where('id', $topic->id)
  101. ->update(['sort' => $newSort--]);
  102. }
  103. DB::commit();
  104. return message("排序更新成功", true);
  105. } catch (\Exception $e) {
  106. DB::rollBack();
  107. return message("排序更新失败:" . $e->getMessage(), false);
  108. }
  109. }
  110. /**
  111. * 编辑或新增视频集
  112. */
  113. public function edit()
  114. {
  115. $data = request()->all();
  116. // 定义需要处理的字段
  117. $fields = ['poster', 'topic_analysis', 'topic_name', 'correct_answer'];
  118. foreach ($fields as $field) {
  119. if (!empty($data[$field])) {
  120. // 如果 show_type == 2 → 处理为图片路径
  121. if (!empty($data['show_type']) && (int) $data['show_type'] === 2) {
  122. if (is_array($data[$field])) {
  123. $data[$field] = array_map(function ($img) {
  124. return get_image_path(trim($img));
  125. }, $data[$field]);
  126. $data[$field] = implode(',', $data[$field]); // 多图用逗号拼接
  127. } else {
  128. $data[$field] = get_image_path(trim($data[$field]));
  129. }
  130. } else {
  131. // show_type != 2 → 文本,直接 trim
  132. if (is_array($data[$field])) {
  133. $data[$field] = implode(',', array_map('trim', $data[$field]));
  134. } else {
  135. $data[$field] = trim($data[$field]);
  136. }
  137. }
  138. }
  139. }
  140. $id = $data['id'] ?? 0;
  141. // 默认字段处理
  142. $data['update_time'] = time();
  143. if (!$id) {
  144. $data['create_time'] = time();
  145. }
  146. return parent::edit($data); // 调用父类的 edit 方法
  147. }
  148. }