mysql表设计:
java代码
接口
/**
* 更新帖子点赞数 收藏数 同时更新中间表
* @param topic
* @param userId
* @param type
*/
void updateByTopic(Topic topic, Long userId, String type);
实现类:
@Override
public void updateByTopic(Topic topic, Long userId, String type) {
TopicMiddleModel model = new TopicMiddleModel();
model.setTopicId(topic.getId());
model.setUserId(userId);
List<TopicMiddle> topicMiddles = topicMiddleSV.selectByModel(model);
TopicMiddle topicMiddle = new TopicMiddle();
if(topicMiddles != null && !topicMiddles.isEmpty()){
topicMiddle = topicMiddles.get(0);
}
if("01".equals(type)){
if(FlagEnum.IS.stringValue().equals(topicMiddle.getGoodFlag())){
throw new BusinessException("该文章已经点过赞了");
}
topic.setGoodNum(topic.getGoodNum()+1);
topicMiddle.setGoodFlag(FlagEnum.IS.stringValue());
}else if("02".equals(type)){
topic.setForwardNum(topic.getForwardNum()+1);
}else if("03".equals(type)){
if(FlagEnum.IS.stringValue().equals(topicMiddle.getCollect())){
throw new BusinessException("该文章已经被收藏过了");
}
topic.setColNum(topic.getColNum()+1);
topicMiddle.setCollect(FlagEnum.IS.stringValue());
}else if ("04".equals(type)){
if(FlagEnum.NO.stringValue().equals(topicMiddle.getGoodFlag())){
throw new BusinessException("该文章还没有点过赞");
}
topic.setGoodNum(topic.getGoodNum()-1);
topicMiddle.setGoodFlag(FlagEnum.NO.stringValue());
}else if ("05".equals(type)){
if((FlagEnum.NO.stringValue().equals(topicMiddle.getCollect()))){
throw new BusinessException("该文章还没有被收藏过");
}
topic.setColNum(topic.getColNum()-1);
topicMiddle.setCollect(FlagEnum.NO.stringValue());
}else {
throw new BusinessException("请传入01-06 有效值");
}
updateByIdSelective(topic);
if(!"02".equals(type)){
if(topicMiddles.isEmpty()){
topicMiddle.setUserId(userId);
topicMiddle.setTopicId(topic.getId());
topicMiddleSV.insertSelective(topicMiddle);
}else {
topicMiddleSV.updateByIdSelective(topicMiddle);
}
}
}