lrf402788946 il y a 4 ans
Parent
commit
66b6952833
1 fichiers modifiés avec 63 ajouts et 27 suppressions
  1. 63 27
      app/service/school.js

+ 63 - 27
app/service/school.js

@@ -26,7 +26,13 @@ class SchoolService extends CrudService {
     assert(name, '缺少学校名称');
     const res = await this.model.create(data);
     if (res) {
-      const obj = { mobile: code, name, type: '2', uid: res._id, passwd: { secret: '12345678' } };
+      const obj = {
+        mobile: code,
+        name,
+        type: '2',
+        uid: res._id,
+        passwd: { secret: '12345678' },
+      };
       await this.umodel.create(obj);
     }
     return res;
@@ -42,6 +48,13 @@ class SchoolService extends CrudService {
     if (!plan) {
       throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '计划信息不存在');
     }
+    const isOutOfDate = this.outOfDate(plan, termid);
+    if (!isOutOfDate) {
+      throw new BusinessError(
+        ErrorCode.BUSINESS,
+        '已经超过上报时间,不允许上报名单'
+      );
+    }
     // 取得学校预计人数
     const num_ = await this.getschnum(plan, type, schid, termid, batchid);
     console.log('*******************');
@@ -137,7 +150,7 @@ class SchoolService extends CrudService {
     const schtime = await this.schmodel.findOne({ schid, planid: plan.id });
     const { arrange } = schtime;
     const r = arrange.find(f => f.batchid === batchid);
-    if (!r) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '没有找到该学校的计划');
+    if (!r) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '没有找到该学校的计划'); }
     const { number } = r;
     return parseInt(number);
     // const { termnum } = plan;
@@ -170,7 +183,15 @@ class SchoolService extends CrudService {
   }
 
   // 获取导入的XLSX文件中的数据
-  async getImportXLSXData(filepath, termid, schid, planid, planyearid, type, batchid) {
+  async getImportXLSXData(
+    filepath,
+    termid,
+    schid,
+    planid,
+    planyearid,
+    type,
+    batchid
+  ) {
     const file = await this.ctx.curl(filepath);
     const workbook = XLSX.read(file.data);
     // 读取内容
@@ -266,7 +287,6 @@ class SchoolService extends CrudService {
           errorcode = '1';
           data.msg = (data.msg || '') + `${msg},`;
         }
-
       }
       if (!data.school_name) {
         errorcode = '1';
@@ -332,28 +352,29 @@ class SchoolService extends CrudService {
   async exportSchool({ trainplanId }) {
     // 批次期次都在这里面
     const trainplan = await this.tmodel.find({ _id: trainplanId });
-    const _headers = [
-      { key: 'title', title: '计划标题' },
-    ];
+    const _headers = [{ key: 'title', title: '计划标题' }];
     // 需要打出的列表
     const _data = trainplan;
-    const headers = _headers.map(({ title }) =>
-      title).map((v, i) =>
-      Object.assign({}, { v, position: String.fromCharCode(65 + i) + 1 })
-    ).reduce(
-      (prev, next) =>
-        Object.assign({}, prev, { [next.position]: { v: next.v } }),
-      {}
-    );
+    const headers = _headers
+      .map(({ title }) => title)
+      .map((v, i) =>
+        Object.assign({}, { v, position: String.fromCharCode(65 + i) + 1 })
+      )
+      .reduce(
+        (prev, next) =>
+          Object.assign({}, prev, { [next.position]: { v: next.v } }),
+        {}
+      );
 
-    const data = _data.map((v, i) =>
-      _headers.map(({ key }, j) =>
-        Object.assign(
-          {},
-          { v: v[key], position: String.fromCharCode(65 + j) + (i + 2) }
+    const data = _data
+      .map((v, i) =>
+        _headers.map(({ key }, j) =>
+          Object.assign(
+            {},
+            { v: v[key], position: String.fromCharCode(65 + j) + (i + 2) }
+          )
         )
       )
-    )
       .reduce((prev, next) => prev.concat(next))
       .reduce(
         (prev, next) =>
@@ -373,11 +394,9 @@ class SchoolService extends CrudService {
     // 构建 workbook 对象
     const nowDate = new Date().getTime();
     const path =
-       'D:\\wwwroot\\service\\service-file\\upload\\train\\' +
-       nowDate +
-       '.xlsx';
+      'D:\\wwwroot\\service\\service-file\\upload\\train\\' + nowDate + '.xlsx';
     const respath =
-       'http://free.liaoningdoupo.com:80/files/train/' + nowDate + '.xlsx';
+      'http://free.liaoningdoupo.com:80/files/train/' + nowDate + '.xlsx';
     const wb = {
       SheetNames: [ 'sheet0' ],
       Sheets: { sheet0: Object.assign({}, output, { '!ref': ref }) },
@@ -418,7 +437,7 @@ class SchoolService extends CrudService {
     }
     return await trainplan.save();
   }
-
+  // 身份证验证
   idCodeValid(code) {
     // 身份证号合法性验证
     // 支持15位和18位身份证号
@@ -464,7 +483,12 @@ class SchoolService extends CrudService {
       pass: true,
       msg: '验证成功',
     };
-    if (!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|[xX])$/.test(code)) {
+    if (
+      !code ||
+      !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|[xX])$/.test(
+        code
+      )
+    ) {
       row = {
         pass: false,
         msg: '身份证号格式错误',
@@ -501,6 +525,18 @@ class SchoolService extends CrudService {
     }
     return row;
   }
+  // 判断是否超出该期前3天
+  outOfDate(plan, termid) {
+    const term = plan.termnum.find(f => ObjectId(termid).equals(f._id));
+    const { batchnum } = term;
+    let startList = batchnum.map(i => ({ start: i.startdate }));
+    startList = _.orderBy(startList, [ 'start' ], [ 'asc' ]);
+    const start = _.get(_.head(startList), 'start');
+    const limit = moment(start).subtract(3, 'days').format('YYYY-MM-DD');
+    const now = moment().format('YYYY-MM-DD');
+    const res = moment(now).isBefore(limit);
+    return res;
+  }
 }
 
 module.exports = SchoolService;