Merge branch 'master' of 39.106.16.162:/home/git/teaching-backend/teaching-backend

master
youhang20021127 2 months ago
commit 16070cf855
  1. 10
      src/main/java/com/teaching/backend/controller/cms/CmsEssayController.java
  2. 3
      src/main/java/com/teaching/backend/model/entity/cms/CmsEssay.java
  3. 6
      src/main/java/com/teaching/backend/model/entity/favour/SeCourseFavour.java
  4. 2
      src/main/java/com/teaching/backend/service/cms/ICmsEssayService.java
  5. 20
      src/main/java/com/teaching/backend/service/impl/cms/CmsEssayServiceImpl.java
  6. 60
      src/main/java/com/teaching/backend/service/impl/favour/SeCourseFavourServiceImpl.java

@ -87,4 +87,14 @@ public class CmsEssayController {
public BaseResponse<String> editStatus(@RequestBody CmsEssay cmsEssay){ public BaseResponse<String> editStatus(@RequestBody CmsEssay cmsEssay){
return cmsEssayService.editStatus(cmsEssay); return cmsEssayService.editStatus(cmsEssay);
} }
/**
* 浏览量
* @param id
* @return
*/
@PutMapping("/views/{id}")
public BaseResponse<String> views(@PathVariable Integer id){
return cmsEssayService.views(id);
}
} }

