浏览代码

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

reloaded 5 年之前
父节点
当前提交
738125311f

+ 2 - 0
app/controller/.bedroom.js

@@ -6,6 +6,7 @@ module.exports = {
       'batch',
       'gender',
       'floor',
+      'ibeacon',
       'status'
     ]
   },
@@ -21,6 +22,7 @@ module.exports = {
       'batch',
       'gender',
       'floor',
+      'ibeacon',
       'status'
     ]
   },

+ 4 - 2
app/controller/.location.js

@@ -1,7 +1,8 @@
 module.exports = {
   create: {
     requestBody: [
-      '!name'
+      '!name',
+      'ibeacon'
     ]
   },
   destroy: {
@@ -11,7 +12,8 @@ module.exports = {
   update: {
     params: ['!id'],
     requestBody: [
-      'name'
+      'name',
+      'ibeacon'
     ]
   },
   show: {

+ 5 - 0
app/controller/attendance.js

@@ -20,6 +20,11 @@ class AttendanceController extends Controller {
     this.ctx.ok({ res });
   }
 
+  async attendancecreate() {
+    const res = await this.service.attendancecreate(this.ctx.request.body);
+    this.ctx.ok({ res });
+  }
+
 }
 
 module.exports = CrudController(AttendanceController, meta);

+ 6 - 0
app/controller/bedroom.js

@@ -19,6 +19,12 @@ class BedroomController extends Controller {
     this.ctx.ok({ data: res });
   }
 
+  async ibeacon() {
+    console.log(this.ctx.request.body);
+    const res = await this.service.ibeacon(this.ctx.request.body);
+    this.ctx.ok({ ...res });
+  }
+
 }
 
 module.exports = CrudController(BedroomController, meta);

+ 1 - 0
app/model/attendance.js

@@ -6,6 +6,7 @@ const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
 const attendInfo = new Schema({
   date: { type: String, required: false, maxLength: 200 }, // 日期
   time: { type: String, required: false, maxLength: 200 }, // 时间
+  type: { type: String, required: false, maxLength: 10 }, // 考勤类别0、上课考勤 1、寝室考勤
   status: { type: String, required: false, maxLength: 200 }, // 是否签到,0-未签到,1-签到,2-迟到
 });
 

+ 1 - 0
app/model/bedroom.js

@@ -9,6 +9,7 @@ const BedroomSchema = {
   batch: { type: String, required: false, maxLength: 200 }, // 批次
   gender: { type: String, required: false, maxLength: 200 }, // 男女限制
   floor: { type: String, required: false, maxLength: 200 }, // 楼层
+  ibeacon: { type: String, required: true, maxLength: 500 }, // 蓝牙设备号
   status: { type: String, required: false, maxLength: 200, default: '0' }, // 状态:0-启用,1-停用
 };
 

+ 1 - 0
app/model/location.js

@@ -5,6 +5,7 @@ const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
 // 位置表
 const LocationSchema = {
   name: { type: String, required: true, maxLength: 500 }, // 名称
+  ibeacon: { type: String, required: true, maxLength: 500 }, // 蓝牙设备号
 };
 
 

+ 2 - 0
app/router.js

@@ -47,6 +47,7 @@ module.exports = app => {
   router.resources('bedroom', '/api/train/bedroom', controller.bedroom); // index、create、show、destroy
   router.post('bedroom', '/api/train/bedroom/update/:id', controller.bedroom.update);
   router.post('bedroom', '/api/train/bedroom/apart', controller.bedroom.apart);
+  router.post('bedroom', '/api/train/bedroom/ibeacon', controller.bedroom.ibeacon);
 
   // 班级表设置路由
   router.post('class', '/api/train/class/notice', controller.class.notice);
@@ -117,6 +118,7 @@ module.exports = app => {
   router.get('/api/train/attendance/wxauth', controller.attendance.wxauth); // 统计完成度
   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.resources('school', '/api/train/school', controller.school); // index、create、show、destroy

+ 104 - 0
app/service/attendance.js

@@ -3,6 +3,7 @@
 
 const assert = require('assert');
 const _ = require('lodash');
+const sd = require('silly-datetime');
 const { ObjectId } = require('mongoose').Types;
 const { CrudService } = require('naf-framework-mongoose/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
@@ -11,6 +12,109 @@ class AttendanceService extends CrudService {
   constructor(ctx) {
     super(ctx, 'attendance');
     this.model = this.ctx.model.Attendance;
+    this.stumodel = this.ctx.model.Student;
+    this.clamodel = this.ctx.model.Class;
+    this.lomodel = this.ctx.model.Location;
+    this.bmodel = this.ctx.model.Bedroom;
+  }
+
+  async attendancecreate(data) {
+    const { openid, type, ibeacon } = data;
+    assert(openid && type && ibeacon, '缺少信息项');
+    // 通过openid取得学生信息
+    const user = await this.ctx.service.user.findByOpenid(openid);
+    if (!user) {
+      throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '用户不存在');
+    }
+    const student = await this.stumodel.findById(user.uid);
+    if (!student) {
+      throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '学生信息不存在');
+    }
+    // type为0是上课考勤
+    if (type === '0') {
+      const classes = await this.clamodel.findById(student.classid);
+      if (!classes) {
+        throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '班级信息不存在');
+      }
+      const location = await this.lomodel.findById(classes.jslocationid);
+      if (!location) {
+        throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '位置信息不存在');
+      }
+      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 datetime = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
+      // TODO 考勤时间没有处理
+      const newData = { date: datetime.substr(0, 10), time: datetime.substr(11, 5), type, status: '1' };
+      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)) {
+            result = true;
+            break;
+          }
+        }
+        if (!result) {
+          const newattend = await attendance.attend.create(newData);
+          console.log('newattend:', newattend);
+          attendance.attend.push(newattend);
+          await attendance.save();
+        }
+      } else {
+        const newdata = { termid: student.termid,
+          batchid: student.batchid,
+          classid: student.classid,
+          studentid: student.id,
+          attend: [ newData ],
+        };
+        await this.model.create(newdata);
+      }
+    } else if (type === '1') {
+      console.log('寝室考勤');
+      const beedroom = await this.bmodel.findOne({ code: student.bedroom });
+      if (!beedroom) {
+        throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '寝室信息不存在');
+      }
+      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 datetime = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
+      // TODO 考勤时间没有处理
+      const newData = { date: datetime.substr(0, 10), time: datetime.substr(11, 5), type, status: '1' };
+      if (attendance) {
+        // TODO: 保存数据
+        const attends = attendance.attend;
+        let result = false;
+        for (const elm of attends) {
+          if (elm.date === datetime.substr(0, 10) && elm.type === '1') {
+            result = true;
+            break;
+          }
+        }
+        if (!result) {
+          const newattend = await attendance.attend.create(newData);
+          console.log('newattend:', newattend);
+          attendance.attend.push(newattend);
+          await attendance.save();
+        }
+      } else {
+        const newdata = { termid: student.termid,
+          batchid: student.batchid,
+          classid: student.classid,
+          studentid: student.id,
+          attend: [ newData ],
+        };
+        await this.model.create(newdata);
+      }
+
+    }
+
   }
 
 }

+ 18 - 0
app/service/bedroom.js

@@ -18,6 +18,24 @@ class BedroomService extends CrudService {
 
   }
 
+  async ibeacon(data) {
+    assert(data.openid, '用户信息不能为空');
+    // 通过openid取得学生信息
+    const user = await this.ctx.service.user.findByOpenid(data.openid);
+    if (!user) {
+      throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '用户不存在');
+    }
+    const student = await this.smodel.findById(user.uid);
+    if (!student) {
+      throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '学生信息不存在');
+    }
+    const beedroom = await this.model.findOne({ code: student.bedroom });
+    if (!beedroom) {
+      throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '寝室信息不存在');
+    }
+    return { data: { ibeacon: beedroom.ibeacon } };
+  }
+
   // 一键分寝
   async apart(data) {
     const { trainplanid, termid, batchid } = data;