You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
5.5 KiB

package com.community.pocket.repository.android;
import com.community.pocket.entity.po.Forum;
import com.community.pocket.entity.vo.android.*;
import com.community.pocket.repository.BaseDao;
import com.community.pocket.util.TableName;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.LookupOperation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List;
/**
* 帖子数据接口
*/
@Repository
public class ForumDao extends BaseDao<Forum> {
private static final Logger LOG = LoggerFactory.getLogger(ForumDao.class);
@Value("${forum.content-length}")
private int contentLength;
//添加回复数
public boolean update(ForumReplyForm forumReplyForm) {
return mongoTemplate.updateFirst(new Query(Criteria.where("id").is(new ObjectId(forumReplyForm.getForumId()))),
new Update().inc("reply", 1), entityClass()).wasAcknowledged();
}
//关联用户表查询
private static final LookupOperation lookupOperation = LookupOperation.newLookup().
from(TableName.info). //关联从表名
localField("username"). //主表关联字段
foreignField("_id").//从表关联的字段
as("myInfo"); //查询结果名
//查找热门贴
public List<ForumHot> rankReply(Forum.ForumType type, int num) {
List<ForumHot> forumHots=new ArrayList<>();
List<Forum> forumList =mongoTemplate.find(new Query(Criteria.where("forumType").is(type)).with(Sort.by("reply").descending()).limit(num),entityClass());
for(Forum forum:forumList){
ForumHot forumHot=new ForumHot();
forumHot.setForumId(forum.getId().toString());
forumHot.setReply(forum.getReply());
forumHot.setTitle(forum.getTitle());
forumHots.add(forumHot);
}
return forumHots;
}
//查找帖子
private List<ForumVo> loadForum(AggregationOperation... operation) {
try {
Aggregation aggregation = Aggregation.newAggregation(operation);
return mongoTemplate.aggregate(aggregation, TableName.forum, ForumVo.class).getMappedResults();
} catch (Exception e) {
e.printStackTrace();
LOG.error(e.toString());
}
return null;
}
//查找所有帖子
public List<ForumVo> loadNewForum() {
return loadForum(lookupOperation, Aggregation.unwind("myInfo"), Aggregation.sort(Sort.by("time").descending()));
}
//查找最新帖子
public List<ForumVo> loadMyForum(String username) {
return loadForum(lookupOperation, Aggregation.unwind("myInfo"), Aggregation.match(Criteria.where("username").is(username)), Aggregation.sort(Sort.by("time").descending()));
}
//保存活动贴
public Forum save(ActiveForm activeForm){
try {
Forum forum=new Forum();
forum.setUsername(activeForm.getUsername());
forum.setForumType(Forum.ForumType.active);
forum.setReply(0);
forum.setTitle(activeForm.getTitle());
forum.setTime(System.currentTimeMillis());
if(activeForm.getContent().length()>contentLength){
forum.setContent(activeForm.getContent().substring(0,contentLength));
}else{
forum.setContent(activeForm.getContent());
}
return save(forum);
} catch (Exception e) {
e.printStackTrace();
LOG.error(e.toString());
}
return null;
}
//保存动态
public Forum save(ForumForm forumForm){
try {
Forum forum=new Forum();
forum.setContent(forumForm.getContent());
forum.setTitle(forumForm.getTitle());
forum.setForumType(Forum.ForumType.topic);
forum.setTime(System.currentTimeMillis());
forum.setReply(0);
forum.setUsername(forumForm.getUsername());
return save(forum);
} catch (Exception e) {
e.printStackTrace();
LOG.error(e.toString());
}
return null;
}
//保存投诉
public Forum save(ComplainForm complainForm){
try {
Forum forum=new Forum();
forum.setContent(complainForm.getContent());
forum.setTitle(complainForm.getTitle());
forum.setForumType(Forum.ForumType.complan);
forum.setTime(System.currentTimeMillis());
forum.setReply(0);
forum.setUsername(complainForm.getUsername());
return save(forum);
} catch (Exception e) {
e.printStackTrace();
LOG.error(e.toString());
}
return null;
}
//保存
@Override
public Forum save(Forum forum) {
try {
return mongoTemplate.save(forum);
} catch (Exception e) {
e.printStackTrace();
LOG.error(e.toString());
}
return null;
}
@Override
public Class<Forum> entityClass() {
return Forum.class;
}
}