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

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

parent ae9968b0
......@@ -42,7 +42,6 @@ function getBatchStatus(key,callback){
}
});
}
function setStatus(key,status,expire){
var _status = status || {status:0};
redis.set(key,JSON.stringify(_status),function(err){
......@@ -52,7 +51,6 @@ function setStatus(key,status,expire){
redis.expire(key,expire || 180);
});
}
function canStartIf(key,cannot,can){
getBatchStatus(key,function(err,store){
if(err){
......@@ -144,21 +142,7 @@ function getThreadBatchOperateConditions(req){
}
return conditions;
}
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) {
function batchAsyncUpdateThread(req,res,handlFn){
var conditions = getThreadBatchOperateConditions(req);
var key = thread_batch_prefix + req.session.user.ent_code;
var nickName = req.body.nickName;
......@@ -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);
}
};
var deleteAll= function(condition,callback){
var progressAll= function(condition,callback){
ForumThread.find(conditions).count(function(err,count){
if(err || !count){
return callback(err, true);
......@@ -197,29 +181,22 @@ router.post('/threadManagement/threads/list/deleteAllAsync', function(req, res,
_.each(_.range(page_total),function(pageNo){
tasks.push(function(cb){
ForumThread.find(conditions).select('_id').skip((pageNo)*pageSize).limit(pageSize)
.exec(function(err, docs) {
if (err) {
return cb(err, null);
}
var chunks=[];
if(docs && docs.length>0){
_.forEach(docs,function(doc){
chunks.push(function(cb2){
forumThreadService.logicDeleteThreadById(doc._id, function(err) {
if (err) {
cb2(err, null);
} else {
forumAboutMEService.updateThreadStatus(doc._id, 3);
cb2(null, true);
}
.exec(function(err, docs) {
if (err) {
return cb(err, null);
}
var chunks=[];
if(docs && docs.length>0){
_.forEach(docs,function(doc){
chunks.push(function(cb2){
handlFn(doc,cb2);
});
});
}
async.parallel(chunks,function(err){
cb(err, true);
});
}
async.parallel(chunks,function(err){
cb(err, true);
});
});
});
});
async.series(tasks,function(err){
......@@ -234,9 +211,9 @@ router.post('/threadManagement/threads/list/deleteAllAsync', function(req, res,
return res.json(returnCode.BUSY);
}
if(handling){//正在处理
return res.json(_.assign({
data:false
}, returnCode.SUCCESS));
return res.json(_.assign({
data:false
}, returnCode.SUCCESS));
}
},function(){
if (mid) {
......@@ -252,7 +229,7 @@ router.post('/threadManagement/threads/list/deleteAllAsync', function(req, res,
conditions.from = {
"$in" : user_ids
};
deleteAll(conditions,callback);
progressAll(conditions,callback);
}
});
} else if (nickName) {
......@@ -269,22 +246,98 @@ router.post('/threadManagement/threads/list/deleteAllAsync', function(req, res,
conditions.from = {
"$in" : user_ids
};
deleteAll(conditions,callback);
progressAll(conditions,callback);
}
});
} else {
deleteAll(conditions,callback);
progressAll(conditions,callback);
}
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) {
var conditions = getThreadBatchOperateConditions(req);
var key = thread_batch_prefix + req.session.user.ent_code;
var nickName = req.body.nickName;
batchAsyncUpdateThread(req,res,function(thread,cb){
forumThreadService.updateThreadById(thread._id, {status:0}, function(err) {
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 status = status;//status=0屏蔽,status=2删除
var start_time = new Date().getTime();
var pageSize = 100;
var ingInterval = (function(){//定时设置正在进行
......@@ -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);
}
};
var closeAll= function(condition,callback){
ForumThread.find(conditions).count(function(err,count){
var progressAll= function(condition,callback){
ForumComment.find(conditions).count(function(err,count){
if(err || !count){
return callback(err, true);
}
......@@ -318,7 +371,7 @@ router.post('/threadManagement/threads/list/closeAllAsync', function(req, res, n
var page_total = Math.ceil((count || 0) / pageSize) + 1;
_.each(_.range(page_total),function(pageNo){
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) {
if (err) {
return cb(err, null);
......@@ -327,14 +380,9 @@ router.post('/threadManagement/threads/list/closeAllAsync', function(req, res, n
if(docs && docs.length>0){
_.forEach(docs,function(doc){
chunks.push(function(cb2){
forumThreadService.updateThreadById(doc._id, {status:0}, function(err) {
if (err) {
cb2(err, null);
} else {
forumAboutMEService.updateThreadStatus(doc._id, 0);
cb2(null, true);
}
});
updateCommentStatus(ent_code,doc,status,function(err){
cb2(err,null);
})
});
});
}
......@@ -366,138 +414,29 @@ router.post('/threadManagement/threads/list/closeAllAsync', function(req, res, n
if (err) {
callback(err, null);
} else {
//查询对应用户的文章
//查询对应用户
var user_ids = [];
for(var i in users){
for (var i in users) {
user_ids.push(users[i]._id);
}
conditions.from = {
"$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);
$in: user_ids
}
conditions.from = {
"$in" : user_ids
};
closeAll(conditions,callback);
progressAll(conditions,callback);
}
});
} else {
closeAll(conditions,callback);
progressAll(conditions,callback);
}
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){
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) {
function getCommentBatchOperateConditions(req){
var tid = req.params.tid || null,
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 = 2;//删除状态为2
var conditions = {
ent_code: ent_code,
thread: tid,
......@@ -519,152 +458,24 @@ router.post('/threadManagement/threads/:tid/comment/search/deleteAllAsync', func
$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));
return conditions;
}
router.get('/threadManagement/threads/:tid/comment/list/batch/result',function(req,res,next){
var key = comment_batch_prefix + req.session.user.ent_code + req.params.tid;
getBatchStatus(key,function(err,store){
if(err){
return res.json(returnCode.BUSY);
}
};
var deleteAll= 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
}
deleteAll(conditions,callback);
}
});
} else if(tid){
deleteAll(conditions,callback);
} else {
res.json(returnCode.WRONG_PARAM);
}
res.json(_.assign({
data:store
}, returnCode.SUCCESS));
});
});
//屏蔽帖子的评论(查询结果)
//异步删除帖子的评论(查询结果)
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) {
var tid = req.params.tid || null,
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);
}
batchAsyncUpdateCommentStatus(0,req,res);
});
\ 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