Commit 7ca535bb authored by 刘文胜's avatar 刘文胜

搜索帖子评论增加 按指定字段排序

parent b088abfe
......@@ -898,7 +898,12 @@ router.get('/threadManagement/threads/:tid/comment/search', function(req, res, n
mid = req.query.mid,
floor_start = req.query.floor_start,
floor_end = req.query.floor_end,
content = req.query.content || '';
content = req.query.content || '',
sortBy = req.query.sort;
var sort = {};
if(sortBy){
sort[sortBy] = -1;
}
var conditions = {
ent_code: req.session.user.ent_code,
thread: tid,
......@@ -921,7 +926,7 @@ router.get('/threadManagement/threads/:tid/comment/search', function(req, res, n
};
}
if (tid && mid) {
forumCommentService.getCommentListByMid(mid, conditions, pageNo, pageSize, function(err, results) {
forumCommentService.getCommentListByMidOrderBy(mid, conditions,sort, pageNo, pageSize, function(err, results) {
if (err) {
console.error(err);
res.json(returnCode.BUSY);
......@@ -930,7 +935,7 @@ router.get('/threadManagement/threads/:tid/comment/search', function(req, res, n
}
});
} else if(tid){
forumCommentService.getAllComment(conditions, pageNo, pageSize, function(err, results) {
forumCommentService.getAllCommentOrderBy(conditions,sort, pageNo, pageSize, function(err, results) {
if (err) {
console.log(err);
res.json(returnCode.BUSY);
......
......@@ -192,6 +192,40 @@ exports.getAllComment = function(conditions, pageNo, pageSize, callback) {
});
};
exports.getAllCommentOrderBy = function(conditions,orderBy, pageNo, pageSize, callback) {
countAll(conditions, function(err, count) {
if (err) {
console.error(err);
callback(err, null);
} else {
var skip = (pageNo - 1) * pageSize;
var limit = count - skip > pageSize ? pageSize : (count - skip);
var sort = {
created:-1
};
if(orderBy){
sort = orderBy;
}
ForumComment.find(conditions,listCommentFields).populate({
path: 'from',
select: 'uid mid nickName icon exp honorTitles'
}).populate('to','uid mid nickName icon exp').limit(limit).skip(skip).sort(sort).exec(function(err, docs) {
if (err) {
console.error(err);
callback(err, null);
} else {
var obj = {};
obj.total = count;
obj.pageNo = pageNo;
obj.pageSize = pageSize;
obj.items = docs;
callback(null, obj);
}
});
}
});
};
//评论
function populateComment(doc, callback) {
if (doc && doc.comments.length > 0) {
......@@ -445,6 +479,54 @@ exports.getCommentListByMid = function(mid,conditions, pageNo, pageSize, callbac
};
exports.getCommentListByMidOrderBy = function(mid,conditions,orderBy, pageNo, pageSize, callback) {
//查询到用户
forumUserService.searchMembersByMid(mid, function(err, users) {
if (err) {
console.error(err);
callback(err, null);
} else {
//查询对应用户的文章
var user_ids = [];
for(var i in users){
user_ids.push(users[i]._id);
}
conditions.from = {
$in:user_ids
}
countAll(conditions, function(err, count) {
if (err) {
callback(err);
} else {
var skip = (pageNo - 1) * pageSize;
var limit = count - skip > pageSize ? pageSize : (count - skip);
var sort = {
created:-1
};
if(orderBy){
sort = orderBy;
}
ForumComment.find(conditions).populate('from').populate('to').limit(limit).skip(skip).sort(sort).exec(function(err, docs) {
if (err) {
console.error(err);
callback(err, null);
} else {
var obj = {};
obj.total = count;
obj.pageNo = pageNo;
obj.pageSize = pageSize;
obj.items = docs;
callback(null, obj);
}
});
}
});
}
});
};
exports.getCommentParent = function(cid, callback) {
ForumComment.findOne({
comments: {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment