|
|
@@ -207,59 +207,139 @@ public class LifeCommentService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 取消点赞操作
|
|
|
+ * <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 cancelLike(String userId, String huifuId, String type) {
|
|
|
- LambdaQueryWrapper<LifeLikeRecord> lifeLikeRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
- lifeLikeRecordLambdaQueryWrapper.eq(LifeLikeRecord::getDianzanId, userId);
|
|
|
- lifeLikeRecordLambdaQueryWrapper.eq(LifeLikeRecord::getHuifuId, huifuId);
|
|
|
- lifeLikeRecordLambdaQueryWrapper.eq(LifeLikeRecord::getType, type);
|
|
|
- lifeLikeRecordLambdaQueryWrapper.eq(LifeLikeRecord::getDeleteFlag, 0);
|
|
|
- List<LifeLikeRecord> record = lifeLikeRecordMapper.selectList(lifeLikeRecordLambdaQueryWrapper);
|
|
|
- if (!CollectionUtils.isEmpty(record)) {
|
|
|
- LambdaUpdateWrapper<LifeLikeRecord> lifeLikeRecordLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
|
|
|
- lifeLikeRecordLambdaUpdateWrapper.eq(LifeLikeRecord::getHuifuId, huifuId);
|
|
|
- lifeLikeRecordLambdaUpdateWrapper.eq(LifeLikeRecord::getDianzanId, userId);
|
|
|
- lifeLikeRecordLambdaUpdateWrapper.eq(LifeLikeRecord::getType, type);
|
|
|
- lifeLikeRecordMapper.delete(lifeLikeRecordLambdaUpdateWrapper);
|
|
|
-
|
|
|
- if ("1".equals(type)) {
|
|
|
- LambdaUpdateWrapper<StoreComment> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
|
|
|
- lambdaUpdateWrapper.eq(StoreComment::getId, huifuId).gt(StoreComment::getLikeCount, 0);
|
|
|
- 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).gt(LifeUserDynamics::getDianzanCount, 0);
|
|
|
- lambdaUpdateWrapper.setSql("dianzan_count = dianzan_count - 1");
|
|
|
- return lifeUserDynamicsMapper.update(null, lambdaUpdateWrapper);
|
|
|
- } else if ("3".equals(type)) {
|
|
|
- LambdaUpdateWrapper<LifeActivity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
|
|
|
- lambdaUpdateWrapper.eq(LifeActivity::getId, huifuId).gt(LifeActivity::getDianzanCount, 0);
|
|
|
- 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).gt(StoreMenu::getLikeCount, 0);
|
|
|
- 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).gt(StoreClockIn::getLikeCount, 0);
|
|
|
- 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).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);
|
|
|
+ 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<LifeLikeRecord> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(LifeLikeRecord::getDianzanId, userId)
|
|
|
+ .eq(LifeLikeRecord::getHuifuId, huifuId)
|
|
|
+ .eq(LifeLikeRecord::getType, type)
|
|
|
+ .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;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除点赞记录(逻辑删除)
|
|
|
+ LambdaUpdateWrapper<LifeLikeRecord> updateWrapper = new LambdaUpdateWrapper<>();
|
|
|
+ updateWrapper.eq(LifeLikeRecord::getHuifuId, huifuId)
|
|
|
+ .eq(LifeLikeRecord::getDianzanId, userId)
|
|
|
+ .eq(LifeLikeRecord::getType, type)
|
|
|
+ .eq(LifeLikeRecord::getDeleteFlag, CommonConstant.DELETE_FLAG_UNDELETE);
|
|
|
+ int deleteResult = lifeLikeRecordMapper.delete(updateWrapper);
|
|
|
+
|
|
|
+ if (deleteResult <= 0) {
|
|
|
+ log.warn("删除点赞记录失败,userId={},huifuId={},type={}", userId, huifuId, type);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据类型更新对应表的点赞数
|
|
|
+ int updateResult = decreaseLikeCountByType(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 decreaseLikeCountByType(String huifuId, String type) {
|
|
|
+ try {
|
|
|
+ if (CommonConstant.LIKE_TYPE_COMMENT.equals(type)) {
|
|
|
+ // 类型1:评论
|
|
|
+ LambdaUpdateWrapper<StoreComment> updateWrapper = new LambdaUpdateWrapper<>();
|
|
|
+ updateWrapper.eq(StoreComment::getId, huifuId)
|
|
|
+ .gt(StoreComment::getLikeCount, 0)
|
|
|
+ .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)
|
|
|
+ .gt(LifeUserDynamics::getDianzanCount, 0)
|
|
|
+ .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)
|
|
|
+ .gt(LifeActivity::getDianzanCount, 0)
|
|
|
+ .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)
|
|
|
+ .gt(StoreMenu::getLikeCount, 0)
|
|
|
+ .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)
|
|
|
+ .gt(StoreClockIn::getLikeCount, 0)
|
|
|
+ .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)
|
|
|
+ .gt(SecondGoods::getLikeCount, 0)
|
|
|
+ .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, Integer.parseInt(huifuId))
|
|
|
+ .setSql("like_count = IF(like_count > 0, like_count - 1, 0)");
|
|
|
+ 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;
|
|
|
-}
|
|
|
|
|
|
public int addOrUpdateStore(LifeComment store) {
|
|
|
if (StringUtils.isEmpty(store.getId())) {
|