Commit 8d0d2d5b by linboxuan

ucommune-back operate api

parent 72212885
const system = require("../../../system"); const system = require("../../../system");
const ServiceBase = require("../../sve.base"); const ServiceBase = require("../../sve.base");
var moment = require('moment')
class OrderInfoService extends ServiceBase { class OrderInfoService extends ServiceBase {
constructor() { constructor() {
super("dbcorder", ServiceBase.getDaoName(OrderInfoService)); super("dbcorder", ServiceBase.getDaoName(OrderInfoService));
...@@ -53,35 +54,126 @@ class OrderInfoService extends ServiceBase { ...@@ -53,35 +54,126 @@ class OrderInfoService extends ServiceBase {
} }
async getStatistics(pobj, actionBody) {// uk运营后台 业务统计 // ----------------------------------------------uk运营后台 开始
var needsCount = await this.needInfoDao.findCount({where:{uapp_id:actionBody.uappId}});// 需求量 // uk运营后台 业务统计
async getStatistics(pobj, actionBody) {
var sql = "SELECT DISTINCT need.needNo,count(1) as count " // 需求量sql 查询关于渠道的需求订单
var needsCountSql = "select count(1) as count from n_need_info where uapp_id= " + actionBody.uappId
// 转换率sql 查询需求表并且有需求方案
var conversionRateListSql = "SELECT DISTINCT need.needNo,count(1) as count "
+ " FROM n_need_info AS need LEFT JOIN n_need_solution AS solution ON need.needNo = solution.needNo " + " FROM n_need_info AS need LEFT JOIN n_need_solution AS solution ON need.needNo = solution.needNo "
+ " where solution.orderNo is not null and need.uapp_id = " + actionBody.uappId; + " where solution.orderNo is not null and need.uapp_id = " + actionBody.uappId;
var conversionRateList = await this.needInfoDao.customQuery(sql);
if(conversionRateList.length != 1) {
conversionRate = 0;
}
var conversionRate = (conversionRateList[0]["count"] / needsCount).toFixed(2);// 需求订单转化为订单的数量 保留两位
// 订单相关sql 查询订单量/订单交易额/公司宝结算额/渠道毛利
var payObjSql = "select sum(totalSum) as totalSum,sum(pfProfitSum) as parentProfit,sum(channelProfitSum) as grossProfit, count(1) as count "
+ " from c_order_info where uapp_id=" + actionBody.uappId + " and orderStatus!=1";
var sql = "select sum(totalSum) as totalSum,sum(pfProfitSum) as parentProfit,sum(channelProfitSum) as grossProfit, count(1) as count " // 整合起始时间/截止时间
+ " from c_order_info where uapp_id=" + actionBody.uappId + " and orderStatus=2"; var paramWhere = {}
var payObj = await this.customQuery(sql);// 订单量/交易总额/公司宝结算额/对方毛利 if(actionBody.startTime && actionBody.entTime) {
if(payObj.length != 1) { var startTime = actionBody.startTime.trim() + " 00:00:00";
var entTime = actionBody.entTime + " 23:59:59";
needsCountSql += " and updated_at >=:startTime and updated_at<=:entTime";// 关于需求用了uupdate_at,订单用的created_at
conversionRateListSql += " and need.updated_at >=:startTime and need.updated_at<=:entTime";
payObjSql += " and created_at >=:startTime and created_at<=:entTime";
paramWhere.startTime = startTime;
paramWhere.entTime = entTime;
}
// 需求量
var needsCountList = await this.needInfoDao.customQuery(needsCountSql,paramWhere);
var needsCount = needsCountList[0]["count"];
if(needsCountList.length != 0) {
needsCount = 0;
}
// 需求订单转化为订单的数量 保留两位 成为订单的需求 / 需求数量
var conversionRateList = await this.needInfoDao.customQuery(conversionRateListSql,paramWhere);
var conversionRate = (conversionRateList[0]["count"] / needsCount).toFixed(2);
if(conversionRateList.length != 1 || conversionRateList[0]["count"] == 0) {
conversionRate = 0;
} }
// 订单量/交易总额/公司宝结算额/对方毛利
var payObj = await this.customQuery(payObjSql,paramWhere);
payObj = payObj[0];
let resultObj = { let resultObj = {
needsCount: needsCount, needsCount: needsCount,
payCount:payObj[0].count, payCount: payObj.count ? payObj.count : 0,
conversionRate: conversionRate, conversionRate: conversionRate,
paySumCount: payObj[0].totalSum, paySumCount: payObj.totalSum ? payObj.totalSum : 0,
parentProfit: payObj[0].parentProfit, parentProfit: payObj.parentProfit ? payObj.parentProfit : 0,
grossProfit: payObj[0].grossProfit grossProfit: payObj.grossProfit ? payObj.grossProfit : 0
}
var result = system.getResultSuccess(resultObj);
return result;
}
// 趋势分析
async getTrend(pobj, actionBody) {
var startTime = moment().subtract(1, "years").format("YYYY-MM-DD HH:mm:ss");
var entTime = moment().format("YYYY-MM-DD HH:mm:ss");
// date
var monthList = [];
for(var i = 11;i >= 0;i--) {
monthList.push(moment().subtract(i, "month").format("YYYY-MM"))
}
var paramWhere = {
uappId:actionBody.uappId,
startTime: startTime,
entTime: entTime
}
// 根据月份分组需求订单
var needListSql = "SELECT DATE_FORMAT(created_at, '%Y-%m') AS month,count(*) as count "
+ " FROM n_need_info where uapp_id=:uappId and created_at >=:startTime and created_at<=:entTime GROUP BY month"
var needList = await this.needInfoDao.customQuery(needListSql,paramWhere);
// 根据月份分组支付订单
var orderListSql = "SELECT DATE_FORMAT(created_at, '%Y-%m') AS month,count(*) as count "
+ " FROM c_order_info where orderStatus=2 and uapp_id=:uappId and created_at >=:startTime and created_at<=:entTime GROUP BY month"
var orderList = await this.customQuery(orderListSql,paramWhere);
// 处理数据
for(var i =0;i < monthList.length;i++) {
var needMonthList = []
for(var j = 0;j < needList.length;j++) {
needMonthList.push(needList[j]["month"])
}
if(needMonthList.indexOf(monthList[i]) < 0) {
var obj = {
month: monthList[i],
count:0
}
needList.push(obj)
}
var orderMonthList = []
for(var j = 0;j < orderList.length;j++) {
orderMonthList.push(orderList[j]["month"])
}
if(orderMonthList.indexOf(monthList[i]) < 0) {
var obj = {
month: monthList[i],
count:0
}
orderList.push(obj)
}
}
function sortByKey(array, key) {
return array.sort(function(a, b) {
var x = a[key]; var y = b[key];
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}
orderList = sortByKey(orderList,'month')
needList = sortByKey(needList,'month')
let resultObj = {
date: monthList,
pay: orderList,
need: needList
} }
var result = system.getResultSuccess(resultObj); var result = system.getResultSuccess(resultObj);
return result; return result;
} }
// ----------------------------------------------uk运营后台 结束
} }
module.exports = OrderInfoService; module.exports = OrderInfoService;
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