liuyu 4 years ago
parent
commit
e7d1c071fa
3 changed files with 49 additions and 11 deletions
  1. 9 3
      app/controller/.job.js
  2. 2 0
      app/model/job.js
  3. 38 8
      app/service/school.js

+ 9 - 3
app/controller/.job.js

@@ -11,7 +11,9 @@ module.exports = {
       'plannum',
       'schnum',
       'isstore',
-      'createtime'
+      'createtime',
+      'type',
+      'reason',
     ]
   },
   destroy: {
@@ -31,7 +33,9 @@ module.exports = {
       'plannum',
       'schnum',
       'isstore',
-      'createtime'
+      'createtime',
+      'type',
+      'reason',
     ]
   },
   show: {
@@ -53,7 +57,9 @@ module.exports = {
         plannum: 'plannum',
         schnum: 'schnum',
         isstore: 'isstore',
-        createtime: 'createtime'
+        createtime: 'createtime',
+        type: 'type',
+        reason: 'reason',
       }
     },
     service: 'query',

+ 2 - 0
app/model/job.js

@@ -15,6 +15,8 @@ const JobSchema = {
   schnum: { type: String, required: false, maxLength: 20 }, // 学校上传人数
   isstore: { type: String, required: false, maxLength: 20 }, // 是否入库 0、不入库1、入库
   createtime: { type: String, required: false, maxLength: 200 }, // 上传时间
+  reason: { type: String, required: false }, // 原因
+  type: { type: String, required: false, maxLength: 200 }, // 类型 0、普通 1、特殊
 };
 
 const schema = new Schema(JobSchema, { toJSON: { virtuals: true } });

+ 38 - 8
app/service/school.js

@@ -17,6 +17,7 @@ class SchoolService extends CrudService {
     this.umodel = this.ctx.model.User;
     this.tmodel = this.ctx.model.Trainplan;
     this.jmodel = this.ctx.model.Job;
+    this.schmodel = this.ctx.model.Schtime;
   }
 
   async stuimport(data) {
@@ -29,12 +30,8 @@ class SchoolService extends CrudService {
     if (!plan) {
       throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '计划信息不存在');
     }
-    const schs = await plan.school;
-    const sch_ = _.find(schs, { code: schid });
-    let num_ = '0';
-    if (sch_) {
-      num_ = sch_.num;
-    }
+    // 取得学校预计人数
+    const num_ = this.getschnum(plan, type, schid, termid);
     const planid = plan.id;
     const planyearid = plan.planyearid;
     // 取得excle中数据
@@ -58,11 +55,11 @@ class SchoolService extends CrudService {
     }
     const nowtime = moment().locale('zh-cn').format('YYYY-MM-DD HH:mm:ss');
     if (studatas.length > Number(num_)) {
-      const jobdata = { code: schid, name: schname, planid: plan.id, termid, term: trem_.term, filepath, studs: JSON.stringify(studatas), plannum: num_, schnum: studatas.length, isstore: '0', createtime: nowtime };
+      const jobdata = { code: schid, name: schname, planid: plan.id, termid, term: trem_.term, filepath, studs: JSON.stringify(studatas), plannum: num_, schnum: studatas.length, isstore: '0', createtime: nowtime, type, reason: '学校上传人数超过预期人数' };
       await this.jmodel.create(jobdata);
       throw new BusinessError(ErrorCode.SERVICE_FAULT, '学校上传人数超过预期人数');
     } else if (studatas.length < Number(num_)) {
-      const jobdata = { code: schid, name: schname, planid: plan.id, termid, term: trem_.term, filepath, studs: JSON.stringify(studatas), plannum: num_, schnum: studatas.length, isstore: '0', createtime: nowtime };
+      const jobdata = { code: schid, name: schname, planid: plan.id, termid, term: trem_.term, filepath, studs: JSON.stringify(studatas), plannum: num_, schnum: studatas.length, isstore: '0', createtime: nowtime, type, reason: '学校上传人数少于预期人数' };
       await this.jmodel.create(jobdata);
       throw new BusinessError(ErrorCode.SERVICE_FAULT, '学校上传人数少于预期人数');
     }
@@ -78,6 +75,39 @@ class SchoolService extends CrudService {
     return datacheck;
   }
 
+  // 取得学校预计人数
+  async getschnum(plan, type, schid, termid) {
+    const schtime = await this.schmodel.findOne({ schid, planid: plan.id });
+    let { arrange } = schtime;
+    const { termnum } = plan;
+    arrange = _.groupBy(arrange, 'term');
+    const keys = Object.keys(arrange);
+    let arr = keys.map(key => {
+      const rt = termnum.find(f => f.term === key);
+      let ar = arrange[key];
+      ar = ar.map(a => {
+        const rb = rt.batchnum.find(f => f.batch === a.batch);
+        if (rb) {
+          const bh = _.head(rb.class);
+          const { type } = bh;
+          a.type = type;
+          return a;
+        }
+      });
+      let garr = _.groupBy(ar, 'type');
+      const gks = Object.keys(garr);
+      garr = gks.map(gk => {
+        const { term, termid } = _.head(garr[gk]);
+        const number = garr[gk].reduce((p, n) => p + n.number * 1, 0);
+        return { term, termid, number, type: gk };
+      });
+      return garr;
+    });
+    arr = arr.flat();
+    const obj_ = _.find(arr, { termid, type });
+    return obj_.number;
+  }
+
   // 获取导入的XLSX文件中的数据
   async getImportXLSXData(filepath, termid, schid, planid, planyearid, type) {
     console.log(filepath);