无关风月
2025-01-01 ca070436770f715572405880cfe548c02f639c73
ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TCourseController.java
@@ -34,9 +34,11 @@
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
 * <p>
@@ -86,13 +88,13 @@
    @PostMapping(value = "/delete")
    public R delete(String ids) {
        String[] split = ids.split(",");
        List<Integer> courseIds = new ArrayList<>();
        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));
            courseIds.add(Integer.parseInt(id));
        }
        courseService.removeBatchByIds(courseIds);
        coursePartService.remove(Wrappers.lambdaQuery(TCoursePart.class).in(TCoursePart::getCourseId,courseIds));
        courseCommentService.remove(Wrappers.lambdaQuery(TCourseComment.class).in(TCourseComment::getCourseId,courseIds));
        return R.ok();
    }
    //编辑
@@ -107,25 +109,40 @@
    @PostMapping(value = "/list")
    public R<Page<TCourse>> list(@RequestBody CourseQuery informationQuery) {
        Long userId = tokenService.getLoginUser().getUserId();
        Page<TCourse> 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()));
        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);
        List<TTechnicalTitle> tTechnicalTitles = tTechnicalTitleService.lambdaQuery().list();
        List<TTitleMajor> tTitleMajors = majorService.lambdaQuery().list();
        List<TRegion> regions = regionService.lambdaQuery().list();
        List<TOrder> orders = orderService.lambdaQuery()
                .eq(TOrder::getGoodType, 1)
                .eq(TOrder::getPaymentStatus, 2).list();
        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());
            TTechnicalTitle tTechnicalTitle = tTechnicalTitles.stream().filter(e -> e.getId().equals(record.getTechnicalId())).findFirst().orElse(null);
            TTitleMajor tTitleMajor = tTitleMajors.stream().filter(e -> e.getId().equals(record.getMajorId())).findFirst().orElse(null);
            TRegion region = regions.stream().filter(e -> e.getId().equals(record.getRegionId())).findFirst().orElse(null);
            record.setRegionName(region.getProvinceName()+"-"+region.getName());
            record.setTechnicalName(tTechnicalTitle.getTitileName()+"-"+tTitleMajor.getMajorName());
            if (cacheSet!=null){
                if (cacheSet.contains(record.getId())){
                    record.setIsCollect(1);
@@ -142,7 +159,8 @@
            }else {
                record.setStudyNum(cacheSet1.size());
            }
            int count = orders.stream().filter(e -> e.getGoodId().equals(record.getId())).collect(Collectors.toList()).size();
            record.setBuyNum(count);
        }
        return R.ok(page);
    }
@@ -152,13 +170,15 @@
    @PostMapping(value = "/other")
    public R<List<TCourse>> other(@RequestParam Long id) {
        List<TCourse> list = courseService.lambdaQuery().ne(TCourse::getId, id).last("limit 3").list();
        List<TRegion> regions = regionService.lambdaQuery().list();
        List<TTechnicalTitle> tTechnicalTitles = tTechnicalTitleService.lambdaQuery().list();
        List<TTitleMajor> tTitleMajors = majorService.lambdaQuery().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());
            TRegion region = regions.stream().filter(e -> e.getId().equals(record.getRegionId())).findFirst().orElse(null);
            TTechnicalTitle tTechnicalTitle = tTechnicalTitles.stream().filter(e -> e.getId().equals(record.getTechnicalId())).findFirst().orElse(null);
            TTitleMajor tTitleMajor = tTitleMajors.stream().filter(e -> e.getId().equals(record.getMajorId())).findFirst().orElse(null);
            record.setRegionName(region.getProvinceName()+"-"+region.getName());
            record.setTechnicalName(tTechnicalTitle.getTitileName()+"-"+tTitleMajor.getMajorName());
        }
        return R.ok(list);
    }
@@ -201,11 +221,11 @@
        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());
        TRegion region = regionService.getById(record.getRegionId());
        record.setRegionName(region.getProvinceName()+"-"+region.getName());
        TTechnicalTitle technicalTitle = tTechnicalTitleService.getById(record.getTechnicalId() );
        TTitleMajor tTitleMajor = majorService.getById(record.getMajorId());
        record.setTechnicalName(technicalTitle.getTitileName()+"-"+tTitleMajor.getMajorName());
        Long userId = tokenService.getLoginUser().getUserId();
