xiangpei
2024-05-09 7fead3526920951ed8f68d0f9aaf23fe632158ae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.mindskip.xzs.service.impl;
 
import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.base.RestResponse;
import com.mindskip.xzs.context.WebContext;
import com.mindskip.xzs.domain.ExamPaperAnswer;
import com.mindskip.xzs.domain.SelfPractice;
import com.mindskip.xzs.domain.vo.SelfPracticeVO;
import com.mindskip.xzs.repository.QuestionSubjectMapper;
import com.mindskip.xzs.repository.SelfPracticeMapper;
import com.mindskip.xzs.repository.SubjectMapper;
import com.mindskip.xzs.service.QuestionSubjectService;
import com.mindskip.xzs.service.SelfPracticeService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * @author:xp
 * @date:2024/5/9 16:16
 */
@Service
@RequiredArgsConstructor
public class SelfPracticeServiceImpl implements SelfPracticeService {
 
    private final SelfPracticeMapper selfPracticeMapper;
    private final WebContext webContext;
    private final QuestionSubjectMapper questionSubjectMapper;
    private final SubjectMapper subjectMapper;
 
    @Override
    public RestResponse add(SelfPracticeVO vo) {
        SelfPractice entity = new SelfPractice();
        BeanUtils.copyProperties(vo, entity);
        entity.setSubjects(JSON.toJSONString(vo.getSubjects()));
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        entity.setDeleted(0);
        entity.setDoNum(0);
        entity.setUserId(webContext.getCurrentUser().getId());
        selfPracticeMapper.add(entity);
        return RestResponse.ok("创建成功");
    }
 
    @Override
    public RestResponse page(SelfPracticeVO query) {
        if (! Objects.equals(3, webContext.getCurrentUser().getRole())) {
            query.setUserId(webContext.getCurrentUser().getId());
        }
        PageInfo<SelfPracticeVO> info = PageHelper.startPage(query.getPageNum(), query.getPageSize()).doSelectPageInfo(() ->
                selfPracticeMapper.page(query));
        info.getList().stream().forEach(item -> {
            List<Integer> subjects = JSON.parseArray(item.getSubjectString(), Integer.class);
            item.setSubjects(subjects);
            List<String> subjectNames = subjectMapper.selectSubjectName(subjects);
            if (! CollectionUtils.isEmpty(subjectNames)) {
                item.setSubjectNames(subjectNames.stream().collect(Collectors.joining("、")));
            }
            item.setTotalQuestionNum(questionSubjectMapper.countQuestionNum(subjects));
        });
        return RestResponse.ok(info);
    }
 
    @Override
    public RestResponse remove(List<Integer> ids) {
        selfPracticeMapper.remove(ids);
        return RestResponse.ok("删除成功");
    }
}