src/main/java/com/mzl/flower/dto/request/film/FilmWorksQueryDTO.java
@@ -10,12 +10,21 @@ @ApiModelProperty(value = "中文名称") private String nameCn; @ApiModelProperty(value = "中文名称") private String nameEn; @ApiModelProperty("片场类型(FILMSET_TYPE)") private String type; @ApiModelProperty("发布状态(COMMON_PUBLISH_STATUS)") private String status; @ApiModelProperty("用户类型") private String userType; @ApiModelProperty("年份") private String releaseYear; @ApiModelProperty("分类1-为你精选,2-光影社区") private Integer classify; src/main/java/com/mzl/flower/mapper/film/CommentLikesMapper.java
@@ -2,7 +2,9 @@ import com.mzl.flower.entity.film.CommentLikes; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import org.springframework.stereotype.Repository; /** @@ -30,4 +32,9 @@ @Select("select * from comment_likes where create_by = #{userId} and comment_id = #{commentId} ") CommentLikes getCommentLikeByUserIdAndCommentId(String userId, Integer commentId); @Update("UPDATE comment_likes SET status = #{status}, update_by = #{updateBy}, update_time = NOW() WHERE id = #{id}") int updateStatusById(@Param("id") Long id, @Param("status") Boolean status, @Param("updateBy") String updateBy); } src/main/java/com/mzl/flower/mapper/film/CommentPoMapper.java
@@ -3,7 +3,9 @@ import com.mzl.flower.entity.film.CommentPo; import com.mzl.flower.entity.film.CommentPoExample; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import java.time.LocalDate; import java.util.List; @@ -99,4 +101,8 @@ int updateByPrimaryKey(CommentPo record); List<CommentPo> selectByArticleId(Integer articleId); @Select("SELECT COUNT(*) FROM film_comments WHERE film_id = #{filmId} AND DATE(create_time) = #{date} and deleted = '0' ") int countByFilmIdAndDate(Long id, LocalDate yesterday); } src/main/java/com/mzl/flower/mapper/film/FilmCollectsMapper.java
@@ -7,6 +7,8 @@ import org.apache.ibatis.annotations.Update; import org.springframework.stereotype.Repository; import java.time.LocalDate; /** * <p> * 影视作品收藏表 Mapper 接口 @@ -25,4 +27,7 @@ int updateStatusById(@Param("id") Long id, @Param("status") Boolean status, @Param("updateBy") String updateBy); @Select("SELECT COUNT(*) FROM film_collects WHERE film_id = #{filmId} AND DATE(create_time) = #{date} and deleted = '0' ") int countByFilmIdAndDate(Long id, LocalDate yesterday); } src/main/java/com/mzl/flower/mapper/film/FilmLikesMapper.java
@@ -7,6 +7,8 @@ import org.apache.ibatis.annotations.Update; import org.springframework.stereotype.Repository; import java.time.LocalDate; /** * <p> * 影视作品点赞表 Mapper 接口 @@ -26,4 +28,7 @@ int updateStatusById(@Param("id") Long id, @Param("status") Boolean status, @Param("updateBy") String updateBy); @Select("SELECT COUNT(*) FROM film_likes WHERE film_id = #{filmId} AND DATE(create_time) = #{date} and deleted = '0' ") int countByFilmIdAndDate(Long id, LocalDate yesterday); } src/main/java/com/mzl/flower/mapper/film/FilmWorksMapper.java
@@ -26,4 +26,8 @@ List<FilmWorksVO> queryPage(@Param("dto") FilmWorksQueryDTO dto, Page page); FilmWorksVO selectInfoById(@Param("id") Long id, @Param("userId") String userId); //获取权重最高的数据 @Select("select * from film_works where sticky_weight = (select max(sticky_weight) from film_works) limit 1") FilmWorks getTopStickyWeight(); } src/main/java/com/mzl/flower/schedule/ScheduleService.java
@@ -1,5 +1,11 @@ package com.mzl.flower.schedule; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.mzl.flower.entity.film.FilmWorks; import com.mzl.flower.mapper.film.CommentPoMapper; import com.mzl.flower.mapper.film.FilmCollectsMapper; import com.mzl.flower.mapper.film.FilmLikesMapper; import com.mzl.flower.mapper.film.FilmWorksMapper; import com.mzl.flower.service.customer.CustomerService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.time.DateFormatUtils; @@ -8,12 +14,24 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.time.LocalDate; import java.util.List; @Component @Slf4j public class ScheduleService { @Autowired @Resource private CustomerService customerService; @Resource private FilmWorksMapper filmWorksMapper; @Resource private FilmLikesMapper filmLikesMapper; @Resource private FilmCollectsMapper filmCollectsMapper; @Resource private CommentPoMapper commentPoMapper; @Scheduled(cron = "0 15 17 * * ?") @Profile("prod") @@ -28,4 +46,40 @@ customerService.checkVipExpireTime(); } //降低置顶权重。每天晚上11点执行一次 @Scheduled(cron = "0 0 23 * * ?") public void downFilmWorksStickyWeight() { // 每日减少10点权重,最低不低于0 filmWorksMapper.update(null, new LambdaUpdateWrapper<FilmWorks>().setSql("sticky_weight = GREATEST(sticky_weight - 10, 0)")); log.info("每日置顶权重降低完成"); } //提高置顶权重。每天早上6点执行一次(设计当天点赞+5,评论+10,收藏+10) @Scheduled(cron = "0 0 6 * * ?") public void upFilmWorksStickyWeight() { // 获取昨天(当天)的互动数据 LocalDate yesterday = LocalDate.now().minusDays(1); // 获取所有需要更新的作品 List<FilmWorks> worksList = filmWorksMapper.selectList(null); worksList.forEach(work -> { // 获取点赞数(需要实现统计方法) int likes = filmLikesMapper.countByFilmIdAndDate(work.getId(), yesterday); // 获取评论数(需要实现统计方法) int comments = filmCollectsMapper.countByFilmIdAndDate(work.getId(), yesterday); // 获取收藏数(需要实现统计方法) int collects = commentPoMapper.countByFilmIdAndDate(work.getId(), yesterday); // 计算权重增量 int increment = likes * 5 + comments * 10 + collects * 10; if (increment > 0) { filmWorksMapper.update(null, new LambdaUpdateWrapper<FilmWorks>().eq(FilmWorks::getId, work.getId()).setSql("sticky_weight = sticky_weight + " + increment)); } }); log.info("每日互动权重加成完成"); } } src/main/java/com/mzl/flower/service/film/FilmWorksService.java
@@ -31,6 +31,10 @@ void changeStatus(Long id); void changeTopState(Long id); void changeDownState(Long id); void batchDelete(BatchDTO dto); void batchPublish(BatchDTO dto); src/main/java/com/mzl/flower/service/film/impl/CommentLikesServiceImpl.java
@@ -1,6 +1,7 @@ package com.mzl.flower.service.film.impl; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.mzl.flower.config.exception.ValidationException; import com.mzl.flower.config.security.SecurityUtils; import com.mzl.flower.dto.request.film.CommentLikesDTO; import com.mzl.flower.dto.request.film.CommentLikesQueryDTO; @@ -37,16 +38,32 @@ //查询有没有对应人员的点赞信息 CommentLikes commentLikes = commentLikesMapper.getCommentLikeByUserIdAndCommentId(SecurityUtils.getUserId(), commentLikesDTO.getCommentId()); if (commentLikes == null) { CommentLikes commentLikes1 = new CommentLikes(); commentLikes1.setCommentId(commentLikesDTO.getCommentId()); commentLikes1.setStatus(true); commentLikes1.create(); commentLikesMapper.insert(commentLikes1); commentLikes = new CommentLikes(); commentLikes.setCommentId(commentLikesDTO.getCommentId()); commentLikes.setStatus(true); commentLikes.setCreateBy(SecurityUtils.getUserId()); commentLikes.create(); if (commentLikesMapper.insert(commentLikes) <= 0) { throw new ValidationException("添加评论点赞失败"); } } else { // 状态取反 commentLikes.setStatus(!commentLikes.getStatus()); commentLikes.update(SecurityUtils.getUserId()); commentLikesMapper.updateById(commentLikes); // 校验主键是否存在 if (commentLikes.getId() == null) { throw new ValidationException("主键 ID 不能为空"); } int affectedRows = commentLikesMapper.updateStatusById( commentLikes.getId(), // 确保 id 是 Long 类型 commentLikes.getStatus(), SecurityUtils.getUserId() // 更新人 ); if (affectedRows <= 0) { throw new ValidationException("更新作品点赞状态失败"); } } } src/main/java/com/mzl/flower/service/film/impl/CommentServiceImpl.java
@@ -279,7 +279,7 @@ //获取用户信息 CustomerDTO currentCustomer = customerMapper.getCurrentCustomer(commentDTO.getCreateBy()); if(!ObjectUtils.isEmpty(currentCustomer)){ commentDTO.setCommentUserName(currentCustomer.getNickName()); commentDTO.setCommentUserName(currentCustomer.getName()); commentDTO.setPicture(currentCustomer.getCover()); User user = userService.findByTel(currentCustomer.getTel(), Constants.USER_TYPE.customer.name()); if(!ObjectUtils.isEmpty(user)){ src/main/java/com/mzl/flower/service/film/impl/FilmLikesServiceImpl.java
@@ -50,7 +50,7 @@ filmLikes.setStatus(true); filmLikes.setCreateBy(SecurityUtils.getUserId()); if (filmLikesMapper.insert(filmLikes) <= 0) { throw new ValidationException("添加评论点赞失败"); throw new ValidationException("添加作品点赞失败"); } } else { // 状态取反 @@ -67,7 +67,7 @@ SecurityUtils.getUserId() // 更新人 ); if (affectedRows <= 0) { throw new ValidationException("更新评论点赞状态失败"); throw new ValidationException("更新作品点赞状态失败"); } } return true; src/main/java/com/mzl/flower/service/film/impl/FilmWorksServiceImpl.java
@@ -122,6 +122,40 @@ filmWorksMapper.updateById(filmWork); } @Override public void changeTopState(Long id) { FilmWorks filmWork = filmWorksMapper.selectById(id); if (filmWork == null) { throw new ValidationException("找不到id为" + id + "的公告"); } if (Constants.COMMON_PUBLISH_STATUS.unpublished.name().equals(filmWork.getStatus())) { throw new ValidationException("未发布作品不能置顶,请先发布"); } FilmWorks filmWorkTop = filmWorksMapper.getTopStickyWeight(); if (!ObjectUtils.isEmpty(filmWorkTop)) { filmWork.setStickyWeight(filmWorkTop.getStickyWeight() + 100); } else { filmWork.setStickyWeight(100); } filmWorksMapper.updateById(filmWork); } @Override public void changeDownState(Long id) { FilmWorks filmWork = filmWorksMapper.selectById(id); if (filmWork == null) { throw new ValidationException("找不到id为" + id + "的公告"); } if (Constants.COMMON_PUBLISH_STATUS.unpublished.name().equals(filmWork.getStatus())) { throw new ValidationException("未发布作品无需清除权重"); } filmWork.setStickyWeight(0); filmWorksMapper.updateById(filmWork); } @Override public void batchDelete(BatchDTO dto) { filmWorksMapper.deleteBatchIds(dto.getIds()); src/main/java/com/mzl/flower/web/film/FilmWorksController.java
@@ -75,6 +75,21 @@ return returnData(R.SUCCESS.getCode(), null); } @GetMapping("/filmWorks/setTop") @ApiOperation(value = "文章置顶", notes = "文章置顶") public ResponseEntity<ReturnDataDTO> setTop(@NotNull(message = "id不能为空") Long id) { filmWorksService.changeTopState(id); return returnData(R.SUCCESS.getCode(), null); } @GetMapping("/filmWorks/setDown") @ApiOperation(value = "取消置顶", notes = "取消置顶") public ResponseEntity<ReturnDataDTO> setDown(@NotNull(message = "id不能为空") Long id) { filmWorksService.changeDownState(id); return returnData(R.SUCCESS.getCode(), null); } @PostMapping("/filmWorks/delete/batch") @ApiOperation(value = "批量删除", notes = "批量删除") src/main/resources/mapper/film/FilmWorksMapper.xml
@@ -54,11 +54,20 @@ <if test="dto.nameCn != null and dto.nameCn != ''"> AND t.name_cn LIKE CONCAT('%', #{dto.nameCn}, '%') </if> <if test="dto.nameEn != null and dto.nameEn != ''"> AND t.name_en LIKE CONCAT('%', #{dto.nameEn}, '%') </if> <if test="dto.status != null and dto.status != ''"> AND t.status = #{dto.status} </if> <if test="dto.type != null and dto.type != ''"> AND t.type = #{dto.type} </if> <if test="dto.userType != null and dto.userType != ''"> AND t.user_type = #{dto.userType} </if> <if test="dto.releaseYear != null and dto.releaseYear != ''"> AND t.release_year = #{dto.releaseYear} </if> <if test="dto.classify != null and dto.classify != ''"> AND t.classify = #{dto.classify} @@ -81,7 +90,7 @@ OR t.director LIKE CONCAT('%', #{dto.keywords}, '%')) </if> ORDER BY t.update_time DESC t.sticky_weight DESC </select>