xiangpei
2024-07-05 8d42b23c07433f11cb0b1e16d9c74b0e29fd35ce
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package com.ycl.jxkg.service.impl;
 
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycl.jxkg.base.Result;
import com.ycl.jxkg.base.SystemCode;
import com.ycl.jxkg.context.WebContext;
import com.ycl.jxkg.domain.entity.Meet;
import com.ycl.jxkg.domain.entity.MeetStudent;
import com.ycl.jxkg.domain.entity.StudyRecord;
import com.ycl.jxkg.domain.form.MeetForm;
import com.ycl.jxkg.domain.query.MeetQuery;
import com.ycl.jxkg.domain.vo.MeetVO;
import com.ycl.jxkg.enums.MeetStatusEnum;
import com.ycl.jxkg.mapper.ClassesUserMapper;
import com.ycl.jxkg.mapper.MeetMapper;
import com.ycl.jxkg.mapper.MeetStudentMapper;
import com.ycl.jxkg.mapper.StudyRecordMapper;
import com.ycl.jxkg.rabbitmq.msg.MeetStatusMsg;
import com.ycl.jxkg.rabbitmq.product.Producer;
import com.ycl.jxkg.service.MeetService;
import com.ycl.jxkg.utils.DateTimeUtil;
import com.ycl.jxkg.utils.PageUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * 会议表 服务实现类
 *
 * @author flq
 * @since 2024-06-17
 */
@Service
@RequiredArgsConstructor
public class MeetServiceImpl extends ServiceImpl<MeetMapper, Meet> implements MeetService {
 
    private final MeetMapper meetMapper;
 
    @Autowired
    private WebContext webContext;
    @Autowired
    private ClassesUserMapper classesUserMapper;
    private final Producer producer;
    private final MeetStudentMapper meetStudentMapper;
    private final StudyRecordMapper studyRecordMapper;
    /**
     * 添加
     * @param form
     * @return
     */
    @Override
    public Result add(MeetForm form) {
        Meet entity = MeetForm.getEntityByForm(form, null);
        entity.setStatus(MeetStatusEnum.Wait.getCode());
        entity.setCreateUser(webContext.getCurrentUser().getId());
        // 设置乐观锁版本
        entity.setUpdateVersion(0);
        if (baseMapper.insert(entity) > 0) {
            this.sendMQ(entity, 0);
        }
        return Result.ok("添加成功");
    }
 
    /**
     * 修改
     * @param form
     * @return
     */
    @Override
    public Result update(MeetForm form) {
        Meet entity = baseMapper.selectById(form.getId());
        // 为空抛IllegalArgumentException,做全局异常处理
        Assert.notNull(entity, "记录不存在");
        BeanUtils.copyProperties(form, entity);
        // 如果修改成功发送mq消息
        if (baseMapper.updateById(entity) > 0) {
            this.sendMQ(entity, entity.getUpdateVersion());
        }
        return Result.ok("修改成功");
    }
 
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public Result remove(List<String> ids) {
        baseMapper.deleteBatchIds(ids);
        return Result.ok("删除成功");
    }
 
    /**
     * id删除
     * @param id
     * @return
     */
    @Override
    public Result removeById(String id) {
        baseMapper.deleteById(id);
        return Result.ok("删除成功");
    }
 
    /**
     * 教师分页查询
     * @param query
     * @return
     */
    @Override
    public Result page(MeetQuery query) {
        Integer id = webContext.getCurrentUser().getId();
        //查自己创建的房间
        query.setTeacherId(id);
        IPage<MeetVO> page = PageUtil.getPage(query, MeetVO.class);
        baseMapper.getPage(page, query);
 
        return Result.ok().data(page.getRecords()).total(page.getTotal());
    }
 
