fixed: 线上课添加

This commit is contained in:
none 2023-03-17 13:58:14 +08:00
parent a231e7e6a0
commit 526d661255
2 changed files with 51 additions and 47 deletions

View File

@ -93,25 +93,20 @@ public class CourseController {
public JsonResponse store(@RequestBody @Validated CourseRequest req) throws ParseException { public JsonResponse store(@RequestBody @Validated CourseRequest req) throws ParseException {
Course course = courseService.createWithCategoryIdsAndDepIds(req.getTitle(), req.getThumb(), req.getIsShow(), req.getCategoryIds(), req.getDepIds()); Course course = courseService.createWithCategoryIdsAndDepIds(req.getTitle(), req.getThumb(), req.getIsShow(), req.getCategoryIds(), req.getDepIds());
Map<String, Map<String, Object>[]> chapters = req.getChapters();
Date now = new Date(); Date now = new Date();
if (req.getHours() != null) {//无章节课时配置 if (req.getHours() != null) {//无章节课时配置
List<CourseHour> insertHours = new ArrayList<>(); List<CourseHour> insertHours = new ArrayList<>();
for (Map<String, Object> hourItem : req.getHours()) { final Integer[] chapterSort = {0};
// 资源类型 for (CourseRequest.HourItem hourItem : req.getHours()) {
String typeVal = MapUtils.getString(hourItem, "type");
// 时长
Integer durationVal = MapUtils.getInteger(hourItem, "duration");
// 资源ID
Integer ridVal = MapUtils.getInteger(hourItem, "rid");
insertHours.add(new CourseHour() {{ insertHours.add(new CourseHour() {{
setChapterId(0);
setCourseId(course.getId()); setCourseId(course.getId());
setType(typeVal); setChapterId(0);
setDuration(durationVal); setSort(chapterSort[0]++);
setRid(ridVal); setTitle(hourItem.getName());
setType(hourItem.getType());
setDuration(hourItem.getDuration());
setRid(hourItem.getRid());
setCreatedAt(now); setCreatedAt(now);
}}); }});
} }
@ -119,20 +114,17 @@ public class CourseController {
hourService.saveBatch(insertHours); hourService.saveBatch(insertHours);
} }
} else { } else {
if (chapters == null) { if (req.getChapters() == null || req.getChapters().size() == 0) {
return JsonResponse.error("请配置课时"); return JsonResponse.error("请配置课时");
} }
List<CourseHour> insertHours = new ArrayList<>(); List<CourseHour> insertHours = new ArrayList<>();
final Integer[] chapterSort = {0}; final Integer[] chapterSort = {0};
for (Map.Entry<String, Map<String, Object>[]> entry : chapters.entrySet()) { for (CourseRequest.ChapterItem chapterItem : req.getChapters()) {
String chapterName = entry.getKey();
Map<String, Object>[] hoursList = entry.getValue();
CourseChapter tmpChapter = new CourseChapter() {{ CourseChapter tmpChapter = new CourseChapter() {{
setCourseId(course.getId()); setCourseId(course.getId());
setSort(chapterSort[0]++); setSort(chapterSort[0]++);
setName(chapterName); setName(chapterItem.getName());
setCreatedAt(now); setCreatedAt(now);
setUpdatedAt(now); setUpdatedAt(now);
}}; }};
@ -140,22 +132,16 @@ public class CourseController {
chapterService.save(tmpChapter); chapterService.save(tmpChapter);
final Integer[] hourSort = {0}; final Integer[] hourSort = {0};
for (Map<String, Object> hourItem : hoursList) { for (CourseRequest.HourItem hourItem : chapterItem.getHours()) {
// 资源类型
String typeVal = MapUtils.getString(hourItem, "type");
// 时长
Integer durationVal = MapUtils.getInteger(hourItem, "duration");
// 资源ID
Integer ridVal = MapUtils.getInteger(hourItem, "rid");
insertHours.add(new CourseHour() {{ insertHours.add(new CourseHour() {{
setChapterId(tmpChapter.getId()); setChapterId(tmpChapter.getId());
setCourseId(course.getId()); setCourseId(course.getId());
setType(typeVal);
setDuration(durationVal);
setRid(ridVal);
setCreatedAt(now);
setSort(hourSort[0]++); setSort(hourSort[0]++);
setTitle(hourItem.getName());
setType(hourItem.getType());
setDuration(hourItem.getDuration());
setRid(hourItem.getRid());
setCreatedAt(now);
}}); }});
} }
} }

View File

@ -5,6 +5,7 @@ import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import lombok.Data; import lombok.Data;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@ -34,29 +35,46 @@ public class CourseRequest {
@JsonProperty("category_ids") @JsonProperty("category_ids")
private Integer[] categoryIds; private Integer[] categoryIds;
// 格式 @Data
// [ public static class HourItem {
// '章节名' => [ private String name;
// [ private String type;
// 'name' => '课时名', private Integer duration;
// 'type' => '课时类型', // 可选值[VIDEO] private Integer rid;
// 'duration' => 时长, // 单位[] }
// 'rid' => 资源ID, // 如果是type=VIDEO的话则对应视频的id
// ]... @Data
// ]... public static class ChapterItem {
// ] private String name;
@NotNull(message = "chapters参数不存在") private List<HourItem> hours;
private Map<String, Map<String, Object>[]> chapters; }
// 格式 // 格式
// [ // [
// {
// 'name' => '章节名',
// 'hours' => [
// [ // [
// 'name' => '课时名', // 'name' => '课时名',
// 'type' => '课时类型', // 'type' => '课时类型',
// 'duration' => '时长', // 'duration' => '时长',
// 'rid' => '资源id', // 'rid' => '资源id',
// ]... // ],...
// ],
// }...
// ]
@NotNull(message = "chapters参数不存在")
private List<ChapterItem> chapters;
// 格式
// [
// {
// 'name' => '课时名',
// 'type' => '课时类型',
// 'duration' => '时长',
// 'rid' => '资源id',
// }...
// ] // ]
@NotNull(message = "hours参数不存在") @NotNull(message = "hours参数不存在")
private Map<String, Object>[] hours; private List<HourItem> hours;
} }