在learning-online-learning-api
工程的com.swx.learning.api
包下创建控制类
@Api(value = "我的课程表接口", tags = "我的课程表接口") @RestController @RequestMapping("/choosecourse") public class MyCourseTablesController {
}
|
在learning-online-learning-service
工程的com.swx.learning.service
包下创建接口类
public interface MyCourseTablesService { }
|
实现类
@Service public class MyCourseTablesServiceImpl implements MyCourseTablesService {
}
|
查询学习资格
一共有三个状态
{ "702001": "可学习", "702002": "没支付", "702003": "已过期", }
|
接口信息
返回结果VO
CourseCategoryTreeVO@Data @EqualsAndHashCode(callSuper = true) public class XcCourseTablesVO extends XcCourseTables { public String learnStatus; }
|
查询资格Service
在 MyCourseTablesService 中定义查询学习资格方法
MyCourseTablesServicepublic interface MyCourseTablesService {
public XcCourseTablesVO getLearningStatus(String userId, Long courseId); }
|
实现该方法
MyCourseTablesServiceImpl
@Service public class MyCourseTablesServiceImpl implements MyCourseTablesService { private final XcCourseTablesService xcCourseTablesService;
public MyCourseTablesServiceImpl(XcCourseTablesService xcCourseTablesService) { this.xcCourseTablesService = xcCourseTablesService; }
@Override public XcCourseTablesVO getLearningStatus(String userId, Long courseId) { return xcCourseTablesService.getLearningStatus(userId, courseId); } }
|
查询学习资格
找到 XcCourseTablesService 服务类,定义如下的方法
XcCourseTablesServicepublic interface XcCourseTablesService extends IService<XcCourseTables> {
XcCourseTablesVO getLearningStatus(String userId, Long courseId); }
|
实现上述方法:
XcCourseTablesServiceImpl@Service public class XcCourseTablesServiceImpl extends ServiceImpl<XcCourseTablesMapper, XcCourseTables> implements XcCourseTablesService {
@Override public XcCourseTablesVO getLearningStatus(String userId, Long courseId) { XcCourseTables courseTable = getCourseTable(userId, courseId); XcCourseTablesVO xcCourseTablesVO = new XcCourseTablesVO(); if (courseTable == null) { xcCourseTablesVO.setLearnStatus("702002"); return xcCourseTablesVO; } boolean before = courseTable.getValidtimeEnd().isBefore(LocalDateTime.now()); BeanUtils.copyProperties(courseTable, xcCourseTablesVO); if (before) { xcCourseTablesVO.setLearnStatus("702003"); } else { xcCourseTablesVO.setLearnStatus("702001"); } return xcCourseTablesVO; } }
|
查询资格Controller
MyCourseTablesController@Api(value = "我的课程表接口", tags = "我的课程表接口") @RestController @RequestMapping("/choosecourse") public class MyCourseTablesController {
private final MyCourseTablesService myCourseTablesService;
public MyCourseTablesController(MyCourseTablesService myCourseTablesService) { this.myCourseTablesService = myCourseTablesService; }
@ApiOperation("查询学习资格") @PostMapping("/learnstatus/{courseId}") public XcCourseTablesVO getLearningStatus(@PathVariable("courseId") Long courseId) { SecurityUtil.XcUser user = SecurityUtil.getUser(); String userId = user.getId(); return myCourseTablesService.getLearningStatus(userId, courseId); } }
|
添加选课信息
要判断课程是否收费以及详细信息,远程调用内容微服务,查询已发布的课程信息。具体业务流程如下:
接口信息
返回结果VO
CourseCategoryTreeVO@Data @EqualsAndHashCode(callSuper = true) public class XcChooseCourseVO extends XcChooseCourse { public String learnStatus; }
|
查询已发布课程
修改内容微服务,增加查询已发布课程的接口,新建client
包,不使用@ResponseResult注解,可防止返回包装类,直接返回原始类型。
@RestController public class CoursePublishClient { private final CoursePublishService coursePublishService;
public CoursePublishClient(CoursePublishService coursePublishService) { this.coursePublishService = coursePublishService; }
@ApiOperation("查询课程发布信息") @GetMapping("/r/coursepublish/{courseId}") public CoursePublish getCoursePublish(@PathVariable("courseId") Long courseId) { return coursePublishService.getById(courseId); } }
|
在learning-online-learning-service
工程下创建Feign的调用接口:
@FeignClient(value = "content-api", fallbackFactory = ContentServiceClientFallbackFactory.class) public interface ContentServiceClient { @GetMapping("/content/r/coursepublish/{courseId}") public CoursePublish getCoursePublish(@PathVariable("courseId") Long courseId); }
|
熔断降级
import org.springframework.cloud.openfeign.FallbackFactory;
@Slf4j @Component public class ContentServiceClientFallbackFactory implements FallbackFactory<ContentServiceClient> {
@Override public ContentServiceClient create(Throwable throwable) { return courseId -> { log.error("远程调用内容服务熔断降级: 课程ID: {}", courseId); return null; }; } }
|
添加选课Service
在 MyCourseTablesService 中定义添加选课方法
MyCourseTablesServicepublic interface MyCourseTablesService {
public XcChooseCourseVO addChooseCourse(String userId, Long courseId); }
|
实现该方法
MyCourseTablesServiceImpl
@Service public class MyCourseTablesServiceImpl implements MyCourseTablesService { private final XcChooseCourseService xcChooseCourseService; private final XcCourseTablesService xcCourseTablesService; private final ContentServiceClient contentServiceClient;
public MyCourseTablesServiceImpl(XcChooseCourseService xcChooseCourseService, XcCourseTablesService xcCourseTablesService, ContentServiceClient contentServiceClient) { this.xcChooseCourseService = xcChooseCourseService; this.xcCourseTablesService = xcCourseTablesService; this.contentServiceClient = contentServiceClient; }
@Override @Transactional(rollbackFor = Exception.class) public XcChooseCourseVO addChooseCourse(String userId, Long courseId) { CoursePublish coursePublish = Optional.ofNullable(contentServiceClient.getCoursePublish(courseId)) .orElseThrow(() -> new BizException("课程不存在"));
String charge = coursePublish.getCharge(); XcChooseCourse chooseCourse = null; if ("201000".equals(charge)) { chooseCourse = xcChooseCourseService.addFreeCourse(userId, coursePublish); XcCourseTables xcCourseTables = xcCourseTablesService.addCourseTables(chooseCourse); } else { chooseCourse = xcChooseCourseService.addChargeCourse(userId, coursePublish); } XcChooseCourseVO xcChooseCourseVO = new XcChooseCourseVO(); BeanUtils.copyProperties(chooseCourse, xcChooseCourseVO); XcCourseTablesVO xcCourseTablesVO = getLearningStatus(userId, courseId); xcChooseCourseVO.setLearnStatus(xcCourseTablesVO.getLearnStatus()); return xcChooseCourseVO; } }
|
添加课程记录
这里提供两个方法,分别是添加收费课程的记录和付费课程的记录
找到 XcChooseCourseService 服务类,定义上述两个方法:
XcChooseCourseServicepublic interface XcChooseCourseService extends IService<XcChooseCourse> {
XcChooseCourse addFreeCourse(String userId, CoursePublish coursePublish);
XcChooseCourse addChargeCourse(String userId, CoursePublish coursePublish); }
|
实现该方法:
XcChooseCourseServiceImpl@Service public class XcChooseCourseServiceImpl extends ServiceImpl<XcChooseCourseMapper, XcChooseCourse> implements XcChooseCourseService {
@Override @Transactional(rollbackFor = Exception.class) public XcChooseCourse addFreeCourse(String userId, CoursePublish coursePublish) { return addCourse(userId, coursePublish, false); }
@Override @Transactional(rollbackFor = Exception.class) public XcChooseCourse addChargeCourse(String userId, CoursePublish coursePublish) { return addCourse(userId, coursePublish, true); }
public XcChooseCourse addCourse(String userId, CoursePublish coursePublish, boolean isCharge) { String status = "701001"; String orderType = "700001"; if (isCharge) { status = "701002"; orderType = "700002"; } Long courseId = coursePublish.getId(); List<XcChooseCourse> chooseCourses = list(Wrappers.<XcChooseCourse>lambdaQuery() .eq(XcChooseCourse::getUserId, userId) .eq(XcChooseCourse::getCourseId, courseId) .eq(XcChooseCourse::getStatus, status) .eq(XcChooseCourse::getOrderType, orderType)); if (!chooseCourses.isEmpty()) { return chooseCourses.get(0); }
XcChooseCourse chooseCourse = new XcChooseCourse(); chooseCourse.setCourseId(courseId); chooseCourse.setCourseName(coursePublish.getName()); chooseCourse.setCompanyId(coursePublish.getCompanyId()); chooseCourse.setUserId(userId); chooseCourse.setOrderType(orderType); chooseCourse.setCreateDate(LocalDateTime.now()); chooseCourse.setCoursePrice(coursePublish.getPrice()); chooseCourse.setValidDays(365); chooseCourse.setStatus(status); chooseCourse.setValidtimeStart(LocalDateTime.now()); chooseCourse.setValidtimeEnd(chooseCourse.getValidtimeStart().plusDays(chooseCourse.getValidDays())); boolean save = save(chooseCourse); if (!save) { throw new BizException("添加选课记录失败"); } return chooseCourse; } }
|
添加我的课程表
免费课程可直接加入我的课程表,收费课程需要支付完成后方可加入我的课程表
找到 XcCourseTablesService 服务类,定义如下的方法
XcCourseTablesServicepublic interface XcCourseTablesService extends IService<XcCourseTables> {
XcCourseTables addCourseTables(XcChooseCourse xcChooseCourse);
XcCourseTables getCourseTable(String userId, Long courseId); }
|
实现上述方法:
XcCourseTablesServiceImpl@Service public class XcCourseTablesServiceImpl extends ServiceImpl<XcCourseTablesMapper, XcCourseTables> implements XcCourseTablesService {
@Override @Transactional(rollbackFor = Exception.class) public XcCourseTables addCourseTables(XcChooseCourse xcChooseCourse) { String status = xcChooseCourse.getStatus(); if (!status.equals("701001")) { throw new BizException("选课没有成功,无法添加到课程表"); } XcCourseTables courseTables = getCourseTable(xcChooseCourse.getUserId(), xcChooseCourse.getCourseId()); if (courseTables != null) { return courseTables; } courseTables = new XcCourseTables(); BeanUtils.copyProperties(xcChooseCourse, courseTables); courseTables.setChooseCourseId(xcChooseCourse.getId()); courseTables.setCourseType(xcChooseCourse.getOrderType()); courseTables.setUpdateDate(LocalDateTime.now()); boolean save = save(courseTables); if (!save) { throw new BizException("添加我的课程表失败"); } return courseTables; }
@Override public XcCourseTables getCourseTable(String userId, Long courseId) { return getOne(Wrappers.<XcCourseTables>lambdaQuery() .eq(XcCourseTables::getUserId, userId) .eq(XcCourseTables::getCourseId, courseId)); }
}
|
添加选课Controller
MyCourseTablesController@Api(value = "我的课程表接口", tags = "我的课程表接口") @RestController @RequestMapping("/choosecourse") public class MyCourseTablesController {
private final MyCourseTablesService myCourseTablesService;
public MyCourseTablesController(MyCourseTablesService myCourseTablesService) { this.myCourseTablesService = myCourseTablesService; }
@ApiOperation("添加选课") @PostMapping("/{courseId}") public XcChooseCourseVO addChooseCourse(@PathVariable("courseId") Long courseId) { SecurityUtil.XcUser user = SecurityUtil.getUser(); String userId = user.getId(); return myCourseTablesService.addChooseCourse(userId, courseId); } }
|
查询课程表
接口信息
参数DTO
@Data public class MyCourseTableParamsDTO { String userId; private String courseType; private String sortType; private String expiresType;
int page = 1; int startIndex; int size = 4; }
|
查询Service
在 MyCourseTablesService 中定义查询我的课程表方法
MyCourseTablesService
PageResult<XcCourseTables> getMyCourseTables(MyCourseTableParamsDTO dto);
|
实现该方法:
MyCourseTablesServiceImpl
@Override public PageResult<XcCourseTables> getMyCourseTables(MyCourseTableParamsDTO dto) { String userId = dto.getUserId(); int size = dto.getSize(); int pageNo = dto.getPage(); Page<XcCourseTables> iPage = new Page<>(pageNo, size); Page<XcCourseTables> pageResult = xcCourseTablesService.page(iPage, Wrappers.<XcCourseTables>lambdaQuery() .eq(XcCourseTables::getUserId, userId)); return new PageResult<>(pageResult.getRecords(), pageResult.getTotal(), pageNo, size); }
|
查询Controller
MyCourseTablesController@ApiOperation("我的课程表") @GetMapping("/mycoursetable") public PageResult<XcCourseTables> getMyCourseTables(MyCourseTableParamsDTO dto) { SecurityUtil.XcUser user = SecurityUtil.getUser(); dto.setUserId(user.getId()); return myCourseTablesService.getMyCourseTables(dto); }
|