From 9360fd5020c6e41b4481d9cf6a0341db2a7807f5 Mon Sep 17 00:00:00 2001 From: joylink_zhangsai <1021828630@qq.com> Date: Thu, 24 Dec 2020 18:45:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E8=AE=AD=E7=94=9F=E6=88=90=E5=90=8E?= =?UTF-8?q?=E5=8E=9F=E6=9C=89=E8=AF=BE=E7=A8=8B=E6=81=A2=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/publish/LessonController.java | 4 +- .../rtss/dao/LsRelChapterTrainingDAO.java | 12 + .../joylink/rtss/services/ExamService.java | 2 +- .../rtss/services/ILessonDraftService.java | 3 +- .../joylink/rtss/services/ILessonService.java | 32 +- .../rtss/services/LessonDraftService.java | 5 +- .../joylink/rtss/services/LessonService.java | 313 +++++++++++------- .../rtss/services/UserUsageStatsService.java | 2 +- .../rtss/services/training/GenerateTask.java | 110 ++++-- .../services/training/ITrainingV1Service.java | 3 + .../services/training/TrainingV1Service.java | 10 +- .../vo/client/LessonChapterTrainingRelVO.java | 8 +- .../rtss/vo/client/LessonChapterVO.java | 5 +- 13 files changed, 346 insertions(+), 163 deletions(-) diff --git a/src/main/java/club/joylink/rtss/controller/publish/LessonController.java b/src/main/java/club/joylink/rtss/controller/publish/LessonController.java index 4f0e2a77f..086317566 100644 --- a/src/main/java/club/joylink/rtss/controller/publish/LessonController.java +++ b/src/main/java/club/joylink/rtss/controller/publish/LessonController.java @@ -50,7 +50,7 @@ public class LessonController { @ApiOperation(value = "根据条件获取课程列表") @GetMapping(path = "") public List queryLessons(LessonQueryVO lessonQueryVO) { - return this.iLessonService.queryLessons(lessonQueryVO); + return this.iLessonService.queryValidLessons(lessonQueryVO); } @ApiOperation(value = "根据班级获取关联课程列表") @@ -68,7 +68,7 @@ public class LessonController { @ApiOperation(value = "获取课程列表") @GetMapping(path = "/listOfMap") public List queryLessonsOfMap(Long mapId) { - return this.iLessonService.queryLessonsOfMap(mapId); + return this.iLessonService.queryValidLessonsOfMap(mapId); } @ApiOperation(value = "分页获取已发布的课程") diff --git a/src/main/java/club/joylink/rtss/dao/LsRelChapterTrainingDAO.java b/src/main/java/club/joylink/rtss/dao/LsRelChapterTrainingDAO.java index c615acb79..1e601f580 100644 --- a/src/main/java/club/joylink/rtss/dao/LsRelChapterTrainingDAO.java +++ b/src/main/java/club/joylink/rtss/dao/LsRelChapterTrainingDAO.java @@ -2,6 +2,7 @@ package club.joylink.rtss.dao; import club.joylink.rtss.entity.LsRelChapterTraining; import club.joylink.rtss.entity.LsRelChapterTrainingExample; +import club.joylink.rtss.vo.client.LessonChapterTrainingRelVO; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; @@ -24,4 +25,15 @@ public interface LsRelChapterTrainingDAO extends MyBatisBaseDao" + "") int batchInsertWithId(@Param("list") List relChapterTrainingList); + + @Insert(value = "") + int batchInsert(@Param("list") List relChapterTrainingList); } diff --git a/src/main/java/club/joylink/rtss/services/ExamService.java b/src/main/java/club/joylink/rtss/services/ExamService.java index 00732ea05..cdd65ce25 100644 --- a/src/main/java/club/joylink/rtss/services/ExamService.java +++ b/src/main/java/club/joylink/rtss/services/ExamService.java @@ -338,7 +338,7 @@ public class ExamService implements IExamService{ criteria.andLessonIdEqualTo(queryVO.getLessonId()); } else if (Objects.nonNull(queryVO.getMapId())) { // 地图id查询 - List lessonList = this.iLessonService.queryLessonsOfMap(queryVO.getMapId()); + List lessonList = this.iLessonService.queryValidLessonsOfMap(queryVO.getMapId()); if (!CollectionUtils.isEmpty(lessonList)) { List lessonIdList = lessonList.stream() .map(LessonVO::getId) diff --git a/src/main/java/club/joylink/rtss/services/ILessonDraftService.java b/src/main/java/club/joylink/rtss/services/ILessonDraftService.java index db512e868..4ef5cffb6 100644 --- a/src/main/java/club/joylink/rtss/services/ILessonDraftService.java +++ b/src/main/java/club/joylink/rtss/services/ILessonDraftService.java @@ -27,8 +27,9 @@ public interface ILessonDraftService { /** * 创建课程 * @param lessonVo + * @return */ - void createLesson(LessonVO lessonVo, UserVO userVO); + Long createLesson(LessonVO lessonVo, UserVO userVO); /** * 从发布课程创建 diff --git a/src/main/java/club/joylink/rtss/services/ILessonService.java b/src/main/java/club/joylink/rtss/services/ILessonService.java index 0b5034974..1b661a676 100644 --- a/src/main/java/club/joylink/rtss/services/ILessonService.java +++ b/src/main/java/club/joylink/rtss/services/ILessonService.java @@ -17,10 +17,6 @@ public interface ILessonService { /** * 获取课程树 - * - * @param id - * @param userVO - * @return */ LessonTreeVO getLessonTree(Long id, UserVO userVO); @@ -32,6 +28,11 @@ public interface ILessonService { */ LessonVO getLessonDetail(Long id, UserVO userVO); + /** + * 查询地图下的课程、章节、章节和实训的关联关系 + */ + List queryLessonDetail(Long mapId); + /** * 获取课程详细信息 * @param id @@ -42,11 +43,15 @@ public interface ILessonService { LessonVO findByMapAndNameAndPrdType(Long mapId, String name, String prdType); /** - * 查询课程列表 - * @param lessonQueryVO - * @return + * 查询有效的课程列表 */ - List queryLessons(LessonQueryVO lessonQueryVO); + List queryValidLessons(LessonQueryVO lessonQueryVO); + + /** + * 查询课程列表(无视状态) + * @param mapId + */ + List queryLessons(Long mapId); /** * 更新所属城市 @@ -121,7 +126,10 @@ public interface ILessonService { Map copy(Long sourceMapId, Long targetMapId, Map trainingRelMap, UserVO user); - List queryLessonsOfMap(Long mapId); + /** + * 查询地图下有效的课程 + */ + List queryValidLessonsOfMap(Long mapId); List findByMapIdAndPrdType(Long mapId, String prdType); @@ -142,4 +150,10 @@ public interface ILessonService { * 获取有效的课程 */ List getValidLesson(List ids, String prdType); + + /** + * 删除并添加章节-实训关联关系 + * @param relVOS + */ + void addChapterTrainingRelAfterDelete(List relVOS); } diff --git a/src/main/java/club/joylink/rtss/services/LessonDraftService.java b/src/main/java/club/joylink/rtss/services/LessonDraftService.java index 4da6b5c06..7856a7f29 100644 --- a/src/main/java/club/joylink/rtss/services/LessonDraftService.java +++ b/src/main/java/club/joylink/rtss/services/LessonDraftService.java @@ -8,7 +8,6 @@ import club.joylink.rtss.entity.LsDraftLessonChapterExample.Criteria; import club.joylink.rtss.exception.BusinessExceptionAssertEnum; import club.joylink.rtss.vo.UserVO; import club.joylink.rtss.vo.client.*; -import club.joylink.rtss.vo.client.map.MapVO; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import lombok.extern.slf4j.Slf4j; @@ -87,12 +86,13 @@ public class LessonDraftService implements ILessonDraftService { @Override @Transactional - public void createLesson(LessonVO lessonVo, UserVO userVO) { + public Long createLesson(LessonVO lessonVo, UserVO userVO) { LsDraftLesson lesson = lessonVo.convert2Draft(); lesson.setUpdateTime(LocalDateTime.now()); lesson.setCreatorId(userVO.getId()); lesson.setStatus(BusinessConsts.ReleaseReview.RELEASE_STATUS_01); this.lsDraftLessonDAO.insert(lesson); + return lesson.getId(); } @Override @@ -532,4 +532,5 @@ public class LessonDraftService implements ILessonDraftService { example.createCriteria().andTrainingIdIn(trainingIdList); this.draftRelChapterTrainingDAO.deleteByExample(example); } + } diff --git a/src/main/java/club/joylink/rtss/services/LessonService.java b/src/main/java/club/joylink/rtss/services/LessonService.java index 1a5220eb7..2e600406a 100644 --- a/src/main/java/club/joylink/rtss/services/LessonService.java +++ b/src/main/java/club/joylink/rtss/services/LessonService.java @@ -26,6 +26,7 @@ import org.springframework.validation.annotation.Validated; import java.time.LocalDateTime; import java.util.*; +import java.util.function.Function; import java.util.stream.Collectors; /** @@ -146,6 +147,39 @@ public class LessonService implements ILessonService { return lessonVo; } + @Override + public List queryLessonDetail(Long mapId) { + //查询课程 + List lessons = findLessonEntities(mapId); + List lessonVOS = LessonVO.convert2PublishVO(lessons); + if (!CollectionUtils.isEmpty(lessons)) { + Map lessonVOMap = lessonVOS.stream().collect(Collectors.toMap(LessonVO::getId, Function.identity())); + //查询章节 + List lessonIds = lessons.stream().map(LsLesson::getId).collect(Collectors.toList()); + List chapters = findChapterEntities(lessonIds); + if (!CollectionUtils.isEmpty(chapters)) { + List chapterVOS = LessonChapterVO.convert2PublishVO(chapters); + Map> groupByLessonIdChapterMap = chapterVOS.stream().collect(Collectors.groupingBy(LessonChapterVO::getLessonId)); + groupByLessonIdChapterMap.forEach((lessonId, chapterList) -> { + LessonVO lessonVO = lessonVOMap.get(lessonId); + if (lessonVO != null) + lessonVO.setChapters(chapterList); + }); + //查询章节和实训的关联关系 + List chapterIds = chapters.stream().map(LsLessonChapter::getId).collect(Collectors.toList()); + List rels = findChapterTrainingRelEntities(chapterIds); + List relVOS = LessonChapterTrainingRelVO.convert2VOList(rels); + Map> groupByLessonIdRelVOMap = relVOS.stream().collect(Collectors.groupingBy(LessonChapterTrainingRelVO::getLessonId)); + groupByLessonIdRelVOMap.forEach((lessonId, relList) -> { + LessonVO lessonVO = lessonVOMap.get(lessonId); + if (lessonVO != null) + lessonVO.setRel(relList); + }); + } + } + return lessonVOS; + } + @Override public LessonVO getLessonInfo(Long id) { LsLesson lesson = this.lessonDAO.selectByPrimaryKey(id); @@ -194,7 +228,7 @@ public class LessonService implements ILessonService { } else { addRelLessonClass(publishVO, publishedLesson.getId()); } - }else{ + } else { if (!CollectionUtils.isEmpty(studentRelLessonClasses)) { this.studentRelLessonClassDAO.deleteByExample(relLessonClassExample); } @@ -208,28 +242,6 @@ public class LessonService implements ILessonService { this.saveChapterDetail(newLesson, lessonVo); } - /** - * 增加课程班级关系 - */ - private void addRelLessonClass(LessonPublishVO publishVO, Long id) { - publishVO.getClassIdList().forEach(classId -> { - StudentRelLessonClass relLessonClass = new StudentRelLessonClass(); - relLessonClass.setClassId(classId); - relLessonClass.setLessonId(id); - this.studentRelLessonClassDAO.insert(relLessonClass); - }); - } - - private LessonVO findByMapAndName(Long mapId, String name) { - LsLessonExample lessonExample = new LsLessonExample(); - lessonExample.createCriteria().andMapIdEqualTo(mapId).andNameEqualTo(name); - List lessonList = this.lessonDAO.selectByExample(lessonExample); - if (CollectionUtils.isEmpty(lessonList)) { - return null; - } - return new LessonVO(lessonList.get(0)); - } - @Override public LessonVO findByMapAndNameAndPrdType(Long mapId, String name, String prdType) { LsLessonExample lessonExample = new LsLessonExample(); @@ -241,97 +253,8 @@ public class LessonService implements ILessonService { return new LessonVO(lessonList.get(0)); } - /** - * 保存课程章节及章节关联的实训信息 - */ - private void saveChapterDetail(LsLesson newLesson, LessonVO lessonVo) { - // 清除关系表 - LsRelChapterTrainingExample relExample = new LsRelChapterTrainingExample(); - relExample.createCriteria().andLessonIdEqualTo(newLesson.getId()); - this.relChapterTrainingDAO.deleteByExample(relExample); - // 清空章节信息 - LsLessonChapterExample example = new LsLessonChapterExample(); - example.createCriteria().andLessonIdEqualTo(newLesson.getId()); - this.lessonChapterDAO.deleteByExample(example); - // 保存 - Map idChangedMap = new HashMap<>(); - this.saveChaptersInfo(newLesson, lessonVo.getChapters(), idChangedMap); - this.saveRelChapterTraining(newLesson, lessonVo.getRel(), idChangedMap); - } - - /** - * 保存课程章节信息 - */ - private void saveChaptersInfo(LsLesson newLesson, List chapterVos, Map idChangedMap) { - List chapters = LessonChapterVO.convert2PublishDB(chapterVos).stream() - .sorted(Comparator.comparing(LsLessonChapter::getParentId)).collect(Collectors.toList()); - if (!CollectionUtils.isEmpty(chapters)) { - chapters.forEach(chapter -> { - Long oldId = chapter.getId(); - chapter.setLessonId(newLesson.getId()); - if (chapter.getParentId() != 0) { - chapter.setParentId(idChangedMap.get(chapter.getParentId())); - } - this.lessonChapterDAO.insert(chapter); - idChangedMap.put(oldId, chapter.getId()); - }); - } - } - - /** - * 保存章节-实训 关联数据 - */ - private void saveRelChapterTraining(LsLesson newLesson, List relVos, Map idChangedMap) { - List relList = LessonChapterTrainingRelVO.convert2PublishDB(relVos); - if (!CollectionUtils.isEmpty(relList)) { - relList.forEach(rel -> { - rel.setLessonId(newLesson.getId()); - rel.setChapterId(idChangedMap.get(rel.getChapterId())); - this.relChapterTrainingDAO.insert(rel); - }); - } - } - - /** - * 创建并插入课程版本信息 - */ - private void buildAndInsertLessonVersion(LsLesson lesson) { - LsLessonVersion latest = this.getLatestLessonVersion(lesson.getId()); - String version = null == latest ? null : latest.getVersion(); - LsLessonVersion newVersion = buildVersion(lesson, version); - this.lessonVersionDAO.insert(newVersion); - } - - /** - * 创建新的版本信息 - */ - private LsLessonVersion buildVersion(LsLesson lesson, String old) { - String version = VersionUtil.generateNext(old); - LsLessonVersion lessonVersion = new LsLessonVersion(); - lessonVersion.setLessonId(lesson.getId()); - lessonVersion.setCreatorId(lesson.getCreatorId()); - lessonVersion.setUpdateTime(lesson.getUpdateTime()); - lessonVersion.setVersion(version); - return lessonVersion; - } - - /** - * 获取最新的课程版本信息 - */ - private LsLessonVersion getLatestLessonVersion(Long lessonId) { - LsLessonVersionExample example = new LsLessonVersionExample(); - example.createCriteria().andLessonIdEqualTo(lessonId); - example.setOrderByClause("update_time desc"); - List list = this.lessonVersionDAO.selectByExample(example); - if (CollectionUtils.isEmpty(list)) { - return null; - } else { - return list.get(0); - } - } - @Override - public List queryLessons(LessonQueryVO lessonQueryVO) { + public List queryValidLessons(LessonQueryVO lessonQueryVO) { LsLessonExample example = new LsLessonExample(); example.setOrderByClause("id desc"); Criteria criteria = example.createCriteria() @@ -343,6 +266,15 @@ public class LessonService implements ILessonService { return LessonVO.convert2PublishVO(list); } + @Override + public List queryLessons(@NonNull Long mapId) { + LsLessonExample example = new LsLessonExample(); + example.setOrderByClause("id desc"); + example.createCriteria().andMapIdEqualTo(mapId); + List list = this.lessonDAO.selectByExample(example); + return LessonVO.convert2PublishVO(list); + } + @Override public void updateCity(Long mapId) { MapVO mapVO = this.iMapService.getMapInfoById(mapId); @@ -502,7 +434,7 @@ public class LessonService implements ILessonService { @Transactional public Map copy(Long sourceMapId, Long targetMapId, Map trainingRelMap, UserVO user) { - List lessonList = findEntityByMapId(sourceMapId); + List lessonList = findLessonEntities(sourceMapId); Map relationMap = new HashMap<>(); if (CollectionUtils.isEmpty(lessonList)) { return relationMap; @@ -546,14 +478,8 @@ public class LessonService implements ILessonService { return relationMap; } - private List findEntityByMapId(Long mapId) { - LsLessonExample example = new LsLessonExample(); - example.createCriteria().andMapIdEqualTo(mapId); - return lessonDAO.selectByExample(example); - } - @Override - public List queryLessonsOfMap(Long mapId) { + public List queryValidLessonsOfMap(Long mapId) { LsLessonExample example = new LsLessonExample(); example.createCriteria() .andMapIdEqualTo(mapId) @@ -611,6 +537,17 @@ public class LessonService implements ILessonService { return lessonDAO.getValidLesson(ids, prdType); } + @Override + public void addChapterTrainingRelAfterDelete(List relVOS) { + //删除旧数据 + List ids = relVOS.stream().map(LessonChapterTrainingRelVO::getTrainingId).collect(Collectors.toList()); + LsRelChapterTrainingExample example = new LsRelChapterTrainingExample(); + example.createCriteria().andIdIn(ids); + relChapterTrainingDAO.deleteByExample(example); + //添加新数据 + relChapterTrainingDAO.batchInsert(relVOS); + } + private void generateLessonBy(UserVO userVO, MapVO mapVO, BusinessConsts.Lesson.PrdInfo prdLessonInfo, String prdType) { //课程 LsLesson lesson = new LsLesson(); @@ -742,4 +679,138 @@ public class LessonService implements ILessonService { } } } + + /** + * 保存课程章节及章节关联的实训信息 + */ + private void saveChapterDetail(LsLesson newLesson, LessonVO lessonVo) { + // 清除关系表 + LsRelChapterTrainingExample relExample = new LsRelChapterTrainingExample(); + relExample.createCriteria().andLessonIdEqualTo(newLesson.getId()); + this.relChapterTrainingDAO.deleteByExample(relExample); + // 清空章节信息 + LsLessonChapterExample example = new LsLessonChapterExample(); + example.createCriteria().andLessonIdEqualTo(newLesson.getId()); + this.lessonChapterDAO.deleteByExample(example); + // 保存 + Map idChangedMap = new HashMap<>(); + this.saveChaptersInfo(newLesson, lessonVo.getChapters(), idChangedMap); + this.saveRelChapterTraining(newLesson, lessonVo.getRel(), idChangedMap); + } + + /** + * 保存课程章节信息 + */ + private void saveChaptersInfo(LsLesson newLesson, List chapterVos, Map idChangedMap) { + List chapters = LessonChapterVO.convert2PublishDB(chapterVos).stream() + .sorted(Comparator.comparing(LsLessonChapter::getParentId)).collect(Collectors.toList()); + if (!CollectionUtils.isEmpty(chapters)) { + chapters.forEach(chapter -> { + Long oldId = chapter.getId(); + chapter.setLessonId(newLesson.getId()); + if (chapter.getParentId() != 0) { + chapter.setParentId(idChangedMap.get(chapter.getParentId())); + } + this.lessonChapterDAO.insert(chapter); + idChangedMap.put(oldId, chapter.getId()); + }); + } + } + + /** + * 保存章节-实训 关联数据 + */ + private void saveRelChapterTraining(LsLesson newLesson, List relVos, Map idChangedMap) { + List relList = LessonChapterTrainingRelVO.convert2PublishDB(relVos); + if (!CollectionUtils.isEmpty(relList)) { + relList.forEach(rel -> { + rel.setLessonId(newLesson.getId()); + rel.setChapterId(idChangedMap.get(rel.getChapterId())); + this.relChapterTrainingDAO.insert(rel); + }); + } + } + + /** + * 创建并插入课程版本信息 + */ + private void buildAndInsertLessonVersion(LsLesson lesson) { + LsLessonVersion latest = this.getLatestLessonVersion(lesson.getId()); + String version = null == latest ? null : latest.getVersion(); + LsLessonVersion newVersion = buildVersion(lesson, version); + this.lessonVersionDAO.insert(newVersion); + } + + /** + * 创建新的版本信息 + */ + private LsLessonVersion buildVersion(LsLesson lesson, String old) { + String version = VersionUtil.generateNext(old); + LsLessonVersion lessonVersion = new LsLessonVersion(); + lessonVersion.setLessonId(lesson.getId()); + lessonVersion.setCreatorId(lesson.getCreatorId()); + lessonVersion.setUpdateTime(lesson.getUpdateTime()); + lessonVersion.setVersion(version); + return lessonVersion; + } + + /** + * 获取最新的课程版本信息 + */ + private LsLessonVersion getLatestLessonVersion(Long lessonId) { + LsLessonVersionExample example = new LsLessonVersionExample(); + example.createCriteria().andLessonIdEqualTo(lessonId); + example.setOrderByClause("update_time desc"); + List list = this.lessonVersionDAO.selectByExample(example); + if (CollectionUtils.isEmpty(list)) { + return null; + } else { + return list.get(0); + } + } + + /** + * 增加课程班级关系 + */ + private void addRelLessonClass(LessonPublishVO publishVO, Long id) { + publishVO.getClassIdList().forEach(classId -> { + StudentRelLessonClass relLessonClass = new StudentRelLessonClass(); + relLessonClass.setClassId(classId); + relLessonClass.setLessonId(id); + this.studentRelLessonClassDAO.insert(relLessonClass); + }); + } + + private LessonVO findByMapAndName(Long mapId, String name) { + LsLessonExample lessonExample = new LsLessonExample(); + lessonExample.createCriteria().andMapIdEqualTo(mapId).andNameEqualTo(name); + List lessonList = this.lessonDAO.selectByExample(lessonExample); + if (CollectionUtils.isEmpty(lessonList)) { + return null; + } + return new LessonVO(lessonList.get(0)); + } + + private List findLessonEntities(@NonNull Long mapId) { + LsLessonExample example = new LsLessonExample(); + example.createCriteria().andMapIdEqualTo(mapId); + return lessonDAO.selectByExample(example); + } + + private List findChapterEntities(@NonNull List lessonIds) { + if (CollectionUtils.isEmpty(lessonIds)) + return new ArrayList<>(); + LsLessonChapterExample example = new LsLessonChapterExample(); + example.createCriteria().andLessonIdIn(lessonIds); + return lessonChapterDAO.selectByExample(example); + } + + private List findChapterTrainingRelEntities(@NonNull List chapterIds) { + if (CollectionUtils.isEmpty(chapterIds)) + return new ArrayList<>(); + LsRelChapterTrainingExample example = new LsRelChapterTrainingExample(); + example.createCriteria().andChapterIdIn(chapterIds); + return relChapterTrainingDAO.selectByExample(example); + } + } diff --git a/src/main/java/club/joylink/rtss/services/UserUsageStatsService.java b/src/main/java/club/joylink/rtss/services/UserUsageStatsService.java index 2f64019b5..e8be3019b 100644 --- a/src/main/java/club/joylink/rtss/services/UserUsageStatsService.java +++ b/src/main/java/club/joylink/rtss/services/UserUsageStatsService.java @@ -119,7 +119,7 @@ public class UserUsageStatsService implements IUserUsageStatsService { } usageTotalStatsVO.setTrainingUserCount(0L); usageTotalStatsVO.setTrainingTime(0L); - List lessonVOList = iLessonService.queryLessonsOfMap(mapVO.getId()); + List lessonVOList = iLessonService.queryValidLessonsOfMap(mapVO.getId()); List lessonIds = lessonVOList.stream().map(LessonVO::getId).collect(Collectors.toList()); if (!CollectionUtils.isEmpty(lessonIds)) { UserTrainingStatsExample userTrainingStatsExample = new UserTrainingStatsExample(); diff --git a/src/main/java/club/joylink/rtss/services/training/GenerateTask.java b/src/main/java/club/joylink/rtss/services/training/GenerateTask.java index d805ad5c6..e8aaf89fe 100644 --- a/src/main/java/club/joylink/rtss/services/training/GenerateTask.java +++ b/src/main/java/club/joylink/rtss/services/training/GenerateTask.java @@ -1,10 +1,8 @@ package club.joylink.rtss.services.training; import club.joylink.rtss.constants.MapPrdTypeEnum; -import club.joylink.rtss.services.ICommandService; -import club.joylink.rtss.services.IMapService; -import club.joylink.rtss.services.IOperateService; -import club.joylink.rtss.services.IRunPlanTemplateService; +import club.joylink.rtss.entity.Training; +import club.joylink.rtss.services.*; import club.joylink.rtss.services.training.data.GenerateConfig; import club.joylink.rtss.services.training.generatornew.GeneratorFactoryNew; import club.joylink.rtss.services.training.generatornew.GeneratorNew; @@ -12,6 +10,8 @@ import club.joylink.rtss.simulation.cbtc.Simulation; import club.joylink.rtss.simulation.cbtc.build.SimulationBuildParams; import club.joylink.rtss.simulation.cbtc.build.SimulationBuilder; import club.joylink.rtss.vo.client.CommandDefinitionVO; +import club.joylink.rtss.vo.client.LessonChapterTrainingRelVO; +import club.joylink.rtss.vo.client.LessonVO; import club.joylink.rtss.vo.client.OperateDefinitionVO; import club.joylink.rtss.vo.client.map.MapVO; import club.joylink.rtss.vo.client.runplan.RunPlanVO; @@ -26,6 +26,7 @@ import org.springframework.util.CollectionUtils; import java.time.LocalDateTime; import java.time.LocalTime; import java.util.*; +import java.util.function.Function; import java.util.stream.Collectors; @Slf4j @@ -52,11 +53,17 @@ public class GenerateTask { @Autowired private ICommandService iCommandService; + @Autowired + private ILessonService iLessonService; + + @Autowired + private ILessonDraftService iLessonDraftService; + private List generateNew(GenerateConfig config, MapVO mapVO, Simulation simulation) { // 查询生成规则 GeneratorNew generator = this.generatorFactoryNew.getGenerator(config.getTrainingType()); if (Objects.isNull(generator)) { - log.warn("无对应实训类型[{}]的规则生成器",config.getTrainingType()); + log.warn("无对应实训类型[{}]的规则生成器", config.getTrainingType()); return null; } List results = new ArrayList<>(); @@ -79,7 +86,7 @@ public class GenerateTask { this.iTrainingV1Service.batchSaveAutoGenerateTrainings(trainingVOList); } catch (Exception e) { log.error(MapPrdTypeEnum.getMapPrdTypeEnumByCode(config.getPrdType()) + ":" + operateType + " 生成失败", e); - results.add(MapPrdTypeEnum.getMapPrdTypeEnumByCode(config.getPrdType()) + ":" +operateType + " 生成失败"); + results.add(MapPrdTypeEnum.getMapPrdTypeEnumByCode(config.getPrdType()) + ":" + operateType + " 生成失败"); } }); }); @@ -106,18 +113,17 @@ public class GenerateTask { /** * 批量更新自动生成实训的用时/说明 - * - * @param config */ public void batchUpdateGenerateTraining(GenerateConfig config) { this.iTrainingV1Service.batchUpdateGenerateTraining(config); } - /**生成新地图实训,只能在一键生成接口调用*/ + /** + * 生成新地图实训,只能在一键生成接口调用 + */ @Transactional public String generateNewTrainings(Long mapId, MapVO mapVO) { log.warn("generate training start...[{}]", LocalTime.now()); - try{ // 查询通用运行图 RunPlanVO planVO = this.iRunPlanTemplateService.getFirstRunPlanByMapId(mapVO.getId()); this.delete(mapId); @@ -141,7 +147,7 @@ public class GenerateTask { groupByOperationObject.forEach((o, cdo) -> { config.setTrainingType(o); config.setOperateType(cdo.stream().map(cd -> cd.getOperate()).collect(Collectors.toList())); - List list =this.generateNew(config, mapVO,simulation); + List list = this.generateNew(config, mapVO, simulation); if (!CollectionUtils.isEmpty(list)) { results.addAll(list); } @@ -149,20 +155,82 @@ public class GenerateTask { }); log.warn("generate training end...[{}]", LocalTime.now()); return CollectionUtils.isEmpty(results) ? "成功" : String.join(",", results); - }finally { + } + + @Async + @Transactional + public String generateTrainings(Long mapId) { + try { + Objects.requireNonNull(mapId, "地图id不能为空"); + //获取地图下章节和实训的关联关系,记录章节下关联的实训的类型和数量 + List lessonVOS = iLessonService.queryLessonDetail(mapId); + List rels = lessonVOS.stream().flatMap(lessonVO -> { + List rel = lessonVO.getRel(); + if (rel == null) + return null; +// rel = new ArrayList<>(); + return rel.stream(); + }).collect(Collectors.toList()); + lessonVOS = null; + Map trainingMap = iTrainingV1Service.findEntities(mapId) + .stream().collect(Collectors.toMap(Training::getId, Function.identity())); + Map chapterTrainingRelMap = new HashMap<>(); + for (LessonChapterTrainingRelVO rel : rels) { + Training training = trainingMap.get(rel.getTrainingId()); + chapterTrainingRelMap.put(rel, training); + } + trainingMap = null; + rels = null; + Map>>>>> oldRelMap + = chapterTrainingRelMap.entrySet().stream().collect( + Collectors.groupingBy(entry -> entry.getKey().getChapterId(), + Collectors.groupingBy(entry -> entry.getValue().getPrdType(), + Collectors.groupingBy(entry -> entry.getValue().getType(), + Collectors.groupingBy(entry -> entry.getValue().getOperateType()))))); + chapterTrainingRelMap = null; + //生成实训 + MapVO mapVO = iMapService.getMapDetail(mapId); + String result = generateNewTrainings(mapId, mapVO); + //根据上面的记录结果重建章节-实训关联关系 + Map>>> newTrainingMap = iTrainingV1Service.findEntities(mapId) + .stream().collect(Collectors.groupingBy(Training::getPrdType, Collectors.groupingBy(Training::getType, Collectors.groupingBy(Training::getOperateType)))); + List newRelList = new ArrayList<>(); + oldRelMap.forEach((chapterId, oldTrainingInfo) -> { + oldTrainingInfo.forEach((prdType, typeAndOperateType) -> { + Map>> groupByTypeTrainingMap = newTrainingMap.get(prdType); + if (CollectionUtils.isEmpty(groupByTypeTrainingMap)) { + return; + } + typeAndOperateType.forEach((type, operateTypeAndRelEntries) -> { + Map> groupByOperateTypeTrainingMap = groupByTypeTrainingMap.get(type); + if (CollectionUtils.isEmpty(groupByOperateTypeTrainingMap)) { + return; + } + operateTypeAndRelEntries.forEach((operateType, relEntries) -> { + List trainings = groupByOperateTypeTrainingMap.get(operateType); + if (CollectionUtils.isEmpty(trainings)) + return; + int trainingIndex = 0; + for (Map.Entry entry : relEntries) { + LessonChapterTrainingRelVO oldRel = entry.getKey(); + oldRel.setTrainingId(trainings.get(trainingIndex).getId()); + newRelList.add(oldRel); + trainingIndex++; + if (trainingIndex >= trainings.size()) + return; + } + }); + }); + }); + }); + iLessonService.addChapterTrainingRelAfterDelete(newRelList); + return result; + } finally { runningmapIds.remove(mapId); } } - @Async - public String generateTrainings(Long mapId){ - - Objects.requireNonNull(mapId, "地图id不能为空"); - MapVO mapVO = iMapService.getMapDetail(mapId); - return generateNewTrainings(mapId, mapVO); - } - - public Set getRunningmapIds(){ + public Set getRunningmapIds() { return this.runningmapIds; } diff --git a/src/main/java/club/joylink/rtss/services/training/ITrainingV1Service.java b/src/main/java/club/joylink/rtss/services/training/ITrainingV1Service.java index 922dbc9e2..960a71535 100644 --- a/src/main/java/club/joylink/rtss/services/training/ITrainingV1Service.java +++ b/src/main/java/club/joylink/rtss/services/training/ITrainingV1Service.java @@ -11,6 +11,7 @@ import club.joylink.rtss.vo.client.training.TrainingNewVO; import club.joylink.rtss.vo.client.training.TrainingQueryVO; import club.joylink.rtss.vo.client.training.TrainingResultVO; import club.joylink.rtss.vo.client.training.TrainingStepVO; +import lombok.NonNull; import java.util.List; import java.util.Map; @@ -187,4 +188,6 @@ public interface ITrainingV1Service { * @return */ List findEntities(Long mapId, String prdType); + + List findEntities(@NonNull Long mapId); } diff --git a/src/main/java/club/joylink/rtss/services/training/TrainingV1Service.java b/src/main/java/club/joylink/rtss/services/training/TrainingV1Service.java index a30ab1429..5ba070933 100644 --- a/src/main/java/club/joylink/rtss/services/training/TrainingV1Service.java +++ b/src/main/java/club/joylink/rtss/services/training/TrainingV1Service.java @@ -24,6 +24,7 @@ import club.joylink.rtss.vo.client.training.TrainingResultVO; import club.joylink.rtss.vo.client.training.TrainingStepVO; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; +import lombok.NonNull; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -689,9 +690,16 @@ public class TrainingV1Service implements ITrainingV1Service { } @Override - public List findEntities(Long mapId, String prdType) { + public List findEntities(@NonNull Long mapId, @NonNull String prdType) { TrainingExample example = new TrainingExample(); example.createCriteria().andMapIdEqualTo(mapId).andPrdTypeEqualTo(prdType); return trainingDAO.selectByExample(example); } + + @Override + public List findEntities(@NonNull Long mapId) { + TrainingExample example = new TrainingExample(); + example.createCriteria().andMapIdEqualTo(mapId); + return trainingDAO.selectByExample(example); + } } diff --git a/src/main/java/club/joylink/rtss/vo/client/LessonChapterTrainingRelVO.java b/src/main/java/club/joylink/rtss/vo/client/LessonChapterTrainingRelVO.java index 2f70b0d27..60690d62a 100644 --- a/src/main/java/club/joylink/rtss/vo/client/LessonChapterTrainingRelVO.java +++ b/src/main/java/club/joylink/rtss/vo/client/LessonChapterTrainingRelVO.java @@ -1,9 +1,9 @@ package club.joylink.rtss.vo.client; -import com.fasterxml.jackson.databind.annotation.JsonSerialize; -import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import club.joylink.rtss.entity.LsDraftRelChapterTraining; import club.joylink.rtss.entity.LsRelChapterTraining; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -22,6 +22,8 @@ public class LessonChapterTrainingRelVO { */ private String lessonCode; + private Long lessonId; + /** * 课程章节ID */ @@ -51,6 +53,7 @@ public class LessonChapterTrainingRelVO { public LessonChapterTrainingRelVO(LsDraftRelChapterTraining rel) { // this.lessonCode = rel.getLessonCode(); + this.lessonId = rel.getLessonId(); this.chapterId = rel.getChapterId(); this.trainingId = rel.getTrainingId(); this.orderNum = rel.getOrderNum(); @@ -59,6 +62,7 @@ public class LessonChapterTrainingRelVO { public LessonChapterTrainingRelVO(LsRelChapterTraining rel) { // this.lessonCode = rel.getLessonCode(); + this.lessonId = rel.getLessonId(); this.chapterId = rel.getChapterId(); this.trainingId = rel.getTrainingId(); this.orderNum = rel.getOrderNum(); diff --git a/src/main/java/club/joylink/rtss/vo/client/LessonChapterVO.java b/src/main/java/club/joylink/rtss/vo/client/LessonChapterVO.java index f62831706..aad91a056 100644 --- a/src/main/java/club/joylink/rtss/vo/client/LessonChapterVO.java +++ b/src/main/java/club/joylink/rtss/vo/client/LessonChapterVO.java @@ -1,11 +1,11 @@ package club.joylink.rtss.vo.client; +import club.joylink.rtss.entity.LsDraftLessonChapter; +import club.joylink.rtss.entity.LsLessonChapter; import club.joylink.rtss.vo.client.training.TrainingNewVO; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; -import club.joylink.rtss.entity.LsDraftLessonChapter; -import club.joylink.rtss.entity.LsLessonChapter; import io.swagger.annotations.ApiModelProperty; import lombok.Getter; import lombok.NoArgsConstructor; @@ -88,6 +88,7 @@ public class LessonChapterVO implements Comparable { public LessonChapterVO(LsLessonChapter chapter) { this.id = chapter.getId(); + this.lessonId = chapter.getLessonId(); this.name = chapter.getName(); this.remarks = chapter.getRemarks(); this.parentId = chapter.getParentId();