Browse Source

支持点赞员工,优化点赞接口及服务

zhangchen 1 ngày trước cách đây
mục cha
commit
f519596c5c

+ 1 - 1
alien-entity/src/main/java/shop/alien/entity/store/LifeLikeRecord.java

@@ -23,7 +23,7 @@ public class LifeLikeRecord {
 
     private String huifuId;
 
-    @ApiModelProperty(value = "1-评论 2-社区动态 3-活动 4-推荐菜 5-店铺打卡 6-二手商品")
+    @ApiModelProperty(value = "1-评论 2-社区动态 3-活动 4-推荐菜 5-店铺打卡 6-二手商品 7-律师评分 8-点赞员工")
     private String type;
 
     private Integer dianzanCount;

+ 4 - 0
alien-entity/src/main/java/shop/alien/entity/store/StoreStaffConfig.java

@@ -148,4 +148,8 @@ public class StoreStaffConfig extends Model<StoreStaffConfig> {
     @ApiModelProperty(value = "运动健身-基本信息")
     @TableField(exist = false)
     private StoreStaffFitnessBase fitnessBase;
+
+    @ApiModelProperty(value = "点赞数量")
+    @TableField("like_count")
+    private Integer likeCount;
 }

+ 44 - 10
alien-store/src/main/java/shop/alien/store/controller/LifeCommentController.java

@@ -34,19 +34,54 @@ public class LifeCommentController {
 
     private final FileUpload fileUpload;
 
+    /**
+     * 点赞接口
+     *
+     * @param userId  用户ID
+     * @param huifuId 被点赞对象ID(评论ID/动态ID/活动ID等)
+     * @param type    点赞类型(1-评论 2-社区动态 3-活动 4-推荐菜 5-店铺打卡 6-二手商品 7-律师评分 8-点赞员工)
+     * @return 点赞结果
+     */
     @ApiOperation("点赞")
     @ApiOperationSupport(order = 1)
-    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户id", dataType = "String", paramType = "query"),
-            @ApiImplicitParam(name = "huifuId", value = "huifuId", dataType = "String", paramType = "query"),
-            @ApiImplicitParam(name = "type", value = "type", dataType = "String", paramType = "query")})
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "String", paramType = "query", required = true),
+            @ApiImplicitParam(name = "huifuId", value = "被点赞对象ID(评论ID/动态ID/活动ID等)", dataType = "String", paramType = "query", required = true),
+            @ApiImplicitParam(name = "type", value = "点赞类型(1-评论 2-社区动态 3-活动 4-推荐菜 5-店铺打卡 6-二手商品 7-律师评分 8-点赞员工)", dataType = "String", paramType = "query", required = true)
+    })
     @PostMapping("/like")
