lrf 2 年之前
父節點
當前提交
dd82bf03a5
共有 3 個文件被更改,包括 92 次插入3 次删除
  1. 28 0
      app/controller/statistics/admin.js
  2. 54 2
      app/service/statistics/admin.js
  3. 10 1
      app/z_router/statistics/admin.js

+ 28 - 0
app/controller/statistics/admin.js

@@ -12,5 +12,33 @@ class AdminController extends Controller {
     const res = await this.service.todo(this.ctx.query);
     const res = await this.service.todo(this.ctx.query);
     this.ctx.ok({ data: res });
     this.ctx.ok({ data: res });
   }
   }
+  async stockWarning() {
+    const res = await this.service.stockWarning(this.ctx.query);
+    this.ctx.ok({ data: res });
+  }
+  async notDealAfterSale() {
+    const res = await this.service.notDealAfterSale(this.ctx.query);
+    this.ctx.ok({ data: res });
+  }
+  async notSend() {
+    const res = await this.service.notSend(this.ctx.query);
+    this.ctx.ok({ data: res });
+  }
+  async notPay() {
+    const res = await this.service.notPay(this.ctx.query);
+    this.ctx.ok({ data: res });
+  }
+  async makeOrder() {
+    const res = await this.service.makeOrder(this.ctx.query);
+    this.ctx.ok({ data: res });
+  }
+  async makeAfterSale() {
+    const res = await this.service.makeAfterSale(this.ctx.query);
+    this.ctx.ok({ data: res });
+  }
+  async sellTotal() {
+    const res = await this.service.sellTotal(this.ctx.query);
+    this.ctx.ok({ data: res });
+  }
 }
 }
 module.exports = CrudController(AdminController, {});
 module.exports = CrudController(AdminController, {});

+ 54 - 2
app/service/statistics/admin.js

