Commit 53002c13 authored by 姜登's avatar 姜登

excel

parent 61d65f92
Pipeline #22017 failed with stage
......@@ -44,7 +44,32 @@ class AccountController extends Controller {
}
async analyseDownload() {
const { ctx, service } = this;
const input_params = ctx.request.query;
const rule = {
start_date: {
required: true,
type: 'string',
format: /^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$|^[1-9]\d{3}-(0[1-9]|1[0-2])$/,
message: '日期格式错误',
},
end_date: {
required: true,
type: 'string',
format: /^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$|^[1-9]\d{3}-(0[1-9]|1[0-2])$/,
message: '日期格式错误',
},
product: {
required: false,
type: 'string'
},
}
ctx.validate(rule, input_params);
if (input_params.start_date > input_params.end_date) {
ctx.throw(400, '起始时间不能大于结束时间');
}
const analyse_Buffer = await service.userDetail.analyseDownload(input_params);
ctx.body = analyse_Buffer;
}
}
......
'use strict';
const Service = require('egg').Service;
const nodeExcel = require('node-xlsx');
const urlencode = require('urlencode');
const moment = require('moment');
class UserDetailService extends Service {
......@@ -101,21 +104,73 @@ class UserDetailService extends Service {
} else {
listMap.set(detail[i].day, { money: parseFloat(detail[i].cost), amount: detail[i].pull_volume });
}
ret.overview.use_money += parseFloat(detail[i].cost);
ret.overview.use_money += Number(parseFloat(detail[i].cost).toFixed(2));
ret.overview.use_amount += detail[i].pull_volume;
if (listMap.size > 0) {
listMap.forEach((value, key) => {
ret.list.push({
date: key,
money: Number((value.money).toFixed(2)),
amount: Number((value.amount).toFixed(0))
})
}
if (listMap.size > 0) {
listMap.forEach((value, key) => {
ret.list.push({
date: key,
money: Number((value.money).toFixed(2)),
amount: Number((value.amount).toFixed(0))
})
})
}
}
return ret;
}
async analyseDownload({ start_date, end_date, product }) {
const { ctx } = this;
const user_id = ctx.userId;
let buffer = nodeExcel.build([{ name: 'sheet1', data: [[]] }]);
let where = { user_id };
if (product) {
ret.product = product;
where.service = product;
}
const appKeys = await ctx.yizhiModel.UserService.findAll({
attributes: ['service', 'appkey'],
where
});
if (appKeys) {
const appList = [];
appKeys.forEach((value, index) => {
if (!appList.includes(value.appkey)) {
appList.push(value.appkey);
}
});
where = {
user_id: ctx.userId,
appKey: appList,
day: {
$lte: end_date,
$gte: start_date
},
};
const detail = await ctx.dataModel.YzbillCost.findAll({
attributes: ['appKey', 'service', 'day', 'query_volumn', 'pull_volume', 'price', 'cost'],
where,
order: [['day', 'desc']]
})
let data = ['appKey', '产品名称', '日期', '查询量(请求量)', '产品调用量(结果拉取量)', '产品单价', '消耗金额'];
for (let i = 0; i < detail.length; i++) {
data.push([detail[i].appKey, detail[i].service, detail[i].day, detail[i].query_volumn, detail[i].pull_volume, detail[i].price, detail[i].cost])
}
return ret;
buffer = nodeExcel.build([{ name: 'sheet1', data }]);
}
ctx.set('Content-Type', 'application/octet-stream');
let name = urlencode(moment(new Date()).format('YYYY-MM-DD') + '.xlsx', "utf-8");
ctx.set("Content-Disposition", "attachment; filename* = UTF-8''" + name);
return buffer;
}
}
......
......@@ -15,7 +15,9 @@
"egg-validate": "^2.0.1",
"moment": "^2.22.2",
"mysql2": "^1.6.1",
"node-cache": "^4.2.0"
"node-cache": "^4.2.0",
"node-xlsx": "^0.15.0",
"urlencode": "^1.1.0"
},
"devDependencies": {
"autod": "^3.0.1",
......
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