attendance.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const sd = require('silly-datetime');
  5. const moment = require('moment');
  6. const { ObjectId } = require('mongoose').Types;
  7. const { CrudService } = require('naf-framework-mongoose/lib/service');
  8. const { BusinessError, ErrorCode } = require('naf-core').Error;
  9. class AttendanceService extends CrudService {
  10. constructor(ctx) {
  11. super(ctx, 'attendance');
  12. this.model = this.ctx.model.Attendance;
  13. this.stumodel = this.ctx.model.Student;
  14. this.clamodel = this.ctx.model.Class;
  15. this.lomodel = this.ctx.model.Location;
  16. this.bmodel = this.ctx.model.Bedroom;
  17. this.setmodel = this.ctx.model.Setting;
  18. }
  19. async attendancecreate(data) {
  20. const { openid, type, ibeacon } = data;
  21. assert(openid && type && ibeacon, '缺少信息项');
  22. // 通过openid取得学生信息
  23. const user = await this.ctx.service.user.findByOpenid(openid);
  24. if (!user) {
  25. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '用户不存在');
  26. }
  27. const student = await this.stumodel.findById(user.uid);
  28. if (!student) {
  29. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '学生信息不存在');
  30. }
  31. // 取得设置表内考勤时间
  32. const setting = await this.setmodel.findOne();
  33. if (!setting) {
  34. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '考勤设置信息不存在');
  35. }
  36. // type为0是上课考勤
  37. if (type === '0') {
  38. const classes = await this.clamodel.findById(student.classid);
  39. if (!classes) {
  40. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '班级信息不存在');
  41. }
  42. const location = await this.lomodel.findById(classes.jslocationid);
  43. if (!location) {
  44. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '位置信息不存在');
  45. }
  46. if (location.ibeacon !== ibeacon) {
  47. throw new BusinessError(ErrorCode.BUSINESS, '考勤位置不正确');
  48. }
  49. const attendance = await this.model.findOne({ termid: student.termid, batchid: student.batchid, classid: student.classid, studentid: student.id });
  50. const datetime = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
  51. // TODO 考勤时间没有处理
  52. const diffres = await this.islate(datetime, setting, type);
  53. if (diffres === 0) {
  54. throw new BusinessError(ErrorCode.BUSINESS, '考勤时间不正确,请稍后在签到');
  55. }
  56. const newData = { date: datetime.substr(0, 10), time: datetime.substr(11, 5), type, status: diffres + '' };
  57. if (attendance) {
  58. // TODO: 保存数据
  59. const attends = attendance.attend;
  60. let result = false;
  61. for (const elm of attends) {
  62. if (elm.date === datetime.substr(0, 10)
  63. && elm.type === '0'
  64. && elm.time.substr(0, 2) === datetime.substr(11, 2)) {
  65. result = true;
  66. break;
  67. }
  68. }
  69. if (!result) {
  70. const newattend = await attendance.attend.create(newData);
  71. console.log('newattend:', newattend);
  72. attendance.attend.push(newattend);
  73. await attendance.save();
  74. }
  75. } else {
  76. const newdata = { termid: student.termid,
  77. batchid: student.batchid,
  78. classid: student.classid,
  79. studentid: student.id,
  80. attend: [ newData ],
  81. };
  82. await this.model.create(newdata);
  83. }
  84. } else if (type === '1') {
  85. console.log('寝室考勤');
  86. const beedroom = await this.bmodel.findOne({ code: student.bedroom });
  87. if (!beedroom) {
  88. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '寝室信息不存在');
  89. }
  90. if (beedroom.ibeacon !== ibeacon) {
  91. throw new BusinessError(ErrorCode.BUSINESS, '考勤位置不正确');
  92. }
  93. const attendance = await this.model.findOne({ termid: student.termid, batchid: student.batchid, classid: student.classid, studentid: student.id });
  94. const datetime = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
  95. // TODO 考勤时间没有处理
  96. const diffres = await this.islate(datetime, setting, type);
  97. if (diffres === 0) {
  98. throw new BusinessError(ErrorCode.BUSINESS, '考勤时间不正确,请稍后在签到');
  99. }
  100. const newData = { date: datetime.substr(0, 10), time: datetime.substr(11, 5), type, status: diffres + '' };
  101. if (attendance) {
  102. // TODO: 保存数据
  103. const attends = attendance.attend;
  104. let result = false;
  105. for (const elm of attends) {
  106. if (elm.date === datetime.substr(0, 10) && elm.type === '1') {
  107. result = true;
  108. break;
  109. }
  110. }
  111. if (!result) {
  112. const newattend = await attendance.attend.create(newData);
  113. console.log('newattend:', newattend);
  114. attendance.attend.push(newattend);
  115. await attendance.save();
  116. }
  117. } else {
  118. const newdata = { termid: student.termid,
  119. batchid: student.batchid,
  120. classid: student.classid,
  121. studentid: student.id,
  122. attend: [ newData ],
  123. };
  124. await this.model.create(newdata);
  125. }
  126. }
  127. }
  128. async test() {
  129. const setting = await this.setmodel.findOne();
  130. if (!setting) {
  131. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '考勤设置信息不存在');
  132. }
  133. const datetime = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
  134. // TODO 考勤时间没有处理
  135. const diffres = await this.islate(datetime, setting, '0');
  136. console.log(diffres);
  137. }
  138. // 判断上课考勤是否迟到和开始
  139. async islate(nowdata, setting, type) {
  140. nowdata = nowdata + ':00';
  141. let res = 0;
  142. if (type === '0') {
  143. const am_start = nowdata.substr(0, 10) + ' ' + setting.am_start + ':00';
  144. const am_end = nowdata.substr(0, 10) + ' ' + setting.am_end + ':00';
  145. const pm_start = nowdata.substr(0, 10) + ' ' + setting.pm_start + ':00';
  146. const pm_end = nowdata.substr(0, 10) + ' ' + setting.pm_end + ':00';
  147. const resamstart = moment(nowdata).diff(moment(am_start), 'minutes');
  148. const resamend = moment(nowdata).diff(moment(am_end), 'minutes');
  149. const respmstart = moment(nowdata).diff(moment(pm_start), 'minutes');
  150. const respmend = moment(nowdata).diff(moment(pm_end), 'minutes');
  151. // 当前时间大于am开始时间
  152. if (resamstart > 0) {
  153. // 当前时间小于pm开始时间
  154. if (respmstart < 0) {
  155. // 判断上午考勤是否迟到
  156. if (resamend > 0) {
  157. res = 2;
  158. } else {
  159. res = 1;
  160. }
  161. } else {
  162. // 判断下午考勤是否迟到
  163. if (respmend > 0) {
  164. res = 2;
  165. } else {
  166. res = 1;
  167. }
  168. }
  169. } else if (type === '1') {
  170. const bd_start = nowdata.substr(0, 10) + ' ' + setting.bd_start + ':00';
  171. const bd_end = nowdata.substr(0, 10) + ' ' + setting.bd_end + ':00';
  172. const resstart = moment(nowdata).diff(moment(bd_start), 'minutes');
  173. const resend = moment(nowdata).diff(moment(bd_end), 'minutes');
  174. if (resstart > 0) {
  175. if (resend > 0) {
  176. res = 2;
  177. } else {
  178. res = 1;
  179. }
  180. }
  181. }
  182. }
  183. return res;
  184. }
  185. }
  186. module.exports = AttendanceService;