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;
/**
*
* 课程列表 前端控制器
*
*
* @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> list(@RequestBody CourseQuery informationQuery) {
Long userId = tokenService.getLoginUser().getUserId();
Page 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 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