lrf402788946 4 سال پیش
والد
کامیت
fdddb0a079
9فایلهای تغییر یافته به همراه185 افزوده شده و 22 حذف شده
  1. 4 1
      app/controller/task.js
  2. 1 1
      app/router.js
  3. 0 8
      app/service/attendance.js
  4. 0 1
      app/service/lesson.js
  5. 0 2
      app/service/questionnaire.js
  6. 0 1
      app/service/student.js
  7. 149 0
      app/service/task.js
  8. 4 1
      app/service/user.js
  9. 27 7
      app/service/util.js

+ 4 - 1
app/controller/task.js

@@ -7,12 +7,15 @@ const { CrudController } = require('naf-framework-mongoose/lib/controller');
 
 // 作业管理
 class TaskController extends Controller {
-
   constructor(ctx) {
     super(ctx);
     this.service = this.ctx.service.task;
   }
 
+  async export() {
+    const res = await this.service.export(this.ctx.request.body);
+    this.ctx.ok(res);
+  }
 }
 
 module.exports = CrudController(TaskController, meta);

+ 1 - 1
app/router.js

@@ -672,7 +672,7 @@ module.exports = app => {
   // 问卷导出
   router.post('questionnaire', '/api/train/questionnaire/export', controller.questionnaire.export);
   // 作业导出
-  // router.post('task', '/api/train/task/export', controller.task.export);
+  router.post('task', '/api/train/task/export', controller.task.export);
   // 学生证书打印状态更改
   router.post('student', '/api/train/student/printcert', controller.student.printCert);
 

+ 0 - 8
app/service/attendance.js

@@ -126,10 +126,8 @@ class AttendanceService extends CrudService {
         studentid: student.id,
       });
       const datetime = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
-      console.log(datetime);
       // TODO 考勤时间没有处理
       const diffres = await this.islate(datetime, setting, type);
