attendance.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 newdatastu = { termid: student.termid,
  77. batchid: student.batchid,
  78. classid: student.classid,
  79. studentid: student.id,
  80. stuname: student.name,
  81. schname: student.school_name,
  82. schid: student.schid,
  83. attend: [ newData ],
  84. };
  85. await this.model.create(newdatastu);
  86. }
  87. } else if (type === '1') {
  88. console.log('寝室考勤');
  89. const beedroom = await this.bmodel.findOne({ code: student.bedroom });
  90. if (!beedroom) {
  91. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '寝室信息不存在');
  92. }
  93. if (beedroom.ibeacon !== ibeacon) {
  94. throw new BusinessError(ErrorCode.BUSINESS, '考勤位置不正确');
  95. }
  96. const attendance = await this.model.findOne({ termid: student.termid, batchid: student.batchid, classid: student.classid, studentid: student.id });
  97. const datetime = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
  98. // TODO 考勤时间没有处理
  99. const diffres = await this.islate(datetime, setting, type);
  100. if (diffres === 0) {
  101. throw new BusinessError(ErrorCode.BUSINESS, '考勤时间不正确,请稍后在签到');
  102. }
  103. const newData = { date: datetime.substr(0, 10), time: datetime.substr(11, 5), type, status: diffres + '' };
  104. if (attendance) {
  105. // TODO: 保存数据
  106. const attends = attendance.attend;
  107. let result = false;
  108. for (const elm of attends) {
  109. if (elm.date === datetime.substr(0, 10) && elm.type === '1') {
  110. result = true;
  111. break;
  112. }
  113. }
  114. if (!result) {
  115. const newattend = await attendance.attend.create(newData);
  116. console.log('newattend:', newattend);
  117. attendance.attend.push(newattend);
  118. await attendance.save();
  119. }
  120. } else {
  121. const newdatastu = { termid: student.termid,
  122. batchid: student.batchid,
  123. classid: student.classid,
  124. studentid: student.id,
  125. schid: student.schid,
  126. stuname: student.name,
  127. schname: student.school_name,
  128. attend: [ newData ],
  129. };
  130. await this.model.create(newdatastu);
  131. }
  132. }
  133. }
  134. async test() {
  135. const setting = await this.setmodel.findOne();
  136. if (!setting) {
  137. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '考勤设置信息不存在');
  138. }
  139. const datetime = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
  140. // TODO 考勤时间没有处理
  141. const diffres = await this.islate(datetime, setting, '0');
  142. console.log(diffres);
  143. }
  144. // 判断上课考勤是否迟到和开始
  145. async islate(nowdata, setting, type) {
  146. nowdata = nowdata + ':00';
  147. let res = 0;
  148. if (type === '0') {
  149. const am_start = nowdata.substr(0, 10) + ' ' + setting.am_start + ':00';
  150. const am_end = nowdata.substr(0, 10) + ' ' + setting.am_end + ':00';
  151. const pm_start = nowdata.substr(0, 10) + ' ' + setting.pm_start + ':00';
  152. const pm_end = nowdata.substr(0, 10) + ' ' + setting.pm_end + ':00';
  153. const resamstart = moment(nowdata).diff(moment(am_start), 'minutes');
  154. const resamend = moment(nowdata).diff(moment(am_end), 'minutes');
  155. const respmstart = moment(nowdata).diff(moment(pm_start), 'minutes');
  156. const respmend = moment(nowdata).diff(moment(pm_end), 'minutes');
  157. // 当前时间大于am开始时间
  158. if (resamstart > 0) {
  159. // 当前时间小于pm开始时间
  160. if (respmstart < 0) {
  161. // 判断上午考勤是否迟到
  162. if (resamend > 0) {
  163. res = 2;
  164. } else {
  165. res = 1;
  166. }
  167. } else {
  168. // 判断下午考勤是否迟到
  169. if (respmend > 0) {
  170. res = 2;
  171. } else {
  172. res = 1;
  173. }
  174. }
  175. } else if (type === '1') {
  176. const bd_start = nowdata.substr(0, 10) + ' ' + setting.bd_start + ':00';
  177. const bd_end = nowdata.substr(0, 10) + ' ' + setting.bd_end + ':00';
  178. const resstart = moment(nowdata).diff(moment(bd_start), 'minutes');
  179. const resend = moment(nowdata).diff(moment(bd_end), 'minutes');
  180. if (resstart > 0) {
  181. if (resend > 0) {
  182. res = 2;
  183. } else {
  184. res = 1;
  185. }
  186. }
  187. }
  188. }
  189. return res;
  190. }
  191. }
  192. module.exports = AttendanceService;