attendance.js 7.5 KB

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