|
@@ -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);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
}
|