| | |
| | | package com.ycl.jxkg.service.impl; |
| | | |
| | | import com.alibaba.fastjson2.JSONArray; |
| | | 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.mapper.MeetMapper; |
| | | import com.ycl.jxkg.service.MeetService; |
| | | import com.ycl.jxkg.base.Result; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | 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.vo.MeetVO; |
| | | import com.ycl.jxkg.domain.query.MeetQuery; |
| | | import com.ycl.jxkg.domain.vo.MeetVO; |
| | | import com.ycl.jxkg.domain.vo.StudentSimpleVO; |
| | | 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 lombok.RequiredArgsConstructor; |
| | | import com.ycl.jxkg.utils.PageUtil; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.util.Assert; |
| | | |
| | | import java.util.ArrayList; |
| | | import org.springframework.util.CollectionUtils; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | private final MeetMapper meetMapper; |
| | | |
| | | @Autowired |
| | | protected WebContext webContext; |
| | | private WebContext webContext; |
| | | @Autowired |
| | | private ClassesUserMapper classesUserMapper; |
| | | private final Producer producer; |
| | | private final MeetStudentMapper meetStudentMapper; |
| | | private final StudyRecordMapper studyRecordMapper; |
| | | /** |
| | | * 添加 |
| | | * @param form |
| | |
| | | @Override |
| | | public Result add(MeetForm form) { |
| | | Meet entity = MeetForm.getEntityByForm(form, null); |
| | | entity.setStudentIds(JSONArray.toJSONString(form.getStudentIds())); |
| | | entity.setStatus(MeetStatusEnum.Wait.getCode()); |
| | | entity.setCreateUser(webContext.getCurrentUser().getId()); |
| | | baseMapper.insert(entity); |
| | | // 设置乐观锁版本 |
| | | entity.setUpdateVersion(0); |
| | | if (baseMapper.insert(entity) > 0) { |
| | | this.sendMQ(entity, 0); |
| | | } |
| | | return Result.ok("添加成功"); |
| | | } |
| | | |
| | |
| | | @Override |
| | | public Result update(MeetForm form) { |
| | | Meet entity = baseMapper.selectById(form.getId()); |
| | | |
| | | // 为空抛IllegalArgumentException,做全局异常处理 |
| | | Assert.notNull(entity, "记录不存在"); |
| | | BeanUtils.copyProperties(form, entity); |
| | | entity.setStudentIds(JSONArray.toJSONString(form.getStudentIds())); |
| | | baseMapper.updateById(entity); |
| | | // 如果修改成功发送mq消息 |
| | | if (baseMapper.updateById(entity) > 0) { |
| | | this.sendMQ(entity, entity.getUpdateVersion()); |
| | | } |
| | | return Result.ok("修改成功"); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 分页查询 |
| | | * 教师分页查询 |
| | | * @param query |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Result page(MeetQuery query) { |
| | | |
| | | IPage<Meet> page = PageUtil.getPage(query, Meet.class); |
| | | Integer id = webContext.getCurrentUser().getId(); |
| | | //查自己创建的房间 |
| | | query.setTeacherId(id); |
| | | IPage<MeetVO> page = PageUtil.getPage(query, MeetVO.class); |
| | | baseMapper.getPage(page, query); |
| | | List<Meet> records = page.getRecords(); |
| | | List<MeetVO> vos = new ArrayList<>(); |
| | | for (Meet record : records) { |
| | | MeetVO meetVO = new MeetVO(); |
| | | BeanUtils.copyProperties(record,meetVO); |
| | | meetVO.setStudentIds(JSONArray.parseArray(record.getStudentIds(),Integer.class)); |
| | | vos.add(meetVO); |
| | | |
| | | 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("您暂未加入班级"); |
| | | } |
| | | return Result.ok().data(vos).total(page.getTotal()); |
| | | 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(); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Override |
| | | public Result detail(Integer id) { |
| | | MeetVO vo = baseMapper.getById(id); |
| | | Meet vo = baseMapper.getById(id); |
| | | Assert.notNull(vo, "记录不存在"); |
| | | return Result.ok().data(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(),"房间尚未开始或已结束"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | .collect(Collectors.toList()); |
| | | return Result.ok().data(vos); |
| | | } |
| | | |
| | | @Override |
| | | public Result getStudentList(Integer meetId, String keyword) { |
| | | List<StudentSimpleVO> studentList = baseMapper.getStudentList(meetId, keyword); |
| | | return Result.ok().data(studentList); |
| | | } |
| | | |
| | | /** |
| | | * 发送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())); |
| | | } |
| | | |
| | | } |