@@ -111,10 +111,11 @@ class AdminService extends CrudService {
    * @property {Array} start 开始
    * @property {Array} start 开始
    * @property {Array} end 结束
    * @property {Array} end 结束
    * @property {Array} range 指定的时间范围;如果没传,则根据type自动生成
    * @property {Array} range 指定的时间范围;如果没传,则根据type自动生成
+   * @property {String} status 订单状态(1为,支付单)
    */
    */
   async makeOrder(query) {
   async makeOrder(query) {
     const pipline = [];
     const pipline = [];
-    const { type = 'week', start, end, shop } = query;
+    const { type = 'week', start, end, shop, status } = query;
     let timeRange = [];
     let timeRange = [];
     if (type === 'custom') {
     if (type === 'custom') {
       const r = moment(start).isBefore(end);
       const r = moment(start).isBefore(end);
@@ -125,6 +126,9 @@ class AdminService extends CrudService {
     if (ObjectId.isValid(shop)) {
     if (ObjectId.isValid(shop)) {
       pipline.push({ $match: { shop } });
       pipline.push({ $match: { shop } });
     }
     }
+    if (status) {
+      pipline.push({ $match: { status } });
+    }
     // 时间格式化: create_date:日期,用来分组统计; create_time: 时间,用来过滤数据
     // 时间格式化: create_date:日期,用来分组统计; create_time: 时间,用来过滤数据
     pipline.push({
     pipline.push({
       $project: {
       $project: {
@@ -157,7 +161,7 @@ class AdminService extends CrudService {
    */
    */
   async makeAfterSale(query) {
   async makeAfterSale(query) {
     const pipline = [];
     const pipline = [];
-    const { type = 'week', start, end, shop } = query;
+    const { type = 'week', start, end, shop, status } = query;
     let timeRange = [];
     let timeRange = [];
     if (type === 'custom') {
     if (type === 'custom') {
       const r = moment(start).isBefore(end);
       const r = moment(start).isBefore(end);
@@ -168,6 +172,9 @@ class AdminService extends CrudService {
     if (ObjectId.isValid(shop)) {
     if (ObjectId.isValid(shop)) {
       pipline.push({ $match: { shop } });
       pipline.push({ $match: { shop } });
     }
     }
+    if (status) {
+      pipline.push({ $match: { status } });
+    }
     // 时间格式化: create_date:日期,用来分组统计; create_time: 时间,用来过滤数据
     // 时间格式化: create_date:日期,用来分组统计; create_time: 时间,用来过滤数据
     pipline.push({
     pipline.push({
       $project: {
       $project: {
@@ -194,6 +201,51 @@ class AdminService extends CrudService {
     return result;
     return result;
   }
   }
 
 
+  /**
+   * 查询销售额
+   * @param {Object} query 查询条件
+   */
+  async sellTotal(query) {
+    const pipline = [];
+    const { type = 'week', start, end, shop } = query;
+    let timeRange = [];
+    if (type === 'custom') {
+      const r = moment(start).isBefore(end);
+      if (r) timeRange.push(start, end);
+      else timeRange.push(end, start);
+    } else timeRange = this.getDefaultRange(type);
+    // 先用商店过滤下
+    if (ObjectId.isValid(shop)) {
+      pipline.push({ $match: { shop } });
+    }
+    pipline.push({ $match: { status: '1' } });
+    // 时间格式化: create_date:日期,用来分组统计; create_time: 时间,用来过滤数据
+    pipline.push({
+      $project: {
+        create_date: { $dateToString: { date: '$meta.createdAt', ...this.pipDateFormatDateObject() } },
+        create_time: { $dateToString: { date: '$meta.createdAt', ...this.pipDateFormatTimeObject() } },
+        money: '$pay.pay_money',
+      },
+    });
+    pipline.push({ $match: { $and: [{ create_time: { $gte: _.head(timeRange) } }, { create_time: { $lte: _.last(timeRange) } }] } });
+    pipline.push({
+      $group: {
+        _id: '$create_date',
+        money: { $sum: '$money' },
+      },
+    });
+    const fs = await this.orderModel.aggregate(pipline);
+    const list = this.getRangeDateList(timeRange);
+    const result = [];
+    for (const d of list) {
+      const r = fs.find(f => f._id === d);
+      const obj = { date: d, money: 0 };
+      if (r) obj.money = r.money;
+      result.push(obj);
+    }
+    return result;
+  }
+
   // 聚合总数管道
   // 聚合总数管道
   totalPip() {
   totalPip() {
     return {
     return {

+ 10 - 1
app/z_router/statistics/admin.js

@@ -6,7 +6,16 @@ const routerRegister = require(regPath);
 const rkey = 'statistics/admin';
 const rkey = 'statistics/admin';
 const ckey = 'statistics.admin';
 const ckey = 'statistics.admin';
 const keyZh = '中台统计';
 const keyZh = '中台统计';
-const routes = [{ method: 'get', path: `${rkey}/todo`, controller: `${ckey}.todo`, name: `${ckey}Todo`, zh: `${keyZh}待办查询` }];
+const routes = [
+  { method: 'get', path: `${rkey}/todo`, controller: `${ckey}.todo`, name: `${ckey}Todo`, zh: `${keyZh}待办查询` },
+  { method: 'get', path: `${rkey}/stockWarning`, controller: `${ckey}.stockWarning`, name: `${ckey}stockWarning`, zh: `${keyZh}库存警告` },
+  { method: 'get', path: `${rkey}/notDealAfterSale`, controller: `${ckey}.notDealAfterSale`, name: `${ckey}notDealAfterSale`, zh: `${keyZh}未处理售后` },
+  { method: 'get', path: `${rkey}/notSend`, controller: `${ckey}.notSend`, name: `${ckey}notSend`, zh: `${keyZh}未发货` },
+  { method: 'get', path: `${rkey}/notPay`, controller: `${ckey}.notPay`, name: `${ckey}notPay`, zh: `${keyZh}未付款订单数` },
+  { method: 'get', path: `${rkey}/makeOrder`, controller: `${ckey}.makeOrder`, name: `${ckey}makeOrder`, zh: `${keyZh}统计支付订单` },
+  { method: 'get', path: `${rkey}/makeAfterSale`, controller: `${ckey}.makeAfterSale`, name: `${ckey}makeAfterSale`, zh: `${keyZh}售后数` },
+  { method: 'get', path: `${rkey}/sellTotal`, controller: `${ckey}.sellTotal`, name: `${ckey}sellTotal`, zh: `${keyZh}查询销售额` },
+];
 
 
 module.exports = app => {
 module.exports = app => {
   routerRegister(app, routes, keyZh, rkey, ckey);
   routerRegister(app, routes, keyZh, rkey, ckey);