mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-06-20 01:52:42 +08:00
优化学员的学习记录删除
This commit is contained in:
parent
af95beadad
commit
ab9e8da4b8
@ -2,9 +2,11 @@ package xyz.playedu.api.controller.backend;
|
||||
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import xyz.playedu.api.domain.UserCourseRecord;
|
||||
import xyz.playedu.api.event.UserCourseRecordDestroyEvent;
|
||||
import xyz.playedu.api.request.backend.CourseUserDestroyRequest;
|
||||
import xyz.playedu.api.service.UserCourseRecordService;
|
||||
import xyz.playedu.api.service.UserService;
|
||||
@ -12,7 +14,9 @@ import xyz.playedu.api.types.JsonResponse;
|
||||
import xyz.playedu.api.types.paginate.CourseUserPaginateFilter;
|
||||
import xyz.playedu.api.types.paginate.PaginationResult;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
@ -28,6 +32,9 @@ public class CourseUserController {
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext ctx;
|
||||
|
||||
@GetMapping("/index")
|
||||
public JsonResponse index(@PathVariable(name = "courseId") Integer courseId, @RequestParam HashMap<String, Object> params) {
|
||||
Integer page = MapUtils.getInteger(params, "page", 1);
|
||||
@ -61,7 +68,14 @@ public class CourseUserController {
|
||||
if (req.getIds().size() == 0) {
|
||||
return JsonResponse.error("请选择需要删除的数据");
|
||||
}
|
||||
userCourseRecordService.destroy(courseId, req.getIds());
|
||||
List<UserCourseRecord> records = userCourseRecordService.chunks(req.getIds(), new ArrayList<>() {{
|
||||
add("user_id");
|
||||
add("id");
|
||||
}});
|
||||
for (UserCourseRecord record : records) {
|
||||
userCourseRecordService.removeById(record);
|
||||
ctx.publishEvent(new UserCourseRecordDestroyEvent(this, record.getUserId(), courseId));
|
||||
}
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,27 @@
|
||||
package xyz.playedu.api.event;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
* @create 2023/4/4 10:12
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
public class UserCourseRecordDestroyEvent extends ApplicationEvent {
|
||||
|
||||
private Integer userId;
|
||||
private Integer courseId;
|
||||
private Date createdAt;
|
||||
|
||||
public UserCourseRecordDestroyEvent(Object source, Integer userId, Integer courseId) {
|
||||
super(source);
|
||||
this.userId = userId;
|
||||
this.courseId = courseId;
|
||||
this.createdAt = new Date();
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package xyz.playedu.api.listener;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
import xyz.playedu.api.event.UserCourseRecordDestroyEvent;
|
||||
import xyz.playedu.api.service.UserCourseHourRecordService;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
* @create 2023/4/4 10:16
|
||||
*/
|
||||
@Component
|
||||
public class UserCourseRecordDestroyListener {
|
||||
|
||||
@Autowired
|
||||
private UserCourseHourRecordService userCourseHourRecordService;
|
||||
|
||||
@EventListener
|
||||
public void emptyUserCourseHourRecords(UserCourseRecordDestroyEvent event) {
|
||||
userCourseHourRecordService.remove(event.getUserId(), event.getCourseId());
|
||||
}
|
||||
|
||||
}
|
@ -24,5 +24,7 @@ public interface UserCourseHourRecordService extends IService<UserCourseHourReco
|
||||
|
||||
void removeByCourseId(Integer courseId);
|
||||
|
||||
void remove(Integer userId, Integer courseId);
|
||||
|
||||
List<UserCourseHourRecordCountMapper> getUserCourseHourCount(Integer userId, List<Integer> courseIds, Integer isFinished);
|
||||
}
|
||||
|
@ -25,4 +25,6 @@ public interface UserCourseRecordService extends IService<UserCourseRecord> {
|
||||
void destroy(Integer courseId, List<Integer> ids);
|
||||
|
||||
void removeByCourseId(Integer courseId);
|
||||
|
||||
List<UserCourseRecord> chunks(List<Integer> ids, List<String> fields);
|
||||
}
|
||||
|
@ -99,6 +99,11 @@ public class UserCourseHourRecordServiceImpl extends ServiceImpl<UserCourseHourR
|
||||
}
|
||||
return getBaseMapper().getUserCourseHourCount(userId, courseIds, isFinished);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Integer userId, Integer courseId) {
|
||||
remove(query().getWrapper().eq("user_id", userId).eq("course_id", courseId));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -99,6 +99,11 @@ public class UserCourseRecordServiceImpl extends ServiceImpl<UserCourseRecordMap
|
||||
public void removeByCourseId(Integer courseId) {
|
||||
remove(query().getWrapper().eq("course_id", courseId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserCourseRecord> chunks(List<Integer> ids, List<String> fields) {
|
||||
return list(query().getWrapper().in("id", ids).select(fields));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user