mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-06-22 11:52:43 +08:00
优化学员最近学习记录api
This commit is contained in:
parent
c6edf48fb0
commit
9778f1c06a
@ -37,6 +37,9 @@ public class UserController {
|
||||
@Autowired
|
||||
private CourseService courseService;
|
||||
|
||||
@Autowired
|
||||
private CourseHourService hourService;
|
||||
|
||||
@Autowired
|
||||
private UserCourseRecordService userCourseRecordService;
|
||||
|
||||
@ -166,11 +169,16 @@ public class UserController {
|
||||
@GetMapping("/latest-learn")
|
||||
public JsonResponse latestLearn() {
|
||||
// 读取当前学员最近100条学习的线上课
|
||||
List<Integer> courseIds = userCourseHourRecordService.getLatestCourseIds(FCtx.getId(), 100);
|
||||
if (courseIds == null || courseIds.size() == 0) {
|
||||
List<UserCourseHourRecord> userCourseHourRecords = userCourseHourRecordService.getLatestCourseIds(FCtx.getId(), 100);
|
||||
if (userCourseHourRecords == null || userCourseHourRecords.size() == 0) {
|
||||
return JsonResponse.data(new ArrayList<>());
|
||||
}
|
||||
|
||||
List<Integer> courseIds = userCourseHourRecords.stream().map(UserCourseHourRecord::getCourseId).toList();
|
||||
List<Integer> hourIds = userCourseHourRecords.stream().map(UserCourseHourRecord::getHourId).toList();
|
||||
Map<Integer, UserCourseHourRecord> hour2Record = userCourseHourRecords.stream().collect(Collectors.toMap(UserCourseHourRecord::getHourId, e -> e));
|
||||
Map<Integer, Integer> course2hour = userCourseHourRecords.stream().collect(Collectors.toMap(UserCourseHourRecord::getCourseId, UserCourseHourRecord::getHourId));
|
||||
|
||||
// 线上课
|
||||
Map<Integer, Course> courses = courseService.chunks(courseIds, new ArrayList<>() {{
|
||||
add("id");
|
||||
@ -181,15 +189,24 @@ public class UserController {
|
||||
add("is_required");
|
||||
}}).stream().collect(Collectors.toMap(Course::getId, e -> e));
|
||||
|
||||
// 线上课课时
|
||||
Map<Integer, CourseHour> hours = hourService.chunk(hourIds).stream().collect(Collectors.toMap(CourseHour::getId, e -> e));
|
||||
|
||||
// 获取学员的线上课进度
|
||||
Map<Integer, UserCourseRecord> records = userCourseRecordService.chunk(FCtx.getId(), courseIds).stream().collect(Collectors.toMap(UserCourseRecord::getCourseId, e -> e));
|
||||
List<UserLatestLearn> userLatestLearns = new ArrayList<>();
|
||||
for (Integer courseId : courseIds) {
|
||||
UserCourseRecord record = records.get(courseId);
|
||||
Course tmpCourse = courses.get(courseId);
|
||||
UserCourseRecord record = records.get(courseId);//线上课学习进度
|
||||
Course tmpCourse = courses.get(courseId);//线上课
|
||||
Integer tmpHourId = course2hour.get(courseId);//最近学习的课时id
|
||||
UserCourseHourRecord tmpUserCourseHourRecord = hour2Record.get(tmpHourId);//课时学习进度
|
||||
CourseHour tmpHour = hours.get(tmpHourId);//课时
|
||||
|
||||
userLatestLearns.add(new UserLatestLearn() {{
|
||||
setCourse(tmpCourse);
|
||||
setUserCourseRecord(record);
|
||||
setHourRecord(tmpUserCourseHourRecord);
|
||||
setLastLearnHour(tmpHour);
|
||||
}});
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,7 @@ import java.util.List;
|
||||
*/
|
||||
@Mapper
|
||||
public interface UserCourseHourRecordMapper extends BaseMapper<UserCourseHourRecord> {
|
||||
List<Integer> getLatestCourseIds(Integer userId, Integer size);
|
||||
List<UserCourseHourRecord> getUserLatestRecords(Integer userId, Integer size);
|
||||
}
|
||||
|
||||
|
||||
|
@ -30,4 +30,6 @@ public interface CourseHourService extends IService<CourseHour> {
|
||||
void updateSort(List<Integer> ids, Integer cid);
|
||||
|
||||
List<Integer> getRidsByCourseId(Integer courseId, String type);
|
||||
|
||||
List<CourseHour> chunk(List<Integer> hourIds);
|
||||
}
|
||||
|
@ -19,5 +19,5 @@ public interface UserCourseHourRecordService extends IService<UserCourseHourReco
|
||||
|
||||
List<UserCourseHourRecord> getRecords(Integer userId, Integer courseId);
|
||||
|
||||
List<Integer> getLatestCourseIds(Integer userId, Integer size);
|
||||
List<UserCourseHourRecord> getLatestCourseIds(Integer userId, Integer size);
|
||||
}
|
||||
|
@ -99,6 +99,11 @@ public class CourseHourServiceImpl extends ServiceImpl<CourseHourMapper, CourseH
|
||||
public List<Integer> getRidsByCourseId(Integer courseId, String type) {
|
||||
return list(query().getWrapper().eq("course_id", courseId).eq("type", type)).stream().map(CourseHour::getRid).toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CourseHour> chunk(List<Integer> hourIds) {
|
||||
return list(query().getWrapper().in("id", hourIds));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -90,8 +90,8 @@ public class UserCourseHourRecordServiceImpl extends ServiceImpl<UserCourseHourR
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> getLatestCourseIds(Integer userId, Integer size) {
|
||||
return getBaseMapper().getLatestCourseIds(userId, size);
|
||||
public List<UserCourseHourRecord> getLatestCourseIds(Integer userId, Integer size) {
|
||||
return getBaseMapper().getUserLatestRecords(userId, size);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,8 @@ package xyz.playedu.api.types.response;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
import xyz.playedu.api.domain.Course;
|
||||
import xyz.playedu.api.domain.CourseHour;
|
||||
import xyz.playedu.api.domain.UserCourseHourRecord;
|
||||
import xyz.playedu.api.domain.UserCourseRecord;
|
||||
|
||||
/**
|
||||
@ -15,4 +17,8 @@ public class UserLatestLearn {
|
||||
private Course course;
|
||||
@JsonProperty("record")
|
||||
private UserCourseRecord userCourseRecord;
|
||||
@JsonProperty("last_learn_hour")
|
||||
private CourseHour lastLearnHour;
|
||||
@JsonProperty("hour_record")
|
||||
private UserCourseHourRecord hourRecord;
|
||||
}
|
||||
|
@ -26,10 +26,16 @@
|
||||
created_at,updated_at
|
||||
</sql>
|
||||
|
||||
<select id="getLatestCourseIds" resultType="java.lang.Integer">
|
||||
select distinct `user_course_hour_records`.`course_id`
|
||||
from `user_course_hour_records`
|
||||
where `user_course_hour_records`.`user_id` = #{userId}
|
||||
order by `user_course_hour_records`.`updated_at` desc limit #{size};
|
||||
<select id="getUserLatestRecords" resultType="xyz.playedu.api.domain.UserCourseHourRecord">
|
||||
select `t1`.*
|
||||
from `user_course_hour_records` as `t1`
|
||||
inner join (select `course_id`, max(`updated_at`) as `latest_at`
|
||||
from `user_course_hour_records`
|
||||
group by `course_id`) as `t2`
|
||||
on `t2`.`course_id` = `t1`.`course_id` and
|
||||
`t2`.`latest_at` = `t1`.`updated_at`
|
||||
where `t1`.`user_id` = #{userId}
|
||||
order by `t1`.`updated_at` desc
|
||||
limit #{size};
|
||||
</select>
|
||||
</mapper>
|
||||
|
Loading…
x
Reference in New Issue
Block a user