attendance.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const sd = require('silly-datetime');
  5. const { ObjectId } = require('mongoose').Types;
  6. const { CrudService } = require('naf-framework-mongoose/lib/service');
  7. const { BusinessError, ErrorCode } = require('naf-core').Error;
  8. class AttendanceService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'attendance');
  11. this.model = this.ctx.model.Attendance;
  12. this.stumodel = this.ctx.model.Student;
  13. this.clamodel = this.ctx.model.Class;
  14. this.lomodel = this.ctx.model.Location;
  15. this.bmodel = this.ctx.model.Bedroom;
  16. }
  17. async attendancecreate(data) {
  18. const { openid, type, ibeacon } = data;
  19. assert(openid && type && ibeacon, '缺少信息项');
  20. // 通过openid取得学生信息
  21. const user = await this.ctx.service.user.findByOpenid(openid);
  22. if (!user) {
  23. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '用户不存在');
  24. }
  25. const student = await this.stumodel.findById(user.uid);
  26. if (!student) {
  27. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '学生信息不存在');
  28. }
  29. // type为0是上课考勤
  30. if (type === '0') {
  31. const classes = await this.clamodel.findById(student.classid);
  32. if (!classes) {
  33. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '班级信息不存在');
  34. }
  35. const location = await this.lomodel.findById(classes.jslocationid);
  36. if (!location) {
  37. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '位置信息不存在');
  38. }
  39. if (location.ibeacon !== ibeacon) {
  40. throw new BusinessError(ErrorCode.BUSINESS, '考勤位置不正确');
  41. }
  42. const attendance = await this.model.findOne({ termid: student.termid, batchid: student.batchid, classid: student.classid, studentid: student.id });
  43. const datetime = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
  44. // TODO 考勤时间没有处理
  45. const newData = { date: datetime.substr(0, 10), time: datetime.substr(11, 5), type, status: '1' };
  46. if (attendance) {
  47. // TODO: 保存数据
  48. const attends = attendance.attend;
  49. let result = false;
  50. for (const elm of attends) {
  51. if (elm.date === datetime.substr(0, 10)
  52. && elm.type === '0'
  53. && elm.time.substr(0, 2) === datetime.substr(11, 2)) {
  54. result = true;
  55. break;
  56. }
  57. }
  58. if (!result) {
  59. const newattend = await attendance.attend.create(newData);
  60. console.log('newattend:', newattend);
  61. attendance.attend.push(newattend);
  62. await attendance.save();
  63. }
  64. } else {
  65. const newdata = { termid: student.termid,
  66. batchid: student.batchid,
  67. classid: student.classid,
  68. studentid: student.id,
  69. attend: [ newData ],
  70. };
  71. await this.model.create(newdata);
  72. }
  73. } else if (type === '1') {
  74. console.log('寝室考勤');
  75. const beedroom = await this.bmodel.findOne({ code: student.bedroom });
  76. if (!beedroom) {
  77. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '寝室信息不存在');
  78. }
  79. if (beedroom.ibeacon !== ibeacon) {
  80. throw new BusinessError(ErrorCode.BUSINESS, '考勤位置不正确');
  81. }
  82. const attendance = await this.model.findOne({ termid: student.termid, batchid: student.batchid, classid: student.classid, studentid: student.id });
  83. const datetime = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
  84. // TODO 考勤时间没有处理
  85. const newData = { date: datetime.substr(0, 10), time: datetime.substr(11, 5), type, status: '1' };
  86. if (attendance) {
  87. // TODO: 保存数据
  88. const attends = attendance.attend;
  89. let result = false;
  90. for (const elm of attends) {
  91. if (elm.date === datetime.substr(0, 10) && elm.type === '1') {
  92. result = true;
  93. break;
  94. }
  95. }
  96. if (!result) {
  97. const newattend = await attendance.attend.create(newData);
  98. console.log('newattend:', newattend);
  99. attendance.attend.push(newattend);
  100. await attendance.save();
  101. }
  102. } else {
  103. const newdata = { termid: student.termid,
  104. batchid: student.batchid,
  105. classid: student.classid,
  106. studentid: student.id,
  107. attend: [ newData ],
  108. };
  109. await this.model.create(newdata);
  110. }
  111. }
  112. }
  113. }
  114. module.exports = AttendanceService;