Commit c8d5a0da authored by 杨翌文's avatar 杨翌文

删除多余文件

parent b22f5699
'use strict';
var mongoose = require('mongoose');
var ForumThread = mongoose.model('ForumThread');
var ForumComment = mongoose.model('ForumComment');
var ForumShare = mongoose.model('ForumShare');
var forumCommentService = require('./forumCommentService');
var async = require('async');
var then = require('thenjs');
//查询帖子
exports.findThreadByPage = function(pageNo, pageSize, q, callback) {
then(function(cont) {
ForumThread.find(q).populate('from').count(cont);
}).then(function(cont, count) {
var skip = (pageNo - 1) * pageSize;
var limit = count - skip > pageSize ? pageSize : (count - skip);
ForumThread.find(q).skip(skip).limit(limit).sort('-created').exec(function(err, docs) {
cont(err, count, docs);
});
}).then(function(cont, count, threads) {
var rsJson = {
result: true,
total: count,
datas: threads
};
callback(null, rsJson);
}).fail(function(cont, err) {
console.error(err);
var rsJson = {
result: false,
err: err
};
callback(err, rsJson);
});
};
//查询评论
exports.findCommentByPage = function(pageNo, pageSize, q, callback) {
then(function(cont) {
ForumComment.find(q).populate('from').count(cont);
}).then(function(cont, count) {
var skip = (pageNo - 1) * pageSize;
var limit = count - skip > pageSize ? pageSize : (count - skip);
ForumComment.find(q).skip(skip).limit(limit).sort('-created').populate('from').exec(function(err, docs) {
cont(err, count, docs);
});
}).then(function(cont, count, threads) {
var rsJson = {
result: true,
total: count,
datas: threads
};
callback(null, rsJson);
}).fail(function(cont, err) {
console.error(err);
var rsJson = {
result: false,
err: err
};
callback(err, rsJson);
});
};
//创建文章
exports.createThread = function(entity, callback) {
if (!entity.share) {
var forum = new ForumThread(entity);
forum.save(function(err, forum) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, forum);
}
});
} else {
var forumShare = new ForumShare(entity.share);
forumShare.ent_code = entity.ent_code;
forumShare.save(function(err, forumShare) {
if (err) {
console.error(err);
callback(err, null);
} else {
entity.share = forumShare._id;
var forum = new ForumThread(entity);
forum.save(function(err, forum) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, forum);
}
});
}
});
}
};
//根据ID获取文章记录,不查评论,子话题
exports.getById = function(id, callback) {
ForumThread.findOne({
_id: id
}, function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, result);
}
});
}
//根据ID获取文章
exports.getThreadById = function(tid, callback) {
async.parallel([
function(cb) {
var conditions = {
_id: tid
};
ForumThread.find(conditions).populate('from').populate('info').populate('share').exec(function(err, docs) {
if (err) {
console.error(err);
callback(err, null);
} else {
var thread = {};
if (docs && docs.length > 0) {
thread = docs[0];
cb(null, thread);
} else {
cb(null, thread);
}
}
});
},
function(cb) {
var conditions = {
thread: tid,
level: '1'
};
//获取最新10条评论
forumCommentService.getAllComment(conditions, 1, 10, function(err, results) {
if (err) {
console.log(err);
cb(err, null);
} else {
cb(null, results);
}
});
},
function(cb) {
//获取话题子文章列表
var conditions = {
pid: tid,
type: 2
};
getAllThreadByFidHelp(conditions, 1, 10, null, function(err, threads) {
if (err) {
console.error(err);
cb(err, null);
} else {
// console.log(threads);
cb(null, threads);
}
});
},
function(cb) {
//获取照片墙最新文章列表
var conditions = {
pid: tid,
type: 3
};
getAllThreadByFidHelp(conditions, 1, 10, '-created', function(err, threads) {
if (err) {
console.error(err);
cb(err, null);
} else {
cb(null, threads);
}
});
},
function(cb) {
//获取照片墙最热文章列表
var conditions = {
pid: tid,
type: 3
};
getAllThreadByFidHelp(conditions, 1, 10, '-praise_count', function(err, threads) {
if (err) {
console.error(err);
cb(err, null);
} else {
cb(null, threads);
}
});
},
function(cb) {
//获取发帖人数
ForumThread.aggregate({
$match: {
pid: mongoose.Types.ObjectId(tid)
}
}, {
$group: {
_id: {
from: '$from'
},
count: {
$sum: 1
}
}
}, function(err, data) {
if (err) {
cb(err, null);
} else {
cb(null, data.length);
}
});
}
], function(err, results) {
if (err) {
callback(err, null);
} else {
var thread = results[0];
var comments = results[1];
var subThreads = results[2] || []; //话题子文章列表
var latestPhotos = results[3] || []; //最新照片墙列表
var hotPhotos = results[4] || []; //最热照片墙列表
var canyuPeopleCount = results[5] || 0; //参与人数
var threadObj = thread.toObject();
threadObj.comments = comments;
threadObj.subThreads = subThreads;
threadObj.latestPhotos = latestPhotos;
threadObj.hotPhotos = hotPhotos;
threadObj.canyuPeopleCount = canyuPeopleCount;
callback(null, threadObj);
}
});
};
//根据ID更新文章
exports.updateThreadById = function(tid, entity, callback) {
var shareEntity = entity.share;
if (entity.share._id) {
entity.share = entity.share._id;
}else{
entity.share = '';
}
if (entity.share) {
ForumThread.update({
_id: tid
}, entity, null, function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
ForumShare.update({
_id: shareEntity._id
}, shareEntity, null, function(err, se) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, entity);
}
});
}
});
} else {
if (shareEntity.title || shareEntity.description || shareEntity.icon) {
shareEntity.ent_code = entity.ent_code;
var forumShare = new ForumShare(shareEntity);
forumShare.save(function(err, forumShare) {
entity.share = forumShare._id;
ForumThread.update({
_id: tid
}, entity, null, function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, entity);
}
});
});
} else {
delete entity.share;
ForumThread.update({
_id: tid
}, entity, null, function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, entity);
}
});
}
}
};
//根据ID更新文章
exports.deleteThreadById = function(tid, callback) {
ForumThread.remove({
_id: tid
}, function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, null);
}
});
};
//一级评论
function populateComment(doc, callback) {
if (doc && doc.comments.length > 0) {
var asyncTasks = [];
doc.comments.forEach(function(comment) {
asyncTasks.push(function(callback) {
ForumComment.populate(comment, {
path: 'from to',
select: 'uid nickName icon displayName displayIcon comments'
}, function(err, c) {
if (err) {
console.error(err);
callback(null, null);
} else {
// console.log(comment);
if (comment.comments) {
populateSubComment(comment, function(err, results) {
if (err) {
console.error(err);
callback(null, null);
} else {
var newobj = comment.toObject();
newobj.comments = results;
callback(null, newobj);
}
});
} else {
callback(null, doc);
}
}
});
});
});
async.parallel(asyncTasks, function(err, results) {
if (err) {
console.log(err);
callback(null, null);
} else {
if (results && results.length > 0) {
callback(null, results[0]);
} else {
callback(null, {});
}
// callback(null, results);
}
});
} else {
callback(null, doc);
}
}
//二级评论
function populateSubComment(subComments, callback) {
if (subComments.comments && subComments.comments.length > 0) {
var asyncTasks = [];
subComments.comments.forEach(function(comment) {
asyncTasks.push(function(callback) {
ForumComment.findOne({
_id: comment
}).populate({
path: 'from to',
select: 'uid nickName icon'
}).exec(function(err, c) {
if (err) {
console.error(err);
callback(null, null);
} else {
callback(null, c);
}
});
});
});
async.parallel(asyncTasks, function(err, results) {
if (err) {
console.log(err);
callback(null, null);
} else {
callback(null, results);
}
});
} else {
callback(null, null);
}
}
//获取数量
function countAllByFid(conditions, callback) {
ForumThread.count(conditions, function(err, count) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, count);
}
});
}
//导出获取数量函数
exports.getAllCountByFid = function(conditions, callback) {
countAllByFid(conditions, callback);
}
//获取话题、照片墙子文章数据
function getSubThreads(doc, sort, callback) {
var conditions = {
pid: doc._id
};
countAllByFid(conditions, function(err, count) {
if (err) {
console.error(err);
callback(err, null);
} else {
var sortBy = '-top -created';
if (sort) {
sortBy = sort;
}
ForumThread.find(conditions).populate('from').sort(sortBy).exec(function(err, docs) {
if (err) {
console.error(err);
callback(err, null);
} else {
var obj = {};
obj.total = count;
obj.items = docs;
var newDoc = doc.toObject();
newDoc.subThreads = obj;
callback(null, newDoc);
}
});
}
});
}
function getAllThreadByFidHelp(conditions, pageNo, pageSize, sort, callback) {
countAllByFid(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 sortBy = '-topTime -created';
if (sort) {
sortBy = sort;
}
ForumThread.find(conditions).populate('from').populate('info').populate({
path: 'tag'
}).populate({
path: 'comments',
options: {
limit: 5,
sort: '-created'
},
select: 'from to created content'
}).limit(limit).skip(skip).sort(sortBy).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;
if (docs && docs.length > 0) {
var asyncTasks = [];
docs.forEach(function(doc) {
// console.log(doc);
if (doc.type === 1 || doc.level !== 1) { //非照片墙或文章时获取评论
asyncTasks.push(function(callback) {
populateComment(doc, callback);
});
} else {
asyncTasks.push(function(callback) {
getSubThreads(doc, null, callback);
});
}
});
async.parallel(asyncTasks, function(err, results) {
if (err) {
console.error(err);
callback(null, null);
} else {
obj.items = results;
callback(null, obj);
}
});
} else {
callback(null, obj);
}
}
});
}
});
}
//获取全部列表数据
exports.getAllThreadByFid = function(conditions, pageNo, pageSize, sort, callback) {
getAllThreadByFidHelp(conditions, pageNo, pageSize, sort, callback);
};
//根据板块ID更新板块下的top为0,并把当前文章的top设为1(置顶)
exports.updateTopByThreadId = function(infoId, threadId, callback) {
// ForumThread.update({info:infoId}, {top:0},{multi:true}, function(err, doc) {
// if(err){
// callback(err,null);
// }else{
// ForumThread.findOneAndUpdate({_id:threadId}, {top:1}, function(err, doc) {
// if(err){
// callback(err,null);
// }else{
// callback(null,null);
// }
// });
// }
// });
ForumThread.findOneAndUpdate({
_id: threadId
}, {
top: 1,
topTime: new Date()
}, function(err, doc) {
if (err) {
callback(err, null);
} else {
callback(null, null);
}
});
};
//根据板块ID更新板块下的top为0,并把当前文章的top设为1(置顶)
exports.updateUnTopByThreadId = function(infoId, threadId, callback) {
ForumThread.findOneAndUpdate({
_id: threadId
}, {
top: 0,
topTime: null
}, function(err, doc) {
if (err) {
callback(err, null);
} else {
callback(null, null);
}
});
};
//更新文章点赞数
exports.updateThreadRaiseCount = function(threadId, callback) {
ForumThread.update({
_id: threadId
}, {
$inc: {
praise_count: 1
}
}, {
w: 1,
safe: true
},
function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, null);
}
});
};
//更新文章分享数
exports.updateThreadShareCount = function(threadId, callback) {
ForumThread.update({
_id: threadId
}, {
$inc: {
share_count: 1
}
}, {
w: 1,
safe: true
},
function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, null);
}
});
};
//更新文章评论数
exports.updateThreadCommentCount = function(threadId, callback) {
ForumThread.update({
_id: threadId
}, {
$inc: {
comment_count: 1
}
}, {
w: 1,
safe: true
},
function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, null);
}
});
};
//更新文章浏览数
exports.updateThreadPvCount = function(threadId, callback) {
ForumThread.update({
_id: threadId
}, {
$inc: {
pv_count: 1
}
}, {
w: 1,
safe: true
},
function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, null);
}
});
};
'use strict';
var mongoose = require('mongoose');
var ForumThread = mongoose.model('ForumThread');
var ForumComment = mongoose.model('ForumComment');
var ForumShare = mongoose.model('ForumShare');
var forumCommentService = require('./forumCommentService');
var async = require('async');
var then = require('thenjs');
//查询帖子
exports.findThreadByPage = function(pageNo, pageSize, q, callback) {
then(function(cont) {
ForumThread.find(q).populate('from').count(cont);
}).then(function(cont, count) {
var skip = (pageNo - 1) * pageSize;
var limit = count - skip > pageSize ? pageSize : (count - skip);
ForumThread.find(q).skip(skip).limit(limit).sort('-created').exec(function(err, docs) {
cont(err, count, docs);
});
}).then(function(cont, count, threads) {
var rsJson = {
result: true,
total: count,
datas: threads
};
callback(null, rsJson);
}).fail(function(cont, err) {
console.error(err);
var rsJson = {
result: false,
err: err
};
callback(err, rsJson);
});
};
//查询评论
exports.findCommentByPage = function(pageNo, pageSize, q, callback) {
then(function(cont) {
ForumComment.find(q).populate('from').count(cont);
}).then(function(cont, count) {
var skip = (pageNo - 1) * pageSize;
var limit = count - skip > pageSize ? pageSize : (count - skip);
ForumComment.find(q).skip(skip).limit(limit).sort('-created').populate('from').exec(function(err, docs) {
cont(err, count, docs);
});
}).then(function(cont, count, threads) {
var rsJson = {
result: true,
total: count,
datas: threads
};
callback(null, rsJson);
}).fail(function(cont, err) {
console.error(err);
var rsJson = {
result: false,
err: err
};
callback(err, rsJson);
});
};
//创建文章
exports.createThread = function(entity, callback) {
if (!entity.share) {
var forum = new ForumThread(entity);
forum.save(function(err, forum) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, forum);
}
});
} else {
var forumShare = new ForumShare(entity.share);
forumShare.ent_code = entity.ent_code;
forumShare.save(function(err, forumShare) {
if (err) {
console.error(err);
callback(err, null);
} else {
entity.share = forumShare._id;
var forum = new ForumThread(entity);
forum.save(function(err, forum) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, forum);
}
});
}
});
}
};
//根据ID获取文章记录,不查评论,子话题
exports.getById = function(id, callback) {
ForumThread.findOne({
_id: id
}, function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, result);
}
});
}
//根据ID获取文章
exports.getThreadById = function(tid, callback) {
async.parallel([
function(cb) {
var conditions = {
_id: tid
};
ForumThread.find(conditions).populate('from').populate('info').populate('share').exec(function(err, docs) {
if (err) {
console.error(err);
callback(err, null);
} else {
var thread = {};
if (docs && docs.length > 0) {
thread = docs[0];
cb(null, thread);
} else {
cb(null, thread);
}
}
});
},
function(cb) {
var conditions = {
thread: tid,
level: '1'
};
//获取最新10条评论
forumCommentService.getAllComment(conditions, 1, 10, function(err, results) {
if (err) {
console.log(err);
cb(err, null);
} else {
cb(null, results);
}
});
},
function(cb) {
//获取话题子文章列表
var conditions = {
pid: tid,
type: 2
};
getAllThreadByFidHelp(conditions, 1, 10, null, function(err, threads) {
if (err) {
console.error(err);
cb(err, null);
} else {
// console.log(threads);
cb(null, threads);
}
});
},
function(cb) {
//获取照片墙最新文章列表
var conditions = {
pid: tid,
type: 3
};
getAllThreadByFidHelp(conditions, 1, 10, '-created', function(err, threads) {
if (err) {
console.error(err);
cb(err, null);
} else {
cb(null, threads);
}
});
},
function(cb) {
//获取照片墙最热文章列表
var conditions = {
pid: tid,
type: 3
};
getAllThreadByFidHelp(conditions, 1, 10, '-praise_count', function(err, threads) {
if (err) {
console.error(err);
cb(err, null);
} else {
cb(null, threads);
}
});
},
function(cb) {
//获取发帖人数
ForumThread.aggregate({
$match: {
pid: mongoose.Types.ObjectId(tid)
}
}, {
$group: {
_id: {
from: '$from'
},
count: {
$sum: 1
}
}
}, function(err, data) {
if (err) {
cb(err, null);
} else {
cb(null, data.length);
}
});
}
], function(err, results) {
if (err) {
callback(err, null);
} else {
var thread = results[0];
var comments = results[1];
var subThreads = results[2] || []; //话题子文章列表
var latestPhotos = results[3] || []; //最新照片墙列表
var hotPhotos = results[4] || []; //最热照片墙列表
var canyuPeopleCount = results[5] || 0; //参与人数
var threadObj = thread.toObject();
threadObj.comments = comments;
threadObj.subThreads = subThreads;
threadObj.latestPhotos = latestPhotos;
threadObj.hotPhotos = hotPhotos;
threadObj.canyuPeopleCount = canyuPeopleCount;
callback(null, threadObj);
}
});
};
//根据ID更新文章
exports.updateThreadById = function(tid, entity, callback) {
var shareEntity = entity.share;
if (entity.share&&entity.share._id) {
entity.share = entity.share._id;
}else{
entity.share = '';
}
if (entity.share) {
ForumThread.update({
_id: tid
}, entity, null, function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
ForumShare.update({
_id: shareEntity._id
}, shareEntity, null, function(err, se) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, entity);
}
});
}
});
} else {
if (shareEntity&&(shareEntity.title || shareEntity.description || shareEntity.icon)) {
shareEntity.ent_code = entity.ent_code;
var forumShare = new ForumShare(shareEntity);
forumShare.save(function(err, forumShare) {
entity.share = forumShare._id;
ForumThread.update({
_id: tid
}, entity, null, function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, entity);
}
});
});
} else {
delete entity.share;
ForumThread.update({
_id: tid
}, entity, null, function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, entity);
}
});
}
}
};
//根据ID更新文章
exports.deleteThreadById = function(tid, callback) {
ForumThread.remove({
_id: tid
}, function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, null);
}
});
};
//一级评论
function populateComment(doc, callback) {
if (doc && doc.comments.length > 0) {
var asyncTasks = [];
doc.comments.forEach(function(comment) {
asyncTasks.push(function(callback) {
ForumComment.populate(comment, {
path: 'from to',
select: 'uid nickName icon displayName displayIcon comments'
}, function(err, c) {
if (err) {
console.error(err);
callback(null, null);
} else {
// console.log(comment);
if (comment.comments) {
populateSubComment(comment, function(err, results) {
if (err) {
console.error(err);
callback(null, null);
} else {
var newobj = comment.toObject();
newobj.comments = results;
callback(null, newobj);
}
});
} else {
callback(null, doc);
}
}
});
});
});
async.parallel(asyncTasks, function(err, results) {
if (err) {
console.log(err);
callback(null, null);
} else {
if (results && results.length > 0) {
callback(null, results[0]);
} else {
callback(null, {});
}
// callback(null, results);
}
});
} else {
callback(null, doc);
}
}
//二级评论
function populateSubComment(subComments, callback) {
if (subComments.comments && subComments.comments.length > 0) {
var asyncTasks = [];
subComments.comments.forEach(function(comment) {
asyncTasks.push(function(callback) {
ForumComment.findOne({
_id: comment
}).populate({
path: 'from to',
select: 'uid nickName icon'
}).exec(function(err, c) {
if (err) {
console.error(err);
callback(null, null);
} else {
callback(null, c);
}
});
});
});
async.parallel(asyncTasks, function(err, results) {
if (err) {
console.log(err);
callback(null, null);
} else {
callback(null, results);
}
});
} else {
callback(null, null);
}
}
//获取数量
function countAllByFid(conditions, callback) {
ForumThread.count(conditions, function(err, count) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, count);
}
});
}
//导出获取数量函数
exports.getAllCountByFid = function(conditions, callback) {
countAllByFid(conditions, callback);
}
//获取话题、照片墙子文章数据
function getSubThreads(doc, sort, callback) {
var conditions = {
pid: doc._id
};
countAllByFid(conditions, function(err, count) {
if (err) {
console.error(err);
callback(err, null);
} else {
var sortBy = '-top -created';
if (sort) {
sortBy = sort;
}
ForumThread.find(conditions).populate('from').sort(sortBy).exec(function(err, docs) {
if (err) {
console.error(err);
callback(err, null);
} else {
var obj = {};
obj.total = count;
obj.items = docs;
var newDoc = doc.toObject();
newDoc.subThreads = obj;
callback(null, newDoc);
}
});
}
});
}
function getAllThreadByFidHelp(conditions, pageNo, pageSize, sort, callback) {
countAllByFid(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 sortBy = '-topTime -created';
if (sort) {
sortBy = sort;
}
ForumThread.find(conditions).populate('from').populate('info').populate({
path: 'tag'
}).populate({
path: 'comments',
options: {
limit: 5,
sort: '-created'
},
select: 'from to created content'
}).limit(limit).skip(skip).sort(sortBy).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;
if (docs && docs.length > 0) {
var asyncTasks = [];
docs.forEach(function(doc) {
// console.log(doc);
if (doc.type === 1 || doc.level !== 1) { //非照片墙或文章时获取评论
asyncTasks.push(function(callback) {
populateComment(doc, callback);
});
} else {
asyncTasks.push(function(callback) {
getSubThreads(doc, null, callback);
});
}
});
async.parallel(asyncTasks, function(err, results) {
if (err) {
console.error(err);
callback(null, null);
} else {
obj.items = results;
callback(null, obj);
}
});
} else {
callback(null, obj);
}
}
});
}
});
}
//获取全部列表数据
exports.getAllThreadByFid = function(conditions, pageNo, pageSize, sort, callback) {
getAllThreadByFidHelp(conditions, pageNo, pageSize, sort, callback);
};
//根据板块ID更新板块下的top为0,并把当前文章的top设为1(置顶)
exports.updateTopByThreadId = function(infoId, threadId, callback) {
// ForumThread.update({info:infoId}, {top:0},{multi:true}, function(err, doc) {
// if(err){
// callback(err,null);
// }else{
// ForumThread.findOneAndUpdate({_id:threadId}, {top:1}, function(err, doc) {
// if(err){
// callback(err,null);
// }else{
// callback(null,null);
// }
// });
// }
// });
ForumThread.findOneAndUpdate({
_id: threadId
}, {
top: 1,
topTime: new Date()
}, function(err, doc) {
if (err) {
callback(err, null);
} else {
callback(null, null);
}
});
};
//根据板块ID更新板块下的top为0,并把当前文章的top设为1(置顶)
exports.updateUnTopByThreadId = function(infoId, threadId, callback) {
ForumThread.findOneAndUpdate({
_id: threadId
}, {
top: 0,
topTime: null
}, function(err, doc) {
if (err) {
callback(err, null);
} else {
callback(null, null);
}
});
};
//更新文章点赞数
exports.updateThreadRaiseCount = function(threadId, callback) {
ForumThread.update({
_id: threadId
}, {
$inc: {
praise_count: 1
}
}, {
w: 1,
safe: true
},
function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, null);
}
});
};
//更新文章分享数
exports.updateThreadShareCount = function(threadId, callback) {
ForumThread.update({
_id: threadId
}, {
$inc: {
share_count: 1
}
}, {
w: 1,
safe: true
},
function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, null);
}
});
};
//更新文章评论数
exports.updateThreadCommentCount = function(threadId, callback) {
ForumThread.update({
_id: threadId
}, {
$inc: {
comment_count: 1
}
}, {
w: 1,
safe: true
},
function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, null);
}
});
};
//更新文章浏览数
exports.updateThreadPvCount = function(threadId, callback) {
ForumThread.update({
_id: threadId
}, {
$inc: {
pv_count: 1
}
}, {
w: 1,
safe: true
},
function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, null);
}
});
};
'use strict';
var mongoose = require('mongoose');
var ForumThread = mongoose.model('ForumThread');
var ForumComment = mongoose.model('ForumComment');
var ForumShare = mongoose.model('ForumShare');
var forumCommentService = require('./forumCommentService');
var async = require('async');
var then = require('thenjs');
//查询帖子
exports.findThreadByPage = function(pageNo, pageSize, q, callback) {
then(function(cont) {
ForumThread.find(q).populate('from').count(cont);
}).then(function(cont, count) {
var skip = (pageNo - 1) * pageSize;
var limit = count - skip > pageSize ? pageSize : (count - skip);
ForumThread.find(q).skip(skip).limit(limit).sort('-created').exec(function(err, docs) {
cont(err, count, docs);
});
}).then(function(cont, count, threads) {
var rsJson = {
result: true,
total: count,
datas: threads
};
callback(null, rsJson);
}).fail(function(cont, err) {
console.error(err);
var rsJson = {
result: false,
err: err
};
callback(err, rsJson);
});
};
//查询评论
exports.findCommentByPage = function(pageNo, pageSize, q, callback) {
then(function(cont) {
ForumComment.find(q).populate('from').count(cont);
}).then(function(cont, count) {
var skip = (pageNo - 1) * pageSize;
var limit = count - skip > pageSize ? pageSize : (count - skip);
ForumComment.find(q).skip(skip).limit(limit).sort('-created').populate('from').exec(function(err, docs) {
cont(err, count, docs);
});
}).then(function(cont, count, threads) {
var rsJson = {
result: true,
total: count,
datas: threads
};
callback(null, rsJson);
}).fail(function(cont, err) {
console.error(err);
var rsJson = {
result: false,
err: err
};
callback(err, rsJson);
});
};
//创建文章
exports.createThread = function(entity, callback) {
if (!entity.share) {
var forum = new ForumThread(entity);
forum.save(function(err, forum) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, forum);
}
});
} else {
var forumShare = new ForumShare(entity.share);
forumShare.ent_code = entity.ent_code;
forumShare.save(function(err, forumShare) {
if (err) {
console.error(err);
callback(err, null);
} else {
entity.share = forumShare._id;
var forum = new ForumThread(entity);
forum.save(function(err, forum) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, forum);
}
});
}
});
}
};
//根据ID获取文章记录,不查评论,子话题
exports.getById = function(id, callback) {
ForumThread.findOne({
_id: id
}, function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, result);
}
});
}
//根据ID获取文章
exports.getThreadById = function(tid, callback) {
async.parallel([
function(cb) {
var conditions = {
_id: tid
};
ForumThread.find(conditions).populate('from').populate('info').populate('share').exec(function(err, docs) {
if (err) {
console.error(err);
callback(err, null);
} else {
var thread = {};
if (docs && docs.length > 0) {
thread = docs[0];
cb(null, thread);
} else {
cb(null, thread);
}
}
});
},
function(cb) {
var conditions = {
thread: tid,
level: '1'
};
//获取最新10条评论
forumCommentService.getAllComment(conditions, 1, 10, function(err, results) {
if (err) {
console.log(err);
cb(err, null);
} else {
cb(null, results);
}
});
},
function(cb) {
//获取话题子文章列表
var conditions = {
pid: tid,
type: 2
};
getAllThreadByFidHelp(conditions, 1, 10, null, function(err, threads) {
if (err) {
console.error(err);
cb(err, null);
} else {
// console.log(threads);
cb(null, threads);
}
});
},
function(cb) {
//获取照片墙最新文章列表
var conditions = {
pid: tid,
type: 3
};
getAllThreadByFidHelp(conditions, 1, 10, '-created', function(err, threads) {
if (err) {
console.error(err);
cb(err, null);
} else {
cb(null, threads);
}
});
},
function(cb) {
//获取照片墙最热文章列表
var conditions = {
pid: tid,
type: 3
};
getAllThreadByFidHelp(conditions, 1, 10, '-praise_count', function(err, threads) {
if (err) {
console.error(err);
cb(err, null);
} else {
cb(null, threads);
}
});
},
function(cb) {
//获取发帖人数
ForumThread.aggregate({
$match: {
pid: mongoose.Types.ObjectId(tid)
}
}, {
$group: {
_id: {
from: '$from'
},
count: {
$sum: 1
}
}
}, function(err, data) {
if (err) {
cb(err, null);
} else {
cb(null, data.length);
}
});
}
], function(err, results) {
if (err) {
callback(err, null);
} else {
var thread = results[0];
var comments = results[1];
var subThreads = results[2] || []; //话题子文章列表
var latestPhotos = results[3] || []; //最新照片墙列表
var hotPhotos = results[4] || []; //最热照片墙列表
var canyuPeopleCount = results[5] || 0; //参与人数
var threadObj = thread.toObject();
threadObj.comments = comments;
threadObj.subThreads = subThreads;
threadObj.latestPhotos = latestPhotos;
threadObj.hotPhotos = hotPhotos;
threadObj.canyuPeopleCount = canyuPeopleCount;
callback(null, threadObj);
}
});
};
//根据ID更新文章
exports.updateThreadById = function(tid, entity, callback) {
var shareEntity = entity.share || {};
if (entity.share && entity.share._id) {
entity.share = entity.share._id;
}else{
entity.share = '';
}
if (entity.share) {
ForumThread.update({
_id: tid
}, entity, null, function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
ForumShare.update({
_id: shareEntity._id
}, shareEntity, null, function(err, se) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, entity);
}
});
}
});
} else {
if (shareEntity.title || shareEntity.description || shareEntity.icon) {
shareEntity.ent_code = entity.ent_code;
var forumShare = new ForumShare(shareEntity);
forumShare.save(function(err, forumShare) {
entity.share = forumShare._id;
ForumThread.update({
_id: tid
}, entity, null, function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, entity);
}
});
});
} else {
delete entity.share;
ForumThread.update({
_id: tid
}, entity, null, function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, entity);
}
});
}
}
};
//根据ID更新文章
exports.deleteThreadById = function(tid, callback) {
ForumThread.remove({
_id: tid
}, function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, null);
}
});
};
//一级评论
function populateComment(doc, callback) {
if (doc && doc.comments.length > 0) {
var asyncTasks = [];
doc.comments.forEach(function(comment) {
asyncTasks.push(function(callback) {
ForumComment.populate(comment, {
path: 'from to',
select: 'uid nickName icon displayName displayIcon comments'
}, function(err, c) {
if (err) {
console.error(err);
callback(null, null);
} else {
// console.log(comment);
if (comment.comments) {
populateSubComment(comment, function(err, results) {
if (err) {
console.error(err);
callback(null, null);
} else {
var newobj = comment.toObject();
newobj.comments = results;
callback(null, newobj);
}
});
} else {
callback(null, doc);
}
}
});
});
});
async.parallel(asyncTasks, function(err, results) {
if (err) {
console.log(err);
callback(null, null);
} else {
if (results && results.length > 0) {
callback(null, results[0]);
} else {
callback(null, {});
}
// callback(null, results);
}
});
} else {
callback(null, doc);
}
}
//二级评论
function populateSubComment(subComments, callback) {
if (subComments.comments && subComments.comments.length > 0) {
var asyncTasks = [];
subComments.comments.forEach(function(comment) {
asyncTasks.push(function(callback) {
ForumComment.findOne({
_id: comment
}).populate({
path: 'from to',
select: 'uid nickName icon'
}).exec(function(err, c) {
if (err) {
console.error(err);
callback(null, null);
} else {
callback(null, c);
}
});
});
});
async.parallel(asyncTasks, function(err, results) {
if (err) {
console.log(err);
callback(null, null);
} else {
callback(null, results);
}
});
} else {
callback(null, null);
}
}
//获取数量
function countAllByFid(conditions, callback) {
ForumThread.count(conditions, function(err, count) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, count);
}
});
}
//导出获取数量函数
exports.getAllCountByFid = function(conditions, callback) {
countAllByFid(conditions, callback);
}
//获取话题、照片墙子文章数据
function getSubThreads(doc, sort, callback) {
var conditions = {
pid: doc._id
};
countAllByFid(conditions, function(err, count) {
if (err) {
console.error(err);
callback(err, null);
} else {
var sortBy = '-top -created';
if (sort) {
sortBy = sort;
}
ForumThread.find(conditions).populate('from').sort(sortBy).exec(function(err, docs) {
if (err) {
console.error(err);
callback(err, null);
} else {
var obj = {};
obj.total = count;
obj.items = docs;
var newDoc = doc.toObject();
newDoc.subThreads = obj;
callback(null, newDoc);
}
});
}
});
}
function getAllThreadByFidHelp(conditions, pageNo, pageSize, sort, callback) {
countAllByFid(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 sortBy = '-topTime -created';
if (sort) {
sortBy = sort;
}
ForumThread.find(conditions).populate('from').populate('info').populate({
path: 'tag'
}).populate({
path: 'comments',
options: {
limit: 5,
sort: '-created'
},
select: 'from to created content'
}).limit(limit).skip(skip).sort(sortBy).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;
if (docs && docs.length > 0) {
var asyncTasks = [];
docs.forEach(function(doc) {
// console.log(doc);
if (doc.type === 1 || doc.level !== 1) { //非照片墙或文章时获取评论
asyncTasks.push(function(callback) {
populateComment(doc, callback);
});
} else {
asyncTasks.push(function(callback) {
getSubThreads(doc, null, callback);
});
}
});
async.parallel(asyncTasks, function(err, results) {
if (err) {
console.error(err);
callback(null, null);
} else {
obj.items = results;
callback(null, obj);
}
});
} else {
callback(null, obj);
}
}
});
}
});
}
//获取全部列表数据
exports.getAllThreadByFid = function(conditions, pageNo, pageSize, sort, callback) {
getAllThreadByFidHelp(conditions, pageNo, pageSize, sort, callback);
};
//根据板块ID更新板块下的top为0,并把当前文章的top设为1(置顶)
exports.updateTopByThreadId = function(infoId, threadId, callback) {
// ForumThread.update({info:infoId}, {top:0},{multi:true}, function(err, doc) {
// if(err){
// callback(err,null);
// }else{
// ForumThread.findOneAndUpdate({_id:threadId}, {top:1}, function(err, doc) {
// if(err){
// callback(err,null);
// }else{
// callback(null,null);
// }
// });
// }
// });
ForumThread.findOneAndUpdate({
_id: threadId
}, {
top: 1,
topTime: new Date()
}, function(err, doc) {
if (err) {
callback(err, null);
} else {
callback(null, null);
}
});
};
//根据板块ID更新板块下的top为0,并把当前文章的top设为1(置顶)
exports.updateUnTopByThreadId = function(infoId, threadId, callback) {
ForumThread.findOneAndUpdate({
_id: threadId
}, {
top: 0,
topTime: null
}, function(err, doc) {
if (err) {
callback(err, null);
} else {
callback(null, null);
}
});
};
//更新文章点赞数
exports.updateThreadRaiseCount = function(threadId, callback) {
ForumThread.update({
_id: threadId
}, {
$inc: {
praise_count: 1
}
}, {
w: 1,
safe: true
},
function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, null);
}
});
};
//更新文章分享数
exports.updateThreadShareCount = function(threadId, callback) {
ForumThread.update({
_id: threadId
}, {
$inc: {
share_count: 1
}
}, {
w: 1,
safe: true
},
function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, null);
}
});
};
//更新文章评论数
exports.updateThreadCommentCount = function(threadId, callback) {
ForumThread.update({
_id: threadId
}, {
$inc: {
comment_count: 1
}
}, {
w: 1,
safe: true
},
function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, null);
}
});
};
//更新文章浏览数
exports.updateThreadPvCount = function(threadId, callback) {
ForumThread.update({
_id: threadId
}, {
$inc: {
pv_count: 1
}
}, {
w: 1,
safe: true
},
function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, null);
}
});
};
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