    /**
     * 学生分页查询
     * @param query
     * @return
     */
    @Override
    public Result studentPage(MeetQuery query) {
        Integer userId = webContext.getCurrentUser().getId();
        //查出学生所在班级
        List<Integer> classes = classesUserMapper.getClassesByUserId(userId);
        if(CollectionUtils.isEmpty(classes)){
            return Result.ok("您暂未加入班级");
        }
        query.setClassesIds(classes);
        IPage<MeetVO> page = PageUtil.getPage(query, MeetVO.class);
        baseMapper.getPage(page, query);
 
        return Result.ok().data(page.getRecords()).total(page.getTotal());
    }
 
    @Override
    public Result start(MeetForm form) {
        Meet entity = baseMapper.selectById(form.getId());
        // 为空抛IllegalArgumentException,做全局异常处理
        Assert.notNull(entity, "记录不存在");
        // 不使用updateById这种方式,避免乐观锁加一。
        new LambdaUpdateChainWrapper<>(meetMapper)
                .eq(Meet::getId, entity.getId())
                .set(Meet::getStatus,form.getStatus())
                .update();
        return Result.ok();
    }
 
    /**
     * 根据id查找
     * @param id
     * @return
     */
    @Override
    public Result detail(Integer id) {
        Meet vo = baseMapper.getById(id);
        Assert.notNull(vo, "记录不存在");
        if(vo.getStatus().equals(MeetStatusEnum.Starting.getCode())){
            Integer userId = webContext.getCurrentUser().getId();
            //验证有没有重复进入(重复进入会导致上课次数增多)
            QueryWrapper<MeetStudent> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("meet_id",id);
            queryWrapper.eq("student_id",userId);
            queryWrapper.eq("version",vo.getUpdateVersion());
            MeetStudent one = meetStudentMapper.selectOne(queryWrapper);
            if(one != null){
                return Result.ok();
            }
            //增加学生上课记录
            MeetStudent meetStudent = new MeetStudent();
            meetStudent.setMeetId(id);
            meetStudent.setStudentId(userId);
            meetStudent.setCreateTime(new Date());
            meetStudent.setStartTime(vo.getStartTime());
            meetStudent.setVersion(vo.getUpdateVersion());
            meetStudent.setMeetName(vo.getMeetName());
            //TODO:暂时处理,后期考虑jitsiApi
            meetStudent.setDuringTime(0L);
            meetStudentMapper.insert(meetStudent);
            //学生学习档案更新
            StudyRecord studyRecord = studyRecordMapper.getByStudentId(userId);
            if(studyRecord ==null){
                StudyRecord record = new StudyRecord();
                record.setMeetCount(0);
                record.setStudyTime(0L);
                record.setStudentId(userId);
                studyRecordMapper.insert(record);
            }else {
                studyRecord.setMeetCount(studyRecord.getMeetCount()+1);
                studyRecordMapper.updateById(studyRecord);
            }
            return Result.ok();
        }else {
            return Result.fail(SystemCode.InnerError.getCode(),"房间尚未开始或已结束");
        }
    }
 
    /**
     * 列表
     * @return
     */
    @Override
    public Result all() {
        List<Meet> entities = baseMapper.selectList(null);
        List<MeetVO> vos = entities.stream()
                .map(entity -> MeetVO.getVoByEntity(entity, null))
                .collect(Collectors.toList());
        return Result.ok().data(vos);
    }
 
    /**
     * 发送mq消息
     *
     * @param entity 考试实体类
     * @param version 乐观锁版本
     */
    public void sendMQ(Meet entity, Integer version) {
        MeetStatusMsg finishedMsg = new MeetStatusMsg();
        finishedMsg.setVersion(version);
        finishedMsg.setMeetId(entity.getId());
        finishedMsg.setMeetStatus(MeetStatusEnum.End.getCode());
        producer.meetMsg(entity.getId(), JSON.toJSONString(finishedMsg), DateTimeUtil.getTwoTimeDiffMS(entity.getEndTime(), new Date()));
    }
 
}