| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- <?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')->orderBy('id', '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'] : []
- ];
- }
- 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 = floatval($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 = round($totalScore, 2);
- $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()];
- }
- }
- /**
- * 获取记录详情
- * @return array
- * @since 2020/11/11
- * @author laravel开发员
- */
- public function info()
- {
- // 记录ID
- $id = request()->input("id", 0);
- $info = [];
- if ($id) {
- $info = $this->model->getInfo($id);
- // 如果试题数量或总分为空,从数据库重新获取
- if ($info && (empty($info['topic_count']) || empty($info['score_total']))) {
- // 获取该试卷下所有有效题目的统计信息
- $topicModel = new ExamTopicModel();
- $statistics = $topicModel
- ->where('paper_id', $id)
- ->where('mark', 1)
- ->selectRaw('COUNT(*) as topic_count, SUM(score) as score_total')
- ->first();
- if ($statistics) {
- // 如果试题数量为空,使用数据库中的值
- if (empty($info['topic_count'])) {
- $info['topic_count'] = intval($statistics->topic_count ?? 0);
- }
- // 如果总分为空,使用数据库中的值
- if (empty($info['score_total'])) {
- $info['score_total'] = floatval($statistics->score_total ?? 0);
- }
- }
- }
- }
- return message(MESSAGE_OK, true, $info);
- }
- /**
- * 添加或编辑记录
- * @return array
- * @since 2020/11/11
- * @author laravel开发员
- */
- public function edit()
- {
- // 获取参数
- $argList = func_get_args();
- // 查询条件
- $data = isset($argList[0]) ? $argList[0] : [];
- // 是否打印SQL
- $is_sql = isset($argList[1]) ? $argList[1] : false;
- if (!$data) {
- $data = request()->all();
- }
- // 获取试卷ID
- $paperId = isset($data['id']) ? intval($data['id']) : 0;
- // 如果试题数量或总分为空,从数据库重新获取
- // 检查字段是否为 null、未设置、空字符串或0(如果为空,从数据库重新计算)
- $topicCountEmpty = !isset($data['topic_count']) || $data['topic_count'] === null || $data['topic_count'] === '' || $data['topic_count'] === 0;
- $scoreTotalEmpty = !isset($data['score_total']) || $data['score_total'] === null || $data['score_total'] === '' || $data['score_total'] === 0;
- if ($paperId > 0 && ($topicCountEmpty || $scoreTotalEmpty)) {
- // 获取该试卷下所有有效题目的统计信息
- $topicModel = new ExamTopicModel();
- $statistics = $topicModel
- ->where('paper_id', $paperId)
- ->where('mark', 1)
- ->selectRaw('COUNT(*) as topic_count, SUM(score) as score_total')
- ->first();
- if ($statistics) {
- // 如果试题数量为空,使用数据库中的值
- if ($topicCountEmpty) {
- $data['topic_count'] = intval($statistics->topic_count ?? 0);
- }
- // 如果总分为空,使用数据库中的值
- if ($scoreTotalEmpty) {
- $data['score_total'] = floatval($statistics->score_total ?? 0);
- }
- }
- }
- // 调用父类的 edit 方法
- $error = '';
- $rowId = $this->model->edit($data, $error, $is_sql);
- if ($rowId) {
- return message(MESSAGE_OK, true, ['id' => $rowId]);
- }
- return message($error, false);
- }
- /**
- * 删除七天之前标记软删除的数据
- */
- 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();
- }
- }
|