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.

88 lines
3.3 KiB

package com.community.pocket.repository.android;
import com.community.pocket.entity.po.Forum;
import com.community.pocket.entity.po.ForumContent;
import com.community.pocket.entity.vo.android.ForumContentVo;
import com.community.pocket.entity.vo.android.ForumForm;
import com.community.pocket.entity.vo.android.ForumReplyForm;
import com.community.pocket.entity.vo.android.QueryForum;
import com.community.pocket.repository.BaseDao;
import com.community.pocket.util.TableName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
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.stereotype.Repository;
import java.util.List;
/**
* 帖子正文详情
*/
@Repository
public class ForumContentDao extends BaseDao<ForumContent> {
private static final Logger LOG = LoggerFactory.getLogger(ForumDao.class);
@Autowired
private ForumDao forumDao;
//关联用户表查询
private static final LookupOperation lookupOperation = LookupOperation.newLookup().
from(TableName.info). //关联从表名
localField("username"). //主表关联字段
foreignField("_id").//从表关联的字段
as("myInfo"); //查询结果名
//查找帖子详情
public List<ForumContentVo> find(QueryForum queryForum) {
Aggregation aggregation = Aggregation.newAggregation(lookupOperation, Aggregation.unwind("myInfo"), Aggregation.match(Criteria.where("forumId").is(queryForum.getForumId())));
return mongoTemplate.aggregate(aggregation, TableName.forumContent, ForumContentVo.class).getMappedResults();
}
/**
* 保存回复
*/
public ForumContent save(ForumReplyForm forumReplyForm) {
ForumContent forumContent = new ForumContent();
forumContent.setContent(forumReplyForm.getContent());
forumContent.setForumId(forumReplyForm.getForumId());
forumContent.setTime(System.currentTimeMillis());
long tower = mongoTemplate.count(new Query(Criteria.where("forumId").is(forumContent.getForumId())), entityClass());
forumContent.setTower(tower + 1);
forumContent.setUsername(forumReplyForm.getUsername());
return save(forumContent);
}
/**
* 保存帖子详情
*/
public ForumContent save(ForumForm forumForm, Forum forum) {
ForumContent forumContent = new ForumContent();
forumContent.setContent(forumForm.getContent());
forumContent.setForumId(forum.getId().toString());
forumContent.setTime(forum.getTime());
forumContent.setTower(1L);
forumContent.setUsername(forum.getUsername());
return save(forumContent);
}
//保存
@Override
public ForumContent save(ForumContent forumContent) {
try {
return mongoTemplate.save(forumContent);
} catch (Exception e) {
e.printStackTrace();
LOG.error(e.toString());
}
return null;
}
@Override
public Class<ForumContent> entityClass() {
return ForumContent.class;
}
}