|
@@ -5,11 +5,13 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.ObjectUtils;
|
|
import org.springframework.util.ObjectUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
+import shop.alien.store.util.CommonConstant;
|
|
|
import shop.alien.entity.second.SecondGoods;
|
|
import shop.alien.entity.second.SecondGoods;
|
|
|
import shop.alien.entity.store.*;
|
|
import shop.alien.entity.store.*;
|
|
|
import shop.alien.entity.store.vo.LifePinglunVo;
|
|
import shop.alien.entity.store.vo.LifePinglunVo;
|
|
@@ -23,8 +25,12 @@ import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 评论
|
|
|
|
|
|
|
+ * 评论服务类
|
|
|
|
|
+ *
|
|
|
|
|
+ * @author system
|
|
|
|
|
+ * @since 2025-01-XX
|
|
|
*/
|
|
*/
|
|
|
|
|
+@Slf4j
|
|
|
@Service
|
|
@Service
|
|
|
@RequiredArgsConstructor
|
|
@RequiredArgsConstructor
|
|
|
public class LifeCommentService {
|
|
public class LifeCommentService {
|
|
@@ -49,56 +55,139 @@ public class LifeCommentService {
|
|
|
|
|
|
|
|
private final SecondGoodsMapper secondGoodsMapper;
|
|
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) {
|
|
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 lifeLikeRecord = new LifeLikeRecord();
|
|
|
lifeLikeRecord.setCreatedTime(new Date());
|
|
lifeLikeRecord.setCreatedTime(new Date());
|
|
|
lifeLikeRecord.setHuifuId(huifuId);
|
|
lifeLikeRecord.setHuifuId(huifuId);
|
|
|
lifeLikeRecord.setDianzanId(userId);
|
|
lifeLikeRecord.setDianzanId(userId);
|
|
|
lifeLikeRecord.setType(type);
|
|
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.eq(StoreClockIn::getId, huifuId).gt(StoreClockIn::getLikeCount, 0);
|
|
|
lambdaUpdateWrapper.setSql("like_count = like_count - 1");
|
|
lambdaUpdateWrapper.setSql("like_count = like_count - 1");
|
|
|
return storeClockInMapper.update(null, lambdaUpdateWrapper);
|
|
return storeClockInMapper.update(null, lambdaUpdateWrapper);
|
|
|
- }else if ("6".equals(type)) {
|
|
|
|
|
|
|
+ } else if ("6".equals(type)) {
|
|
|
LambdaUpdateWrapper<SecondGoods> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
|
|
LambdaUpdateWrapper<SecondGoods> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
|
|
|
lambdaUpdateWrapper.eq(SecondGoods::getId, huifuId).gt(SecondGoods::getLikeCount, 0);
|
|
lambdaUpdateWrapper.eq(SecondGoods::getId, huifuId).gt(SecondGoods::getLikeCount, 0);
|
|
|
lambdaUpdateWrapper.setSql("like_count = like_count - 1");
|
|
lambdaUpdateWrapper.setSql("like_count = like_count - 1");
|
|
|
return secondGoodsMapper.update(null, lambdaUpdateWrapper);
|
|
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;
|
|
return 0;
|