Commit bae4552f authored by 陈家荣's avatar 陈家荣

增加社区报表

parent 14b57d23
......@@ -225,7 +225,7 @@ router.get('/infos/list', function(req, res, next) {
});
//板块报表
//板块报表
router.post('/info/report/:id', function(req, res, next) {
// 1.获取文章的创建日期,和今天的日期,转换成日期数组
var info_id = req.body.id,
......@@ -272,7 +272,8 @@ router.post('/info/report/:id', function(req, res, next) {
created: {
$gte: new Date(d + ' 00:00:00'),
$lte: new Date(d + ' 23:59:59')
}
},
info: info_id
};
var group = {
_id: {
......@@ -333,7 +334,7 @@ router.post('/info/report/:id', function(req, res, next) {
//导出板块报表
//导出板块报表
router.get('/info/report/exportXlsReport/:id', function(req, res, next) {
var info_id = req.params.id,
ent_code = req.session.user.ent_code,
......@@ -493,3 +494,253 @@ router.get('/info/default', function(req, res, next) {
}
});
//社区报表
router.post('/infos/report', function(req, res, next) {
// 1.获取文章的创建日期,和今天的日期,转换成日期数组
var ent_code = req.session.user.ent_code,
begin_time = req.body.begin_time,
end_time = req.body.end_time,
all_data_array = get_all_date(begin_time, end_time);
//查询访问量最多的5条记录所用
var conditions = {
ent_code: ent_code,
level: 1,
status:{$ne:3}
};
var sort = '-pv_count -created';
if (begin_time && end_time) {
var uv_datas = [];
var thread_datas = [];
//统计UV
var uv_tasks = [];
_.forEach(all_data_array, function(d) {
uv_tasks.push(function(cont) {
var match = {
ent_code:ent_code,
created: {
$gte: new Date(d + ' 00:00:00'),
$lte: new Date(d + ' 23:59:59')
}
};
var group = {
_id: {
user: '$user'
},
count: {
$sum: 1
}
};
mapReduce(ForumUvLog, match, group, d, cont);
});
});
//统计发帖数
var thread_tasks = [];
_.forEach(all_data_array, function(d) {
thread_tasks.push(function(cont) {
var match = {
ent_code:ent_code,
created: {
$gte: new Date(d + ' 00:00:00'),
$lte: new Date(d + ' 23:59:59')
}
};
var group = {
_id: {
_id: '$_id'
},
count: {
$sum: 1
}
};
mapReduce(ForumThread, match, group, d, cont);
});
});
then.parallel([function(con) {
//UV
then.parallel(uv_tasks).then(function(cont, datas) {
uv_datas = datas;
con();
}).fail(function(cont, err) {
con();
});
}, function(con) {
//发帖数
then.parallel(thread_tasks).then(function(cont, datas) {
// console.log(datas);
thread_datas = datas;
con();
}).fail(function(cont, err) {
console.log(err);
con();
});
}]).then(function(cont, datas) {
var data = {
uv_datas: uv_datas,
thread_datas: thread_datas
}
cont(null,data);
}).then(function(cont, data) {
//查询浏览量最高的5个帖子
forumThreadService.getAllThreadByFid(conditions, 1, 5, sort, function(err, results) {
if (err) {
console.error(err);
res.json(returnCode.BUSY);
} else {
res.json({
result: true,
errcode: 0,
data: data,
date_array: all_data_array,
threads: results.items? results.items:null
});
}
});
}).fail(function(cont, err) {
console.error(err);
res.json({
result: false,
err: err
});
});
} else {
res.json({
result: false,
err: '参数不合法'
});
}
});
//导出子板块报表
router.get('/infos/report/exportXlsReport', function(req, res, next) {
var ent_code = req.session.user.ent_code,
begin_time = req.query.begin_time,
end_time = req.query.end_time,
all_data_array = get_all_date(begin_time, end_time);
if (begin_time && end_time) {
var uv_datas = [];
var thread_datas = [];
//统计UV
var uv_tasks = [];
_.forEach(all_data_array, function(d) {
uv_tasks.push(function(cont) {
var match = {
ent_code:ent_code,
created: {
$gte: new Date(d + ' 00:00:00'),
$lte: new Date(d + ' 23:59:59')
}
};
var group = {
_id: {
user: '$user'
},
count: {
$sum: 1
}
};
mapReduce(ForumUvLog, match, group, d, cont);
});
});
//统计发帖数
var thread_tasks = [];
_.forEach(all_data_array, function(d) {
thread_tasks.push(function(cont) {
var match = {
ent_code:ent_code,
created: {
$gte: new Date(d + ' 00:00:00'),
$lte: new Date(d + ' 23:59:59')
}
};
var group = {
_id: {
_id: '$_id'
},
count: {
$sum: 1
}
};
mapReduce(ForumThread, match, group, d, cont);
});
});
then.parallel([function(con) {
//UV
then.parallel(uv_tasks).then(function(cont, datas) {
uv_datas = datas;
con();
}).fail(function(cont, err) {
con();
});
}, function(con) {
//发帖数
then.parallel(thread_tasks).then(function(cont, datas) {
// console.log(datas);
thread_datas = datas;
con();
}).fail(function(cont, err) {
console.log(err);
con();
});
}]).then(function(cont, datas) {
var conf = {};
conf.cols = [
{caption:'日期', type:'string'},
{caption:'访问人数', type:'number'},
{caption:'发帖数', type:'number'}
];
conf.rows = [];
for(var i =0;i<all_data_array.length;i+=1){
conf.rows.push(
[
all_data_array[i],
uv_datas[i].count,
thread_datas[i].count
]
);
}
var filename = '社区报表.xlsx';
var userAgent = (req.headers['user-agent']||'').toLowerCase();
if(userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
res.setHeader('Content-Disposition', 'attachment; filename=' + encodeURIComponent(filename));
} else if(userAgent.indexOf('firefox') >= 0) {
res.setHeader('Content-Disposition', 'attachment; filename*="utf8\'\'' + encodeURIComponent(filename)+'"');
} else {
/* safari等其他非主流浏览器只能自求多福了 */
res.setHeader('Content-Disposition', 'attachment; filename=' + new Buffer(filename).toString('binary'));
}
var result = nodeExcel.execute(conf);
res.setHeader('Content-Type', 'application/vnd.ms-excel;charset=utf-8');
// res.setHeader('Content-Disposition', 'attachment; filename=InfoReport.xlsx');
res.end(result, 'binary');
}).fail(function(cont, err) {
console.error(err);
res.json({
result: false,
err: err
});
});
} else {
res.json({
result: false,
err: '参数不合法'
});
}
});
\ 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