分享

Java的新项目学成在线笔记-day19(十三)

 印度阿三17 2019-08-08
4 自动添加选课开发 
  4.1 学习服务添加选课 
4.1.1需求分析 
学习服务接收MQ发送添加选课消息,执行添加 选 课操作。 添加选课成功向学生选课表插入记录、向历史任务表插入记录、并向MQ发送“完成选课”消息。
  4.1.2 RabbitMQ配置 
学习服务监听MQ的添加选课队列,并且声明完成选课队列,配置代码同订单服务中RabbitMQ配置   4.1.3 Dao 
学生选课Dao:

[AppleScript] 纯文本查看 复制代码

?

1

public interface XcLearningCourseRepository extends JpaRepository<XcLearningCourse, String> {     //根据用户和课程查询选课记录,用于判断是否添加选课     XcLearningCourse findXcLearningCourseByUserIdAndCourseId(String userId, String courseId); }


历史任务Dao:
 

[AppleScript] 纯文本查看 复制代码

?

1

public interface XcTaskHisRepository extends JpaRepository<XcTaskHis,String> {   }


4.1.4 Service 
1、添加选课方法 向xc_learning_course添加记录,为保证不重复添加选课,先查询历史任务表,如果从历史任务表查询不到任务说 明此任务还没有处理,此时则添加选课并添加历史任务。
在学习服务中编码如下代码:
 

[AppleScript] 纯文本查看 复制代码

?

01

02

03

04

05

06

07

08

09

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

//完成选课  

  @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);

        }

 

[AppleScript] 纯文本查看 复制代码

?

1

return new ResponseResult(CommonCode.SUCCESS);     }

来源:https://www./content-1-382151.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多