Procházet zdrojové kódy

Merge branch 'master' of http://git.cc-lotus.info/new_train/service-center

cheny před 4 roky
rodič
revize
2d0f9a4cee

+ 73 - 0
app/controller/.job.js

@@ -0,0 +1,73 @@
+module.exports = {
+  create: {
+    requestBody: [
+      'code',
+      'name',
+      'planid',
+      'termid',
+      'term',
+      'filepath',
+      'studs',
+      'plannum',
+      'schnum',
+      'isstore',
+      'createtime',
+      'type',
+      'reason',
+    ]
+  },
+  destroy: {
+    params: ['!id'],
+    service: 'delete'
+  },
+  update: {
+    params: ['!id'],
+    requestBody: [
+      'code',
+      'name',
+      'planid',
+      'termid',
+      'term',
+      'filepath',
+      'studs',
+      'plannum',
+      'schnum',
+      'isstore',
+      'createtime',
+      'type',
+      'reason',
+    ]
+  },
+  show: {
+    parameters: {
+      params: ['!id']
+    },
+    service: 'fetch'
+  },
+  index: {
+    parameters: {
+      query: {
+        code: 'code',
+        name: 'name',
+        planid: 'planid',
+        termid: 'termid',
+        term: 'term',
+        filepath: 'filepath',
+        studs: 'studs',
+        plannum: 'plannum',
+        schnum: 'schnum',
+        isstore: 'isstore',
+        createtime: 'createtime',
+        type: 'type',
+        reason: 'reason',
+      }
+    },
+    service: 'query',
+    options: {
+      query: ['skip', 'limit'],
+      sort: ['meta.createdAt'],
+      desc: true,
+      count: true
+    }
+  },
+};

+ 3 - 0
app/controller/.student.js

@@ -34,6 +34,7 @@ module.exports = {
       "score",
       "diy",
       "isComming",
+      "type",
     ],
   },
   destroy: {
@@ -76,6 +77,7 @@ module.exports = {
       "score",
       "diy",
       "isComming",
+      "type",
     ],
   },
   show: {
@@ -120,6 +122,7 @@ module.exports = {
         score: "score",
         diy: "diy",
         isComming:"isComming",
+        type: "type",
       },
     },
     service: "query",

+ 18 - 0
app/controller/job.js

@@ -0,0 +1,18 @@
+'use strict';
+
+const _ = require('lodash');
+const meta = require('./.job.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 任务表管理
+class JobController extends Controller {
+
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.job;
+  }
+
+}
+
+module.exports = CrudController(JobController, meta);

+ 5 - 0
app/controller/student.js

@@ -56,6 +56,11 @@ class StudentController extends Controller {
     const data = await this.service.deletestus(this.ctx.request.body);
     this.ctx.ok({ data });
   }
+
+  async updatabedroom() {
+    const data = await this.service.updatabedroom(this.ctx.request.body);
+    this.ctx.ok({ data });
+  }
 }
 
 module.exports = CrudController(StudentController, meta);

+ 29 - 0
app/model/job.js

@@ -0,0 +1,29 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+
+// 任务表
+const JobSchema = {
+  code: { type: String, required: false, maxLength: 200 }, // 学校代码
+  name: { type: String, required: false, maxLength: 500 }, // 学校名称
+  planid: { type: String, required: false, maxLength: 200 }, // 计划id
+  termid: { type: String, required: false, maxLength: 200 }, // 期id
+  term: { type: String, required: false, maxLength: 500 }, // 期名称
+  filepath: { type: String, required: false, maxLength: 500 }, // 文件地址
+  studs: { type: String, required: false }, // 学生数据
+  plannum: { type: String, required: false, maxLength: 20 }, // 计划人数
+  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 } });
+schema.index({ id: 1 });
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Job', schema, 'job');
+};

+ 1 - 0
app/model/student.js

