|
@@ -6,6 +6,7 @@ const _ = require('lodash');
|
|
const { ObjectId } = require('mongoose').Types;
|
|
const { ObjectId } = require('mongoose').Types;
|
|
const { CrudService } = require('naf-framework-mongoose/lib/service');
|
|
const { CrudService } = require('naf-framework-mongoose/lib/service');
|
|
const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
|
+const moment = require('moment');
|
|
const XLSX = require('xlsx');
|
|
const XLSX = require('xlsx');
|
|
|
|
|
|
class SchoolService extends CrudService {
|
|
class SchoolService extends CrudService {
|
|
@@ -15,10 +16,12 @@ class SchoolService extends CrudService {
|
|
this.smodel = this.ctx.model.Student;
|
|
this.smodel = this.ctx.model.Student;
|
|
this.umodel = this.ctx.model.User;
|
|
this.umodel = this.ctx.model.User;
|
|
this.tmodel = this.ctx.model.Trainplan;
|
|
this.tmodel = this.ctx.model.Trainplan;
|
|
|
|
+ this.jmodel = this.ctx.model.Job;
|
|
|
|
+ this.schmodel = this.ctx.model.Schtime;
|
|
}
|
|
}
|
|
|
|
|
|
async stuimport(data) {
|
|
async stuimport(data) {
|
|
- const { filepath, termid, schid } = data;
|
|
|
|
|
|
+ const { filepath, termid, schid, type } = data;
|
|
assert(filepath, 'filepath不能为空');
|
|
assert(filepath, 'filepath不能为空');
|
|
assert(termid, 'termid不能为空');
|
|
assert(termid, 'termid不能为空');
|
|
assert(schid, 'schid不能为空');
|
|
assert(schid, 'schid不能为空');
|
|
@@ -27,27 +30,41 @@ class SchoolService extends CrudService {
|
|
if (!plan) {
|
|
if (!plan) {
|
|
throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '计划信息不存在');
|
|
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 planid = plan.id;
|
|
const planyearid = plan.planyearid;
|
|
const planyearid = plan.planyearid;
|
|
// 取得excle中数据
|
|
// 取得excle中数据
|
|
const _filepath = this.ctx.app.config.baseUrl + filepath;
|
|
const _filepath = this.ctx.app.config.baseUrl + filepath;
|
|
console.log(_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);
|
|
const datacheck = await this.datacheck(studatas);
|
|
if (datacheck.errorcode === '1') {
|
|
if (datacheck.errorcode === '1') {
|
|
return datacheck;
|
|
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) {
|
|
for (const stu of studatas) {
|
|
const res = await this.smodel.create(stu);
|
|
const res = await this.smodel.create(stu);
|
|
@@ -60,8 +77,41 @@ class SchoolService extends CrudService {
|
|
return datacheck;
|
|
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文件中的数据
|
|
// 获取导入的XLSX文件中的数据
|
|
- async getImportXLSXData(filepath, termid, schid, planid, planyearid) {
|
|
|
|
|
|
+ async getImportXLSXData(filepath, termid, schid, planid, planyearid, type) {
|
|
console.log(filepath);
|
|
console.log(filepath);
|
|
const file = await this.ctx.curl(filepath);
|
|
const file = await this.ctx.curl(filepath);
|
|
const workbook = XLSX.read(file.data);
|
|
const workbook = XLSX.read(file.data);
|
|
@@ -118,6 +168,7 @@ class SchoolService extends CrudService {
|
|
schid,
|
|
schid,
|
|
planid,
|
|
planid,
|
|
planyearid,
|
|
planyearid,
|
|
|
|
+ type,
|
|
});
|
|
});
|
|
}
|
|
}
|
|
exceldata = [ ...exceldata, ..._datas ];
|
|
exceldata = [ ...exceldata, ..._datas ];
|