|
|
|
@ -1,13 +1,19 @@ |
|
|
|
|
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.*; |
|
|
|
|
import com.community.pocket.entity.vo.web.ForumCheck; |
|
|
|
|
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.PageRequest; |
|
|
|
|
import org.springframework.data.domain.Sort; |
|
|
|
|
import org.springframework.data.mongodb.core.aggregation.Aggregation; |
|
|
|
|
import org.springframework.data.mongodb.core.aggregation.AggregationOperation; |
|
|
|
@ -16,6 +22,7 @@ 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; |
|
|
|
@ -31,18 +38,33 @@ public class ForumDao extends BaseDao<Forum> { |
|
|
|
|
@Value("${forum.content-length}") |
|
|
|
|
private int contentLength; |
|
|
|
|
|
|
|
|
|
//活动贴
|
|
|
|
|
//关联帖子外键属性
|
|
|
|
|
private static final String idKey = "_id"; |
|
|
|
|
//定义管道操作
|
|
|
|
|
private static final String activeKey = "activeDto"; |
|
|
|
|
//关联
|
|
|
|
|
private static final LookupOperation activeforumLookup = LookupOperation.newLookup().from(TableName.active).localField(idKey).foreignField(idKey).as(activeKey); |
|
|
|
|
|
|
|
|
|
//投诉贴
|
|
|
|
|
//关联属性
|
|
|
|
|
private static final String complainKey = "complainDto"; |
|
|
|
|
//定义管道操作
|
|
|
|
|
private static final LookupOperation complainforumLookup = LookupOperation.newLookup().from(TableName.complain).localField(idKey).foreignField(idKey).as(complainKey); |
|
|
|
|
//关联属性
|
|
|
|
|
private static final String infoKey = "info"; |
|
|
|
|
|
|
|
|
|
//关联用户外键属性
|
|
|
|
|
private static final String usernameKey = "username"; |
|
|
|
|
//定义管道操作
|
|
|
|
|
private static final LookupOperation infoLookup = LookupOperation.newLookup().from(TableName.info).localField(usernameKey).foreignField(idKey).as(infoKey); |
|
|
|
|
|
|
|
|
|
//添加回复数
|
|
|
|
|
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) { |
|
|
|
@ -50,7 +72,7 @@ public class ForumDao extends BaseDao<Forum> { |
|
|
|
|
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.setForumId(forum.getId()); |
|
|
|
|
forumHot.setReply(forum.getReply()); |
|
|
|
|
forumHot.setTitle(forum.getTitle()); |
|
|
|
|
forumHots.add(forumHot); |
|
|
|
@ -59,10 +81,11 @@ public class ForumDao extends BaseDao<Forum> { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//查找帖子
|
|
|
|
|
private List<ForumVo> loadForum(AggregationOperation... operation) { |
|
|
|
|
private List<ForumDto> loadForum(AggregationOperation... operation) { |
|
|
|
|
try { |
|
|
|
|
Aggregation aggregation = Aggregation.newAggregation(operation); |
|
|
|
|
return mongoTemplate.aggregate(aggregation, TableName.forum, ForumVo.class).getMappedResults(); |
|
|
|
|
LOG.info(aggregation.toString()); |
|
|
|
|
return mongoTemplate.aggregate(aggregation, TableName.forum, ForumDto.class).getMappedResults(); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
LOG.error(e.toString()); |
|
|
|
@ -70,29 +93,62 @@ public class ForumDao extends BaseDao<Forum> { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//查找所有帖子
|
|
|
|
|
public List<ForumVo> loadNewForum() { |
|
|
|
|
return loadForum(lookupOperation, Aggregation.unwind("myInfo"), Aggregation.sort(Sort.by("time").descending())); |
|
|
|
|
//设置投诉贴投诉人详细信息
|
|
|
|
|
private void loadComplain(ForumDto forumDto) { |
|
|
|
|
Complain complain = forumDto.getComplainDto(); |
|
|
|
|
complain.setInfo(mongoTemplate.findOne(new Query(Criteria.where(idKey).is(complain.getComplain())), MyInfo.class)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//查找最新帖子
|
|
|
|
|
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 Page<ForumDto> loadForum(ForumQuery forumQuery) { |
|
|
|
|
// 根据审核状态按时间从大到小排序的帖子
|
|
|
|
|
try { |
|
|
|
|
Criteria criteria = new Criteria(); |
|
|
|
|
if (!StringUtils.isEmpty(forumQuery.getStatus())) { |
|
|
|
|
Criteria.where("status").is(forumQuery.getStatus()); |
|
|
|
|
} |
|
|
|
|
if (!StringUtils.isEmpty(forumQuery.getUsername())) { |
|
|
|
|
criteria = criteria.and("username").is(forumQuery.getUsername()); |
|
|
|
|
} |
|
|
|
|
Query query = new Query(criteria).with(PageRequest.of(forumQuery.getCurrentPage().intValue() - 1, pageSize)); |
|
|
|
|
long currentCount = mongoTemplate.count(query, entityClass()); |
|
|
|
|
if (currentCount > 0) { |
|
|
|
|
long count = mongoTemplate.count(new Query(criteria), entityClass()); |
|
|
|
|
Page<ForumDto> page = new Page<>(count, forumQuery.getCurrentPage(), pageSize); |
|
|
|
|
List<ForumDto> forumDtos = loadForum(complainforumLookup, activeforumLookup, 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; |
|
|
|
|
} else { |
|
|
|
|
return new Page<>(0L, forumQuery.getCurrentPage(), pageSize); |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
LOG.error(e.toString()); |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//保存活动贴
|
|
|
|
|
public Forum save(ActiveForm activeForm){ |
|
|
|
|
public Forum save(ActiveVo activeVo) { |
|
|
|
|
try { |
|
|
|
|
Forum forum=new Forum(); |
|
|
|
|
forum.setUsername(activeForm.getUsername()); |
|
|
|
|
Forum forum = new Forum(); |
|
|
|
|
forum.setUsername(activeVo.getUsername()); |
|
|
|
|
forum.setForumType(Forum.ForumType.active); |
|
|
|
|
forum.setReply(0); |
|
|
|
|
forum.setTitle(activeForm.getTitle()); |
|
|
|
|
forum.setTitle(activeVo.getTitle()); |
|
|
|
|
forum.setTime(System.currentTimeMillis()); |
|
|
|
|
if(activeForm.getContent().length()>contentLength){ |
|
|
|
|
forum.setContent(activeForm.getContent().substring(0,contentLength)); |
|
|
|
|
}else{ |
|
|
|
|
forum.setContent(activeForm.getContent()); |
|
|
|
|
forum.setStatus(Forum.ForumStatus.uncheck); |
|
|
|
|
if (activeVo.getContent().length() > contentLength) { |
|
|
|
|
forum.setContent(activeVo.getContent().substring(0, contentLength)); |
|
|
|
|
} else { |
|
|
|
|
forum.setContent(activeVo.getContent()); |
|
|
|
|
} |
|
|
|
|
return save(forum); |
|
|
|
|
} catch (Exception e) { |
|
|
|
@ -103,15 +159,16 @@ public class ForumDao extends BaseDao<Forum> { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//保存动态
|
|
|
|
|
public Forum save(ForumForm forumForm){ |
|
|
|
|
public Forum save(ForumVo forumVo) { |
|
|
|
|
try { |
|
|
|
|
Forum forum=new Forum(); |
|
|
|
|
forum.setContent(forumForm.getContent()); |
|
|
|
|
forum.setTitle(forumForm.getTitle()); |
|
|
|
|
Forum forum = new Forum(); |
|
|
|
|
forum.setContent(forumVo.getContent()); |
|
|
|
|
forum.setTitle(forumVo.getTitle()); |
|
|
|
|
forum.setForumType(Forum.ForumType.topic); |
|
|
|
|
forum.setTime(System.currentTimeMillis()); |
|
|
|
|
forum.setReply(0); |
|
|
|
|
forum.setUsername(forumForm.getUsername()); |
|
|
|
|
forum.setUsername(forumVo.getUsername()); |
|
|
|
|
forum.setStatus(Forum.ForumStatus.uncheck); |
|
|
|
|
return save(forum); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
@ -121,15 +178,16 @@ public class ForumDao extends BaseDao<Forum> { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//保存投诉
|
|
|
|
|
public Forum save(ComplainForm complainForm){ |
|
|
|
|
public Forum save(ComplainVo complainVo) { |
|
|
|
|
try { |
|
|
|
|
Forum forum=new Forum(); |
|
|
|
|
forum.setContent(complainForm.getContent()); |
|
|
|
|
forum.setTitle(complainForm.getTitle()); |
|
|
|
|
Forum forum = new Forum(); |
|
|
|
|
forum.setContent(complainVo.getContent()); |
|
|
|
|
forum.setTitle(complainVo.getTitle()); |
|
|
|
|
forum.setForumType(Forum.ForumType.complan); |
|
|
|
|
forum.setTime(System.currentTimeMillis()); |
|
|
|
|
forum.setReply(0); |
|
|
|
|
forum.setUsername(complainForm.getUsername()); |
|
|
|
|
forum.setUsername(complainVo.getUsername()); |
|
|
|
|
forum.setStatus(Forum.ForumStatus.uncheck); |
|
|
|
|
return save(forum); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
@ -138,6 +196,11 @@ public class ForumDao extends BaseDao<Forum> { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//审核帖子
|
|
|
|
|
public boolean check(ForumCheck forumCheck) { |
|
|
|
|
return mongoTemplate.updateFirst(new Query(Criteria.where(idKey).is(forumCheck.getId())), Update.update("status", forumCheck.getStatus()), entityClass()).wasAcknowledged(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//保存
|
|
|
|
|
@Override |
|
|
|
|
public Forum save(Forum forum) { |
|
|
|
|