| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace App\Services\Exam;
- use App\Models\ExamPaperModel;
- use App\Models\ExamTopicModel;
- use App\Models\ActionLogModel;
- use App\Services\BaseService;
- use Illuminate\Support\Facades\DB;
- /**
- * 试卷管理-服务类
- * Class PaperService
- */
- class PaperService extends BaseService
- {
- protected static $instance = null;
- protected $model = null;
- public function __construct()
- {
- $this->model = new ExamPaperModel();
- }
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = new PaperService();
- }
- return self::$instance;
- }
- public function customList($params, $pageSize = 15)
- {
- $query = $this->model->where('mark', 1);
- if (!empty($params['scene_type'])) {
- $query->where('scene_type', $params['scene_type']);
- }
- if (!empty($params['type'])) {
- $query->where('type', $params['type']);
- }
- if (!empty($params['subject_id'])) {
- $query->where('subject_id', $params['subject_id']);
- }
- if (!empty($params['is_charge'])) {
- $query->where('is_charge', $params['is_charge']);
- }
- if (!empty($params['keyword'])) {
- $query->where(function ($q) use ($params) {
- $q->where('id', $params['keyword'])
- ->orWhere('name', 'like', "%{$params['keyword']}%");
- });
- }
- $list = $query->orderBy('sort', 'desc')->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list ? $list->toArray() : [];
- return [
- 'pageSize' => $pageSize,
- 'total' => isset($list['total']) ? $list['total'] : 0,
- 'list' => isset($list['data']) ? $list['data'] : []
- ];
- return message("操作成功", true, $list);
- }
- public function createPaperWithTopics(array $formData, array $topicsData)
- {
- if (empty($formData) || empty($topicsData)) {
- return ['code' => 1, 'msg' => '表单数据或题目数据不能为空'];
- }
- DB::beginTransaction();
- try {
- $paperData = array_merge($formData, [
- 'mark' => 1,
- 'create_time' => time(),
- 'score_total' => 0,
- 'topic_count' => 0,
- ]);
- // 创建试卷
- $paperId = $this->model->edit($paperData);
- // 重新加载完整字段
- $paper = $this->model->find($paperId);
- $totalScore = 0;
- $topicCount = 0;
- foreach ($topicsData as $index => &$topic) {
- $score = intval($topic['score'] ?? 0);
- $totalScore += $score;
- $topicCount++;
- $topic = array_merge($topic, [
- 'paper_id' => $paper->id,
- 'sort' => count($topicsData) - $index,
- 'status' => 1,
- 'mark' => 1,
- 'create_time' => time(),
- ]);
- unset($topic['_id']);
- }
- // 批量插入题目
- ExamTopicModel::insert($topicsData);
- // 更新试卷总分和题目数量
- $paper->score_total = $totalScore;
- $paper->topic_count = $topicCount;
- $paper->save();
- DB::commit();
- return ['code' => 0, 'msg' => '创建试卷成功', 'data' => $paper];
- } catch (\Exception $e) {
- DB::rollBack();
- return ['code' => 1, 'msg' => '创建失败:' . $e->getMessage()];
- }
- }
- /**
- * 删除七天之前标记软删除的数据
- */
- public function delete()
- {
- // 设置日志标题
- ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除试卷信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
- ActionLogModel::record();
- // 获取要删除的试卷ID
- $ids = request()->post('id', []);
- if (!is_array($ids)) {
- $ids = [$ids];
- }
- // 删除该试卷下关联的所有试题
- if (!empty($ids)) {
- $topicModel = new ExamTopicModel();
- // 查询该试卷下的所有试题
- $topicIds = $topicModel->whereIn('paper_id', $ids)
- ->where('mark', 1)
- ->pluck('id')
- ->toArray();
- if (!empty($topicIds)) {
- // 软删除试题(标记mark=0)
- $topicModel->whereIn('id', $topicIds)->update([
- 'mark' => 0,
- 'update_time' => time()
- ]);
- }
- }
- // 清理七天前标记软删除的数据
- $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
- $topicModel = new ExamTopicModel();
- $topicModel->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
- return parent::delete();
- }
- }
|