-    public R<Boolean> dianzan(String userId, String huifuId, String type) {
-        log.info("LifeCommentController.like?userId={}, huifuId={}, type={}", userId, huifuId, type);
-        int num = lifeCommentService.like(userId, huifuId, type);
-        if (num == 0) {
-            return R.fail("点赞失败");
+    public R<Boolean> like(@RequestParam("userId") String userId,
+                           @RequestParam("huifuId") String huifuId,
+                           @RequestParam("type") String type) {
+        log.info("点赞请求,userId={},huifuId={},type={}", userId, huifuId, type);
+        
+        try {
+            // 参数校验
+            if (!StringUtils.hasText(userId)) {
+                log.warn("点赞失败,用户ID为空");
+                return R.fail("用户ID不能为空");
+            }
+            if (!StringUtils.hasText(huifuId)) {
+                log.warn("点赞失败,被点赞对象ID为空");
+                return R.fail("被点赞对象ID不能为空");
+            }
+            if (!StringUtils.hasText(type)) {
+                log.warn("点赞失败,点赞类型为空");
+                return R.fail("点赞类型不能为空");
+            }
+            
+            int result = lifeCommentService.like(userId, huifuId, type);
+            if (result > 0) {
+                log.info("点赞成功,userId={},huifuId={},type={}", userId, huifuId, type);
+                return R.success("点赞成功");
+            } else {
+                log.warn("点赞失败,可能已点赞或操作失败,userId={},huifuId={},type={}", userId, huifuId, type);
+                return R.fail("点赞失败,可能已点赞或操作失败");
+            }
+        } catch (Exception e) {
+            log.error("点赞异常,userId={},huifuId={},type={},异常信息:{}", userId, huifuId, type, e.getMessage(), e);
+            return R.fail("点赞失败:" + e.getMessage());
         }
-        return R.success("点赞成功");
     }
 
     @ApiOperation("取消点赞")
@@ -73,7 +108,6 @@ public class LifeCommentController {
         log.info("LifeCommentController.addOrUpdateComment?store={}&image1={}&image2={}", pingun.toString(),
                 image1 == null ? null : image1.getOriginalFilename(),
                 image2 == null ? null : image2.getOriginalFilename());
-        Map<String, Object> response = new HashMap<>();
         try {
             int num = lifeCommentService.addOrUpdateStore(pingun);
             String path = "";

+ 138 - 44
alien-store/src/main/java/shop/alien/store/service/LifeCommentService.java

@@ -5,11 +5,13 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 import org.springframework.util.ObjectUtils;
 import org.springframework.util.StringUtils;
+import shop.alien.store.util.CommonConstant;
 import shop.alien.entity.second.SecondGoods;
 import shop.alien.entity.store.*;
 import shop.alien.entity.store.vo.LifePinglunVo;
@@ -23,8 +25,12 @@ import java.util.*;
 import java.util.stream.Collectors;
 
 /**
- * 评论
+ * 评论服务类
+ *
+ * @author system
+ * @since 2025-01-XX
  */
+@Slf4j
 @Service
 @RequiredArgsConstructor
 public class LifeCommentService {
@@ -49,56 +55,139 @@ public class LifeCommentService {
 
     private final SecondGoodsMapper secondGoodsMapper;
 
+    private final StoreStaffConfigMapper storeStaffConfigMapper;
+
+    /**
+     * 点赞操作
+     * <p>
+     * 检查是否已点赞,如果未点赞则插入点赞记录并更新对应表的点赞数
+     * </p>
+     *
+     * @param userId  用户ID
+     * @param huifuId 被点赞对象ID(评论ID/动态ID/活动ID等)
+     * @param type    点赞类型(1-评论 2-社区动态 3-活动 4-推荐菜 5-店铺打卡 6-二手商品 7-律师评分 8-点赞员工)
+     * @return 更新影响的行数,0表示已点赞或操作失败
+     */
     public int like(String userId, String huifuId, String type) {
-        LambdaUpdateWrapper<LifeLikeRecord> updateWrapper = new LambdaUpdateWrapper<>();
-        updateWrapper.eq(LifeLikeRecord::getType, type);
-        updateWrapper.eq(LifeLikeRecord::getDianzanId, userId);
-        updateWrapper.eq(LifeLikeRecord::getHuifuId, huifuId);
-        updateWrapper.eq(LifeLikeRecord::getDeleteFlag, 0);
-        List<LifeLikeRecord> record = lifeLikeRecordMapper.selectList(updateWrapper);
-        if (CollectionUtils.isEmpty(record)) {
+        log.info("执行点赞操作,userId={},huifuId={},type={}", userId, huifuId, type);
+        
+        // 参数校验
+        if (!StringUtils.hasText(userId)) {
+            log.warn("点赞失败,用户ID为空");
+            return 0;
+        }
+        if (!StringUtils.hasText(huifuId)) {
+            log.warn("点赞失败,被点赞对象ID为空");
+            return 0;
+        }
+        if (!StringUtils.hasText(type)) {
+            log.warn("点赞失败,点赞类型为空");
+            return 0;
+        }
+        
+        try {
+            // 查询是否已有点赞记录(使用 LambdaQueryWrapper 而不是 LambdaUpdateWrapper)
+            LambdaQueryWrapper<LifeLikeRecord> queryWrapper = new LambdaQueryWrapper<>();
+            queryWrapper.eq(LifeLikeRecord::getType, type)
+                    .eq(LifeLikeRecord::getDianzanId, userId)
+                    .eq(LifeLikeRecord::getHuifuId, huifuId)
+                    .eq(LifeLikeRecord::getDeleteFlag, CommonConstant.DELETE_FLAG_UNDELETE);
+            List<LifeLikeRecord> recordList = lifeLikeRecordMapper.selectList(queryWrapper);
+            
+            // 如果已有点赞记录,直接返回0(表示已点赞)
+            if (!CollectionUtils.isEmpty(recordList)) {
+                log.info("用户已点赞,userId={},huifuId={},type={}", userId, huifuId, type);
+                return 0;
+            }
+            
+            // 插入点赞记录
             LifeLikeRecord lifeLikeRecord = new LifeLikeRecord();
             lifeLikeRecord.setCreatedTime(new Date());
             lifeLikeRecord.setHuifuId(huifuId);
             lifeLikeRecord.setDianzanId(userId);
             lifeLikeRecord.setType(type);
-            lifeLikeRecordMapper.insert(lifeLikeRecord);
-
-            if ("1".equals(type)) {
-                LambdaUpdateWrapper<StoreComment> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
-                lambdaUpdateWrapper.eq(StoreComment::getId, huifuId);
-                lambdaUpdateWrapper.setSql("like_count = like_count + 1");
-                return storeCommentMapper.update(null, lambdaUpdateWrapper);
-            } else if ("2".equals(type)) {
-                LambdaUpdateWrapper<LifeUserDynamics> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
-                lambdaUpdateWrapper.eq(LifeUserDynamics::getId, huifuId);
-                lambdaUpdateWrapper.setSql("dianzan_count = dianzan_count + 1");
-                int num = lifeUserDynamicsMapper.update(null, lambdaUpdateWrapper);
-                if (num > 0) insertNotice(userId, huifuId, type);
-                return num;
-            } else if ("3".equals(type)) {
-                LambdaUpdateWrapper<LifeActivity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
-                lambdaUpdateWrapper.eq(LifeActivity::getId, huifuId);
-                lambdaUpdateWrapper.setSql("dianzan_count = dianzan_count + 1");
-                return lifeActivityMapper.update(null, lambdaUpdateWrapper);
-            } else if ("4".equals(type)) {
-                LambdaUpdateWrapper<StoreMenu> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
-                lambdaUpdateWrapper.eq(StoreMenu::getId, huifuId);
-                lambdaUpdateWrapper.setSql("like_count = like_count + 1");
-                return storeRecommendMapper.update(null, lambdaUpdateWrapper);
-            } else if ("5".equals(type)) {
-                LambdaUpdateWrapper<StoreClockIn> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
-                lambdaUpdateWrapper.eq(StoreClockIn::getId, huifuId);
-                lambdaUpdateWrapper.setSql("like_count = like_count + 1");
-                return storeClockInMapper.update(null, lambdaUpdateWrapper);
-            } else if ("6".equals(type)) {
-                LambdaUpdateWrapper<SecondGoods> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
-                lambdaUpdateWrapper.eq(SecondGoods::getId, huifuId);
-                lambdaUpdateWrapper.setSql("like_count = like_count + 1");
-                return secondGoodsMapper.update(null, lambdaUpdateWrapper);
+            int insertResult = lifeLikeRecordMapper.insert(lifeLikeRecord);
+            
+            if (insertResult <= 0) {
+                log.warn("插入点赞记录失败,userId={},huifuId={},type={}", userId, huifuId, type);
+                return 0;
             }
+            
+            // 根据类型更新对应表的点赞数
+            int updateResult = updateLikeCountByType(huifuId, type);
+            
+            // 如果是动态类型,发送通知
+            if (updateResult > 0 && CommonConstant.LIKE_TYPE_DYNAMICS.equals(type)) {
+                insertNotice(userId, huifuId, type);
+            }
+            
+            log.info("点赞操作完成,userId={},huifuId={},type={},更新结果={}", userId, huifuId, type, updateResult);
+            return updateResult;
+        } catch (Exception e) {
+            log.error("点赞操作异常,userId={},huifuId={},type={},异常信息:{}", userId, huifuId, type, e.getMessage(), e);
+            return 0;
+        }
+    }
+    
+    /**
+     * 根据类型更新对应表的点赞数
+     *
+     * @param huifuId 被点赞对象ID
+     * @param type    点赞类型
+     * @return 更新影响的行数
+     */
+    private int updateLikeCountByType(String huifuId, String type) {
+        try {
+            if (CommonConstant.LIKE_TYPE_COMMENT.equals(type)) {
+                // 类型1:评论
+                LambdaUpdateWrapper<StoreComment> updateWrapper = new LambdaUpdateWrapper<>();
+                updateWrapper.eq(StoreComment::getId, huifuId)
+                        .setSql("like_count = like_count + 1");
+                return storeCommentMapper.update(null, updateWrapper);
+            } else if (CommonConstant.LIKE_TYPE_DYNAMICS.equals(type)) {
+                // 类型2:社区动态
+                LambdaUpdateWrapper<LifeUserDynamics> updateWrapper = new LambdaUpdateWrapper<>();
+                updateWrapper.eq(LifeUserDynamics::getId, huifuId)
+                        .setSql("dianzan_count = dianzan_count + 1");
+                return lifeUserDynamicsMapper.update(null, updateWrapper);
+            } else if (CommonConstant.LIKE_TYPE_ACTIVITY.equals(type)) {
+                // 类型3:活动
+                LambdaUpdateWrapper<LifeActivity> updateWrapper = new LambdaUpdateWrapper<>();
+                updateWrapper.eq(LifeActivity::getId, huifuId)
+                        .setSql("dianzan_count = dianzan_count + 1");
+                return lifeActivityMapper.update(null, updateWrapper);
+            } else if (CommonConstant.LIKE_TYPE_MENU.equals(type)) {
+                // 类型4:推荐菜
+                LambdaUpdateWrapper<StoreMenu> updateWrapper = new LambdaUpdateWrapper<>();
+                updateWrapper.eq(StoreMenu::getId, huifuId)
+                        .setSql("like_count = like_count + 1");
+                return storeRecommendMapper.update(null, updateWrapper);
+            } else if (CommonConstant.LIKE_TYPE_CLOCK_IN.equals(type)) {
+                // 类型5:店铺打卡
+                LambdaUpdateWrapper<StoreClockIn> updateWrapper = new LambdaUpdateWrapper<>();
+                updateWrapper.eq(StoreClockIn::getId, huifuId)
+                        .setSql("like_count = like_count + 1");
+                return storeClockInMapper.update(null, updateWrapper);
+            } else if (CommonConstant.LIKE_TYPE_SECOND_GOODS.equals(type)) {
+                // 类型6:二手商品
+                LambdaUpdateWrapper<SecondGoods> updateWrapper = new LambdaUpdateWrapper<>();
+                updateWrapper.eq(SecondGoods::getId, huifuId)
+                        .setSql("like_count = like_count + 1");
+                return secondGoodsMapper.update(null, updateWrapper);
+            } else if (CommonConstant.LIKE_TYPE_STAFF.equals(type)) {
+                // 类型8:点赞员工
+                LambdaUpdateWrapper<StoreStaffConfig> updateWrapper = new LambdaUpdateWrapper<>();
+                updateWrapper.eq(StoreStaffConfig::getId, huifuId)
+                        .setSql("like_count = like_count + 1");
+                return storeStaffConfigMapper.update(null, updateWrapper);
+            } else {
+                log.warn("未知的点赞类型,type={},huifuId={}", type, huifuId);
+                return 0;
+            }
+        } catch (Exception e) {
+            log.error("更新点赞数异常,type={},huifuId={},异常信息:{}", type, huifuId, e.getMessage(), e);
+            return 0;
         }
-        return 0;
     }
 
     /**
@@ -157,11 +246,16 @@ public class LifeCommentService {
             lambdaUpdateWrapper.eq(StoreClockIn::getId, huifuId).gt(StoreClockIn::getLikeCount, 0);
             lambdaUpdateWrapper.setSql("like_count = like_count - 1");
             return storeClockInMapper.update(null, lambdaUpdateWrapper);
-        }else if ("6".equals(type)) {
+        } else if ("6".equals(type)) {
             LambdaUpdateWrapper<SecondGoods> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
             lambdaUpdateWrapper.eq(SecondGoods::getId, huifuId).gt(SecondGoods::getLikeCount, 0);
             lambdaUpdateWrapper.setSql("like_count = like_count - 1");
             return secondGoodsMapper.update(null, lambdaUpdateWrapper);
+        } else if ("8".equals(type)) {
+            LambdaUpdateWrapper<StoreStaffConfig> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
+            lambdaUpdateWrapper.eq(StoreStaffConfig::getId, Integer.parseInt(huifuId))
+                    .setSql("like_count = IF(like_count > 0, like_count - 1, 0)");
+            return storeStaffConfigMapper.update(null, lambdaUpdateWrapper);
         }
     }
         return 0;

+ 12 - 0
alien-store/src/main/java/shop/alien/store/util/CommonConstant.java

@@ -74,4 +74,16 @@ public class CommonConstant {
      */
     public static final Integer DEFAULT_PAGE_SIZE = 10;
     public static final Integer DEFAULT_PAGE_NUM = 1;
+
+    /**
+     * 点赞类型:1-评论 2-社区动态 3-活动 4-推荐菜 5-店铺打卡 6-二手商品 7-律师评分 8-点赞员工
+     */
+    public static final String LIKE_TYPE_COMMENT = "1";
+    public static final String LIKE_TYPE_DYNAMICS = "2";
+    public static final String LIKE_TYPE_ACTIVITY = "3";
+    public static final String LIKE_TYPE_MENU = "4";
+    public static final String LIKE_TYPE_CLOCK_IN = "5";
+    public static final String LIKE_TYPE_SECOND_GOODS = "6";
+    public static final String LIKE_TYPE_LAWYER_SCORE = "7";
+    public static final String LIKE_TYPE_STAFF = "8";
 }