Commit b29eb37e authored by 姜登's avatar 姜登

analyseInfo

parent 6d0b8de5
Pipeline #22013 passed with stage
in 2 seconds
...@@ -15,7 +15,32 @@ class AccountController extends Controller { ...@@ -15,7 +15,32 @@ class AccountController extends Controller {
} }
async analyse() { async analyse() {
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 (start_date > end_date) {
ctx.throw(400, '起始时间不能大于结束时间');
}
const analyse_info = await service.userDetail.analyseInfo(input_params);
ctx.body = analyse_info;
} }
async analyseDownload() { async analyseDownload() {
......
...@@ -6,7 +6,10 @@ module.exports = app => { ...@@ -6,7 +6,10 @@ module.exports = app => {
const billCostMonth = app.dataModel.define('yzbillCostMonth', { const billCostMonth = app.dataModel.define('yzbillCostMonth', {
appKey: { type: INTEGER, primaryKey: true }, appKey: { type: INTEGER, primaryKey: true },
service: STRING, service: STRING,
day_month: STRING, day: {
type: STRING,
field:'day_month'
},
query_volume: INTEGER, query_volume: INTEGER,
succ_volume: INTEGER, succ_volume: INTEGER,
pull_volume: INTEGER, pull_volume: INTEGER,
......
...@@ -111,15 +111,15 @@ class UserService extends Service { ...@@ -111,15 +111,15 @@ class UserService extends Service {
], ],
where: { user_id, day: last_day }, where: { user_id, day: last_day },
}) })
lastday_info.use_amount = lastday_data && lastday_data.amount ? Number((lastday_data.amount).toFixed(2)) : 0; lastday_info.use_amount = lastday_data && lastday_data.amount ? Number(parseFloat(lastday_data.amount).toFixed(2)) : 0;
lastday_info.use_money = lastday_data && lastday_data.cost ? Number((lastday_data.cost).toFixed(2)) : 0; lastday_info.use_money = lastday_data && lastday_data.cost ? Number(parseFloat(lastday_data.cost).toFixed(2)) : 0;
const left_money = await ctx.dataModel.YzbillBalance.findOne({ const left_money = await ctx.dataModel.YzbillBalance.findOne({
attributes: ['balance_fn'], attributes: ['balance_fn'],
where: { user_id, month: last_month }, where: { user_id, month: last_month },
}) })
lastday_info.left_money = left_money && left_money.balance_fn ? Number((left_money.balance_fn).toFixed(2)) : 0; lastday_info.left_money = left_money && left_money.balance_fn ? Number(parseFloat(left_money.balance_fn).toFixed(2)) : 0;
const detail = [] const detail = []
const bill = await ctx.dataModel.YzbillBalance.findAll({ const bill = await ctx.dataModel.YzbillBalance.findAll({
...@@ -131,10 +131,10 @@ class UserService extends Service { ...@@ -131,10 +131,10 @@ class UserService extends Service {
detail.push({ detail.push({
name: bill[i].company, name: bill[i].company,
date: bill[i].month, date: bill[i].month,
start_left: Number((bill[i].balance_bg).toFixed(2)), start_left: Number(parseFloat(bill[i].balance_bg).toFixed(2)),
middle_recharge: Number((bill[i].recharge_m).toFixed(2)), middle_recharge: Number(parseFloat(bill[i].recharge_m).toFixed(2)),
middlefee: Number((bill[i].cost_m).toFixed(2)), middlefee: Number(parseFloat(bill[i].cost_m).toFixed(2)),
endfee: Number((bill[i].balance_fn).toFixed(2)), endfee: Number(parseFloat(bill[i].balance_fn).toFixed(2)),
}) })
} }
......
...@@ -40,6 +40,81 @@ class UserDetailService extends Service { ...@@ -40,6 +40,81 @@ class UserDetailService extends Service {
return ret.get('total'); return ret.get('total');
} }
async analyseInfo({ start_date, end_date, product }) {
const { ctx } = this;
const ret = {
product: 'all',
overview: {
use_money: 0,
use_amount: 0,
},
list: []
}
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);
}
});
let type = 'YzbillCost';
if (start_date.length === '7') {
type = 'YzbillCostMonth';
}
where = {
user_id: ctx.userId,
appKey: appList,
day: {
$lte: end_date,
$gte: start_date
},
};
const ret = await ctx.dataModel[type].findAll({
attributes: ['day', 'pull_volume', 'cost'],
where,
order: [['day', 'desc']]
})
const listMap = new Map();
for (let i = 0; i < ret.length; i++) {
if (listMap.has(ret[i].day)) {
let obj = listMap.get(ret[i].day);
obj.money += parseFloat(ret[i].cost);
obj.amount += ret[i].pull_volume;
listMap.set(ret[i].day, obj);
} else {
listMap.set(ret[i].day, { money: parseFloat(ret[i].cost), amount: ret[i].pull_volume });
}
ret.overview.use_money += parseFloat(ret[i].cost);
ret.overview.use_amount += ret[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))
})
})
}
}
return ret;
}
}
} }
module.exports = UserDetailService; module.exports = UserDetailService;
...@@ -21,7 +21,8 @@ module.exports = () => { ...@@ -21,7 +21,8 @@ module.exports = () => {
username: 'hexin', username: 'hexin',
password: 'gYUHszn9#q', password: 'gYUHszn9#q',
port: 3306, port: 3306,
}], },
],
}; };
config.redis = { config.redis = {
......
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