From 6a957665927308ef7148e95d58124088266772aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=90=8C=E6=96=B0?= <13658798+jjhyyds@user.noreply.gitee.com> Date: Wed, 18 Sep 2024 16:59:45 +0800 Subject: [PATCH] 9.18 --- .../model/entity/favour/SeCourseFavour.java | 6 -- .../favour/SeCourseFavourServiceImpl.java | 60 ++++++++++--------- 2 files changed, 31 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/teaching/backend/model/entity/favour/SeCourseFavour.java b/src/main/java/com/teaching/backend/model/entity/favour/SeCourseFavour.java index 436fe99..ea68f7a 100644 --- a/src/main/java/com/teaching/backend/model/entity/favour/SeCourseFavour.java +++ b/src/main/java/com/teaching/backend/model/entity/favour/SeCourseFavour.java @@ -31,12 +31,6 @@ public class SeCourseFavour implements Serializable { */ private String userId; - /** - * 创建用户 id - */ - private DateTime createtime; - - @TableField(exist = false) private static final long serialVersionUID = 1L; diff --git a/src/main/java/com/teaching/backend/service/impl/favour/SeCourseFavourServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/favour/SeCourseFavourServiceImpl.java index 5cac41a..3403412 100644 --- a/src/main/java/com/teaching/backend/service/impl/favour/SeCourseFavourServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/favour/SeCourseFavourServiceImpl.java @@ -4,6 +4,7 @@ import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateTime; import com.baomidou.mybatisplus.core.conditions.Wrapper; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; @@ -66,40 +67,41 @@ public class SeCourseFavourServiceImpl extends ServiceImpl favourQueryWrapper = new QueryWrapper<>(seCourseFavour); - SeCourseFavour oldSeCourseFavour = this.getOne(favourQueryWrapper); + + LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); + lambdaQueryWrapper.eq(SeCourseFavour::getCourseId, courseId) + .eq(SeCourseFavour::getUserId, userId); + + SeCourseFavour oldSeCourseFavour = seCourseFavourMapper.selectOne(lambdaQueryWrapper); + System.out.println("查询条件: courseId=" + courseId + ", userId=" + userId); + System.out.println("查询结果: " + oldSeCourseFavour); + //已点赞 - if (oldSeCourseFavour != null) { - // 取消点赞 删除记录 - result = this.remove(favourQueryWrapper); - if (result) { - System.out.println("取消点赞成功"); - return ResultUtils.success(result); + + if (oldSeCourseFavour == null) { + // 如果没有记录,则添加收藏 + SeCourseFavour newSeCourseFavour = new SeCourseFavour(); + newSeCourseFavour.setCourseId(courseId); + newSeCourseFavour.setUserId(userId); + int insertResult = seCourseFavourMapper.insert(newSeCourseFavour); + + if (insertResult > 0) { + return ResultUtils.success(true); } else { - throw new BusinessException(ErrorCode.SYSTEM_ERROR); + return ResultUtils.error(ErrorCode.OPERATION_ERROR); } } else { - // 每个用户串行点赞 - // 锁必须要包裹住事务方法 - Lock lock = new ReentrantLock(); - lock.lock(); - try { - result = this.save(seCourseFavour); - } catch (Exception e) { - throw new BusinessException(ErrorCode.OPERATION_ERROR); - } finally { - lock.unlock(); - System.out.println("点赞成功"); - return ResultUtils.success(result); + // 如果有记录,则删除记录,也就是取消收藏 + int deleteResult = seCourseFavourMapper.delete(lambdaQueryWrapper); + + if (deleteResult > 0) { + return ResultUtils.success(false); + } else { + return ResultUtils.error(ErrorCode.OPERATION_ERROR); } } }