Commit 572274a5 authored by 刘文胜's avatar 刘文胜

异步批量 帖子 评论 删除屏蔽

parent ae9968b0
...@@ -42,7 +42,6 @@ function getBatchStatus(key,callback){ ...@@ -42,7 +42,6 @@ function getBatchStatus(key,callback){
} }
}); });
} }
function setStatus(key,status,expire){ function setStatus(key,status,expire){
var _status = status || {status:0}; var _status = status || {status:0};
redis.set(key,JSON.stringify(_status),function(err){ redis.set(key,JSON.stringify(_status),function(err){
...@@ -52,7 +51,6 @@ function setStatus(key,status,expire){ ...@@ -52,7 +51,6 @@ function setStatus(key,status,expire){
redis.expire(key,expire || 180); redis.expire(key,expire || 180);
}); });
} }
function canStartIf(key,cannot,can){ function canStartIf(key,cannot,can){
getBatchStatus(key,function(err,store){ getBatchStatus(key,function(err,store){
if(err){ if(err){
...@@ -144,21 +142,7 @@ function getThreadBatchOperateConditions(req){ ...@@ -144,21 +142,7 @@ function getThreadBatchOperateConditions(req){
} }
return conditions; return conditions;
} }
function batchAsyncUpdateThread(req,res,handlFn){
router.get('/threadManagement/threads/list/batch/result',function(req,res,next){
var key = thread_batch_prefix + req.session.user.ent_code;
getBatchStatus(key,function(err,store){
if(err){
return res.json(returnCode.BUSY);
}
res.json(_.assign({
data:store
}, returnCode.SUCCESS));
});
});
//删除查询结果的所有帖子
router.post('/threadManagement/threads/list/deleteAllAsync', function(req, res, next) {
var conditions = getThreadBatchOperateConditions(req); var conditions = getThreadBatchOperateConditions(req);
var key = thread_batch_prefix + req.session.user.ent_code; var key = thread_batch_prefix + req.session.user.ent_code;
var nickName = req.body.nickName; var nickName = req.body.nickName;
...@@ -187,7 +171,7 @@ router.post('/threadManagement/threads/list/deleteAllAsync', function(req, res, ...@@ -187,7 +171,7 @@ router.post('/threadManagement/threads/list/deleteAllAsync', function(req, res,
setStatus(key,{status:1,start:start_time,end:new Date().getTime()},1800); setStatus(key,{status:1,start:start_time,end:new Date().getTime()},1800);
} }
}; };
var deleteAll= function(condition,callback){ var progressAll= function(condition,callback){
ForumThread.find(conditions).count(function(err,count){ ForumThread.find(conditions).count(function(err,count){
if(err || !count){ if(err || !count){
return callback(err, true); return callback(err, true);
...@@ -197,29 +181,22 @@ router.post('/threadManagement/threads/list/deleteAllAsync', function(req, res, ...@@ -197,29 +181,22 @@ router.post('/threadManagement/threads/list/deleteAllAsync', function(req, res,
_.each(_.range(page_total),function(pageNo){ _.each(_.range(page_total),function(pageNo){
tasks.push(function(cb){ tasks.push(function(cb){
ForumThread.find(conditions).select('_id').skip((pageNo)*pageSize).limit(pageSize) ForumThread.find(conditions).select('_id').skip((pageNo)*pageSize).limit(pageSize)
.exec(function(err, docs) { .exec(function(err, docs) {
if (err) { if (err) {
return cb(err, null); return cb(err, null);
} }
var chunks=[]; var chunks=[];
if(docs && docs.length>0){ if(docs && docs.length>0){
_.forEach(docs,function(doc){ _.forEach(docs,function(doc){
chunks.push(function(cb2){ chunks.push(function(cb2){
forumThreadService.logicDeleteThreadById(doc._id, function(err) { handlFn(doc,cb2);
if (err) {
cb2(err, null);
} else {
forumAboutMEService.updateThreadStatus(doc._id, 3);
cb2(null, true);
}
}); });
}); });
}
async.parallel(chunks,function(err){
cb(err, true);
}); });
}
async.parallel(chunks,function(err){
cb(err, true);
}); });
});
}); });
}); });
async.series(tasks,function(err){ async.series(tasks,function(err){
...@@ -234,9 +211,9 @@ router.post('/threadManagement/threads/list/deleteAllAsync', function(req, res, ...@@ -234,9 +211,9 @@ router.post('/threadManagement/threads/list/deleteAllAsync', function(req, res,
return res.json(returnCode.BUSY); return res.json(returnCode.BUSY);
} }
if(handling){//正在处理 if(handling){//正在处理
return res.json(_.assign({ return res.json(_.assign({
data:false data:false
}, returnCode.SUCCESS)); }, returnCode.SUCCESS));
} }
},function(){ },function(){
if (mid) { if (mid) {
...@@ -252,7 +229,7 @@ router.post('/threadManagement/threads/list/deleteAllAsync', function(req, res, ...@@ -252,7 +229,7 @@ router.post('/threadManagement/threads/list/deleteAllAsync', function(req, res,
conditions.from = { conditions.from = {
"$in" : user_ids "$in" : user_ids
}; };
deleteAll(conditions,callback); progressAll(conditions,callback);
} }
}); });
} else if (nickName) { } else if (nickName) {
...@@ -269,22 +246,98 @@ router.post('/threadManagement/threads/list/deleteAllAsync', function(req, res, ...@@ -269,22 +246,98 @@ router.post('/threadManagement/threads/list/deleteAllAsync', function(req, res,
conditions.from = { conditions.from = {
"$in" : user_ids "$in" : user_ids
}; };
deleteAll(conditions,callback); progressAll(conditions,callback);
} }
}); });
} else { } else {
deleteAll(conditions,callback); progressAll(conditions,callback);
} }
res.json(_.assign({data:true}, returnCode.SUCCESS)); res.json(_.assign({data:true}, returnCode.SUCCESS));
}); });
}
router.get('/threadManagement/threads/list/batch/result',function(req,res,next){
var key = thread_batch_prefix + req.session.user.ent_code;
getBatchStatus(key,function(err,store){
if(err){
return res.json(returnCode.BUSY);
}
res.json(_.assign({
data:store
}, returnCode.SUCCESS));
});
});
//删除查询结果的所有帖子
router.post('/threadManagement/threads/list/deleteAllAsync', function(req, res, next) {
batchAsyncUpdateThread(req,res,function(thread,cb){
forumThreadService.logicDeleteThreadById(thread._id, function(err) {
if (err) {
cb(err, null);
} else {
forumAboutMEService.updateThreadStatus(doc._id, 3);
cb(null, true);
}
});
})
}); });
//屏蔽查询结果的所有帖子 //屏蔽查询结果的所有帖子
router.post('/threadManagement/threads/list/closeAllAsync', function(req, res, next) { router.post('/threadManagement/threads/list/closeAllAsync', function(req, res, next) {
var conditions = getThreadBatchOperateConditions(req); batchAsyncUpdateThread(req,res,function(thread,cb){
var key = thread_batch_prefix + req.session.user.ent_code; forumThreadService.updateThreadById(thread._id, {status:0}, function(err) {
var nickName = req.body.nickName; if (err) {
cb(err, null);
} else {
forumAboutMEService.updateThreadStatus(thread._id, 0);
cb(null, true);
}
});
})
});
function updateCommentStatus(ent_code,comment,status,cb){
if(!ent_code || !comment){
return cb && cb(null,true);
}
var cid = comment._id;
var tid = comment.thread;
async.parallel([
function(callback) {
//更新评论状态
forumCommentService.updateCommentStatusById(cid, status, function(err) {
callback(err);
});
},
function(callback){
forumAboutMEService.updateCommentLevel1Status(cid, status);
callback();
},
function(callback) {
//是否删除评论
if (status == 2 && tid) {
forumThreadService.updateThreadById(tid, {
$pull:{
comments:cid
},
$inc: {
comment_count: -1
}
}, function(err) {
callback(err);
});
} else {
callback();
}
}
], function(err) {
cb(err);
});
}
function batchAsyncUpdateCommentStatus(status,req,res){
var conditions = getCommentBatchOperateConditions(req);
var ent_code = req.session.user.ent_code;
var tid = req.params.tid;
var key = comment_batch_prefix + ent_code + tid;
var mid = req.body.mid; var mid = req.body.mid;
var status = status;//status=0屏蔽,status=2删除
var start_time = new Date().getTime(); var start_time = new Date().getTime();
var pageSize = 100; var pageSize = 100;
var ingInterval = (function(){//定时设置正在进行 var ingInterval = (function(){//定时设置正在进行
...@@ -309,8 +362,8 @@ router.post('/threadManagement/threads/list/closeAllAsync', function(req, res, n ...@@ -309,8 +362,8 @@ router.post('/threadManagement/threads/list/closeAllAsync', function(req, res, n
setStatus(key,{status:1,start:start_time,end:new Date().getTime()},1800); setStatus(key,{status:1,start:start_time,end:new Date().getTime()},1800);
} }
}; };
var closeAll= function(condition,callback){ var progressAll= function(condition,callback){
ForumThread.find(conditions).count(function(err,count){ ForumComment.find(conditions).count(function(err,count){
if(err || !count){ if(err || !count){
return callback(err, true); return callback(err, true);
} }
...@@ -318,7 +371,7 @@ router.post('/threadManagement/threads/list/closeAllAsync', function(req, res, n ...@@ -318,7 +371,7 @@ router.post('/threadManagement/threads/list/closeAllAsync', function(req, res, n
var page_total = Math.ceil((count || 0) / pageSize) + 1; var page_total = Math.ceil((count || 0) / pageSize) + 1;
_.each(_.range(page_total),function(pageNo){ _.each(_.range(page_total),function(pageNo){
tasks.push(function(cb){ tasks.push(function(cb){
ForumThread.find(conditions).select('_id').skip((pageNo)*pageSize).limit(pageSize) ForumComment.find(conditions).select('_id thread').skip((pageNo)*pageSize).limit(pageSize)
.exec(function(err, docs) { .exec(function(err, docs) {
if (err) { if (err) {
return cb(err, null); return cb(err, null);
...@@ -327,14 +380,9 @@ router.post('/threadManagement/threads/list/closeAllAsync', function(req, res, n ...@@ -327,14 +380,9 @@ router.post('/threadManagement/threads/list/closeAllAsync', function(req, res, n
if(docs && docs.length>0){ if(docs && docs.length>0){
_.forEach(docs,function(doc){ _.forEach(docs,function(doc){
chunks.push(function(cb2){ chunks.push(function(cb2){
forumThreadService.updateThreadById(doc._id, {status:0}, function(err) { updateCommentStatus(ent_code,doc,status,function(err){
if (err) { cb2(err,null);
cb2(err, null); })
} else {
forumAboutMEService.updateThreadStatus(doc._id, 0);
cb2(null, true);
}
});
}); });
}); });
} }
...@@ -366,138 +414,29 @@ router.post('/threadManagement/threads/list/closeAllAsync', function(req, res, n ...@@ -366,138 +414,29 @@ router.post('/threadManagement/threads/list/closeAllAsync', function(req, res, n
if (err) { if (err) {
callback(err, null); callback(err, null);
} else { } else {
//查询对应用户的文章 //查询对应用户
var user_ids = []; var user_ids = [];
for(var i in users){ for (var i in users) {
user_ids.push(users[i]._id); user_ids.push(users[i]._id);
} }
conditions.from = { conditions.from = {
"$in" : user_ids $in: user_ids
};
closeAll(conditions,callback);
}
});
} else if (nickName) {
//查询到用户
forumUserService.searchMembersByNickName(nickName, function(err, users) {
if (err) {
callback(err, null);
} else {
//查询对应用户的文章
var user_ids = [];
for(var i in users){
user_ids.push(users[i]._id);
} }
conditions.from = { progressAll(conditions,callback);
"$in" : user_ids
};
closeAll(conditions,callback);
} }
}); });
} else { } else {
closeAll(conditions,callback); progressAll(conditions,callback);
} }
res.json(_.assign({data:true}, returnCode.SUCCESS)); res.json(_.assign({data:true}, returnCode.SUCCESS));
}); });
});
function updateComemntStatus(ent_code,id,status,cb){
if(!ent_code || !id){
return cb && cb(null,true);
}
var cid = id;
async.waterfall([
function(callback) {
//获取评论实例
forumCommentService.getCommentById(cid, callback);
},
function(comment, callback) {
//更新评论状态
forumCommentService.updateCommentStatusById(cid, status, function(err, update) {
callback(err, comment);
});
},
function(comment, callback) {
if (comment && comment.level == 1) {
forumAboutMEService.updateCommentLevel1Status(comment._id, status);
}
if (comment && comment.level == 2) {
forumAboutMEService.updateCommentLevel2Status(comment._id, status);
}
//是否删除评论
if (status == 2) {
if (comment.level == 1) {//一级评论,需要移除文章的comments里的cid,并且评论数-1
forumThreadService.updateThreadById(comment.thread, {
$pull:{
comments:cid
},
$inc: {
comment_count: -1
}
}, function(err, result) {
if(err){
console.log(err);
}
callback();
});
} else {//二级评论,移除父级评论的comments里的cid
forumCommentService.getCommentParent(cid, function(err, p_comment) {
if(err || !p_comment){
console.log(err);
}else{
forumCommentService.updateCommentById(p_comment._id, {
$pull:{
comments:cid
},
$inc: {
comment_count: -1
}
}, function(err, update) {
if(err){
console.log(err);
}
});
}
callback();
});
}
} else {
callback();
}
}
], function(err, restult) {
if (err) {
console.error(err);
}
cb(err,!!!err);
});
} }
function batchUpdateCommentStatus(ent_code,ids,status,callback){ function getCommentBatchOperateConditions(req){
if(!ent_code || !ids){
return callback && callback('参数错误');
}
var tasks = [];
_.forEach(ids,function(id){
task.push(function(cb){
updateComemntStatus(ent_code,id,status,function(err,result){
cb();
})
});
});
async.parallel(tasks,function(err,results){
return callback && callback(err,!!!err);
});
}
//删除帖子的评论(查询结果)
router.post('/threadManagement/threads/:tid/comment/search/deleteAllAsync', function(req, res, next) {
var tid = req.params.tid || null, var tid = req.params.tid || null,
mid = req.body.mid,
floor_start = req.body.floor_start, floor_start = req.body.floor_start,
floor_end = req.body.floor_end, floor_end = req.body.floor_end,
content = req.body.content || ''; content = req.body.content || '';
var ent_code = req.session.user.ent_code; var ent_code = req.session.user.ent_code;
var status = 2;//删除状态为2
var conditions = { var conditions = {
ent_code: ent_code, ent_code: ent_code,
thread: tid, thread: tid,
...@@ -519,152 +458,24 @@ router.post('/threadManagement/threads/:tid/comment/search/deleteAllAsync', func ...@@ -519,152 +458,24 @@ router.post('/threadManagement/threads/:tid/comment/search/deleteAllAsync', func
$lte:floor_end $lte:floor_end
}; };
} }
return conditions;
var callback = function(err, result) { }
if (err) { router.get('/threadManagement/threads/:tid/comment/list/batch/result',function(req,res,next){
console.error(err); var key = comment_batch_prefix + req.session.user.ent_code + req.params.tid;
res.json(returnCode.BUSY); getBatchStatus(key,function(err,store){
} else { if(err){
res.json(_.assign({ return res.json(returnCode.BUSY);
data:result
}, returnCode.SUCCESS));
} }
}; res.json(_.assign({
var deleteAll= function(condition,callback){ data:store
ForumComment.find(conditions).select('_id').exec(function(err, docs) { }, returnCode.SUCCESS));
if (err) { });
console.error(err);
callback(err, null);
} else {
var ids=[];
_.forEach(docs,function(doc){
ids.push(doc._id);
});
var chunks=_.chunk(ids, 100);
var tasks = [];
_.forEach(chunks,function(chunk){
tasks.push(function(cb){
batchUpdateCommentStatus(ent_code,chunk,status,function(err,result){
cb(err,result);
});
});
});
async.series(tasks,function(err){
callback(err, true);
});
}
});
};
if (tid && mid) {
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
}
deleteAll(conditions,callback);
}
});
} else if(tid){
deleteAll(conditions,callback);
} else {
res.json(returnCode.WRONG_PARAM);
}
}); });
//异步删除帖子的评论(查询结果)
//屏蔽帖子的评论(查询结果) router.post('/threadManagement/threads/:tid/comment/search/deleteAllAsync', function(req, res, next) {
batchAsyncUpdateCommentStatus(2,req,res);
});
//异步屏蔽帖子的评论(查询结果)
router.post('/threadManagement/threads/:tid/comment/search/closeAllAsync', function(req, res, next) { router.post('/threadManagement/threads/:tid/comment/search/closeAllAsync', function(req, res, next) {
var tid = req.params.tid || null, batchAsyncUpdateCommentStatus(0,req,res);
mid = req.body.mid,
floor_start = req.body.floor_start,
floor_end = req.body.floor_end,
content = req.body.content || '';
var ent_code = req.session.user.ent_code;
var status = 0;//屏蔽的状态为0
var conditions = {
ent_code: ent_code,
thread: tid,
level:1
};
if (content) {
conditions.content = {
$regex: content,
$options: 'i'
};
}
if(floor_start){
conditions.floor = {
$gte:floor_start
};
}
if(floor_end){
conditions.floor = {
$lte:floor_end
};
}
var callback = function(err, result) {
if (err) {
console.error(err);
res.json(returnCode.BUSY);
} else {
res.json(_.assign({
data:result
}, returnCode.SUCCESS));
}
};
var closeAll= function(condition,callback){
ForumComment.find(conditions).select('_id').exec(function(err, docs) {
if (err) {
console.error(err);
callback(err, null);
} else {
var ids=[];
_.forEach(docs,function(doc){
ids.push(doc._id);
});
var chunks=_.chunk(ids, 100);
var tasks = [];
_.forEach(chunks,function(chunk){
tasks.push(function(cb){
batchUpdateCommentStatus(ent_code,chunk,status,function(err,result){
cb(err,result);
});
});
});
async.series(tasks,function(err){
callback(err, true);
});
}
});
};
if (tid && mid) {
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
}
closeAll(conditions,callback);
}
});
} else if(tid){
closeAll(conditions,callback);
} else {
res.json(returnCode.WRONG_PARAM);
}
}); });
\ No newline at end of file
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