-      console.log(diffres);
       if (diffres === 0) {
         throw new BusinessError(
           ErrorCode.BUSINESS,
@@ -142,7 +140,6 @@ class AttendanceService extends CrudService {
         type,
         status: diffres + '',
       };
-      console.log(newData);
       if (attendance) {
         // TODO: 保存数据
         const attends = attendance.attend;
@@ -186,7 +183,6 @@ class AttendanceService extends CrudService {
 
   // 判断上课考勤是否迟到和开始
   async islate(nowdata, setting, type) {
-    console.log(nowdata, setting, type);
     nowdata = nowdata + ':00';
     let res = 0;
     if (type === '0') {
@@ -222,10 +218,6 @@ class AttendanceService extends CrudService {
       const bd_end = nowdata.substr(0, 10) + ' ' + setting.bd_end + ':00';
       const resstart = moment(nowdata).diff(moment(bd_start), 'minutes');
       const resend = moment(nowdata).diff(moment(bd_end), 'minutes');
-      console.log(bd_start);
-      console.log(bd_end);
-      console.log(resstart);
-      console.log(resend);
       if (resstart > 0) {
         if (resend > 0) {
           res = 2;

+ 0 - 1
app/service/lesson.js

@@ -255,7 +255,6 @@ class LessonService extends CrudService {
 
   // 根据计划id、教师id查询所有班级信息
   async teaclass({ termid, teaid }) {
-    console.log(termid, teaid);
     // 取得传入的计划id与教师id
     // 根据计划id取得所有期
     const lessons = await this.model.find({

+ 0 - 2
app/service/questionnaire.js

@@ -90,7 +90,6 @@ class QuestionnaireService extends CrudService {
     let { data: studentList } = await this.ctx.service.student.query(range);
     if (studentList.length <= 0) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到任何学生信息'); }
     studentList = JSON.parse(JSON.stringify(studentList));
-    console.log(_.head(studentList));
     // 再获取问卷
     let questAnswerList = await this.ctx.model.Uploadquestion.find({
       ...range,
@@ -108,7 +107,6 @@ class QuestionnaireService extends CrudService {
       else if (model === 'classid') i.model = 'classname';
       return i;
     });
-    // TODO 整理导出数据
     let excelData;
     if (direction === 'horizontal') {
       excelData = this.horizontalSetData(studentList, questAnswerList, modelList);

+ 0 - 1
app/service/student.js

@@ -567,7 +567,6 @@ class StudentService extends CrudService {
 
   // 确认学生打印证书
   async printCert({ ids }) {
-    console.log(ids);
     const res = await this.model.updateMany({ _id: { $in: ids.map(i => ObjectId(i)) } }, { cert: '1' });
     return res;
   }

+ 149 - 0
app/service/task.js

@@ -13,6 +13,155 @@ class TaskService extends CrudService {
     this.model = this.ctx.model.Task;
   }
 
+  async export({ range, subid }) {
+    console.log(range, subid);
+    // 找到学生
+    let { data: studentList } = await this.ctx.service.student.query(range);
+    if (studentList.length <= 0) {
+      throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到任何学生信息');
+    }
+    studentList = JSON.parse(JSON.stringify(studentList));
+    // 找到作业
+    const taskList = await this.getTaskInfo(range, subid);
+    // prefix 最少 科目名称/人 可能出现 选择到期:期/班/科目/人; 选择到批:期/批/班/科目/人; 选择到班:科目/人 ;选择计划:期/批/班/科目/人
+    // 找到应该是哪种模式的文件夹,同时可以获得压缩包的名称
+    const { fn, prefix: pn } = await this.getFnDir(range);
+    // 循环作业,之后匹配所需要的信息
+    const arr = [];
+    for (const task of taskList) {
+      const { lessonname, studentid, picurl } = task;
+      const stu = studentList.find(f => f._id === studentid);
+      if (!stu) continue;
+      const { name: studentname, termname, batchname, classname } = stu;
+      let picarr = [];
+      if (_.isString(picurl)) {
+        picarr = [ picurl ];
+      } else {
+        picarr = picurl;
+      }
+      for (const uri of picarr) {
+        const obj = { uri };
+        const keys = Object.keys(pn);
+        let prefix = `${lessonname}/${studentname}/`;
+        if (keys.includes('class')) {
+          prefix = `${classname}/${prefix}`;
+        }
+        if (keys.includes('batchid')) {
+          prefix = `${batchname}/${prefix}`;
+        }
+        if (keys.includes('term')) {
+          prefix = `${termname}/${prefix}`;
+        }
+        obj.prefix = prefix;
+        arr.push(obj);
+      }
+    }
+    return await this.ctx.service.util.toZip(arr, fn);
+  }
+
+  async getTaskInfo(range, subid) {
+    const { planid, termid, batchid, classid } = range;
+    let lessonList = [];
+    // 找到指定范围的课表
+    if (classid) {
+      lessonList = await this.ctx.model.Lesson.find({ classid });
+    } else if (batchid) {
+      lessonList = await this.ctx.model.Lesson.find({ batchid });
+    } else if (termid) {
+      lessonList = await this.ctx.model.Lesson.find({ termid });
+    } else if (planid) {
+      const trainPlan = await this.ctx.model.Trainplan.findById(planid);
+      if (!trainPlan) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到年度计划信息'); }
+      let { termnum } = trainPlan;
+      if (!termnum) {
+        throw new BusinessError(
+          ErrorCode.DATA_NOT_EXIST,
+          '未找到年度计划下的期信息'
+        );
+      }
+      termnum = JSON.parse(JSON.stringify(termnum));
+      const termids = termnum.map(i => i._id);
+      lessonList = await this.ctx.model.Lesson.find({ termid: { $in: termids } });
+    }
+    lessonList = JSON.parse(JSON.stringify(lessonList));
+    // 不需要其他属性,只要课表安排
+    lessonList = lessonList.map(i => i.lessons);
+    lessonList = lessonList.flat();
+    // 过滤出有subid且 !==''的课程
+    lessonList = lessonList.filter(f => f.subid && f.subid !== '');
+    // 如果指定某科,就把这科过滤出来
+    if (subid) lessonList = lessonList.filter(f => f.subid === subid);
+    // 找作业
+    const lessonids = lessonList.map(i => i._id);
+    let taskList = await this.ctx.model.Uploadtask.find({
+      lessonid: { $in: lessonids },
+    });
+    taskList = JSON.parse(JSON.stringify(taskList));
+    // taskList = taskList.map(i => {
+    //   const r = lessonList.find(i => i._id === i.lessonid);
+    //   if (r) i.lessonname = r.subname;
+    //   return i;
+    // });
+    return JSON.parse(JSON.stringify(taskList));
+  }
+
+  async getFnDir(range) {
+    const { planid, termid, batchid, classid } = range;
+    let fn = '作业';
+    const prefix = {};
+    const getData = (termid, batchid, termnum) => {
+      let res = '';
+      if (termid) {
+        const termInfo = termnum.id(termid);
+        if (!termInfo) return res;
+        const { term, batchnum } = termInfo;
+        if (term) res = `第${term}期${res}`;
+        if (batchid && batchnum) {
+          const batchInfo = batchnum.id(batchid);
+          if (!batchInfo) return res;
+          const { batch } = batchInfo;
+          res = `${res}第${batch}批`;
+        }
+      }
+      return res;
+    };
+    // 只有班级
+    if (classid) {
+      const cla = await this.ctx.service.class.fetch({ id: classid });
+      if (cla) {
+        const { name, term } = cla;
+        if (name) fn = `${name.includes('班') ? name : `${name}班`}${fn}`;
+        if (term) fn = `第${term}期${fn}`;
+      }
+    } else {
+      const condition = {};
+      if (planid) condition._id = ObjectId(planid);
+      if (termid) condition['termnum._id'] = ObjectId(termid);
+      if (batchid) condition['termnum.batchnum._id'] = ObjectId(batchid);
+      const trainPlan = await this.ctx.model.Trainplan.findOne(condition);
+      const { termnum, title } = trainPlan;
+      if (!termnum) return;
+      if (termid || batchid) {
+        const r = getData(termid, batchid, termnum);
+        fn = `${r}${fn}`;
+        prefix.class = true;
+        if (batchid) {
+          prefix.term = true;
+          prefix.batch = true;
+        } else {
+          prefix.term = true;
+        }
+      } else {
+        fn = `${title}${fn}`;
+        prefix.term = true;
+        prefix.batch = true;
+        prefix.class = true;
+      }
+    }
+
+    return { fn, prefix };
+
+  }
 }
 
 module.exports = TaskService;

+ 4 - 1
app/service/user.js

@@ -146,7 +146,10 @@ class UserService extends CrudService {
   // 通过openid查询用户信息
   async findByAppOpenid(appopenid) {
     // 通过openid查询用户信息
-    console.warn('in function:findByAppOpenid');
+    if (!appopenid) {
+      console.error('没有appopenid');
+      return;
+    }
     const user = await this.model.findOne({ appopenid });
     return user;
   }

+ 27 - 7
app/service/util.js

@@ -76,7 +76,16 @@ class UtilService extends CrudService {
     return data;
   }
   async utilMethod(data, body) {
-    console.log();
+    // const output = fs.createWriteStream('E:\\exportFile\\test.zip');
+    // const archive = archiver('zip', {
+    //   zlib: { level: 9 },
+    // });
+    // archive.pipe(output);
+    // const res = await this.ctx.curl('http://jytz.jilinjobs.cn/files/task/20200914174650.jpg');
+    // if (res && res.data) {
+    //   archive.append(res.data, { name: 'test.jpg', prefix: 'test/test2/test3' });
+    // }
+    // archive.finalize();
   }
 
   async teacherImport() {
@@ -216,8 +225,9 @@ class UtilService extends CrudService {
     const { app } = this;
     const rootPath = `${app.config.cdn.repos_root_path}`;
     const rooturl = `${app.config.cdn.repos_root_url_experience}`;
-    const path = `${rootPath}${rooturl}`;
+    let path = `${rootPath}${rooturl}`;
     // 如果不存在文件夹,就创建
+    if (process.env.NODE_ENV === 'development') path = 'E:\\exportFile\\';
     if (!fs.existsSync(path)) {
       fs.mkdirSync(path);
     }
@@ -244,7 +254,8 @@ class UtilService extends CrudService {
     const { app } = this;
     const rootPath = `${app.config.cdn.repos_root_path}`;
     const zipPath = `${app.config.cdn.repos_root_url_zip}`;
-    const path = `${rootPath}${zipPath}`;
+    let path = `${rootPath}${zipPath}`;
+    if (process.env.NODE_ENV === 'development') path = 'E:\\exportFile\\';
     if (!fs.existsSync(path)) {
       fs.mkdirSync(path);
     }
@@ -253,13 +264,14 @@ class UtilService extends CrudService {
     for (const file of fileList) {
       let uri = '';
       let filename = '';
+      let prefixs;
       if (_.isString(file)) {
         uri = file;
         const arr = file.split('/');
         const last = _.last(arr);
         if (last) filename = last;
       } else if (_.isObject(file)) {
-        const { uri: furi, url: furl, name } = file;
+        const { uri: furi, url: furl, name, prefix } = file;
         if (furi) uri = furi;
         else if (furl) uri = furl;
         if (name) filename = name;
@@ -268,8 +280,13 @@ class UtilService extends CrudService {
           const last = _.last(arr);
           if (last) filename = last;
         }
+        if (prefix) prefixs = prefix;
       }
-      if (uri && filename) resetFileList.push({ uri, filename });
+      const obj = {};
+      if (uri) obj.uri = uri;
+      if (filename)obj.filename = filename;
+      if (prefixs) obj.prefix = prefixs;
+      resetFileList.push(obj);
     }
     // 导出
     const output = fs.createWriteStream(`${path}${fn}`);
@@ -279,10 +296,13 @@ class UtilService extends CrudService {
     archive.pipe(output);
     // 请求文件,追加进压缩包
     for (const file of resetFileList) {
-      const { uri, filename } = file;
+      const { uri, filename, prefix } = file;
       const res = await this.ctx.curl(`http://127.0.0.1${uri}`);
       if (res && res.data) {
-        archive.append(res.data, { name: filename });
+        const options = {};
+        if (filename)options.name = filename;
+        if (prefix) options.prefix = prefix;
+        if (filename) { archive.append(res.data, options); }
       }
     }
     archive.finalize();