Преглед на файлове

新增接口批量签到

cheny преди 4 години
родител
ревизия
453cb0e261
променени са 3 файла, в които са добавени 78 реда и са изтрити 18 реда
  1. 4 2
      app/controller/attendance.js
  2. 1 1
      app/router.js
  3. 73 15
      app/service/attendance.js

+ 4 - 2
app/controller/attendance.js

@@ -7,7 +7,6 @@ const { CrudController } = require('naf-framework-mongoose/lib/controller');
 
 // 考勤表管理
 class AttendanceController extends Controller {
-
   constructor(ctx) {
     super(ctx);
     this.service = this.ctx.service.attendance;
@@ -24,7 +23,10 @@ class AttendanceController extends Controller {
     const res = await this.service.attendancecreate(this.ctx.request.body);
     this.ctx.ok({ res });
   }
-
+  async attendancecreateList() {
+    const res = await this.service.attendancecreateList(this.ctx.request.body);
+    this.ctx.ok({ res });
+  }
 }
 
 module.exports = CrudController(AttendanceController, meta);

+ 1 - 1
app/router.js

@@ -152,7 +152,7 @@ module.exports = app => {
   router.resources('attendance', '/api/train/attendance', controller.attendance); // index、create、show、destroy
   router.post('attendance', '/api/train/attendance/update/:id', controller.attendance.update);
   router.post('attendance', '/api/train/attendance/attendancecreate', controller.attendance.attendancecreate);
-
+  router.post('attendance', '/api/train/attendance/attendancecreateList', controller.attendance.attendancecreateList);
   // 学校上传学生名单
   router.resources('school', '/api/train/school', controller.school); // index、create、show、destroy
   router.post('school', '/api/train/school/update/:id', controller.school.update);

+ 73 - 15
app/service/attendance.js

@@ -7,6 +7,7 @@ const sd = require('silly-datetime');
 const moment = require('moment');
 const { ObjectId } = require('mongoose').Types;
 const { CrudService } = require('naf-framework-mongoose/lib/service');
+const student = require('../model/student');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 
 class AttendanceService extends CrudService {
@@ -50,22 +51,37 @@ class AttendanceService extends CrudService {
       if (location.ibeacon !== ibeacon) {
         throw new BusinessError(ErrorCode.BUSINESS, '考勤位置不正确');
       }
-      const attendance = await this.model.findOne({ termid: student.termid, batchid: student.batchid, classid: student.classid, studentid: student.id });
+      const attendance = await this.model.findOne({
+        termid: student.termid,
+        batchid: student.batchid,
+        classid: student.classid,
+        studentid: student.id,
+      });
       const datetime = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
       // TODO 考勤时间没有处理
       const diffres = await this.islate(datetime, setting, type);
       if (diffres === 0) {
-        throw new BusinessError(ErrorCode.BUSINESS, '考勤时间不正确,请稍后在签到');
+        throw new BusinessError(
+          ErrorCode.BUSINESS,
+          '考勤时间不正确,请稍后在签到'
+        );
       }
-      const newData = { date: datetime.substr(0, 10), time: datetime.substr(11, 5), type, status: diffres + '' };
+      const newData = {
+        date: datetime.substr(0, 10),
+        time: datetime.substr(11, 5),
+        type,
+        status: diffres + '',
+      };
       if (attendance) {
         // TODO: 保存数据
         const attends = attendance.attend;
         let result = false;
         for (const elm of attends) {
-          if (elm.date === datetime.substr(0, 10)
-              && elm.type === '0'
-              && elm.time.substr(0, 2) === datetime.substr(11, 2)) {
+          if (
+            elm.date === datetime.substr(0, 10) &&
+            elm.type === '0' &&
+            elm.time.substr(0, 2) === datetime.substr(11, 2)
+          ) {
             result = true;
             break;
           }
@@ -77,7 +93,8 @@ class AttendanceService extends CrudService {
           await attendance.save();
         }
       } else {
-        const newdatastu = { termid: student.termid,
+        const newdatastu = {
+          termid: student.termid,
           batchid: student.batchid,
           classid: student.classid,
           planyearid: student.planyearid,
@@ -99,14 +116,27 @@ class AttendanceService extends CrudService {
       if (beedroom.ibeacon !== ibeacon) {
         throw new BusinessError(ErrorCode.BUSINESS, '考勤位置不正确');
       }
-      const attendance = await this.model.findOne({ termid: student.termid, batchid: student.batchid, classid: student.classid, studentid: student.id });
+      const attendance = await this.model.findOne({
+        termid: student.termid,
+        batchid: student.batchid,
+        classid: student.classid,
+        studentid: student.id,
+      });
       const datetime = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
       // TODO 考勤时间没有处理
       const diffres = await this.islate(datetime, setting, type);
       if (diffres === 0) {
-        throw new BusinessError(ErrorCode.BUSINESS, '考勤时间不正确,请稍后在签到');
+        throw new BusinessError(
+          ErrorCode.BUSINESS,
+          '考勤时间不正确,请稍后在签到'
+        );
       }
-      const newData = { date: datetime.substr(0, 10), time: datetime.substr(11, 5), type, status: diffres + '' };
+      const newData = {
+        date: datetime.substr(0, 10),
+        time: datetime.substr(11, 5),
+        type,
+        status: diffres + '',
+      };
       if (attendance) {
         // TODO: 保存数据
         const attends = attendance.attend;
@@ -124,7 +154,8 @@ class AttendanceService extends CrudService {
           await attendance.save();
         }
       } else {
-        const newdatastu = { termid: student.termid,
+        const newdatastu = {
+          termid: student.termid,
           batchid: student.batchid,
           classid: student.classid,
           studentid: student.id,
@@ -135,9 +166,7 @@ class AttendanceService extends CrudService {
         };
         await this.model.create(newdatastu);
       }
-
     }
-
   }
 
   async test() {
@@ -149,7 +178,6 @@ class AttendanceService extends CrudService {
     // TODO 考勤时间没有处理
     const diffres = await this.islate(datetime, setting, '0');
     console.log(diffres);
-
   }
 
   // 判断上课考勤是否迟到和开始
@@ -199,7 +227,37 @@ class AttendanceService extends CrudService {
     }
     return res;
   }
-
+  async attendancecreateList({ studentIds }) {
+    for (const studentId of studentIds) {
+      // 查出这个学生,更新这个学生的签到并签到
+      const student = await this.stumodel.findById(studentId);
+      if (!student) {
+        throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '学生信息不存在');
+      }
+      // 签到学生是否来了
+      student.isComming = '1';
+      await student.save();
+      const attendInfo = {
+        date: new Date().getDate(),
+        time: new Date().getHours(),
+        type: '0',
+        status: '1',
+      };
+      const data = {
+        planyearid: student.planyearid,
+        planid: student.planid,
+        termid: student.termid,
+        batchid: student.batchid,
+        classid: student.classid,
+        schid: student.schid,
+        schname: student.school_name,
+        studentid: student.id,
+        stuname: student.name,
+        attendInfo,
+      };
+      await this.model.create(data);
+    }
+  }
 }
 
 module.exports = AttendanceService;