lrf402788946 4 gadi atpakaļ
vecāks
revīzija
7bf33229c4
4 mainītis faili ar 189 papildinājumiem un 0 dzēšanām
  1. 51 0
      app/controller/statistics.js
  2. 1 0
      app/router.js
  3. 12 0
      app/router/statistics.js
  4. 125 0
      app/service/statistics.js

+ 51 - 0
app/controller/statistics.js

@@ -0,0 +1,51 @@
+'use strict';
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 首页-数据动态统计
+class StatisticsController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.statistics;
+  }
+  async index() {
+    const patent = await this.service.patent(this.ctx.query);
+    const product = await this.service.product(this.ctx.query);
+    const users = await this.service.users(this.ctx.query);
+    const data = await this.service.data(this.ctx.query);
+    this.ctx.ok({ data: { patent, product, users, data } });
+  }
+
+  /**
+   * 首页专利统计
+   */
+  async patent() {
+    const data = await this.service.patent(this.ctx.query);
+    this.ctx.ok({ data });
+  }
+
+  /**
+   * 首页成果统计
+   */
+  async product() {
+    const data = await this.service.product(this.ctx.query);
+    this.ctx.ok({ data });
+  }
+
+  /**
+   * 首页用户统计
+   */
+  async users() {
+    const data = await this.service.users(this.ctx.query);
+    this.ctx.ok({ data });
+  }
+  /**
+   * 首页数据统计
+   */
+  async data() {
+    const data = await this.service.data(this.ctx.query);
+    this.ctx.ok({ data });
+  }
+
+}
+module.exports = CrudController(StatisticsController, {});

+ 1 - 0
app/router.js

@@ -20,4 +20,5 @@ module.exports = app => {
   require('./router/policy')(app); // 高企服务
   require('./router/ticket')(app); // 创新券
   require('./router/ticket_record')(app); // 创新券
+  require('./router/statistics')(app); // 统计
 };

+ 12 - 0
app/router/statistics.js

@@ -0,0 +1,12 @@
+'use strict';
+
+
+module.exports = app => {
+  const { router, controller } = app;
+  const prefix = 'statistics';
+  router.get(prefix, `/${prefix}/index`, controller[prefix].index);
+  router.get(prefix, `/${prefix}/patent`, controller[prefix].patent);
+  router.get(prefix, `/${prefix}/product`, controller[prefix].product);
+  router.get(prefix, `/${prefix}/users`, controller[prefix].users);
+  router.get(prefix, `/${prefix}/data`, controller[prefix].data);
+};

+ 125 - 0
app/service/statistics.js

@@ -0,0 +1,125 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const { ObjectId } = require('mongoose').Types;
+const _ = require('lodash');
+const assert = require('assert');
+
+// 首页-数据动态统计
+class IndexService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'index');
+    this.redis = this.app.redis;
+    this.patentModel = this.ctx.model.Patent; // 专利
+    this.code = this.ctx.model.Code; // 字典
+    this.productModel = this.ctx.model.Product; // 产品
+    this.expertModel = this.ctx.model.Expert; // 专家
+    this.mechanismModel = this.ctx.model.Mechanism;// 机构
+    this.orgModel = this.ctx.model.Organization; // 企业
+    this.policyModel = this.ctx.model.Policy; // 高企政策
+    this.ticketModel = this.ctx.model.Ticket; // 创新券
+  }
+  /**
+   * 首页专利统计
+   */
+  async patent() {
+    const res = await this.patentModel.aggregate([
+      {
+        $group: {
+          _id: '$apply_personal',
+          value: { $sum: 1 },
+        },
+      },
+    ]);
+    let arr = [];
+    const other = { name: '其他', value: 0 };
+    for (const i of res) {
+      const { _id, value } = i;
+      const unitList = this.patentUnitList();
+      const unit = unitList.find(
+        f => (_id && _id.includes(f.name)) || f.name.includes(_id)
+      );
+      if (unit) {
+        // 说明是需要单拎出来的数据,现查arr中是否有该单位:有=>数字合并;没有=>创建条目
+        const { name } = unit;
+        const arrItem = arr.find(f => f.name === name);
+        if (arrItem) {
+          const index = arr.findIndex(f => f.name === name);
+          arr[index] = { name, value: (arrItem.value || 0) + value };
+        } else {
+          arr.push({ name, value });
+        }
+      } else {
+        other.value += value;
+      }
+    }
+    arr = _.orderBy(arr, [ 'value' ], [ 'desc' ]);
+    arr.push(other);
+    return arr;
+  }
+
+  /**
+   * 首页成果统计
+   */
+  async product() {
+    const categroy = await this.code.find({ category: '01' });
+    const condition = { type: '1', status: '2' };
+    const arr = [];
+    for (const c of categroy) {
+      const { name } = c;
+      const value = await this.productModel.count({
+        ...condition,
+        field: name,
+      });
+      arr.push({ value, name });
+    }
+    return arr;
+  }
+
+  /**
+   * 统计用户(企业,专家,机构)数量
+   */
+  async users() {
+    const org = await this.orgModel.count({ status: '1' });
+    const expert = await this.expertModel.count({ status: '1' });
+    const mech = await this.mechanismModel.count({ status: '1' });
+    return { organization: org, expert, mechanism: mech };
+  }
+  /**
+   * 统计各种数据
+   */
+  async data() {
+    // 高企政策服务通过数量 1
+    const policy = await this.policyModel.count({ status: '1' });
+    // 创新券领取成果数量 4
+    const ticket = await this.ticketModel.count({ status: '4' });
+    // 科技成果成功的数量 8
+    const product = await this.productModel.count({ status: '6' });
+    return { policy, ticket, product };
+  }
+
+  /**
+   * 专利单位列表
+   */
+  patentUnitList() {
+    return [
+      { name: '吉林大学' },
+      { name: '长春理工大学' },
+      { name: '东北电力大学' },
+      { name: '吉林工程技术师范学院' },
+      { name: '长春工业大学' },
+      { name: '北华大学' },
+      { name: '吉林农业大学' },
+      { name: '吉林师范大学' },
+      { name: '长春工程学院' },
+      { name: '吉林建筑大学' },
+      { name: '通化师范学院' },
+      { name: '长春大学' },
+      { name: '东北师范大学' },
+      { name: '吉林农业科技学院' },
+    ];
+  }
+
+}
+
+module.exports = IndexService;