Commit 07765f17 authored by 陈家荣's avatar 陈家荣

增加了板块报表的导出板块报表接口

parent 4bdacea3
......@@ -3,6 +3,8 @@ var express = require('express'),
router = express.Router(),
returnCode = require('../../utils/returnCode'),
_ = require('lodash'),
moment = require('moment'),
nodeExcel = require('excel-export'),
then = require('thenjs');
var mongoose = require('mongoose');
......@@ -10,12 +12,60 @@ var ForumInfo = mongoose.model('ForumInfo');
var forumInfoService=require('../../service/forumInfoService');
var forumThreadService=require('../../service/forumThreadService');
var forumGroupService = require('../../service/forumGroupService');
var ForumThread = mongoose.model('ForumThread');
var ForumUvLog = mongoose.model('ForumUVLog');
module.exports = function(app) {
app.use('/admin/forum', router);
};
//格式化日期 (格式:年-月-日)
function date_format(date) {
return moment(date).format('YYYY/MM/DD');
}
//获取日期之间所有日期
function get_all_date(begin, end) {
var date_array = [];
// if (begin === end) return date_array;
if (begin === end){
date_array.push(end.replace(/-/g, "/"));
return date_array;
}
var ab = begin.split('-');
var ae = end.split('-');
var db = new Date();
db.setUTCFullYear(ab[0], ab[1] - 1, ab[2]);
var de = new Date();
de.setUTCFullYear(ae[0], ae[1] - 1, ae[2]);
var unixDb = db.getTime();
var unixDe = de.getTime() + 24 * 60 * 60 * 1000;
for (var k = unixDb; k < unixDe;) {
date_array.push(date_format(new Date(parseInt(k))));
k = k + 24 * 60 * 60 * 1000;
}
return date_array;
}
//统计
function mapReduce(model, match, group, date, callback) {
model.aggregate({
$match: match
}, {
$group: group
},
function(err, res) {
if (err) callback(err);
else {
var data = {
date: date,
count: res.length
};
callback(null, data);
}
});
}
//新增论坛板块
router.post('/info/create', function(req, res, next) {
var rs = {};
......@@ -31,7 +81,7 @@ router.post('/info/create', function(req, res, next) {
if(groups && groups.items.length >0){
cont(null,groups.items[0]);
}else{
//创建默认版块组
//创建默认版块组1
var group = {
name:'默认分组',
ent_code:ent_code
......@@ -173,3 +223,225 @@ router.get('/infos/list', function(req, res, next) {
}
});
});
//板块报表
router.post('/info/report/:id', function(req, res, next) {
// 1.获取文章的创建日期,和今天的日期,转换成日期数组
var info_id = req.body.id,
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);
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')
},
info: info_id
};
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
}
res.json({
result: true,
errcode: 0,
data: data,
date_array: all_data_array
});
}).fail(function(cont, err) {
console.error(err);
res.json({
result: false,
err: err
});
});
} else {
res.json({
result: false,
err: '参数不合法'
});
}
});
//导出板块报表
router.get('/info/report/exportXlsReport/:id', function(req, res, next) {
var info_id = req.params.id,
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')
},
info: info_id
};
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 result = nodeExcel.execute(conf);
res.setHeader('Content-Type', 'application/vnd.ms-excel');
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