xiangpei
2024-05-31 16d10cef208de048f8b325facd143c54b7be9963
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.ycl.jxkg.service.impl;
 
import com.ycl.jxkg.domain.ExamPaper;
import com.ycl.jxkg.domain.TaskExam;
import com.ycl.jxkg.domain.TextContent;
import com.ycl.jxkg.domain.User;
import com.ycl.jxkg.domain.task.TaskItemObject;
import com.ycl.jxkg.mapper.ExamPaperMapper;
import com.ycl.jxkg.mapper.TaskExamMapper;
import com.ycl.jxkg.service.TaskExamService;
import com.ycl.jxkg.service.TextContentService;
import com.ycl.jxkg.service.enums.ActionEnum;
import com.ycl.jxkg.utils.DateTimeUtil;
import com.ycl.jxkg.utils.JsonUtil;
import com.ycl.jxkg.vo.admin.exam.ExamResponseVO;
import com.ycl.jxkg.vo.admin.task.TaskPageRequestVO;
import com.ycl.jxkg.vo.admin.task.TaskRequestVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
@Service
public class TaskExamServiceImpl extends BaseServiceImpl<TaskExam> implements TaskExamService {
 
    private final TaskExamMapper taskExamMapper;
    private final TextContentService textContentService;
    private final ExamPaperMapper examPaperMapper;
 
    @Autowired
    public TaskExamServiceImpl(TaskExamMapper taskExamMapper, TextContentService textContentService, ExamPaperMapper examPaperMapper) {
        super(taskExamMapper);
        this.taskExamMapper = taskExamMapper;
        this.textContentService = textContentService;
        this.examPaperMapper = examPaperMapper;
    }
 
    @Override
    public PageInfo<TaskExam> page(TaskPageRequestVO requestVM) {
        return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() ->
                taskExamMapper.page(requestVM)
        );
    }
 
    @Override
    @Transactional
    public void edit(TaskRequestVO model, User user) {
        ActionEnum actionEnum = (model.getId() == null) ? ActionEnum.ADD : ActionEnum.UPDATE;
        TaskExam taskExam = new TaskExam();
 
        if (actionEnum == ActionEnum.ADD) {
            BeanUtils.copyProperties(model, taskExam);
            Date now = new Date();
            taskExam.setCreateUser(user.getId());
            taskExam.setCreateUserName(user.getUserName());
            taskExam.setCreateTime(now);
            taskExam.setDeleted(false);
 
            //保存任务结构
            TextContent textContent = textContentService.jsonConvertInsert(model.getPaperItems(), now, p -> {
                TaskItemObject taskItemObject = new TaskItemObject();
                taskItemObject.setExamPaperId(p.getId());
                taskItemObject.setExamPaperName(p.getName());
                return taskItemObject;
            });
            textContentService.insertByFilter(textContent);
            taskExam.setFrameTextContentId(textContent.getId());
            taskExamMapper.insertSelective(taskExam);
 
        } else {
            TaskExam old = taskExamMapper.selectByPrimaryKey(model.getId());
            if (Objects.isNull(old)) {
                throw new RuntimeException("数据不存在");
            }
            BeanUtils.copyProperties(taskExam, old);
            TextContent textContent = textContentService.selectById(taskExam.getFrameTextContentId());
            //清空试卷任务的试卷Id,后面会统一设置
            List<Integer> paperIds = JsonUtil.toJsonListObject(textContent.getContent(), TaskItemObject.class)
                    .stream()
                    .map(d -> d.getExamPaperId())
                    .collect(Collectors.toList());
            examPaperMapper.clearTaskPaper(paperIds);
 
            //更新任务结构
            textContentService.jsonConvertUpdate(textContent, model.getPaperItems(), p -> {
                TaskItemObject taskItemObject = new TaskItemObject();
                taskItemObject.setExamPaperId(p.getId());
                taskItemObject.setExamPaperName(p.getName());
                return taskItemObject;
            });
            textContentService.updateByIdFilter(textContent);
            taskExamMapper.updateByPrimaryKeySelective(old);
        }
 
        //更新试卷的taskId
        List<Integer> paperIds = model.getPaperItems().stream().map(d -> d.getId()).collect(Collectors.toList());
        examPaperMapper.updateTaskPaper(taskExam.getId(), paperIds);
        model.setId(taskExam.getId());
    }
 
    @Override
    public TaskRequestVO taskExamToVM(Integer id) {
        TaskExam taskExam = taskExamMapper.selectByPrimaryKey(id);
        TaskRequestVO vo = new TaskRequestVO();
        BeanUtils.copyProperties(taskExam, vo);
        TextContent textContent = textContentService.selectById(taskExam.getFrameTextContentId());
        List<ExamResponseVO> examResponseVOS = JsonUtil.toJsonListObject(textContent.getContent(), TaskItemObject.class).stream().map(tk -> {
            ExamPaper examPaper = examPaperMapper.selectByPrimaryKey(tk.getExamPaperId());
            ExamResponseVO examResponseVO = new ExamResponseVO();
            BeanUtils.copyProperties(examPaper, examResponseVO);
            examResponseVO.setCreateTime(DateTimeUtil.dateFormat(examPaper.getCreateTime()));
            return examResponseVO;
        }).collect(Collectors.toList());
        vo.setPaperItems(examResponseVOS);
        return vo;
    }
 
    @Override
    public List<TaskExam> getByGradeLevel(Integer gradeLevel) {
        return taskExamMapper.getByGradeLevel(gradeLevel);
    }
}