public interface XcLearningCourseRepository extends JpaRepository<XcLearningCourse, String> {
//根据用户和课程查询选课记录,用于判断是否添加选课
XcLearningCourse findXcLearningCourseByUserIdAndCourseId(String userId, String courseId);
}
public interface XcTaskHisRepository extends JpaRepository<XcTaskHis,String> {
}
//完成选课
@Transactional
public ResponseResult addcourse(String userId, String courseId,String valid,Date
startTime,Date endTime,XcTask xcTask){
if (StringUtils.isEmpty(courseId)) {
ExceptionCast.cast(LearningCode.LEARNING_GETMEDIA_ERROR);
}
if (StringUtils.isEmpty(userId)) {
ExceptionCast.cast(LearningCode.CHOOSECOURSE_USERISNULL);
}
if(xcTask == null || StringUtils.isEmpty(xcTask.getId())){
ExceptionCast.cast(LearningCode.CHOOSECOURSE_TASKISNULL);
}
//查询历史任务
Optional<XcTaskHis> optional = xcTaskHisRepository.findById(xcTask.getId());
if(optional.isPresent()){
return new ResponseResult(CommonCode.SUCCESS);
}
XcLearningCourse xcLearningCourse =
xcLearningCourseRepository.findXcLearningCourseByUserIdAndCourseId(userId, courseId);
if (xcLearningCourse == null) {//没有选课记录则添加
xcLearningCourse = new XcLearningCourse();
xcLearningCourse.setUserId(userId);
xcLearningCourse.setCourseId(courseId);
xcLearningCourse.setValid(valid);
xcLearningCourse.setStartTime(startTime);
xcLearningCourse.setEndTime(endTime);
xcLearningCourse.setStatus("501001");
xcLearningCourseRepository.save(xcLearningCourse);
} else {//有选课记录则更新日期
xcLearningCourse.setValid(valid);
xcLearningCourse.setStartTime(startTime);
xcLearningCourse.setEndTime(endTime);
xcLearningCourse.setStatus("501001");
xcLearningCourseRepository.save(xcLearningCourse);
}
//向历史任务表播入记录
Optional<XcTaskHis> optional = xcTaskHisRepository.findById(xcTask.getId());
if(!optional.isPresent()){
//添加历史任务
XcTaskHis xcTaskHis = new XcTaskHis();
BeanUtils.copyProperties(xcTask,xcTaskHis);
xcTaskHisRepository.save(xcTaskHis);
}
return new ResponseResult(CommonCode.SUCCESS);
}
@Component
public class ChooseCourseTask {
private static final Logger LOGGER = LoggerFactory.getLogger(ChooseCourseTask.class);
@Autowired
LearningService learningService;
@Autowired
RabbitTemplate rabbitTemplate;
/**
* 接收选课任务
*/
@RabbitListener(queues = {RabbitMQConfig.XC_LEARNING_ADDCHOOSECOURSE})
public void receiveChoosecourseTask(XcTask xcTask,Message message,Channel channel) throws
IOException {
LOGGER.info("receive choose course task,taskId:{}",xcTask.getId());
//接收到 的消息id
String id = xcTask.getId();
//添加选课
try {
String requestBody = xcTask.getRequestBody();
Map map = JSON.parseObject(requestBody, Map.class);
String userId = (String) map.get("userId");
String courseId = (String) map.get("courseId");
String valid = (String) map.get("valid");
Date startTime = null;
Date endTime = null;
SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY‐MM‐dd HH:mm:ss");
if(map.get("startTime")!=null){
startTime =dateFormat.parse((String) map.get("startTime"));
}
if(map.get("endTime")!=null){
endTime =dateFormat.parse((String) map.get("endTime"));
}
//添加选课
ResponseResult addcourse = learningService.addcourse(userId, courseId,
valid,startTime, endTime,xcTask);
//选课成功发送响应消息
if(addcourse.isSuccess()){
//发送响应消息
rabbitTemplate.convertAndSend(RabbitMQConfig.EX_LEARNING_ADDCHOOSECOURSE,
RabbitMQConfig.XC_LEARNING_FINISHADDCHOOSECOURSE_KEY, xcTask );
LOGGER.info("send finish choose course taskId:{}",id);
}
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("send finish choose course taskId:{}", id);
}
}
}
//删除任务
@Transactional
public void finishTask(String taskId){
Optional<XcTask> taskOptional = xcTaskRepository.findById(taskId);
if(taskOptional.isPresent()){
XcTask xcTask = taskOptional.get();
xcTask.setDeleteTime(new Date());
XcTaskHis xcTaskHis = new XcTaskHis();
BeanUtils.copyProperties(xcTask, xcTaskHis);
xcTaskHisRepository.save(xcTaskHis);
xcTaskRepository.delete(xcTask);
}
}
/**
* 接收选课响应结果
*/
@RabbitListener(queues = {RabbitMQConfig.xc_learning_finishaddchoosecourse})
public void receiveFinishChoosecourseTask(XcTask task,Message message, Channel channel) throws
IOException {
LOGGER.info("receiveChoosecourseTask...{}",task.getId());
//接收到 的消息id
String id = task.getId();
//删除任务,添加历史任务
taskService.finishTask(id);
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |