浏览代码

update 收藏

tjf 2 月之前
父节点
当前提交
ad573aefe8

+ 22 - 0
ruoyi-modules/ruoyi-wuye/src/main/java/org/dromara/controller/communityNews/CommentStarsController.java

@@ -15,8 +15,11 @@ import org.dromara.common.log.enums.BusinessType;
 import org.dromara.common.mybatis.core.page.PageQuery;
 import org.dromara.common.mybatis.core.page.TableDataInfo;
 import org.dromara.common.web.core.BaseController;
+import org.dromara.domain.communityNews.CommentStars;
 import org.dromara.domain.communityNews.bo.CommentStarsBo;
 import org.dromara.domain.communityNews.vo.CommentStarsVo;
+import org.dromara.factory.AsyncFactory;
+import org.dromara.manager.AsyncManager;
 import org.dromara.service.ICommentStarsService;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
@@ -103,4 +106,23 @@ public class CommentStarsController extends BaseController {
                           @PathVariable Long[] starsIds) {
         return toAjax(commentStarsService.deleteWithValidByIds(List.of(starsIds), true));
     }
+
+    /**
+     * 社区资讯收藏或取消收藏
+     */
+    @Log(title = "社区资讯收藏", businessType = BusinessType.INSERT)
+    @PostMapping("/getStars")
+    public R<Void> getStars(@RequestBody CommentStars commentStars) {
+        AsyncManager.me().execute(AsyncFactory.getStars(commentStars));
+        return R.ok();
+    }
+
+    /**
+     * 党建收藏或取消收藏
+     */
+    @PostMapping("/getPartyNewsStars")
+    public R<Void> getPartyNewsStars(@RequestBody CommentStars commentStars) {
+        AsyncManager.me().execute(AsyncFactory.getPartyNewsStars(commentStars));
+        return R.ok();
+    }
 }

+ 1 - 1
ruoyi-modules/ruoyi-wuye/src/main/java/org/dromara/controller/residentInfo/ResidentInfoController.java

@@ -80,7 +80,7 @@ public class ResidentInfoController extends BaseController {
     }
 
     /**
-     * 新增居住人员信息,存储居住人员的详细信息
+     * 管理员新增居住人员信息,默认就审核过
      */
     @SaCheckPermission("wuYe:residentInfo:add")
     @Log(title = "居住人员信息,存储居住人员的详细信息", businessType = BusinessType.INSERT)

+ 7 - 5
ruoyi-modules/ruoyi-wuye/src/main/java/org/dromara/factory/AsyncFactory.java

@@ -3,7 +3,9 @@ package org.dromara.factory;
 
 import org.dromara.common.core.utils.SpringUtils;
 import org.dromara.domain.communityNews.CommentLikes;
+import org.dromara.domain.communityNews.CommentStars;
 import org.dromara.service.ICommentLikesService;
+import org.dromara.service.ICommentStarsService;
 
 import java.util.TimerTask;
 
@@ -41,7 +43,7 @@ public class AsyncFactory
      * @param commentStars 收藏信息
      * @return 任务task
      */
-/*    public static TimerTask getStars(final CommentStars commentStars)
+    public static TimerTask getStars(final CommentStars commentStars)
     {
         return new TimerTask()
         {
@@ -56,12 +58,12 @@ public class AsyncFactory
         };
     }
 
-    *//**
+    /**
      * 党建收藏操作
      *
      * @param commentStars 收藏信息
-     * @return 任务task
-     *//*
+     * @return 任务task*/
+
     public static TimerTask getPartyNewsStars(final CommentStars commentStars)
     {
         return new TimerTask()
@@ -75,7 +77,7 @@ public class AsyncFactory
                 System.out.println("完成异步党建收藏方法");
             }
         };
-    }*/
+    }
 
 
 }

+ 9 - 0
ruoyi-modules/ruoyi-wuye/src/main/java/org/dromara/mapper/CommentStarsMapper.java

@@ -1,5 +1,6 @@
 package org.dromara.mapper;
 
+import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
 import org.dromara.common.mybatis.core.mapper.BaseMapperPlus;
 import org.dromara.domain.communityNews.CommentStars;
 import org.dromara.domain.communityNews.vo.CommentStarsVo;
