|
@@ -4,6 +4,7 @@
|
|
|
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 { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
@@ -16,6 +17,7 @@ class AttendanceService extends CrudService {
|
|
|
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) {
|
|
@@ -30,6 +32,11 @@ class AttendanceService extends CrudService {
|
|
|
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);
|
|
@@ -46,7 +53,11 @@ class AttendanceService extends CrudService {
|
|
|
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' };
|
|
|
+ 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;
|
|
@@ -86,7 +97,11 @@ class AttendanceService extends CrudService {
|
|
|
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' };
|
|
|
+ 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;
|
|
@@ -117,6 +132,66 @@ class AttendanceService extends CrudService {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ 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');
|
|
|
+ console.log(diffres);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 判断上课考勤是否迟到和开始
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
module.exports = AttendanceService;
|