diff --git a/src/main/java/com/teaching/backend/common/ErrorCode.java b/src/main/java/com/teaching/backend/common/ErrorCode.java index 041682c..a67b684 100644 --- a/src/main/java/com/teaching/backend/common/ErrorCode.java +++ b/src/main/java/com/teaching/backend/common/ErrorCode.java @@ -10,7 +10,7 @@ public enum ErrorCode { SUCCESS(200, "ok"), PARAMS_ERROR(40000, "请求参数错误"), - OBJECTIVE_OR_CONTENT_NOT_EXIT(40001, "目标或内容id不存在"), + OBJECTIVE_CONTENT_NOT_EXIT(40001, "目标内容不存在"), KONWID_NOT_EXIT(40002, "没有选择知识点"), OBJECTIVE_OR_CONTENT_EXIT(40003, "该记录已存在"), KNOW_NOT_EXIT(40004, "知识点不存在"), diff --git a/src/main/java/com/teaching/backend/controller/courses/CourseObjectivesController.java b/src/main/java/com/teaching/backend/controller/courses/CourseObjectivesController.java index c6ca203..3383eff 100644 --- a/src/main/java/com/teaching/backend/controller/courses/CourseObjectivesController.java +++ b/src/main/java/com/teaching/backend/controller/courses/CourseObjectivesController.java @@ -9,6 +9,7 @@ import com.teaching.backend.mapper.courses.CourseObjectivesMapper; import com.teaching.backend.model.dto.courses.CourseObjectivesDTO; import com.teaching.backend.model.entity.courses.CourseObjectives; import com.teaching.backend.model.vo.courses.CourseObjectivesTreeVO; +import com.teaching.backend.model.vo.courses.CourseObjectivesVO; import com.teaching.backend.service.courses.ICourseObjectivesService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -76,5 +77,12 @@ public class CourseObjectivesController { return ResultUtils.success(courseObjectivesTreeVO); } + @ApiOperation("列表显示分项目标(用于添加目标内容时选择)") + @GetMapping("/objectiveslist/{courseId}") + public BaseResponse> queryCourseObjectivesForAdd(@PathVariable String courseId){ + List courseObjectivesVOS = courseObjectivesService.queryCourseObjectivesForAdd(courseId); + return ResultUtils.success(courseObjectivesVOS); + } + } diff --git a/src/main/java/com/teaching/backend/controller/courses/ObjectiveContentKnowController.java b/src/main/java/com/teaching/backend/controller/courses/ObjectiveContentKnowController.java index d3fc4d7..bd3497b 100644 --- a/src/main/java/com/teaching/backend/controller/courses/ObjectiveContentKnowController.java +++ b/src/main/java/com/teaching/backend/controller/courses/ObjectiveContentKnowController.java @@ -31,11 +31,11 @@ public class ObjectiveContentKnowController { IObjectiveContentKnowService objectiveContentKnowService; - @ApiOperation("给分项目标或者是内容添加知识点") -// @ValidateParams({"objectiveId","know"}) + @ApiOperation("给知识点关联--目标内容") + @ValidateParams({"objectiveContentId","knowId"}) @PostMapping("/addknows") - public BaseResponse saveKnowsWithObjectiveOrContent(@RequestBody ObjectiveContentKnowDTO objectiveContentKnowDTO){ - String data = objectiveContentKnowService.saveKnowsWithObjectiveOrContent(objectiveContentKnowDTO); + public BaseResponse saveKnowsWithObjectiveContent(@RequestBody ObjectiveContentKnowDTO objectiveContentKnowDTO){ + String data = objectiveContentKnowService.saveKnowsWithObjectiveContent(objectiveContentKnowDTO); return ResultUtils.success(data); } diff --git a/src/main/java/com/teaching/backend/controller/courses/ObjectiveContentsController.java b/src/main/java/com/teaching/backend/controller/courses/ObjectiveContentsController.java index cbe1c5e..b18112f 100644 --- a/src/main/java/com/teaching/backend/controller/courses/ObjectiveContentsController.java +++ b/src/main/java/com/teaching/backend/controller/courses/ObjectiveContentsController.java @@ -31,8 +31,8 @@ public class ObjectiveContentsController { @ValidateParams({"objectiveId"}) @PostMapping("/addcontent") public BaseResponse saveContent(@RequestBody ObjectiveContents objectiveContents){ - objectiveContentsService.save(objectiveContents); - return ResultUtils.success("添加成功"); + String data = objectiveContentsService.saveWithCheck(objectiveContents); + return ResultUtils.success(data); } @ApiOperation("删除目标内容") diff --git a/src/main/java/com/teaching/backend/model/dto/courses/ObjectiveContentKnowDTO.java b/src/main/java/com/teaching/backend/model/dto/courses/ObjectiveContentKnowDTO.java index f0eb4ef..fd46c10 100644 --- a/src/main/java/com/teaching/backend/model/dto/courses/ObjectiveContentKnowDTO.java +++ b/src/main/java/com/teaching/backend/model/dto/courses/ObjectiveContentKnowDTO.java @@ -20,7 +20,7 @@ import java.io.Serializable; * @since 2024-06-13 */ @Data -@ApiModel(description = "分项目标或内容添加知识点") +@ApiModel(description = "目标内容添加知识点") public class ObjectiveContentKnowDTO extends ObjectiveContentKnow { } diff --git a/src/main/java/com/teaching/backend/model/entity/courses/ObjectiveContentKnow.java b/src/main/java/com/teaching/backend/model/entity/courses/ObjectiveContentKnow.java index fb91204..023b03b 100644 --- a/src/main/java/com/teaching/backend/model/entity/courses/ObjectiveContentKnow.java +++ b/src/main/java/com/teaching/backend/model/entity/courses/ObjectiveContentKnow.java @@ -38,12 +38,14 @@ public class ObjectiveContentKnow implements Serializable { /** * 分项目标或内容 的 id */ - private String objectiveOrContent; +// private String objectiveOrContent; + private String objectiveContentId; /** * 关联的知识点 */ - private String know; +// private String know; + private Integer knowId; } diff --git a/src/main/java/com/teaching/backend/model/entity/courses/ObjectiveContents.java b/src/main/java/com/teaching/backend/model/entity/courses/ObjectiveContents.java index 8ccd7ef..ab1a339 100644 --- a/src/main/java/com/teaching/backend/model/entity/courses/ObjectiveContents.java +++ b/src/main/java/com/teaching/backend/model/entity/courses/ObjectiveContents.java @@ -36,8 +36,6 @@ public class ObjectiveContents implements Serializable { @ApiModelProperty(value = "内容",required = true) private String content; - @ApiModelProperty(value = "排序字段",required = true) + @ApiModelProperty(value = "排序字段") private Float orderNum; - - } diff --git a/src/main/java/com/teaching/backend/model/entity/knowtmp/Knowtmp.java b/src/main/java/com/teaching/backend/model/entity/knowtmp/Knowtmp.java index 978d8e4..1e3a0ab 100644 --- a/src/main/java/com/teaching/backend/model/entity/knowtmp/Knowtmp.java +++ b/src/main/java/com/teaching/backend/model/entity/knowtmp/Knowtmp.java @@ -36,5 +36,4 @@ public class Knowtmp implements Serializable { private double hour; - } diff --git a/src/main/java/com/teaching/backend/model/vo/courses/CourseObjectivesVO.java b/src/main/java/com/teaching/backend/model/vo/courses/CourseObjectivesVO.java new file mode 100644 index 0000000..4647a83 --- /dev/null +++ b/src/main/java/com/teaching/backend/model/vo/courses/CourseObjectivesVO.java @@ -0,0 +1,23 @@ +package com.teaching.backend.model.vo.courses; + +import com.teaching.backend.model.entity.courses.CourseObjectives; +import com.teaching.backend.model.entity.courses.ObjectiveContents; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import java.io.Serializable; +import java.util.List; + +@Data +@ApiModel(description = "课程目标详情(简略)") +public class CourseObjectivesVO implements Serializable { + + @ApiModelProperty(value = "目标id") + private String id; + + @ApiModelProperty(value = "目标名称") + private String name; + + +} diff --git a/src/main/java/com/teaching/backend/model/vo/courses/ObjectiveContentKnowVO.java b/src/main/java/com/teaching/backend/model/vo/courses/ObjectiveContentKnowVO.java index d3b6a35..0460cd4 100644 --- a/src/main/java/com/teaching/backend/model/vo/courses/ObjectiveContentKnowVO.java +++ b/src/main/java/com/teaching/backend/model/vo/courses/ObjectiveContentKnowVO.java @@ -1,5 +1,6 @@ package com.teaching.backend.model.vo.courses; +import com.teaching.backend.model.vo.knowtmp.KnowTmpVO; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @@ -12,7 +13,7 @@ import java.util.List; public class ObjectiveContentKnowVO { /** - * 分项目标或内容 的 id + * 目标内容 的 id */ private String objectiveOrContent; @@ -26,17 +27,17 @@ public class ObjectiveContentKnowVO { * 关联的知识点学时合计 */ @ApiModelProperty(value = "关联的知识点学时合计") - private BigDecimal knowsTime; + private Double knowsTime; /** * 关联的知识点学时占比 */ @ApiModelProperty(value = "关联的知识点学时占比") - private String persent; + private Double persent; /** - * 分项目标下面的内容对应的知识点数据 + * 知识点的展示数据 */ - List contentKnowsData; + List knowTmpVOList; } diff --git a/src/main/java/com/teaching/backend/model/vo/knowtmp/KnowTmpVO.java b/src/main/java/com/teaching/backend/model/vo/knowtmp/KnowTmpVO.java new file mode 100644 index 0000000..b0aaab0 --- /dev/null +++ b/src/main/java/com/teaching/backend/model/vo/knowtmp/KnowTmpVO.java @@ -0,0 +1,29 @@ +package com.teaching.backend.model.vo.knowtmp; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +import java.io.Serializable; + +/** + *

+ * + *

+ * + * @author author + * @since 2024-05-31 + */ +@Data +@TableName(value = "knowtmp") +public class KnowTmpVO implements Serializable { + + private Long id; + + private String name; + + private double hour; + +} diff --git a/src/main/java/com/teaching/backend/service/courses/ICourseObjectivesService.java b/src/main/java/com/teaching/backend/service/courses/ICourseObjectivesService.java index 3d3c753..55c1783 100644 --- a/src/main/java/com/teaching/backend/service/courses/ICourseObjectivesService.java +++ b/src/main/java/com/teaching/backend/service/courses/ICourseObjectivesService.java @@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService; import com.teaching.backend.model.dto.courses.CourseObjectivesDTO; import com.teaching.backend.model.entity.courses.CourseObjectives; import com.teaching.backend.model.vo.courses.CourseObjectivesTreeVO; +import com.teaching.backend.model.vo.courses.CourseObjectivesVO; import java.util.List; @@ -37,4 +38,6 @@ public interface ICourseObjectivesService extends IService { * @return */ List queryCourseObjectivesTree(String id); + + List queryCourseObjectivesForAdd(String courseId); } diff --git a/src/main/java/com/teaching/backend/service/courses/IObjectiveContentKnowService.java b/src/main/java/com/teaching/backend/service/courses/IObjectiveContentKnowService.java index 18c2809..b6b1207 100644 --- a/src/main/java/com/teaching/backend/service/courses/IObjectiveContentKnowService.java +++ b/src/main/java/com/teaching/backend/service/courses/IObjectiveContentKnowService.java @@ -15,7 +15,7 @@ import com.teaching.backend.model.vo.courses.ObjectiveContentKnowVO; */ public interface IObjectiveContentKnowService extends IService { - String saveKnowsWithObjectiveOrContent(ObjectiveContentKnowDTO objectiveContentKnowDTO); + String saveKnowsWithObjectiveContent(ObjectiveContentKnowDTO objectiveContentKnowDTO); ObjectiveContentKnowVO getCountData(String objectiveId); } diff --git a/src/main/java/com/teaching/backend/service/courses/IObjectiveContentsService.java b/src/main/java/com/teaching/backend/service/courses/IObjectiveContentsService.java index b031659..b1f3c7c 100644 --- a/src/main/java/com/teaching/backend/service/courses/IObjectiveContentsService.java +++ b/src/main/java/com/teaching/backend/service/courses/IObjectiveContentsService.java @@ -16,4 +16,6 @@ public interface IObjectiveContentsService extends IService { String deleteById(String id); + + String saveWithCheck(ObjectiveContents objectiveContents); } diff --git a/src/main/java/com/teaching/backend/service/impl/courses/CourseObjectivesServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/courses/CourseObjectivesServiceImpl.java index 435f710..1ce4255 100644 --- a/src/main/java/com/teaching/backend/service/impl/courses/CourseObjectivesServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/courses/CourseObjectivesServiceImpl.java @@ -14,8 +14,10 @@ import com.teaching.backend.model.entity.courses.CourseObjectives; import com.teaching.backend.model.entity.courses.ObjectiveContentKnow; import com.teaching.backend.model.entity.courses.ObjectiveContents; import com.teaching.backend.model.vo.courses.CourseObjectivesTreeVO; +import com.teaching.backend.model.vo.courses.CourseObjectivesVO; import com.teaching.backend.service.courses.ICourseObjectivesService; import com.teaching.backend.utils.CourseCode; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -26,6 +28,7 @@ import java.util.List; import java.util.Map; import static com.teaching.backend.utils.CourseCode.TOTAL_OBJECTIVE_HAS_NO_CHILD; +import static com.teaching.backend.utils.CourseCode.TOTAL_OBJECTIVE_TYPE; /** *

@@ -36,6 +39,7 @@ import static com.teaching.backend.utils.CourseCode.TOTAL_OBJECTIVE_HAS_NO_CHILD * @since 2024-05-31 */ @Service +@Slf4j public class CourseObjectivesServiceImpl extends ServiceImpl implements ICourseObjectivesService { @@ -109,12 +113,12 @@ public class CourseObjectivesServiceImpl extends ServiceImpl() - .eq(ObjectiveContentKnow::getObjectiveOrContent, id)); + .eq(ObjectiveContentKnow::getObjectiveContentId, id)); if(count > CourseCode.KNOWS_EXIT.getValue()){ throw new BusinessException(ErrorCode.KNOWS_EXIT); } @@ -204,6 +208,30 @@ public class CourseObjectivesServiceImpl extends ServiceImpl queryCourseObjectivesForAdd(String courseId) { + CourseObjectives courseObjectives = courseObjectivesMapper.selectOne(new LambdaQueryWrapper<>(CourseObjectives.class) + .eq(CourseObjectives::getCourseId, courseId)); + if (courseObjectives == null){ + log.error("课程相关数据不存在:{}",courseId); + throw new BusinessException(ErrorCode.NOT_FOUND_ERROR); + } + String courseTotalObjectiveId = courseObjectives.getId(); + List objectivesLists = courseObjectivesMapper.selectList(new LambdaQueryWrapper<>(CourseObjectives.class) + .eq(CourseObjectives::getPid, courseTotalObjectiveId) + .select(CourseObjectives::getId,CourseObjectives::getType) + .orderByAsc(CourseObjectives::getType) + ); + List courseObjectivesVOS = new ArrayList<>(objectivesLists.size()); + for (CourseObjectives objectives : objectivesLists) { + CourseObjectivesVO courseObjectivesVO = new CourseObjectivesVO(); + courseObjectivesVO.setId(objectives.getId()); + courseObjectivesVO.setName(objectivesTypeMapper.selectById(objectives.getType()).getTypeName()); + courseObjectivesVOS.add(courseObjectivesVO); + } + return courseObjectivesVOS; + } + // 获取目标的内容列表 private List fetchContentsForObjective(String objectiveId) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); diff --git a/src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java index 005414f..ee9f974 100644 --- a/src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java @@ -283,7 +283,7 @@ public class CoursesServiceImpl extends ServiceImpl impl contents+=content; Long know = objectiveContentKnowMapper.selectCount(new LambdaQueryWrapper() - .eq(ObjectiveContentKnow::getObjectiveOrContent, objectiveId)); + .eq(ObjectiveContentKnow::getObjectiveContentId, objectiveId)); knows+=know; } if(contents > 0){ diff --git a/src/main/java/com/teaching/backend/service/impl/courses/ObjectiveContentKnowServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/courses/ObjectiveContentKnowServiceImpl.java index 13720df..c1ee035 100644 --- a/src/main/java/com/teaching/backend/service/impl/courses/ObjectiveContentKnowServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/courses/ObjectiveContentKnowServiceImpl.java @@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.teaching.backend.common.ErrorCode; import com.teaching.backend.exception.BusinessException; +import com.teaching.backend.mapper.Knowtemp.KnowtmpMapper; import com.teaching.backend.mapper.courses.CourseObjectivesMapper; import com.teaching.backend.mapper.courses.CoursesMapper; import com.teaching.backend.mapper.courses.ObjectiveContentKnowMapper; @@ -13,7 +14,9 @@ import com.teaching.backend.model.dto.courses.ObjectiveContentKnowDTO; import com.teaching.backend.model.entity.courses.CourseObjectives; import com.teaching.backend.model.entity.courses.ObjectiveContentKnow; import com.teaching.backend.model.entity.courses.ObjectiveContents; +import com.teaching.backend.model.entity.knowtmp.Knowtmp; import com.teaching.backend.model.vo.courses.ObjectiveContentKnowVO; +import com.teaching.backend.model.vo.knowtmp.KnowTmpVO; import com.teaching.backend.service.courses.IObjectiveContentKnowService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -40,8 +43,8 @@ public class ObjectiveContentKnowServiceImpl extends ServiceImpl knowIds = new ArrayList<>(Arrays.asList(knows.split(","))); - List objectiveContentKnowList = new ArrayList<>(); - for (String knowId : knowIds) { - ObjectiveContentKnow objectiveContentKnow = new ObjectiveContentKnow(); -// objectiveContentKnow.setId(snowflakeGenerator.next()); - objectiveContentKnow.setObjectiveOrContent(objectiveOrContent); - objectiveContentKnow.setKnow(knowId); - objectiveContentKnowList.add(objectiveContentKnow); - } - try { - objectiveContentKnowMapper.insertBatchSomeColumn(objectiveContentKnowList); - } catch (Exception e) { - e.printStackTrace(); - throw new BusinessException(ErrorCode.OBJECTIVE_OR_CONTENT_EXIT); +// String knows = objectiveContentKnowDTO.getKnow(); +// if (knows == null || knows.equals("")){ +// throw new BusinessException(ErrorCode.KONWID_NOT_EXIT); +// } +// List knowIds = new ArrayList<>(Arrays.asList(knows.split(","))); +// List objectiveContentKnowList = new ArrayList<>(); +// for (String knowId : knowIds) { +// ObjectiveContentKnow objectiveContentKnow = new ObjectiveContentKnow(); +//// objectiveContentKnow.setId(snowflakeGenerator.next()); +// objectiveContentKnow.setObjectiveOrContent(objectiveOrContent); +// objectiveContentKnow.setKnow(knowId); +// objectiveContentKnowList.add(objectiveContentKnow); +// } +// try { +// objectiveContentKnowMapper.insertBatchSomeColumn(objectiveContentKnowList); +// } catch (Exception e) { +// e.printStackTrace(); +// throw new BusinessException(ErrorCode.OBJECTIVE_OR_CONTENT_EXIT); +// } + + //添加逻辑变了 变成了给知识点添加关联的--课程目标 + ObjectiveContents objectiveContents = objectiveContentsMapper.selectById(objectiveContentKnowDTO.getObjectiveContentId()); + if (objectiveContents == null) { + throw new BusinessException(ErrorCode.OBJECTIVE_CONTENT_NOT_EXIT); } + int insert = objectiveContentKnowMapper.insert(objectiveContentKnowDTO); - return "添加成功"; + return insert > 0 ? "添加成功" : "添加失败"; } @Override - public ObjectiveContentKnowVO getCountData(String objectiveOrContentId) { - // 查询分项目标或者是下面的内容关联的知识点数据(按设计要求,这个传的就是分项目标的id) - List objectiveContentKnows = objectiveContentKnowMapper.selectList( - new LambdaQueryWrapper() - .eq(ObjectiveContentKnow::getObjectiveOrContent, objectiveOrContentId)); + public ObjectiveContentKnowVO getCountData(String objectiveContentId) { + // 创建ObjectiveContentKnowVO对象,用于存储和返回计算结果 + ObjectiveContentKnowVO objectiveContentKnowVO = new ObjectiveContentKnowVO(); - // 创建ObjectiveContentKnowVO对象,并计算相关数据 - ObjectiveContentKnowVO objectiveContentKnowVO = createObjectiveContentKnowVO(objectiveOrContentId, objectiveContentKnows); - - // 根据分项目标找到课程id + // 根据传入的目标内容ID找到课程ID和总学时 + String objectiveID = objectiveContentsMapper.selectOne(new LambdaQueryWrapper() + .eq(ObjectiveContents::getId, objectiveContentId)).getObjectiveId(); String pid = courseObjectivesMapper.selectOne(new LambdaQueryWrapper() - .eq(CourseObjectives::getId, objectiveOrContentId)).getPid(); + .eq(CourseObjectives::getId, objectiveID)).getPid(); String courseId = courseObjectivesMapper.selectOne(new LambdaQueryWrapper() - .eq(CourseObjectives::getId, pid)).getCourseId(); + .eq(CourseObjectives::getId, pid)).getCourseId(); Integer classhours = coursesMapper.selectById(courseId).getClasshours(); - // 计算并设置比例(百分比) -// BigDecimal percentage = calculatePercentage(objectiveContentKnowVO.getKnowsTime(), classhours); - -// objectiveContentKnowVO.setPersent(percentage + "%"); - - // 查找分项目标下的内容 - List objectiveContents = objectiveContentsMapper.selectList(new LambdaQueryWrapper() - .eq(ObjectiveContents::getObjectiveId, objectiveOrContentId)); + // 获取当前目标内容下关联的知识点数据 + List objectiveContentKnows = objectiveContentKnowMapper.selectList( + new LambdaQueryWrapper() + .eq(ObjectiveContentKnow::getObjectiveContentId, objectiveContentId) + ); - // 如果存在分项目标内容,处理并计算每个内容的相关数据 - if (!CollectionUtils.isEmpty(objectiveContents)) { - List contentKnowVOs = objectiveContents.stream() - .map(content -> createObjectiveContentKnowVO(content.getId(), objectiveContentKnowMapper.selectList( - new LambdaQueryWrapper() - .eq(ObjectiveContentKnow::getObjectiveOrContent, content.getId()) - ))) - .collect(Collectors.toList()); + // 创建并设置ObjectiveContentKnowVO对象的属性 + objectiveContentKnowVO = createObjectiveContentKnowVO(objectiveContentId, objectiveContentKnows); - // 计算每个内容的知识点总学时的比例(百分比) -// contentKnowVOs.forEach(contentKnowVO -> { -// BigDecimal contentPercentage = calculatePercentage(contentKnowVO.getKnowsTime(), classhours); -// contentKnowVO.setPersent(contentPercentage + "%"); -// }); + // 计算并设置该内容的知识点总学时的比例(百分比) + Double contentPercentage = calculatePercentage(objectiveContentKnowVO.getKnowsTime(), classhours); + objectiveContentKnowVO.setPersent(contentPercentage); - // 设置内容的知识点数据 - objectiveContentKnowVO.setContentKnowsData(contentKnowVOs); - } - - // 返回计算结果 + // 返回最终结果 return objectiveContentKnowVO; } - // 创建ObjectiveContentKnowVO对象,并计算知识点数量和总时间 + // 创建ObjectiveContentKnowVO对象,并计算知识点数量、总时间和关联的知识点部分数据 private ObjectiveContentKnowVO createObjectiveContentKnowVO(String objectiveOrContentId, List objectiveContentKnows) { ObjectiveContentKnowVO vo = new ObjectiveContentKnowVO(); vo.setObjectiveOrContent(objectiveOrContentId); vo.setKnowsNumber(objectiveContentKnows.size()); - // 计算知识点的总时间 -// BigDecimal knowsTime = objectiveContentKnows.stream() -// .map(contentKnow -> { -// Know know = knowMapper.selectOne(new LambdaQueryWrapper() -// .eq(Know::getId, contentKnow.getKnow())); -// if (know == null) { -// throw new BusinessException(ErrorCode.KNOW_NOT_EXIT); -// } -// return know.getHour(); -// }) -// .reduce(BigDecimal.ZERO, BigDecimal::add); - -// vo.setKnowsTime(knowsTime); + // 计算知识点的总时间,并收集关联的知识点部分数据 + Double knowsTime = 0.0; + List knowTmpVOList = new ArrayList<>(); + + for (ObjectiveContentKnow contentKnow : objectiveContentKnows) { + Knowtmp know = knowtmpMapper.selectOne(new LambdaQueryWrapper() + .eq(Knowtmp::getId, contentKnow.getKnowId())); + if (know == null) { + throw new BusinessException(ErrorCode.KNOW_NOT_EXIT); + } + knowsTime += know.getHour(); + + // 将当前知识点数据添加到knowTmpVOList中 + KnowTmpVO knowTmpVO = new KnowTmpVO(); + knowTmpVO.setId(know.getId()); + knowTmpVO.setName(know.getName()); + knowTmpVO.setHour(know.getHour()); + knowTmpVOList.add(knowTmpVO); + } + + vo.setKnowsTime(knowsTime); + vo.setKnowTmpVOList(knowTmpVOList); // 设置知识点的部分数据列表 return vo; } - // 计算知识点时间占总学时的比例,并转换为百分比 - private BigDecimal calculatePercentage(BigDecimal knowsTime, Integer classhours) { + // 计算知识点学时占总学时的比例,并返回百分比形式的结果 + private Double calculatePercentage(Double knowsTime, Integer classhours) { if (classhours == null || classhours == 0) { - return BigDecimal.ZERO; + return 0.0; } - // 将classhours转换为BigDecimal类型,以便进行精确计算 - BigDecimal classHoursBigDecimal = new BigDecimal(classhours); - // 计算knowsTime占classhours的比例,先除以classhours再乘以100,保留两位小数,并四舍五入 - return knowsTime.divide(classHoursBigDecimal, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP); + return new BigDecimal(knowsTime) + .divide(new BigDecimal(classhours), 4, RoundingMode.HALF_UP) + .multiply(new BigDecimal(100)) + .setScale(2, RoundingMode.HALF_UP) + .doubleValue(); } -} +} \ No newline at end of file diff --git a/src/main/java/com/teaching/backend/service/impl/courses/ObjectiveContentsServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/courses/ObjectiveContentsServiceImpl.java index b44668e..98e90de 100644 --- a/src/main/java/com/teaching/backend/service/impl/courses/ObjectiveContentsServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/courses/ObjectiveContentsServiceImpl.java @@ -5,16 +5,21 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.teaching.backend.common.ErrorCode; import com.teaching.backend.exception.BusinessException; +import com.teaching.backend.mapper.courses.CourseObjectivesMapper; import com.teaching.backend.mapper.courses.ObjectiveContentKnowMapper; import com.teaching.backend.mapper.courses.ObjectiveContentsMapper; +import com.teaching.backend.model.entity.courses.CourseObjectives; import com.teaching.backend.model.entity.courses.ObjectiveContentKnow; import com.teaching.backend.model.entity.courses.ObjectiveContents; import com.teaching.backend.service.courses.IObjectiveContentsService; import com.teaching.backend.utils.CourseCode; import io.swagger.annotations.Api; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import static com.teaching.backend.utils.CourseCode.*; + /** *