@@ -12,4 +13,12 @@ import org.dromara.domain.communityNews.vo.CommentStarsVo;
  */
 public interface CommentStarsMapper extends BaseMapperPlus<CommentStars, CommentStarsVo> {
 
+    /**
+     * 根据用户id和资讯id更新收藏状态 没删除就已收藏 删除=未收藏
+     * @param commentStars
+     * @return
+     */
+    @InterceptorIgnore(tenantLine = "true", dataPermission = "false")
+    int updateCommentStarsByUserIdAndCommunityId (CommentStars commentStars);
+
 }

+ 11 - 0
ruoyi-modules/ruoyi-wuye/src/main/java/org/dromara/mapper/CommunityNewsMapper.java

@@ -1,5 +1,7 @@
 package org.dromara.mapper;
 
+import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
+import org.apache.ibatis.annotations.Param;
 import org.dromara.common.mybatis.core.mapper.BaseMapperPlus;
 import org.dromara.domain.communityNews.CommunityNews;
 import org.dromara.domain.communityNews.vo.CommunityNewsVo;
@@ -12,4 +14,13 @@ import org.dromara.domain.communityNews.vo.CommunityNewsVo;
  */
 public interface CommunityNewsMapper extends BaseMapperPlus<CommunityNews, CommunityNewsVo> {
 
+    /**
+     * 更新收藏数量
+     * @param communityId
+     * @param userStars
+     * @return
+     */
+    @InterceptorIgnore(tenantLine = "true", dataPermission = "false")
+    public int updateCommunityNewsStars(@Param("communityId") Long communityId, @Param("userStars") String userStars);
+
 }

+ 16 - 0
ruoyi-modules/ruoyi-wuye/src/main/java/org/dromara/service/ICommentStarsService.java

@@ -1,7 +1,9 @@
 package org.dromara.service;
 
+import org.dromara.common.core.domain.R;
 import org.dromara.common.mybatis.core.page.PageQuery;
 import org.dromara.common.mybatis.core.page.TableDataInfo;
+import org.dromara.domain.communityNews.CommentStars;
 import org.dromara.domain.communityNews.bo.CommentStarsBo;
 import org.dromara.domain.communityNews.vo.CommentStarsVo;
 
@@ -65,4 +67,18 @@ public interface ICommentStarsService {
      * @return 是否删除成功
      */
     Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid);
+
+    /**
+     * 社区资讯收藏或取消收藏
+     * @param commentStars
+     * @return
+     */
+    public R<Void> getStars(CommentStars commentStars);
+
+    /**
+     * 党建收藏或取消收藏
+     * @param commentStars
+     * @return
+     */
+    public R<Void> getPartyNewsStars(CommentStars commentStars);
 }

+ 141 - 0
ruoyi-modules/ruoyi-wuye/src/main/java/org/dromara/service/impl/CommentStarsServiceImpl.java

@@ -4,21 +4,32 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import lombok.RequiredArgsConstructor;
+import org.apache.commons.lang3.ObjectUtils;
+import org.dromara.common.core.domain.R;
+import org.dromara.common.core.utils.DateUtils;
 import org.dromara.common.core.utils.MapstructUtils;
 import org.dromara.common.core.utils.StringUtils;
 import org.dromara.common.mybatis.core.page.PageQuery;
 import org.dromara.common.mybatis.core.page.TableDataInfo;
+import org.dromara.common.redis.utils.RedisUtils;
 import org.dromara.domain.communityNews.CommentStars;
+import org.dromara.domain.communityNews.CommunityNews;
 import org.dromara.domain.communityNews.bo.CommentStarsBo;
+import org.dromara.domain.communityNews.vo.CommentInteractionVo;
 import org.dromara.domain.communityNews.vo.CommentStarsVo;
 import org.dromara.mapper.CommentStarsMapper;
+import org.dromara.mapper.CommunityNewsMapper;
 import org.dromara.service.ICommentStarsService;
+import org.dromara.service.ICommunityNewsService;
 import org.springframework.stereotype.Service;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 
