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.
171 lines
7.0 KiB
171 lines
7.0 KiB
package com.community.pocket.repository.android;
|
|
|
|
import com.community.pocket.entity.po.Forum;
|
|
import com.community.pocket.entity.po.android.Complain;
|
|
import com.community.pocket.entity.po.android.MyInfo;
|
|
import com.community.pocket.entity.vo.ForumQuery;
|
|
import com.community.pocket.entity.vo.Page;
|
|
import com.community.pocket.entity.vo.android.ForumDto;
|
|
import com.community.pocket.entity.vo.android.ForumHot;
|
|
import com.community.pocket.entity.vo.android.ForumReplyForm;
|
|
import com.community.pocket.entity.vo.android.ForumVo;
|
|
import com.community.pocket.entity.vo.web.ForumCheck;
|
|
import com.community.pocket.repository.BaseDao;
|
|
import com.community.pocket.util.LookupOperationUtil;
|
|
import com.community.pocket.util.TableName;
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
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.query.Criteria;
|
|
import org.springframework.data.mongodb.core.query.Query;
|
|
import org.springframework.data.mongodb.core.query.Update;
|
|
import org.springframework.stereotype.Repository;
|
|
import org.springframework.util.StringUtils;
|
|
|
|
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();
|
|
}
|
|
|
|
|
|
//查找热门贴
|
|
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());
|
|
forumHot.setReply(forum.getReply());
|
|
forumHot.setTitle(forum.getTitle());
|
|
forumHots.add(forumHot);
|
|
}
|
|
return forumHots;
|
|
}
|
|
|
|
//查找帖子
|
|
private List<ForumDto> loadForum(AggregationOperation... operation) {
|
|
try {
|
|
Aggregation aggregation = Aggregation.newAggregation(operation);
|
|
LOG.info(aggregation.toString());
|
|
return mongoTemplate.aggregate(aggregation, TableName.forum, ForumDto.class).getMappedResults();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
LOG.error(e.toString());
|
|
}
|
|
return null;
|
|
}
|
|
|
|
//设置投诉贴投诉人详细信息
|
|
private void loadComplain(ForumDto forumDto) {
|
|
Complain complain = forumDto.getComplainDto();
|
|
complain.setInfo(mongoTemplate.findOne(new Query(Criteria.where(LookupOperationUtil.idKey).is(complain.getComplain())), MyInfo.class));
|
|
}
|
|
|
|
//查询帖子
|
|
public ForumDto loadForumWithId(ForumQuery queryForum) {
|
|
Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(Criteria.where(LookupOperationUtil.idKey).is(queryForum.getForumId())), LookupOperationUtil.complainforumLookup, LookupOperationUtil.activeforumLookup, LookupOperationUtil.scoreforumLookup, LookupOperationUtil.infoLookup);
|
|
return mongoTemplate.aggregate(aggregation, TableName.forum, ForumDto.class).getUniqueMappedResult();
|
|
}
|
|
|
|
//查询最新帖子
|
|
|
|
public Page<ForumDto> loadForum(ForumQuery forumQuery) {
|
|
// 根据审核状态按时间从大到小排序的帖子
|
|
try {
|
|
List<Criteria> criteriaList = new ArrayList<>();
|
|
if (!StringUtils.isEmpty(forumQuery.getStatus())) {
|
|
criteriaList.add(Criteria.where("status").is(forumQuery.getStatus()));
|
|
}
|
|
if (!StringUtils.isEmpty(forumQuery.getUsername())) {
|
|
criteriaList.add(Criteria.where("username").is(forumQuery.getUsername()));
|
|
}
|
|
if (ObjectUtils.isNotEmpty(forumQuery.getForumType())) {
|
|
criteriaList.add(Criteria.where("forumType").in(forumQuery.getForumType()));
|
|
}
|
|
|
|
Criteria criteria = new Criteria().andOperator(criteriaList.toArray(new Criteria[]{}));
|
|
long count = mongoTemplate.count(new Query(criteria), entityClass());
|
|
Page<ForumDto> page = new Page<>(count, forumQuery.getCurrentPage(), pageSize);
|
|
List<ForumDto> forumDtos = loadForum(LookupOperationUtil.complainforumLookup, LookupOperationUtil.activeforumLookup, LookupOperationUtil.scoreforumLookup, LookupOperationUtil.infoLookup, Aggregation.match(criteria), Aggregation.skip((forumQuery.getCurrentPage() - 1) * pageSize), Aggregation.limit(pageSize), Aggregation.sort(Sort.by("time").descending()));
|
|
if (forumDtos != null) {
|
|
for (ForumDto forumDto : forumDtos) {
|
|
if (forumDto.getComplainDto() != null) {
|
|
loadComplain(forumDto);
|
|
}
|
|
}
|
|
}
|
|
page.setList(forumDtos);
|
|
return page;
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
LOG.error(e.toString());
|
|
}
|
|
return null;
|
|
}
|
|
|
|
//保存动态
|
|
public Forum save(ForumVo forumVo) {
|
|
try {
|
|
Forum forum = new Forum();
|
|
if (forumVo.getContent().length() > contentLength) {
|
|
forum.setContent(forumVo.getContent().substring(0, contentLength));
|
|
} else {
|
|
forum.setContent(forumVo.getContent());
|
|
}
|
|
forum.setTitle(forumVo.getTitle());
|
|
forum.setForumType(forumVo.getForumType());
|
|
forum.setTime(System.currentTimeMillis());
|
|
forum.setReply(0);
|
|
forum.setUsername(forumVo.getUsername());
|
|
forum.setStatus(Forum.ForumStatus.uncheck);
|
|
return save(forum);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
LOG.error(e.toString());
|
|
}
|
|
return null;
|
|
}
|
|
|
|
//审核帖子
|
|
public boolean check(ForumCheck forumCheck) {
|
|
return mongoTemplate.updateFirst(new Query(Criteria.where(LookupOperationUtil.idKey).is(forumCheck.getId())), Update.update("status", forumCheck.getStatus()), entityClass()).wasAcknowledged();
|
|
}
|
|
|
|
//保存
|
|
@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;
|
|
}
|
|
}
|
|
|