@@ -37,6 +37,7 @@ const StudentSchema = {
   score: { type: String, required: false, maxLength: 200, zh: '总分' }, // 总分
   diy: { type: { Object }, required: false, zh: '自定义' }, // 自定义
   isComming: { type: String, default: '0', zh: '签到' }, // 是否签到0否,1是
+  type: { type: String, required: false, maxLength: 200, default: '0' }, // 类型:0-正常,1-特殊
 };
 
 

+ 6 - 0
app/router.js

@@ -53,6 +53,8 @@ module.exports = app => {
   router.post('student', '/api/train/student/deleteclass', controller.student.deleteclass); // 删除学生班级
   router.post('student', '/api/train/student/findbystuids', controller.student.findbystuids);
   router.post('student', '/api/train/student/deletestus', controller.student.deletestus); // 删除学生多条
+  router.post('student', '/api/train/student/updatabedroom', controller.student.updatabedroom); // 批量学生寝室号
+
 
   // 班主任表设置路由
   router.resources('headteacher', '/api/train/headteacher', controller.headteacher); // index、create、show、destroy
@@ -236,4 +238,8 @@ module.exports = app => {
   router.resources('classtype', '/api/train/classtype', controller.classtype); // index、create、show、destroy
   router.post('classtype', '/api/train/classtype/update/:id', controller.classtype.update);
 
+  // 学校上传任务表设置路由
+  router.resources('job', '/api/train/job', controller.job); // index、create、show、destroy
+  router.post('job', '/api/train/job/update/:id', controller.job.update);
+
 };

+ 5 - 2
app/service/bedroom.js

@@ -15,6 +15,7 @@ class BedroomService extends CrudService {
     this.tmodel = this.ctx.model.Trainplan;
     this.cmodel = this.ctx.model.Class;
     this.umodel = this.ctx.model.User;
+    this.ctmodel = this.ctx.model.Classtype;
 
   }
 
@@ -75,7 +76,7 @@ class BedroomService extends CrudService {
       console.log(bedroom.code);
       // 判断当前寝室号是否已有
       // 根据期id查找所有当期学生列表
-      const _stu = await this.getbedroomstudents(termid, batchid, bedroom.code);
+      const _stu = _.filter(studentList, { bedroom: bedroom.code });
       console.log(_stu.length);
       if (bedroom.number !== _stu.length) {
         let i = 0;
@@ -139,7 +140,9 @@ class BedroomService extends CrudService {
   // 取得符合条件的学生列表
   async getstudents(termid, batchid) {
     // 根据期id查找所有当期学生列表
-    const studentList = await this.smodel.find({ termid, batchid }).sort({ gender: -1 });
+    const cltype = await this.ctmodel.find({ bedroom: '0' });
+    const types = _.map(cltype, 'code');
+    const studentList = await this.smodel.find({ termid, batchid, type: { $in: types } }).sort({ gender: -1 });
     return studentList;
   }
 

+ 37 - 1
app/service/class.js

@@ -25,6 +25,8 @@ class ClassService extends CrudService {
     const { planid, termid } = data;
     assert(planid, '计划id为必填项');
     assert(termid, '期id为必填项');
+    // 先自动生成班级
+    await this.autoclass(planid, termid);
     // 根据计划id与期id查询所有批次下的班级
     const newclass = await this.model.find({ planid, termid });
     if (!newclass) {
@@ -39,8 +41,10 @@ class ClassService extends CrudService {
     for (const _class of newclass) {
       // 取得班级人数
       const stunum = _class.number;
+      // 取出相同类型的学生
+      const studentids = await this.getstutype(_students, _class.type);
       // 取出班级人数下的学生id
-      const beforestu = _.take(_students, stunum);
+      const beforestu = _.take(studentids, stunum);
       // 将取出的数据调用更新学生的班级信息方法
       await this.studentup(_class.id, _class.batchid, beforestu);
       // 将班级自动分为组
@@ -50,6 +54,38 @@ class ClassService extends CrudService {
     }
   }
 
+  // 取得同样类型的学生
+  async getstutype(_students, type) {
+    const data = [];
+    for (const stuid of _students) {
+      const student = await this.stumodel.findById(stuid);
+      if (student && student.type === type) {
+        data.push(stuid);
+      }
+    }
+    return data;
+  }
+
+  // 自动生成班级私有方法
+  async autoclass(planid, termid) {
+    // 删除所有计划下的班级
+    await this.model.deleteMany({ planid, termid });
+    // 根据批次id取得当前批次具体信息
+    const res = await this.tmodel.findById(planid);
+    if (!res) {
+      throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
+    }
+    // 循环出所有班级进行添加操作
+    const term = await res.termnum.id(termid);
+    for (const batch of term.batchnum) {
+      const classs = await batch.class;
+      for (const cla of classs) {
+        const newdata = { name: cla.name, number: cla.number, batchid: batch.id, termid: term.id, planid: res.id, type: cla.type, headteacherid: cla.headteacherid };
+        await this.model.create(newdata);
+      }
+    }
+  }
+
   // 根据传入的学生列表和班级id更新学生信息
   async studentup(classid, batchid, beforestu) {
     // 循环学生id

+ 39 - 0
app/service/job.js

@@ -0,0 +1,39 @@
+'use strict';
+
+
+const assert = require('assert');
+const _ = require('lodash');
+const { ObjectId } = require('mongoose').Types;
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+
+class JobService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'job');
+    this.model = this.ctx.model.Job;
+    this.smodel = this.ctx.model.Student;
+  }
+
+  async update({ id }, data) {
+    const job = await this.model.findById(id);
+    if (!job) {
+      throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '任务信息不存在');
+    }
+    job.isstore = data.isstore;
+    const res = await job.save();
+    if (res) {
+      console.log(11111111);
+      if (data.isstore === '1') {
+        console.log('&&&&&&&&&&&&');
+        const studatas = JSON.parse(job.studs);
+        for (const stu of studatas) {
+          await this.smodel.create(stu);
+        }
+      }
+    }
+    return res;
+  }
+
+}
+
+module.exports = JobService;

+ 12 - 8
app/service/lesson.js

@@ -24,14 +24,8 @@ class LessonService extends CrudService {
     if (!res) {
       throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
     }
-    // 取得课程模板信息
-    const _lessonmode = await this.lmodel.findOne({ type: '0' });
-    if (!_lessonmode) {
-      throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '课程模板信息不存在');
-    }
-    // 取得模板内容并转化成json
-    const lessonmode = JSON.parse(_lessonmode.lessons);
     const terms = res.termnum;
+    const _lessonmode = await this.lmodel.find();
     // 循环取得所有期
     for (const elm of terms) {
       // 根据期id清空课程表
@@ -49,13 +43,23 @@ class LessonService extends CrudService {
         });
         // 循环班级
         for (const cla of _classs) {
+          // 取得课程模板信息
+          let lessonmode_ = _.find(_lessonmode, { type: cla.type });
+          if (!lessonmode_) {
+            lessonmode_ = _lessonmode[0];
+            if (!lessonmode_) {
+              throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '课程模板信息不存在');
+            }
+          }
+          // 取得模板内容并转化成json
+          const lessons_ = JSON.parse(lessonmode_.lessons);
           // 记录天数
           let i = 1;
           // 循环天数
           const newlesson = [];
           for (const day of sedays) {
             // 循环课程模板,将模板信息排入班级课程表中
-            for (const lessm of lessonmode) {
+            for (const lessm of lessons_) {
               // 循环插入模板信息
               if (lessm['day' + i] !== '--') {
                 let _subid = '';

+ 64 - 13
app/service/school.js

@@ -6,6 +6,7 @@ const _ = require('lodash');
 const { ObjectId } = require('mongoose').Types;
 const { CrudService } = require('naf-framework-mongoose/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
+const moment = require('moment');
 const XLSX = require('xlsx');
 
 class SchoolService extends CrudService {
@@ -15,10 +16,12 @@ class SchoolService extends CrudService {
     this.smodel = this.ctx.model.Student;
     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) {
-    const { filepath, termid, schid } = data;
+    const { filepath, termid, schid, type } = data;
     assert(filepath, 'filepath不能为空');
     assert(termid, 'termid不能为空');
     assert(schid, 'schid不能为空');
@@ -27,27 +30,41 @@ 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_ = await this.getschnum(plan, type, schid, termid);
+    console.log('*******************');
+    console.log(num_);
+    console.log('*******************');
     const planid = plan.id;
     const planyearid = plan.planyearid;
     // 取得excle中数据
     const _filepath = this.ctx.app.config.baseUrl + filepath;
     console.log(_filepath);
-    const studatas = await this.getImportXLSXData(_filepath, termid, schid, planid, planyearid);
-    console.log(studatas);
-    if (studatas.length > Number(num_)) {
-      throw new BusinessError(ErrorCode.SERVICE_FAULT, '学校上传人数超过预期人数');
-    }
+    const studatas = await this.getImportXLSXData(_filepath, termid, schid, planid, planyearid, type);
     // 将得到的数据校验
     const datacheck = await this.datacheck(studatas);
     if (datacheck.errorcode === '1') {
       return datacheck;
     }
+    const school_ = await this.model.findOne({ code: schid });
+    let schname = '';
+    if (school_) {
+      schname = school_.name;
+    }
+    const trem_ = await plan.termnum.id(termid);
+    if (!trem_) {
+      throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '期信息不存在');
+    }
+    const nowtime = moment().locale('zh-cn').format('YYYY-MM-DD HH:mm:ss');
+    if (studatas.length > 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, type, reason: '学校上传人数超过预期人数,请联系中心管理员' };
+      await this.jmodel.create(jobdata);
+      throw new BusinessError(ErrorCode.SERVICE_FAULT, '学校上传人数超过预期人数,请联系中心管理员');
+    } else if (studatas.length < 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, type, reason: '学校上传人数少于预期人数,请联系中心管理员' };
+      await this.jmodel.create(jobdata);
+      throw new BusinessError(ErrorCode.SERVICE_FAULT, '学校上传人数少于预期人数,请联系中心管理员');
+    }
     // 将数据存入数据库中
     for (const stu of studatas) {
       const res = await this.smodel.create(stu);
@@ -60,8 +77,41 @@ 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) {
+  async getImportXLSXData(filepath, termid, schid, planid, planyearid, type) {
     console.log(filepath);
     const file = await this.ctx.curl(filepath);
     const workbook = XLSX.read(file.data);
@@ -118,6 +168,7 @@ class SchoolService extends CrudService {
         schid,
         planid,
         planyearid,
+        type,
       });
     }
     exceldata = [ ...exceldata, ..._datas ];

+ 14 - 2
app/service/student.js

@@ -52,13 +52,14 @@ class StudentService extends CrudService {
   }
 
   // 查询
-  async seek({ termid, skip, limit }) {
+  async seek({ termid, type, skip, limit }) {
     const total = await this.model.count({
       termid,
+      type,
       $or: [{ classid: null }, { classid: '' }],
     });
     const data = await this.model
-      .find({ termid, $or: [{ classid: null }, { classid: '' }] })
+      .find({ termid, type, $or: [{ classid: null }, { classid: '' }] })
       .skip(Number(skip))
       .limit(Number(limit));
     const result = { total, data };
@@ -191,6 +192,17 @@ class StudentService extends CrudService {
       await this.model.deleteOne({ _id: ObjectId(el) });
     }
   }
+
+  // 批量更新寝室号
+  async updatabedroom(data) {
+    for (const el of data) {
+      const student = await this.model.findById(el.id);
+      if (student) {
+        student.bedroom = el.bedroom;
+        await student.save();
+      }
+    }
+  }
 }
 
 module.exports = StudentService;

+ 1 - 1
app/service/trainplan.js

@@ -68,7 +68,7 @@ class TrainplanService extends CrudService {
       if (status === '1') {
         // 自动生成班级
         // await this.autoclass(res, trainplanold);
-        await this.autoclassNew(res, trainplanold);
+        // await this.autoclassNew(res, trainplanold);
         // 将生成的班级重新将班级排班名
         // await this.autoclassname(res);
         // 发送培训计划信息通知给相应人员