+import static org.dromara.common.core.constant.Constants.*;
+
 /**
  * 社区资讯收藏Service业务层处理
  *
@@ -30,6 +41,8 @@ import java.util.Map;
 public class CommentStarsServiceImpl implements ICommentStarsService {
 
     private final CommentStarsMapper baseMapper;
+    private final CommunityNewsMapper communityNewsMapper;
+    private final ICommunityNewsService communityNewsService;
 
     /**
      * 查询社区资讯收藏
@@ -132,4 +145,132 @@ public class CommentStarsServiceImpl implements ICommentStarsService {
         }
         return baseMapper.deleteByIds(ids) > 0;
     }
+    /**
+     * 社区资讯收藏或取消收藏
+     */
+    @Override
+    public R<Void> getStars(CommentStars commentStars) {
+        //当有人收藏了,需要记录到redis作为未读信息,需要统计未读的总数,需要在文章列表也给对应是否有未读标识
+        Long userId = commentStars.getUserId();
+        //收藏或者取消收藏判断用户是否收藏过,收藏过就取消收藏
+        Long targetId = commentStars.getTargetId();
+        String starsCommunityKey = STARS_COMMUNITY + targetId;
+        //该资讯收藏数量的key
+        String starsUserCountKey = STARS_USER_COUNT + targetId;
+        List<Long> userIdList = RedisUtils.getCacheList(starsCommunityKey);
+        Integer starsCount = 0;
+        CommentInteractionVo commentInteractionVo = new CommentInteractionVo();
+        commentInteractionVo.setType(ONE);
+        if (userIdList != null && userIdList.size() > 0) {
+            //判断该用户是否收藏过
+            if (userIdList.contains(userId)) {
+                commentInteractionVo.setType(TWO);
+                //说明收藏过则-----取消收藏
+                userIdList.remove(userId);
+                if (userIdList.isEmpty()) {
+                    RedisUtils.deleteObject(starsCommunityKey);
+                } else {
+                    RedisUtils.deleteObject(starsCommunityKey);
+                    RedisUtils.setCacheList(starsCommunityKey, userIdList);
+                }
+                //收藏数量-1
+                starsCount = RedisUtils.getCacheObject(starsUserCountKey);
+                RedisUtils.setCacheObject(starsUserCountKey, Math.max(starsCount - 1,0));
+                commentStars.setDelFlag("Y");
+                //更新数据库
+                baseMapper.updateCommentStarsByUserIdAndCommunityId(commentStars);
+            } else {
+                //则-------收藏
+                userIdList.add(userId);
+                RedisUtils.setCacheList(starsCommunityKey, userIdList);
+                //收藏数量+1
+                starsCount = RedisUtils.getCacheObject(starsUserCountKey);
+                RedisUtils.setCacheObject(starsUserCountKey, starsCount + 1);
+                //收藏表新增数据
+                baseMapper.insert(commentStars);
+            }
+        } else {
+            //----------第一次有人收藏
+            userIdList = new ArrayList<>();
+            userIdList.add(userId);
+            RedisUtils.setCacheList(starsCommunityKey, userIdList);
+            starsCount = 1;
+            //收藏数量+1
+            RedisUtils.setCacheObject(starsUserCountKey, starsCount);
+            //收藏表新增数据
+            baseMapper.insert(commentStars);
+        }
+        //更新资讯表中数量
+        communityNewsMapper.updateCommunityNewsStars(targetId, starsCount.toString());
+
+
+        //------设置未读收藏,在redis中新增一条未读的互动--------
+        commentInteractionVo.setTargetType(THR);
+        //根据targetId获取该资讯的内容
+        CommunityNews communityNews = communityNewsMapper.selectById(targetId);
+        commentInteractionVo.setTargetUserId(communityNews.getUserId());
+        commentInteractionVo.setTargetTitle(commentStars.getTargetTitle());
+        commentInteractionVo.setUserId(userId);
+        commentInteractionVo.setAvatar(commentStars.getAvatar());
+        commentInteractionVo.setNickName(commentStars.getNickName());
+        commentInteractionVo.setCreateTime(DateUtils.getNowDate());
+        commentInteractionVo.setTargetId(commentStars.getTargetId());
+        communityNewsService.setCommentInteraction(commentInteractionVo);
+        //----------------------设置未读互动完成--------
+        return R.ok();
+    }
+    /**
+     * 党建收藏或取消收藏
+     *
+     * @param commentStars
+     * @return
+     */
+    @Override
+    public R<Void> getPartyNewsStars(CommentStars commentStars) {
+        Long userId = commentStars.getUserId();
+        //收藏或者取消收藏判断用户是否收藏过,收藏过就取消收藏
+        Long targetId = commentStars.getTargetId();
+        String partyKey = STARS_PARTY_NEWS + userId;
+        List<Long> partyIdList = RedisUtils.getCacheList(partyKey);
+        int starsCount = 0;
+        Object starsPartyNewsCount = RedisUtils.getCacheObject(STARS_PARTY_NEWS_COUNT + targetId);
+        if (partyIdList != null && partyIdList.size() > 0) {
+            if (partyIdList.contains(targetId)) {
+                //说明该党建资讯该用户收藏过     取消收藏
+                partyIdList.remove(targetId);
+                if (partyIdList.size() == 0) {
+                    RedisUtils.deleteObject(partyKey);
+                } else {
+                    RedisUtils.deleteObject(partyKey);
+                    RedisUtils.setCacheList(partyKey, partyIdList);
+                }
+                baseMapper.updateCommentStarsByUserIdAndCommunityId(commentStars);
+                if (ObjectUtils.isNotEmpty(starsPartyNewsCount)) {
+                    RedisUtils.setCacheObject(STARS_PARTY_NEWS_COUNT + targetId, Math.max(Integer.parseInt(starsPartyNewsCount.toString()) - 1,0));
+                }
+            } else {
+                //则收藏
+                partyIdList.add(targetId);
+                RedisUtils.setCacheList(partyKey, partyIdList);
+                //收藏表新增数据
+                baseMapper.insert(commentStars);
+                //收藏数量+1
+                if (ObjectUtils.isNotEmpty(starsPartyNewsCount)) {
+                    starsCount = Integer.parseInt(starsPartyNewsCount.toString()) + 1;
+                    RedisUtils.setCacheObject(STARS_PARTY_NEWS_COUNT + targetId, starsCount);
+                }
+            }
+        } else {
+            partyIdList = new ArrayList<>();
+            //则第一个收藏
+            partyIdList.add(targetId);
+            RedisUtils.setCacheList(partyKey, partyIdList);
+            //收藏表新增数据
+            baseMapper.insert(commentStars);
+            //收藏数量+1
+            RedisUtils.setCacheObject(STARS_PARTY_NEWS_COUNT + targetId, 1);
+
+        }
+        return R.ok();
+    }
 }

