Commit 97e2a131 authored by 刘文胜's avatar 刘文胜

互动消息显示错乱问题

parent 4a2f0440
......@@ -42,6 +42,12 @@ var ForumAboutMeSchema = new Schema({
index: true,
ref: 'ForumComment'
},
commentLevel1From:{//一级评论的作者
type: Schema.Types.ObjectId,
require: false,
index: true,
ref: 'ForumUser'
},
commentLevel1Status:{
type: Number
},
......
'use strict';
var mongoose = require('mongoose');
var ForumThread = mongoose.model('ForumThread');
var ForumComment = mongoose.model('ForumComment');
var ForumAboutMe = mongoose.model('ForumAboutMe');
var THREAD_TYPE=1;
var COMMENTLEVEL1_TYPE=2;
var COMMENTLEVEL2_TYPE=3;
var forumCommentService = require('./forumCommentService');
var forumThreadService = require('./forumThreadService');
var then = require('thenjs');
var _ = require('lodash');
var async = require('async');
/**开始批处理文章和评论到关于我的**/
function canImportThreadToAboutme(tid,callback){
ForumAboutMe.findOne({
thread:tid,
type:THREAD_TYPE
}, function(err, result) {
if (err) {//有错直接忽略错误
callback(null, false);
} else {
callback(null, !result);
}
});
}
function canImportCommentLevel1ToAboutme(cid,callback){
ForumAboutMe.findOne({
commentLevel1:cid,
type:COMMENTLEVEL1_TYPE
}, function(err, result) {
if (err) {//有错直接忽略错误
callback(null, false);
} else {
callback(null, !result);
}
});
}
function canImportCommentLevel2ToAboutme(cid,callback){
ForumAboutMe.findOne({
commentLevel2:cid,
type:COMMENTLEVEL2_TYPE
}, function(err, result) {
if (err) {//有错直接忽略错误
callback(null, false);
} else {
callback(null, !result);
}
});
}
function saveThread(thread){
var forumAboutMe=new ForumAboutMe({
ent_code:thread.ent_code,
from:thread.from,
type:THREAD_TYPE,
thread:thread._id,
threadStatus:thread.status,
created:thread.created || new Date()
});
forumAboutMe.save(function(err){
if(err){
console.log(err);
}
});
}
function saveCommentLevel1(thread,commentLevel1){
var forumAboutMe=new ForumAboutMe({
ent_code:commentLevel1.ent_code,
from:commentLevel1.from,
to:thread.from,
type:COMMENTLEVEL1_TYPE,
thread:thread._id,
threadStatus:thread.status,
commentLevel1:commentLevel1._id,
commentLevel1Status:commentLevel1.status,
created:commentLevel1.created || new Date()
});
forumAboutMe.save(function(err){
if(err){
console.log(err);
}
});
};
function saveCommentLevel2(thread,commentLevel1,commentLevel2){
var forumAboutMe=new ForumAboutMe({
ent_code:commentLevel2.ent_code,
from:commentLevel2.from,
to:commentLevel2.to,
type:COMMENTLEVEL2_TYPE,
thread:thread._id,
threadStatus:thread.status,
commentLevel1:commentLevel1._id,
commentLevel1From:commentLevel1.from,
commentLevel1Status:commentLevel1.status,
commentLevel2:commentLevel2._id,
commentLevel2ThreadFrom:thread.from,
commentLevel2Status:commentLevel2.status,
created:commentLevel2.created || new Date()
});
forumAboutMe.save(function(err){
if(err){
console.log(err);
}
});
};
function FetchCommentToAboutMeByPage(pageNo,pageSize,count,condition,callback){
var skip = (pageNo - 1) * pageSize;
var limit = count - skip > pageSize ? pageSize : (count - skip);
ForumComment.find(condition).skip(skip).limit(limit).select('_id ent_code from to thread comments status created').populate({
path:'thread',
select:'_id ent_code from status created'
}).exec(function(err, commentLevel1s) {
if(commentLevel1s && commentLevel1s.length>0){
_.forEach(commentLevel1s,function(commentLevel1){
canImportCommentLevel1ToAboutme(commentLevel1._id,function(err,result){
if(result){
saveCommentLevel1(commentLevel1.thread || {},commentLevel1);
//子评论
if(commentLevel1.comments && commentLevel1.comments.length>0){
commentLevel1.comments.forEach(function(commentLevel2_id){
canImportCommentLevel2ToAboutme(commentLevel2_id,function(err,result){
if(result){
ForumComment.findOne({_id: commentLevel2_id}).select('_id ent_code from to status created').exec(function(err,commentLevel2){
if(commentLevel2){
saveCommentLevel2(commentLevel1.thread||{},commentLevel1,commentLevel2);
}
});
}
});
});
}
}
});
});
}
setTimeout(callback,1000);
});
}
function FetchThreadToAboutMeByPage(pageNo,pageSize,count,condition,callback){
var skip = (pageNo - 1) * pageSize;
var limit = count - skip > pageSize ? pageSize : (count - skip);
ForumThread.find(condition).skip(skip).limit(limit).select('_id ent_code from status created').exec(function(err, threads) {
if(threads && threads.length>0){
_.forEach(threads,function(thread){
canImportThreadToAboutme(thread._id,function(err,result){
result && saveThread(thread);
})
});
}
setTimeout(callback,1000);
});
}
exports.FetchThreadToAboutMe=function(){
var pageSize = 50;
var tasks = [];
var condition={};
ForumThread.find(condition).count(function(err,count){
if(count){
_.forEach(_.range((count / pageSize) + 1),function(o,pageNO){
tasks.push(function(cb){
FetchThreadToAboutMeByPage(pageNO,pageSize,count,condition,cb);
});
});
async.series(tasks,function(err, result) {
});
}
});
};
exports.FetchCommentToAboutMe=function(){
var pageSize = 50;
var tasks = [];
var condition={level:1};
ForumComment.find(condition).count(function(err,count){
if(count){
_.forEach(_.range((count / pageSize) + 1),function(o,pageNO){
tasks.push(function(cb){
FetchCommentToAboutMeByPage(pageNO,pageSize,count,condition,cb);
});
});
async.series(tasks,function(err, result) {
});
}
});
};
/**结束批处理文章和评论到关于我的**/
exports.saveThread=function(thread){
var forumAboutMe=new ForumAboutMe({
ent_code:thread.ent_code,
......@@ -51,6 +242,7 @@ exports.saveCommentLevel2=function(tid,commentLevel1,commentLevel2){
thread:thread._id,
threadStatus:thread.status,
commentLevel1:commentLevel1._id,
commentLevel1From:commentLevel1.from,
commentLevel1Status:commentLevel1.status,
commentLevel2:commentLevel2._id,
commentLevel2ThreadFrom:thread.from,
......@@ -86,6 +278,7 @@ exports.saveCommentLevel2BySearch=function(tid,l1id,l2id){
thread:thread._id,
threadStatus:thread.status,
commentLevel1:commentLevel1._id,
commentLevel1From:commentLevel1.from,
commentLevel1Status:commentLevel1.status,
commentLevel2:commentLevel2._id,
commentLevel2ThreadFrom:thread.from,
......@@ -203,7 +396,7 @@ exports.me2other=function(ent_code,id,pageNo,pageSize,callback){
var limit = count - skip > pageSize ? pageSize : (count - skip);
var sortBy = '-created';
ForumAboutMe.find(condition).populate({
path: 'from to commentLevel2ThreadFrom',
path: 'from to commentLevel1From commentLevel2ThreadFrom',
select: 'uid nickName icon displayName displayIcon classLevel'
}).populate({
path: 'thread',
......@@ -247,7 +440,7 @@ exports.other2me=function(ent_code,id,pageNo,pageSize,callback){
var limit = count - skip > pageSize ? pageSize : (count - skip);
var sortBy = '-created';
ForumAboutMe.find(condition).populate({
path: 'from to commentLevel2ThreadFrom',
path: 'from to commentLevel1From commentLevel2ThreadFrom',
select: 'uid nickName icon displayName displayIcon classLevel'
}).populate({
path: 'thread',
......
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