@@ -226,18 +246,18 @@
            record.setIsPay(0);
        }
        Set<Object> cacheSet1 = redisCache.getCacheSet("STUDY:" + courseId);
        if (cacheSet1==null){
        Set<Object> studySet = redisCache.getCacheSet("STUDY:" + courseId);
        if (studySet==null){
            record.setStudyNum(0);
        }else {
            record.setStudyNum(cacheSet1.size());
            record.setStudyNum(studySet.size());
        }
        Set<Object> cacheSet2 = redisCache.getCacheSet("COLLECT:" + courseId);
        if (cacheSet2==null){
        Set<Object> collectSet = redisCache.getCacheSet("COLLECT:" + courseId);
        if (collectSet==null){
            record.setCollectNum(0);
        }else {
            record.setCollectNum(cacheSet2.size());
            record.setCollectNum(collectSet.size());
        }
        return R.ok(record);
@@ -248,9 +268,9 @@
    @PostMapping(value = "/view")
    public R view(Long courseId) {
        TCourse byId = courseService.getById(courseId);
        byId.setVisitNum(byId.getVisitNum()+1);
        courseService.updateById(byId);
        TCourse course = courseService.getById(courseId);
        course.setVisitNum(course.getVisitNum()+1);
        courseService.updateById(course);
        return R.ok();
    }
@@ -271,7 +291,7 @@
    @PostMapping(value = "/create")
    public R buy( @RequestParam Long id) throws AlipayApiException {
        Long userId = tokenService.getLoginUser().getUserId();
        TCourse byId = courseService.getById(id);
        TCourse course = courseService.getById(id);
        String code = "KC" + WeChatUtil.generateTradeNumber();
        TOrder order = new TOrder();
@@ -279,9 +299,9 @@
        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.setOrderAmount(course.getCoursePrice());
        order.setPaymentAmount(course.getCoursePrice());
        if (course.getCoursePrice().compareTo(new BigDecimal(0))==0){
            order.setPaymentStatus(2);
        }
@@ -296,16 +316,16 @@
    @ApiOperation(value = "购买",tags = {"web教学视频查询"})
    @PostMapping(value = "/buy")
    public R<PayDto> buy(@RequestParam Integer type, @RequestParam Long orderId) throws AlipayApiException {
        TOrder byId = orderService.getById(orderId);
        TOrder order = 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.setOutTradeNo(order.getCode());
            prepayRequest.setDescription("购买课程");
            prepayRequest.setNotifyUrl("http://www.zhipingwang.com.cn:8081/call-back/buy");
            prepayRequest.setNotifyUrl("https://0ifzoxq2516g.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());
            amount.setTotal(order.getPaymentAmount().multiply(new BigDecimal(100)).intValue());
            prepayRequest.setAmount(amount);
            // 调用下单方法,得到应答
            PrepayResponse response;
@@ -327,7 +347,7 @@
            }
            return null;
        } else {
            String qrCode = AlipayTradePagePay.pay("购买课程",byId.getCode(),byId.getPaymentAmount().toString());
            String qrCode = AlipayTradePagePay.pay("购买课程",order.getCode(),order.getPaymentAmount().toString());
            PayDto payDto = new PayDto();
            payDto.setOrderId(orderId);
            payDto.setQrCode(qrCode);
@@ -340,16 +360,16 @@
                        while (num <= 30) {
                            int min = 2000;
                            Thread.sleep(min);
                            Boolean check = AlipayTradeQuery.check(byId.getCode());
                            Boolean check = AlipayTradeQuery.check(order.getCode());
                            if (check){
                                byId.setPaymentStatus(2);
                                byId.setPaymentType(2);
                                byId.setPayTime(LocalDateTime.now());
                                orderService.updateById(byId);
                                order.setPaymentStatus(2);
                                order.setPaymentType(2);
                                order.setPayTime(LocalDateTime.now());
                                orderService.updateById(order);
                                if (byId.getGoodType()==1){
                                if (order.getGoodType()==1){
                                    try {
                                        TCourse byId1 = courseService.getById(byId.getGoodId());
                                        TCourse byId1 = courseService.getById(order.getGoodId());
                                        byId1.setBuyNum(byId1.getBuyNum()+1);
                                        courseService.updateById(byId1);
                                    }catch (Exception e){
@@ -381,6 +401,14 @@
        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<>());