+ 0 - 1
ruoyi-modules/ruoyi-wuye/src/main/java/org/dromara/service/impl/ResidentInfoServiceImpl.java

@@ -115,7 +115,6 @@ public class ResidentInfoServiceImpl implements IResidentInfoService {
      * @return 是否新增成功
      */
     @Override
-
     public Boolean insertByBo(ResidentInfoBo bo) {
         ResidentInfo add = MapstructUtils.convert(bo, ResidentInfo.class);
         validEntityBeforeSave(add);

+ 4 - 0
ruoyi-modules/ruoyi-wuye/src/main/resources/mapper/wuYe/CommentStarsMapper.xml

@@ -4,4 +4,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="org.dromara.mapper.CommentStarsMapper">
 
+    <update id="updateCommentStarsByUserIdAndCommunityId" parameterType="org.dromara.domain.communityNews.CommentStars">
+        update comment_stars set del_flag = 'Y'
+        where user_id = #{userId} and target_id = #{targetId} and target_type = #{targetType}
+    </update>
 </mapper>

+ 3 - 0
ruoyi-modules/ruoyi-wuye/src/main/resources/mapper/wuYe/CommunityNewsMapper.xml

@@ -4,4 +4,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="org.dromara.mapper.CommunityNewsMapper">
 
+    <update id="updateCommunityNewsStars" parameterType="org.dromara.domain.communityNews.CommunityNews">
+        update community_news set user_stars = #{userStars} where community_id = #{communityId}
+    </update>
 </mapper>