From f0b3b635274fb03dbec066a029aedc4d1b379488 Mon Sep 17 00:00:00 2001 From: wenyu441069198 <14186472+wenyu441069198@user.noreply.gitee.com> Date: Tue, 10 Sep 2024 19:49:46 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=87=E7=AB=A0=E6=B5=8F=E8=A7=88?= =?UTF-8?q?=E9=87=8F=E8=87=AA=E5=8A=A8=E5=8A=A0=E4=B8=80,=E5=8F=91?= =?UTF-8?q?=E5=B8=83=E4=BA=BA=E5=A7=93=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/cms/CmsEssayController.java | 10 ++++++++++ .../backend/model/entity/cms/CmsEssay.java | 3 +++ .../backend/service/cms/ICmsEssayService.java | 2 ++ .../service/impl/cms/CmsEssayServiceImpl.java | 20 +++++++++++++++++++ 4 files changed, 35 insertions(+) diff --git a/src/main/java/com/teaching/backend/controller/cms/CmsEssayController.java b/src/main/java/com/teaching/backend/controller/cms/CmsEssayController.java index 02fa668..1631a0b 100644 --- a/src/main/java/com/teaching/backend/controller/cms/CmsEssayController.java +++ b/src/main/java/com/teaching/backend/controller/cms/CmsEssayController.java @@ -87,4 +87,14 @@ public class CmsEssayController { public BaseResponse editStatus(@RequestBody CmsEssay cmsEssay){ return cmsEssayService.editStatus(cmsEssay); } + + /** + * 浏览量 + * @param id + * @return + */ + @PutMapping("/views/{id}") + public BaseResponse views(@PathVariable Integer id){ + return cmsEssayService.views(id); + } } diff --git a/src/main/java/com/teaching/backend/model/entity/cms/CmsEssay.java b/src/main/java/com/teaching/backend/model/entity/cms/CmsEssay.java index 845f681..d319130 100644 --- a/src/main/java/com/teaching/backend/model/entity/cms/CmsEssay.java +++ b/src/main/java/com/teaching/backend/model/entity/cms/CmsEssay.java @@ -58,6 +58,9 @@ public class CmsEssay implements Serializable { @ApiModelProperty(value = "发布文章的用户id") private Integer userId; +// @TableField(exist = false) + private String username; + @ApiModelProperty(value = "文章文件") private String file; diff --git a/src/main/java/com/teaching/backend/service/cms/ICmsEssayService.java b/src/main/java/com/teaching/backend/service/cms/ICmsEssayService.java index 2bdb32e..49d368e 100644 --- a/src/main/java/com/teaching/backend/service/cms/ICmsEssayService.java +++ b/src/main/java/com/teaching/backend/service/cms/ICmsEssayService.java @@ -28,4 +28,6 @@ public interface ICmsEssayService extends IService { BaseResponse editStatus(CmsEssay cmsEssay); BaseResponse queryEssayById(Integer essayId); + + BaseResponse views(Integer id); } diff --git a/src/main/java/com/teaching/backend/service/impl/cms/CmsEssayServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/cms/CmsEssayServiceImpl.java index aeea607..c25153c 100644 --- a/src/main/java/com/teaching/backend/service/impl/cms/CmsEssayServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/cms/CmsEssayServiceImpl.java @@ -6,9 +6,12 @@ import com.teaching.backend.common.BaseResponse; import com.teaching.backend.common.ResultUtils; import com.teaching.backend.mapper.cms.CmsEssayMapper; import com.teaching.backend.model.entity.cms.CmsEssay; +import com.teaching.backend.model.entity.umsAdmin.UmsUser; import com.teaching.backend.service.cms.ICmsEssayService; +import com.teaching.backend.service.impl.umsAdmin.UmsUserServiceImpl; import org.springframework.stereotype.Service; +import javax.annotation.Resource; import java.time.LocalDate; import java.util.List; @@ -23,10 +26,18 @@ import java.util.List; @Service public class CmsEssayServiceImpl extends ServiceImpl implements ICmsEssayService { + @Resource + private UmsUserServiceImpl userService; + @Override public BaseResponse addEssay(CmsEssay cmsEssay) { //设置文章发布时间 cmsEssay.setPublishTime(LocalDate.now()); + //设置发布人姓名 + String nickName = userService.getById(cmsEssay.getUserId()).getNickName(); + cmsEssay.setUsername(nickName); + //将浏览次数设置为0 + cmsEssay.setReadingNumber(0); //如果数据库里没有文章,文章id设为2 Long count = query().count(); if (count == 0){ @@ -48,6 +59,9 @@ public class CmsEssayServiceImpl extends ServiceImpl i @Override public BaseResponse editEssay(CmsEssay cmsEssay) { + //设置发布人姓名 + String nickName = userService.getById(cmsEssay.getUserId()).getNickName(); + cmsEssay.setUsername(nickName); //获取要修改的文章id Integer id = cmsEssay.getId(); //判断文章id是否为1 @@ -80,4 +94,10 @@ public class CmsEssayServiceImpl extends ServiceImpl i public BaseResponse queryEssayById(Integer essayId) { return ResultUtils.success(getById(essayId)); } + + @Override + public BaseResponse views(Integer id) { + boolean update = update().setSql("reading_number = reading_number + 1 where id = " + id).update(); + return update ? ResultUtils.success("浏览次数加1") : ResultUtils.success("error!!!"); + } } 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 2/2] 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); } } }