package com.ruoyi.web.controller.api;
|
|
|
import cn.hutool.http.HttpException;
|
import com.alibaba.fastjson2.JSONArray;
|
import com.alibaba.fastjson2.JSONObject;
|
import com.alipay.api.AlipayApiException;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.ruoyi.common.core.domain.BasePage;
|
import com.ruoyi.common.core.domain.R;
|
import com.ruoyi.common.core.redis.RedisCache;
|
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.framework.web.service.TokenService;
|
import com.ruoyi.system.domain.*;
|
import com.ruoyi.system.dto.TVideoDto;
|
import com.ruoyi.system.service.*;
|
import com.ruoyi.system.vx.WeChatConfig;
|
import com.ruoyi.system.vx.WeChatUtil;
|
import com.ruoyi.web.controller.query.CourseQuery;
|
import com.ruoyi.web.controller.query.DeclareNoticeQuery;
|
import com.ruoyi.web.controller.query.PayDto;
|
import com.ruoyi.web.controller.query.VideoAddDto;
|
import com.ruoyi.web.controller.tool.AlipayTradePagePay;
|
import com.ruoyi.web.controller.tool.AlipayTradeQuery;
|
import com.wechat.pay.java.core.exception.MalformedMessageException;
|
import com.wechat.pay.java.core.exception.ServiceException;
|
import com.wechat.pay.java.service.partnerpayments.app.model.PrepayResponse;
|
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
|
import io.swagger.annotations.ApiOperation;
|
import org.springframework.web.bind.annotation.*;
|
|
import javax.annotation.Resource;
|
import java.math.BigDecimal;
|
import java.time.LocalDateTime;
|
import java.util.HashSet;
|
import java.util.List;
|
import java.util.Set;
|
|
/**
|
* <p>
|
* 课程列表 前端控制器
|
* </p>
|
*
|
* @author luodangjia
|
* @since 2024-09-19
|
*/
|
@RestController
|
@RequestMapping("/t-course")
|
public class TCourseController {
|
|
@Resource
|
private TCourseService courseService;
|
@Resource
|
private TRegionService regionService;
|
@Resource
|
private TTechnicalTitleService tTechnicalTitleService;
|
@Resource
|
private TTitleMajorService majorService;
|
|
@Resource
|
private TCoursePartService coursePartService;
|
@Resource
|
private TokenService tokenService;
|
@Resource
|
private WeChatConfig weChatConfig;
|
@Resource
|
private TOrderService orderService;
|
@Resource
|
private NativePayService nativePayService;
|
@Resource
|
private RedisCache redisCache;
|
|
|
@ApiOperation(value = "添加",tags = "后台-教学视频管理")
|
@PostMapping(value = "/add")
|
public R add(@RequestBody TCourse course) {
|
courseService.save(course);
|
return R.ok();
|
}
|
|
@Resource
|
private TCourseCommentService courseCommentService;
|
@ApiOperation(value = "删除",tags = "后台-教学视频管理")
|
@PostMapping(value = "/delete")
|
public R delete(String ids) {
|
String[] split = ids.split(",");
|
for (String id : split) {
|
|
courseService.removeById(id);
|
coursePartService.remove(Wrappers.lambdaQuery(TCoursePart.class).eq(TCoursePart::getCourseId,id));
|
courseCommentService.remove(Wrappers.lambdaQuery(TCourseComment.class).eq(TCourseComment::getCourseId,id));
|
|
}
|
return R.ok();
|
}
|
//编辑
|
@ApiOperation(value = "编辑",tags = "后台-教学视频管理")
|
@PostMapping(value = "/edit")
|
public R edit(@RequestBody TCourse course) {
|
courseService.updateById(course);
|
return R.ok();
|
}
|
//列表
|
@ApiOperation(value = "查询",tags = {"后台-教学视频管理","web教学视频查询"})
|
@PostMapping(value = "/list")
|
public R<Page<TCourse>> list(@RequestBody CourseQuery informationQuery) {
|
Long userId = tokenService.getLoginUser().getUserId();
|
Page<TCourse> page;
|
if (informationQuery.getSortType()==1){
|
page = courseService.lambdaQuery()
|
.like(!StringUtils.isEmpty(informationQuery.getName()), TCourse::getCourseName, informationQuery.getName())
|
.eq(informationQuery.getRegionId() != null, TCourse::getRegionId, informationQuery.getRegionId())
|
.eq(informationQuery.getTechnicalId() != null, TCourse::getTechnicalId, informationQuery.getTechnicalId())
|
.eq(informationQuery.getMajorId() != null, TCourse::getMajorId, informationQuery.getMajorId())
|
.eq(informationQuery.getLevel() != null, TCourse::getLevel, informationQuery.getLevel())
|
.orderByDesc(TCourse::getCreateTime)
|
.page(Page.of(informationQuery.getPageNum(), informationQuery.getPageSize()));
|
}else {
|
page = courseService.lambdaQuery()
|
.like(!StringUtils.isEmpty(informationQuery.getName()), TCourse::getCourseName, informationQuery.getName())
|
.eq(informationQuery.getRegionId() != null, TCourse::getRegionId, informationQuery.getRegionId())
|
.eq(informationQuery.getTechnicalId() != null, TCourse::getTechnicalId, informationQuery.getTechnicalId())
|
.eq(informationQuery.getMajorId() != null, TCourse::getMajorId, informationQuery.getMajorId())
|
.eq(informationQuery.getLevel() != null, TCourse::getLevel, informationQuery.getLevel())
|
.orderByDesc(TCourse::getCommitteeSort)
|
.page(Page.of(informationQuery.getPageNum(), informationQuery.getPageSize()));
|
}
|
|
Set<Long> cacheSet = redisCache.getCacheSet("COURSE:" + userId);
|
|
for (TCourse record : page.getRecords()) {
|
TRegion byId = regionService.getById(record.getRegionId());
|
record.setRegionName(byId.getProvinceName()+"-"+byId.getName());
|
TTechnicalTitle byId1 = tTechnicalTitleService.getById(record.getTechnicalId() );
|
TTitleMajor byId2 = majorService.getById(record.getMajorId());
|
record.setTechnicalName(byId1.getTitileName()+"-"+byId2.getMajorName());
|
if (cacheSet!=null){
|
if (cacheSet.contains(record.getId())){
|
record.setIsCollect(1);
|
}else {
|
record.setIsCollect(0);
|
}
|
}else {
|
record.setIsCollect(0);
|
}
|
|
Set<Object> cacheSet1 = redisCache.getCacheSet("STUDY:" + record.getId());
|
if (cacheSet1==null){
|
record.setStudyNum(0);
|
}else {
|
record.setStudyNum(cacheSet1.size());
|
}
|
|
Long count = orderService.lambdaQuery()
|
.eq(TOrder::getGoodId, record.getId())
|
.eq(TOrder::getGoodType, 1)
|
.eq(TOrder::getPaymentStatus, 2).count();
|
record.setBuyNum(Integer.parseInt(String.valueOf(count)));
|
|
}
|
return R.ok(page);
|
}
|
|
|
@ApiOperation(value = "其他课程推荐",tags = {"web教学视频查询"})
|
@PostMapping(value = "/other")
|
public R<List<TCourse>> other(@RequestParam Long id) {
|
List<TCourse> list = courseService.lambdaQuery().ne(TCourse::getId, id).last("limit 3").list();
|
|
for (TCourse record : list) {
|
TRegion byId = regionService.getById(record.getRegionId());
|
record.setRegionName(byId.getProvinceName()+"-"+byId.getName());
|
TTechnicalTitle byId1 = tTechnicalTitleService.getById(record.getTechnicalId() );
|
TTitleMajor byId2 = majorService.getById(record.getMajorId());
|
record.setTechnicalName(byId1.getTitileName()+"-"+byId2.getMajorName());
|
}
|
return R.ok(list);
|
}
|
|
|
|
@ApiOperation(value = "章节管理设置",tags = "后台-教学视频管理")
|
@PostMapping(value = "/video")
|
public R video(@RequestBody VideoAddDto videoAddDto) {
|
//先删除所有数据
|
coursePartService.remove(Wrappers.lambdaQuery(TCoursePart.class).eq(TCoursePart::getCourseId, videoAddDto.getCourseId()));
|
//然后一次新增
|
for (TCoursePart coursePart : videoAddDto.getCourseParts()) {
|
coursePart.setCourseId(videoAddDto.getCourseId());
|
coursePart.setVedioJson(JSONObject.toJSONString(coursePart.getVideoDtos()));
|
coursePart.setId(null);
|
}
|
coursePartService.saveBatch(videoAddDto.getCourseParts());
|
return R.ok();
|
}
|
|
|
|
@ApiOperation(value = "获取章节列表",tags = {"后台-教学视频管理","web教学视频查询"})
|
@PostMapping(value = "/detail/video")
|
public R<List<TCoursePart>> video(Long courseId) {
|
|
List<TCoursePart> list = coursePartService.lambdaQuery().eq(TCoursePart::getCourseId,courseId).orderByAsc(TCoursePart::getIndex1).list();
|
for (TCoursePart tCoursePart : list) {
|
tCoursePart.setVideoDtos(JSONArray.parseArray(tCoursePart.getVedioJson(), TVideoDto.class));
|
}
|
return R.ok(list);
|
|
}
|
|
|
@ApiOperation(value = "获取课程详情",tags = {"后台-教学视频管理","web教学视频查询"})
|
@PostMapping(value = "/detail")
|
public R<TCourse> detail(Long courseId) {
|
|
TCourse record = courseService.getById(courseId);
|
|
TRegion byId = regionService.getById(record.getRegionId());
|
record.setRegionName(byId.getProvinceName()+"-"+byId.getName());
|
TTechnicalTitle byId1 = tTechnicalTitleService.getById(record.getTechnicalId() );
|
TTitleMajor byId2 = majorService.getById(record.getMajorId());
|
record.setTechnicalName(byId1.getTitileName()+"-"+byId2.getMajorName());
|
|
|
Long userId = tokenService.getLoginUser().getUserId();
|
Set<Long> cacheSet = redisCache.getCacheSet("COURSE:" + userId);
|
if (cacheSet!=null){
|
if (cacheSet.contains(record.getId())){
|
record.setIsCollect(1);
|
}else {
|
record.setIsCollect(0);
|
}
|
}else {
|
record.setIsCollect(0);
|
}
|
Long count = orderService.lambdaQuery().eq(TOrder::getGoodType, 1).eq(TOrder::getGoodId, courseId).eq(TOrder::getPaymentStatus,2).eq(TOrder::getUserId, userId).count();
|
if (count>0){
|
record.setIsPay(1);
|
}else {
|
record.setIsPay(0);
|
}
|
|
Set<Object> cacheSet1 = redisCache.getCacheSet("STUDY:" + courseId);
|
if (cacheSet1==null){
|
record.setStudyNum(0);
|
}else {
|
record.setStudyNum(cacheSet1.size());
|
}
|
|
Set<Object> cacheSet2 = redisCache.getCacheSet("COLLECT:" + courseId);
|
if (cacheSet2==null){
|
record.setCollectNum(0);
|
}else {
|
record.setCollectNum(cacheSet2.size());
|
}
|
|
return R.ok(record);
|
|
}
|
|
@ApiOperation(value = "浏览课程调用",tags = {"web教学视频查询"})
|
@PostMapping(value = "/view")
|
public R view(Long courseId) {
|
|
TCourse byId = courseService.getById(courseId);
|
byId.setVisitNum(byId.getVisitNum()+1);
|
courseService.updateById(byId);
|
return R.ok();
|
|
}
|
|
@ApiOperation(value = "学习",tags = {"web教学视频查询"})
|
@PostMapping(value = "/study")
|
public R study(Long courseId) {
|
Long userId = tokenService.getLoginUser().getUserId();
|
|
Set<Long> users = new HashSet<>();
|
users.add(userId);
|
redisCache.setCacheSet("STUDY:" + courseId,users);
|
return R.ok();
|
|
}
|
|
@ApiOperation(value = "生成订单",tags = {"web教学视频查询"})
|
@PostMapping(value = "/create")
|
public R buy( @RequestParam Long id) throws AlipayApiException {
|
Long userId = tokenService.getLoginUser().getUserId();
|
TCourse byId = courseService.getById(id);
|
|
String code = "KC" + WeChatUtil.generateTradeNumber();
|
TOrder order = new TOrder();
|
order.setCode(code);
|
order.setUserId(userId);
|
order.setGoodType(1);
|
order.setGoodId(id);
|
order.setOrderAmount(byId.getCoursePrice());
|
order.setPaymentAmount(byId.getCoursePrice());
|
if (byId.getCoursePrice().compareTo(new BigDecimal(0))==0){
|
order.setPaymentStatus(2);
|
|
}
|
order.setPaymentStatus(1);
|
|
|
orderService.save(order);
|
return R.ok(order.getId());
|
}
|
|
|
@ApiOperation(value = "购买",tags = {"web教学视频查询"})
|
@PostMapping(value = "/buy")
|
public R<PayDto> buy(@RequestParam Integer type, @RequestParam Long orderId) throws AlipayApiException {
|
TOrder byId = orderService.getById(orderId);
|
if (type == 1) {
|
com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest prepayRequest = new com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest();
|
prepayRequest.setAppid(weChatConfig.appId);
|
prepayRequest.setMchid(weChatConfig.merchantId);
|
prepayRequest.setOutTradeNo(byId.getCode());
|
prepayRequest.setDescription("购买课程");
|
prepayRequest.setNotifyUrl("http://vwpmxwbhv59i.guyubao.com/call-back/buy");
|
com.wechat.pay.java.service.payments.nativepay.model.Amount amount = new com.wechat.pay.java.service.payments.nativepay.model.Amount();
|
amount.setTotal(byId.getPaymentAmount().multiply(new BigDecimal(100)).intValue());
|
prepayRequest.setAmount(amount);
|
// 调用下单方法,得到应答
|
PrepayResponse response;
|
try {
|
com.wechat.pay.java.service.payments.nativepay.model.PrepayResponse prepay = nativePayService.prepay(prepayRequest);
|
|
PayDto payDto = new PayDto();
|
payDto.setOrderId(orderId);
|
payDto.setQrCode(prepay.getCodeUrl());
|
return R.ok(payDto);
|
} catch (HttpException e) { // 发送HTTP请求失败
|
// log.error("发送HTTP请求失败: {}", e.getHttpRequest());
|
} catch (ServiceException e) { // 服务返回状态小于200或大于等于300,例如500
|
// log.error("服务返回状态异常: {}", e.getResponseBody());
|
} catch (MalformedMessageException e) { // 服务返回成功,返回体类型不合法,或者解析返回体失败
|
// log.error("返回体类型不合法: {}", e.getMessage());
|
} catch (Exception e) {
|
// log.error("预下单异常: {}", e.getMessage());
|
}
|
return null;
|
} else {
|
String qrCode = AlipayTradePagePay.pay("购买课程",byId.getCode(),byId.getPaymentAmount().toString());
|
PayDto payDto = new PayDto();
|
payDto.setOrderId(orderId);
|
payDto.setQrCode(qrCode);
|
new Thread(new Runnable() {
|
@Override
|
public void run() {
|
try {
|
int num = 1;
|
int wait = 0;
|
while (num <= 30) {
|
int min = 2000;
|
Thread.sleep(min);
|
Boolean check = AlipayTradeQuery.check(byId.getCode());
|
if (check){
|
byId.setPaymentStatus(2);
|
byId.setPaymentType(2);
|
byId.setPayTime(LocalDateTime.now());
|
orderService.updateById(byId);
|
|
if (byId.getGoodType()==1){
|
try {
|
TCourse byId1 = courseService.getById(byId.getGoodId());
|
byId1.setBuyNum(byId1.getBuyNum()+1);
|
courseService.updateById(byId1);
|
}catch (Exception e){
|
e.printStackTrace();
|
}
|
|
}
|
}else {
|
num++;
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
}).start();
|
return R.ok(payDto);
|
}
|
}
|
|
|
@ApiOperation(value = "收藏课程",tags = {"web-个人中心"})
|
@PostMapping(value = "/info/list")
|
public R<Page<TCourse>> infolist(@RequestBody BasePage basePage) {
|
|
Long userId = tokenService.getLoginUser().getUserId();
|
Set<Long> cacheSet = redisCache.getCacheSet("COURSE:" + userId);
|
|
|
if (!cacheSet.isEmpty()) {
|
Page<TCourse> page = courseService.lambdaQuery().in(TCourse::getId, cacheSet).page(Page.of(basePage.getPageNum(), basePage.getPageSize()));
|
for (TCourse record : page.getRecords()) {
|
Set<Object> cacheSet1 = redisCache.getCacheSet("STUDY:" + record.getId());
|
if (cacheSet1==null){
|
record.setStudyNum(0);
|
}else {
|
record.setStudyNum(cacheSet1.size());
|
}
|
}
|
return R.ok(page);
|
}else {
|
return R.ok(new Page<>());
|
}
|
}
|
|
|
|
|
@ApiOperation(value = "判断当前课程是否收藏",tags = {"web-个人中心"})
|
@PostMapping(value = "/collect/is")
|
public R<Boolean> collectis(@RequestParam Long courseId) {
|
|
Long userId = tokenService.getLoginUser().getUserId();
|
Set<Long> cacheSet = redisCache.getCacheSet("COURSE:" + userId);
|
if (cacheSet.contains(courseId)){
|
return R.ok(true);
|
}else {
|
return R.ok(false);
|
}
|
}
|
|
|
|
|
|
|
|
}
|