* 服务实现类 @@ -24,23 +29,50 @@ import org.springframework.stereotype.Service; * @since 2024-06-05 */ @Service +@Slf4j public class ObjectiveContentsServiceImpl extends ServiceImpl implements IObjectiveContentsService { @Autowired ObjectiveContentKnowMapper objectiveContentKnowMapper; @Autowired ObjectiveContentsMapper objectiveContentsMapper; + @Autowired + CourseObjectivesMapper courseObjectivesMapper; @Override public String deleteById(String id) { if (id == null){ throw new BusinessException(ErrorCode.CONTENT_NOT_EXIT); } Long count = objectiveContentKnowMapper.selectCount(new LambdaQueryWrapper() - .eq(ObjectiveContentKnow::getObjectiveOrContent, id)); + .eq(ObjectiveContentKnow::getObjectiveContentId, id)); if(count > CourseCode.KNOWS_EXIT.getValue()){ throw new BusinessException(ErrorCode.KNOWS_EXIT); } int delete = objectiveContentsMapper.deleteById(id); return delete > 0 ? "删除成功!" : "删除失败!"; } + + /** + * 在添加内容的时候需要校验,总目标和思政目标只有一个内容 + * @param objectiveContents + * @return + */ + @Override + public String saveWithCheck(ObjectiveContents objectiveContents) { + CourseObjectives courseObjective = courseObjectivesMapper.selectOne(new LambdaQueryWrapper<>(CourseObjectives.class) + .eq(CourseObjectives::getId, objectiveContents.getObjectiveId())); + if (courseObjective == null){ + log.error("课程目标数据不存在:{}",objectiveContents.getObjectiveId()); + throw new BusinessException(ErrorCode.NOT_FOUND_ERROR); + } + if (courseObjective.getType() == SI_ZHENG_TYPE.getValue() || courseObjective.getType() == TOTAL_OBJECTIVE_TYPE.getValue()){ + Long count = objectiveContentsMapper.selectCount(new LambdaQueryWrapper<>(ObjectiveContents.class) + .eq(ObjectiveContents::getObjectiveId, objectiveContents.getObjectiveId())); + if (count>CONTENT_EXIT.getValue()){ + throw new BusinessException(ErrorCode.CONTENT_EXISTS,"该目标下面已存在一条内容,无需再次添加"); + } + } + int insert = objectiveContentsMapper.insert(objectiveContents); + return insert > 0 ? "添加内容成功" : "添加内容失败"; + } }