Files
nuttyreading-java/src/main/java/com/peanut/modules/psyche/controller/PsycheCourseController.java
2025-02-25 18:02:38 +08:00

121 lines
5.6 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.peanut.modules.psyche.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.peanut.common.utils.R;
import com.peanut.common.utils.ShiroUtils;
import com.peanut.modules.common.entity.*;
import com.peanut.modules.common.service.UserCourseStudyingService;
import com.peanut.modules.common.service.UserVipService;
import com.peanut.modules.master.service.CourseCatalogueService;
import com.peanut.modules.master.service.CourseService;
import com.peanut.modules.master.service.UserCourseBuyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@Slf4j
@RestController("psycheCourse")
@RequestMapping("psyche/course")
public class PsycheCourseController {
@Autowired
private CourseService courseService;
@Autowired
private UserVipService userVipService;
@Autowired
private UserCourseStudyingService userCourseStudyingService;
//我的课程-全部
@RequestMapping("/getUserCourseBuy")
public R getUserCourseBuy(){
MPJLambdaWrapper<CourseEntity> wrapper = new MPJLambdaWrapper<>();
wrapper.distinct();
wrapper.rightJoin(CourseToPsyche.class,CourseToPsyche::getCourseId,CourseEntity::getId);
if (!userVipService.isPsycheVip()){
wrapper.leftJoin(UserCourseBuyEntity.class,UserCourseBuyEntity::getCourseId,CourseEntity::getId);
wrapper.eq(UserCourseBuyEntity::getUserId,ShiroUtils.getUId());
}
wrapper.selectAll(CourseEntity.class);
wrapper.orderByAsc(CourseEntity::getSort);
List<CourseEntity> courseList = courseService.list(wrapper);
for (CourseEntity c:courseList){
int i = userCourseStudyingService.count(new LambdaQueryWrapper<UserCourseStudying>()
.eq(UserCourseStudying::getCourseId,c.getId())
.eq(UserCourseStudying::getUserId,ShiroUtils.getUId()));
c.setIsStudying(i>0?1:0);
}
return R.ok().put("courseList",courseList);
}
//我的课程-过期课程
@RequestMapping("/getCourseExpire")
public R getCourseExpire(){
List courseEntities = null;
MPJLambdaWrapper<CourseEntity> wrapper = new MPJLambdaWrapper<>();
wrapper.distinct();
wrapper.leftJoin(CourseCatalogueEntity.class,CourseCatalogueEntity::getCourseId,CourseEntity::getId);
//关掉本次查询del_flg = 0的条件查询过期课程
wrapper.disableSubLogicDel().rightJoin(UserCourseBuyEntity.class,UserCourseBuyEntity::getCatalogueId,CourseCatalogueEntity::getId);
wrapper.rightJoin(CourseToPsyche.class,CourseToPsyche::getCourseId,CourseEntity::getId);
wrapper.eq(UserCourseBuyEntity::getUserId,ShiroUtils.getUId());
wrapper.eq(UserCourseBuyEntity::getDelFlag,-1);
wrapper.selectAll(CourseEntity.class);
wrapper.selectAs(CourseCatalogueEntity::getId,"catalogueId");
wrapper.selectAs(CourseCatalogueEntity::getTitle,"catalogueTitle");
wrapper.orderByAsc(CourseEntity::getSort);
wrapper.orderByAsc(CourseCatalogueEntity::getSort);
List<Map<String,Object>> list = courseService.listMaps(wrapper);
for (Map<String,Object> courseEntity : list) {
if (!userVipService.isPsycheVip()){
courseEntities.add(courseEntity);
}
}
return R.ok().put("courseList",courseEntities);
}
//我的课程-正在学习(收藏)
@RequestMapping("/getUserCourseStudying")
public R getUserCourseStudying(){
MyUserEntity user = ShiroUtils.getUser();
MPJLambdaWrapper<CourseEntity> wrapper = new MPJLambdaWrapper<>();
wrapper.leftJoin(CourseToPsyche.class,CourseToPsyche::getCourseId,CourseEntity::getId);
wrapper.leftJoin(UserCourseStudying.class,UserCourseStudying::getCourseId,CourseEntity::getId);
wrapper.eq(UserCourseStudying::getUserId,ShiroUtils.getUId());
wrapper.selectAll(CourseEntity.class);
wrapper.orderByAsc(CourseEntity::getSort);
List<CourseEntity> courseEntities = courseService.list(wrapper);
return R.ok().put("courseList",courseEntities);
}
//加入收藏(加入正在学习)
@RequestMapping("/addUserCourseStudying")
public R addUserCourseStudying(@RequestBody Map<String,Integer> map){
int isExist = userCourseStudyingService.count(new LambdaQueryWrapper<UserCourseStudying>()
.eq(UserCourseStudying::getUserId,ShiroUtils.getUId())
.eq(UserCourseStudying::getCourseId,map.get("courseId")));
if (isExist>0){
return R.error("已存在");
}else {
UserCourseStudying userCourseStudying = new UserCourseStudying();
userCourseStudying.setUserId(ShiroUtils.getUId());
userCourseStudying.setCourseId(map.get("courseId"));
userCourseStudyingService.save(userCourseStudying);
return R.ok().put("result",userCourseStudying);
}
}
//移出收藏(移出正在学习)
@RequestMapping("/removeUserCourseStudying")
public R removeUserCourseStudying(@RequestBody Map<String,Integer> map){
userCourseStudyingService.remove(new LambdaQueryWrapper<UserCourseStudying>()
.eq(UserCourseStudying::getUserId,ShiroUtils.getUId()).eq(UserCourseStudying::getCourseId,map.get("courseId")));
return R.ok();
}
}