@ -58,6 +58,9 @@ public class CmsEssay implements Serializable {
@ApiModelProperty(value = "发布文章的用户id") @ApiModelProperty(value = "发布文章的用户id")
private Integer userId; private Integer userId;
// @TableField(exist = false)
private String username;
@ApiModelProperty(value = "文章文件") @ApiModelProperty(value = "文章文件")
private String file; private String file;

@ -31,12 +31,6 @@ public class SeCourseFavour implements Serializable {
*/ */
private String userId; private String userId;
/**
* 创建用户 id
*/
private DateTime createtime;
@TableField(exist = false) @TableField(exist = false)
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;

@ -28,4 +28,6 @@ public interface ICmsEssayService extends IService<CmsEssay> {
BaseResponse<String> editStatus(CmsEssay cmsEssay); BaseResponse<String> editStatus(CmsEssay cmsEssay);
BaseResponse<CmsEssay> queryEssayById(Integer essayId); BaseResponse<CmsEssay> queryEssayById(Integer essayId);
BaseResponse<String> views(Integer id);
} }

@ -6,9 +6,12 @@ import com.teaching.backend.common.BaseResponse;
import com.teaching.backend.common.ResultUtils; import com.teaching.backend.common.ResultUtils;
import com.teaching.backend.mapper.cms.CmsEssayMapper; import com.teaching.backend.mapper.cms.CmsEssayMapper;
import com.teaching.backend.model.entity.cms.CmsEssay; 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.cms.ICmsEssayService;
import com.teaching.backend.service.impl.umsAdmin.UmsUserServiceImpl;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.List; import java.util.List;
@ -23,10 +26,18 @@ import java.util.List;
@Service @Service
public class CmsEssayServiceImpl extends ServiceImpl<CmsEssayMapper, CmsEssay> implements ICmsEssayService { public class CmsEssayServiceImpl extends ServiceImpl<CmsEssayMapper, CmsEssay> implements ICmsEssayService {
@Resource
private UmsUserServiceImpl userService;
@Override @Override
public BaseResponse<String> addEssay(CmsEssay cmsEssay) { public BaseResponse<String> addEssay(CmsEssay cmsEssay) {
//设置文章发布时间 //设置文章发布时间
cmsEssay.setPublishTime(LocalDate.now()); cmsEssay.setPublishTime(LocalDate.now());
//设置发布人姓名
String nickName = userService.getById(cmsEssay.getUserId()).getNickName();
cmsEssay.setUsername(nickName);
//将浏览次数设置为0
cmsEssay.setReadingNumber(0);
//如果数据库里没有文章,文章id设为2 //如果数据库里没有文章,文章id设为2
Long count = query().count(); Long count = query().count();
if (count == 0){ if (count == 0){
@ -48,6 +59,9 @@ public class CmsEssayServiceImpl extends ServiceImpl<CmsEssayMapper, CmsEssay> i
@Override @Override
public BaseResponse<String> editEssay(CmsEssay cmsEssay) { public BaseResponse<String> editEssay(CmsEssay cmsEssay) {
//设置发布人姓名
String nickName = userService.getById(cmsEssay.getUserId()).getNickName();
cmsEssay.setUsername(nickName);
//获取要修改的文章id //获取要修改的文章id
Integer id = cmsEssay.getId(); Integer id = cmsEssay.getId();
//判断文章id是否为1 //判断文章id是否为1
@ -80,4 +94,10 @@ public class CmsEssayServiceImpl extends ServiceImpl<CmsEssayMapper, CmsEssay> i
public BaseResponse<CmsEssay> queryEssayById(Integer essayId) { public BaseResponse<CmsEssay> queryEssayById(Integer essayId) {
return ResultUtils.success(getById(essayId)); return ResultUtils.success(getById(essayId));
} }
@Override
public BaseResponse<String> views(Integer id) {
boolean update = update().setSql("reading_number = reading_number + 1 where id = " + id).update();
return update ? ResultUtils.success("浏览次数加1") : ResultUtils.success("error!!!");
}
} }

@ -4,6 +4,7 @@ import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.Wrapper; 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.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@ -66,40 +67,41 @@ public class SeCourseFavourServiceImpl extends ServiceImpl<SeCourseFavourMapper,
System.out.println("进入点赞" + courseId + userId); System.out.println("进入点赞" + courseId + userId);
//从数据库中校验是否存在courseid //从数据库中校验是否存在courseid
Courses courses = coursesMapper.selectById(courseId); Courses courses = coursesMapper.selectById(courseId);
boolean result = false;
if(courses == null){ if (courses == null) {
return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR,"课程不存在"); return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR, "课程不存在");
} }
//查询数据库记录,该用户是否点赞
SeCourseFavour seCourseFavour = new SeCourseFavour(); LambdaQueryWrapper<SeCourseFavour> lambdaQueryWrapper = new LambdaQueryWrapper<>();
seCourseFavour.setCourseId(courseId); lambdaQueryWrapper.eq(SeCourseFavour::getCourseId, courseId)
seCourseFavour.setUserId(userId); .eq(SeCourseFavour::getUserId, userId);
seCourseFavour.setCreatetime(DateTime.now());
QueryWrapper<SeCourseFavour> favourQueryWrapper = new QueryWrapper<>(seCourseFavour); SeCourseFavour oldSeCourseFavour = seCourseFavourMapper.selectOne(lambdaQueryWrapper);
SeCourseFavour oldSeCourseFavour = this.getOne(favourQueryWrapper); System.out.println("查询条件: courseId=" + courseId + ", userId=" + userId);
System.out.println("查询结果: " + oldSeCourseFavour);
//已点赞 //已点赞
if (oldSeCourseFavour != null) {
// 取消点赞 删除记录 if (oldSeCourseFavour == null) {
result = this.remove(favourQueryWrapper); // 如果没有记录,则添加收藏
if (result) { SeCourseFavour newSeCourseFavour = new SeCourseFavour();
System.out.println("取消点赞成功"); newSeCourseFavour.setCourseId(courseId);
return ResultUtils.success(result); newSeCourseFavour.setUserId(userId);
int insertResult = seCourseFavourMapper.insert(newSeCourseFavour);
if (insertResult > 0) {
return ResultUtils.success(true);
} else { } else {
throw new BusinessException(ErrorCode.SYSTEM_ERROR); return ResultUtils.error(ErrorCode.OPERATION_ERROR);
} }
} else { } else {
// 每个用户串行点赞 // 如果有记录,则删除记录,也就是取消收藏
// 锁必须要包裹住事务方法 int deleteResult = seCourseFavourMapper.delete(lambdaQueryWrapper);
Lock lock = new ReentrantLock();
lock.lock(); if (deleteResult > 0) {
try { return ResultUtils.success(false);
result = this.save(seCourseFavour); } else {
} catch (Exception e) { return ResultUtils.error(ErrorCode.OPERATION_ERROR);
throw new BusinessException(ErrorCode.OPERATION_ERROR);
} finally {
lock.unlock();
System.out.println("点赞成功");
return ResultUtils.success(result);
} }
} }
} }

Loading…
Cancel
Save