'use strict'; const assert = require('assert'); const _ = require('lodash'); 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 { constructor(ctx) { super(ctx, 'attendance'); this.model = this.ctx.model.Attendance; this.stumodel = this.ctx.model.Student; this.umodel = this.ctx.model.User; this.clamodel = this.ctx.model.Class; this.lomodel = this.ctx.model.Location; this.bmodel = this.ctx.model.Bedroom; this.setmodel = this.ctx.model.Setting; } async attendancecreate(data) { // 修改ibeacon=>[],存有 limit 范围内的所有蓝牙设备号 const { openid, type, ibeacon } = data; assert(openid && type && ibeacon, '缺少信息项'); // 通过openid取得学生信息 const user = await this.umodel.findOne({ appopenid: 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, '学生信息不存在'); } // 取得设置表内考勤时间 const setting = await this.setmodel.findOne(); if (!setting) { 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, '位置信息不存在'); } // 判断,location的蓝牙号在不在ibeacon中 const r = ibeacon.find(f => `${f}` === `${location.ibeacon}`); if (!r) { 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 diffres = await this.islate(datetime, setting, type); if (diffres === 0) { throw new BusinessError( ErrorCode.BUSINESS, '考勤时间不正确,请稍后在签到' ); } 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) ) { result = true; break; } } if (!result) { const newattend = await attendance.attend.create(newData); attendance.attend.push(newattend); await attendance.save(); } } else { const newdatastu = { termid: student.termid, batchid: student.batchid, classid: student.classid, planyearid: student.planyearid, planid: student.planid, studentid: student.id, stuname: student.name, schname: student.school_name, schid: student.schid, attend: [ newData ], }; await this.model.create(newdatastu); } } else if (type === '1') { const beedroom = await this.bmodel.findOne({ code: student.bedroom }); if (!beedroom) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '寝室信息不存在'); } const r = ibeacon.find(f => `${f}` === `${beedroom.ibeacon}`); if (!r) { 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 diffres = await this.islate(datetime, setting, type); if (diffres === 0) { throw new BusinessError( ErrorCode.BUSINESS, '考勤时间不正确,请稍后在签到' ); } 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 === '1') { result = true; break; } } if (!result) { const newattend = await attendance.attend.create(newData); attendance.attend.push(newattend); await attendance.save(); } } else { const newdatastu = { termid: student.termid, batchid: student.batchid, classid: student.classid, studentid: student.id, schid: student.schid, stuname: student.name, schname: student.school_name, attend: [ newData ], }; await this.model.create(newdatastu); } } } async test() { const setting = await this.setmodel.findOne(); if (!setting) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '考勤设置信息不存在'); } const datetime = sd.format(new Date(), 'YYYY-MM-DD HH:mm'); // TODO 考勤时间没有处理 const diffres = await this.islate(datetime, setting, '0'); } // 判断上课考勤是否迟到和开始 async islate(nowdata, setting, type) { nowdata = nowdata + ':00'; let res = 0; if (type === '0') { const am_start = nowdata.substr(0, 10) + ' ' + setting.am_start + ':00'; const am_end = nowdata.substr(0, 10) + ' ' + setting.am_end + ':00'; const pm_start = nowdata.substr(0, 10) + ' ' + setting.pm_start + ':00'; const pm_end = nowdata.substr(0, 10) + ' ' + setting.pm_end + ':00'; const resamstart = moment(nowdata).diff(moment(am_start), 'minutes'); const resamend = moment(nowdata).diff(moment(am_end), 'minutes'); const respmstart = moment(nowdata).diff(moment(pm_start), 'minutes'); const respmend = moment(nowdata).diff(moment(pm_end), 'minutes'); // 当前时间大于am开始时间 if (resamstart > 0) { // 当前时间小于pm开始时间 if (respmstart < 0) { // 判断上午考勤是否迟到 if (resamend > 0) { res = 2; } else { res = 1; } } else { // 判断下午考勤是否迟到 if (respmend > 0) { res = 2; } else { res = 1; } } } } else if (type === '1') { const bd_start = nowdata.substr(0, 10) + ' ' + setting.bd_start + ':00'; 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'); if (resstart > 0) { if (resend > 0) { res = 2; } else { res = 1; } } } 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;