| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- <?php
- namespace App\Services\Exam;
- use App\Models\ExamTopicModel;
- use App\Models\ActionLogModel;
- use App\Services\BaseService;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- /**
- * 题目管理-服务类
- * Class TopicService
- */
- class TopicService extends BaseService
- {
- protected static $instance = null;
- protected $model = null;
- public function __construct()
- {
- $this->model = new ExamTopicModel();
- }
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = new TopicService();
- }
- return self::$instance;
- }
- public function customList($param, $pageSize = 15)
- {
- $query = $this->model->where('mark', 1);
- if (!empty($param['paper_id'])) {
- $query->where('paper_id', $param['paper_id']);
- }
- if (!empty($param['keyword'])) {
- $query->where(function ($q) use ($param) {
- $q->where('id', $param['keyword'])
- ->orWhere('topic_name', 'like', "%{$param['keyword']}%");
- });
- }
- $list = $query->orderBy('sort', 'desc')->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list ? $list->toArray() : [];
- // 要处理的字段
- $fields = ['poster', 'topic_analysis', 'topic_name'];
- $items = isset($list['data']) ? $list['data'] : [];
- foreach ($items as &$item) {
- // 1-图片 2-文字
- if ($item['answer_type'] == 1 && in_array($item['topic_type'], ['填空题', '简单题'])) {
- $fields[] = 'correct_answer';
- }
- if (!empty($item['show_type']) && (int) $item['show_type'] === 2) {
- // 图片型 → 转为完整图片路径(支持多图)
- foreach ($fields as $field) {
- if (!empty($item[$field])) {
- $item[$field] = format_image_field($item[$field]);
- }
- }
- } else {
- // 文本型 → 保持原样(如果你想裁剪空格可以加 trim)
- foreach ($fields as $field) {
- if (!empty($item[$field])) {
- $item[$field] = trim($item[$field]);
- }
- }
- }
- // $topicType = isset($item['topic_type'])?$item['topic_type'] : 0;
- // if($topicType=='多选题'){
- //
- // }
- }
- return [
- 'pageSize' => $pageSize,
- 'total' => isset($list['total']) ? $list['total'] : 0,
- 'list' => $items
- ];
- }
- /**
- * 排序更新
- * @param array $param
- * - paper_id
- * - sort_data = [{id:xx, sort:xx}, ...]
- * @return array
- */
- public function sort($param)
- {
- $paperId = $param['paper_id'] ?? 0;
- $sortData = $param['sort_data'] ?? [];
- if (empty($paperId) || empty($sortData)) {
- return message("参数错误", false);
- }
- DB::beginTransaction();
- try {
- // 先更新前端传来的顺序
- foreach ($sortData as $item) {
- $this->model
- ->where('id', $item['id'])
- ->where('paper_id', $paperId)
- ->update([
- 'sort' => $item['sort'],
- 'update_time' => time()
- ]);
- }
- // 再统一重新编号,保证唯一性 + 倒序
- $topics = $this->model
- ->where('paper_id', $paperId)
- ->orderByDesc('sort')
- ->get();
- $newSort = count($topics);
- foreach ($topics as $topic) {
- $this->model
- ->where('id', $topic->id)
- ->update(['sort' => $newSort--]);
- }
- DB::commit();
- return message("排序更新成功", true);
- } catch (\Exception $e) {
- DB::rollBack();
- return message("排序更新失败:" . $e->getMessage(), false);
- }
- }
- /**
- * 编辑或新增题目
- */
- public function edit()
- {
- $data = request()->all();
- // 定义需要处理的字段
- $fields = ['poster', 'topic_analysis', 'topic_name', 'correct_answer'];
- foreach ($fields as $field) {
- if (!empty($data[$field])) {
- // 如果 show_type == 2 → 处理为图片路径
- if (!empty($data['show_type']) && (int) $data['show_type'] === 2) {
- if (is_array($data[$field])) {
- $data[$field] = array_map(function ($img) {
- return get_image_path(trim($img));
- }, $data[$field]);
- $data[$field] = implode(',', $data[$field]); // 多图用逗号拼接
- } else {
- $data[$field] = get_image_path(trim($data[$field]));
- }
- } else {
- // show_type != 2 → 文本,直接 trim
- if (is_array($data[$field])) {
- $data[$field] = implode(',', array_map('trim', $data[$field]));
- } else {
- $data[$field] = trim($data[$field]);
- }
- }
- }
- }
- $id = $data['id'] ?? 0;
- $paperId = $data['paper_id'] ?? 0;
- // 默认字段处理
- $data['update_time'] = time();
- if (!$id) {
- $data['create_time'] = time();
- }
- $data['answer_A'] = $data['answer_A']? $data['answer_A'] : '';
- $data['answer_B'] = $data['answer_B']? $data['answer_B'] : '';
- $data['answer_C'] = $data['answer_C']? $data['answer_C'] : '';
- $data['answer_D'] = $data['answer_D']? $data['answer_D'] : '';
- $data['answer_E'] = $data['answer_E']? $data['answer_E'] : '';
- $data['answer_F'] = $data['answer_F']? $data['answer_F'] : '';
- $data['topic_analysis'] = $data['topic_analysis']? $data['topic_analysis'] : '';
- if($data['topic_type'] == '判断题'){
- $data['answer_A'] = $data['answer_A']? $data['answer_A'] : '正确';
- $data['answer_B'] = $data['answer_B']? $data['answer_B'] : '错误';
- $data['answer_C'] = $data['answer_C']? $data['answer_C'] : '';
- $data['answer_D'] = $data['answer_D']? $data['answer_D'] : '';
- $data['answer_E'] = $data['answer_E']? $data['answer_E'] : '';
- }
- if($data['sort']<=0){
- $data['sort'] = time();
- }
- // 调用父类的 edit 方法
- $result = parent::edit($data);
- // 如果保存成功,更新试卷统计信息
- if ($result['code'] == 0 && $paperId) {
- $this->updatePaperStatistics($paperId);
- }
- return $result;
- }
- /**
- * 更新试卷统计信息(总分和题目数量)
- */
- private function updatePaperStatistics($paperId)
- {
- try {
- // 获取该试卷下所有有效题目的统计信息
- $statistics = $this->model
- ->where('paper_id', $paperId)
- ->where('mark', 1)
- ->selectRaw('COUNT(*) as topic_count, SUM(score) as score_total')
- ->first();
- if ($statistics) {
- // 更新试卷表
- DB::table('exam_papers')
- ->where('id', $paperId)
- ->update([
- 'topic_count' => $statistics->topic_count ?? 0,
- 'score_total' => $statistics->score_total ?? 0,
- 'update_time' => time()
- ]);
- }
- } catch (\Exception $e) {
- // 记录错误但不影响题目保存
- Log::error('更新试卷统计信息失败: ' . $e->getMessage());
- }
- }
- /**
- * 删除七天之前标记软删除的数据
- */
- public function delete()
- {
- // 设置日志标题
- ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除题目信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
- ActionLogModel::record();
- // 获取要删除的题目信息
- $id = request()->input('id', 0);
- $topic = $this->model->find($id);
- $paperId = $topic ? $topic->paper_id : 0;
- // 执行删除操作
- $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
- $result = parent::delete();
- // 如果删除成功,更新试卷统计信息
- if ($result['code'] == 0 && $paperId) {
- $this->updatePaperStatistics($paperId);
- }
- return $result;
- }
- }
|