attendance.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 student = require('../model/student');
  9. const { BusinessError, ErrorCode } = require('naf-core').Error;
  10. class AttendanceService extends CrudService {
  11. constructor(ctx) {
  12. super(ctx, 'attendance');
  13. this.model = this.ctx.model.Attendance;
  14. this.stumodel = this.ctx.model.Student;
  15. this.umodel = this.ctx.model.User;
  16. this.clamodel = this.ctx.model.Class;
  17. this.lomodel = this.ctx.model.Location;
  18. this.bmodel = this.ctx.model.Bedroom;
  19. this.setmodel = this.ctx.model.Setting;
  20. }
  21. async attendancecreate(data) {
  22. // 修改ibeacon=>[],存有 limit 范围内的所有蓝牙设备号
  23. const { openid, type, ibeacon } = data;
  24. assert(openid && type && ibeacon, '缺少信息项');
  25. // 通过openid取得学生信息
  26. const user = await this.umodel.findOne({ appopenid: openid });
  27. if (!user) {
  28. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '用户不存在');
  29. }
  30. console.log(user);
  31. const student = await this.stumodel.findById(user.uid);
  32. if (!student) {
  33. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '学生信息不存在');
  34. }
  35. // 取得设置表内考勤时间
  36. const setting = await this.setmodel.findOne();
  37. if (!setting) {
  38. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '考勤设置信息不存在');
  39. }
  40. // type为0是上课考勤
  41. if (type === '0') {
  42. const classes = await this.clamodel.findById(student.classid);
  43. if (!classes) {
  44. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '班级信息不存在');
  45. }
  46. const location = await this.lomodel.findById(classes.jslocationid);
  47. if (!location) {
  48. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '位置信息不存在');
  49. }
  50. // 判断,location的蓝牙号在不在ibeacon中
  51. const r = ibeacon.find(f => `${f}` === `${location.ibeacon}`);
  52. if (!r) {
  53. throw new BusinessError(ErrorCode.BUSINESS, '考勤位置不正确');
  54. }
  55. const attendance = await this.model.findOne({
  56. termid: student.termid,
  57. batchid: student.batchid,
  58. classid: student.classid,
  59. studentid: student.id,
  60. });
  61. const datetime = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
  62. // TODO 考勤时间没有处理
  63. const diffres = await this.islate(datetime, setting, type);
  64. if (diffres === 0) {
  65. throw new BusinessError(
  66. ErrorCode.BUSINESS,
  67. '考勤时间不正确,请稍后在签到'
  68. );
  69. }
  70. const newData = {
  71. date: datetime.substr(0, 10),
  72. time: datetime.substr(11, 5),
  73. type,
  74. status: diffres + '',
  75. };
  76. if (attendance) {
  77. // TODO: 保存数据
  78. const attends = attendance.attend;
  79. let result = false;
  80. for (const elm of attends) {
  81. if (
  82. elm.date === datetime.substr(0, 10) &&
  83. elm.type === '0' &&
  84. elm.time.substr(0, 2) === datetime.substr(11, 2)
  85. ) {
  86. result = true;
  87. break;
  88. }
  89. }
  90. if (!result) {
  91. const newattend = await attendance.attend.create(newData);
  92. console.log('newattend:', newattend);
  93. attendance.attend.push(newattend);
  94. await attendance.save();
  95. }
  96. } else {
  97. const newdatastu = {
  98. termid: student.termid,
  99. batchid: student.batchid,
  100. classid: student.classid,
  101. planyearid: student.planyearid,
  102. planid: student.planid,
  103. studentid: student.id,
  104. stuname: student.name,
  105. schname: student.school_name,
  106. schid: student.schid,
  107. attend: [ newData ],
  108. };
  109. await this.model.create(newdatastu);
  110. }
  111. } else if (type === '1') {
  112. console.log('寝室考勤');
  113. const beedroom = await this.bmodel.findOne({ code: student.bedroom });
  114. if (!beedroom) {
  115. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '寝室信息不存在');
  116. }
  117. const r = ibeacon.find(f => `${f}` === `${beedroom.ibeacon}`);
  118. if (!r) {
  119. throw new BusinessError(ErrorCode.BUSINESS, '考勤位置不正确');
  120. }
  121. const attendance = await this.model.findOne({
  122. termid: student.termid,
  123. batchid: student.batchid,
  124. classid: student.classid,
  125. studentid: student.id,
  126. });
  127. const datetime = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
  128. // TODO 考勤时间没有处理
  129. const diffres = await this.islate(datetime, setting, type);
  130. if (diffres === 0) {
  131. throw new BusinessError(
  132. ErrorCode.BUSINESS,
  133. '考勤时间不正确,请稍后在签到'
  134. );
  135. }
  136. const newData = {
  137. date: datetime.substr(0, 10),
  138. time: datetime.substr(11, 5),
  139. type,
  140. status: diffres + '',
  141. };
  142. if (attendance) {
  143. // TODO: 保存数据
  144. const attends = attendance.attend;
  145. let result = false;
  146. for (const elm of attends) {
  147. if (elm.date === datetime.substr(0, 10) && elm.type === '1') {
  148. result = true;
  149. break;
  150. }
  151. }
  152. if (!result) {
  153. const newattend = await attendance.attend.create(newData);
  154. console.log('newattend:', newattend);
  155. attendance.attend.push(newattend);
  156. await attendance.save();
  157. }
  158. } else {
  159. const newdatastu = {
  160. termid: student.termid,
  161. batchid: student.batchid,
  162. classid: student.classid,
  163. studentid: student.id,
  164. schid: student.schid,
  165. stuname: student.name,
  166. schname: student.school_name,
  167. attend: [ newData ],
  168. };
  169. await this.model.create(newdatastu);
  170. }
  171. }
  172. }
  173. async test() {
  174. const setting = await this.setmodel.findOne();
  175. if (!setting) {
  176. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '考勤设置信息不存在');
  177. }
  178. const datetime = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
  179. // TODO 考勤时间没有处理
  180. const diffres = await this.islate(datetime, setting, '0');
  181. console.log(diffres);
  182. }
  183. // 判断上课考勤是否迟到和开始
  184. async islate(nowdata, setting, type) {
  185. nowdata = nowdata + ':00';
  186. let res = 0;
  187. if (type === '0') {
  188. const am_start = nowdata.substr(0, 10) + ' ' + setting.am_start + ':00';
  189. const am_end = nowdata.substr(0, 10) + ' ' + setting.am_end + ':00';
  190. const pm_start = nowdata.substr(0, 10) + ' ' + setting.pm_start + ':00';
  191. const pm_end = nowdata.substr(0, 10) + ' ' + setting.pm_end + ':00';
  192. const resamstart = moment(nowdata).diff(moment(am_start), 'minutes');
  193. const resamend = moment(nowdata).diff(moment(am_end), 'minutes');
  194. const respmstart = moment(nowdata).diff(moment(pm_start), 'minutes');
  195. const respmend = moment(nowdata).diff(moment(pm_end), 'minutes');
  196. // 当前时间大于am开始时间
  197. if (resamstart > 0) {
  198. // 当前时间小于pm开始时间
  199. if (respmstart < 0) {
  200. // 判断上午考勤是否迟到
  201. if (resamend > 0) {
  202. res = 2;
  203. } else {
  204. res = 1;
  205. }
  206. } else {
  207. // 判断下午考勤是否迟到
  208. if (respmend > 0) {
  209. res = 2;
  210. } else {
  211. res = 1;
  212. }
  213. }
  214. } else if (type === '1') {
  215. const bd_start = nowdata.substr(0, 10) + ' ' + setting.bd_start + ':00';
  216. const bd_end = nowdata.substr(0, 10) + ' ' + setting.bd_end + ':00';
  217. const resstart = moment(nowdata).diff(moment(bd_start), 'minutes');
  218. const resend = moment(nowdata).diff(moment(bd_end), 'minutes');
  219. if (resstart > 0) {
  220. if (resend > 0) {
  221. res = 2;
  222. } else {
  223. res = 1;
  224. }
  225. }
  226. }
  227. }
  228. return res;
  229. }
  230. async attendancecreateList({ studentIds }) {
  231. for (const studentId of studentIds) {
  232. // 查出这个学生,更新这个学生的签到并签到
  233. const student = await this.stumodel.findById(studentId);
  234. if (!student) {
  235. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '学生信息不存在');
  236. }
  237. // 签到学生是否来了
  238. student.isComming = '1';
  239. await student.save();
  240. const attendInfo = {
  241. date: new Date().getDate(),
  242. time: new Date().getHours(),
  243. type: '0',
  244. status: '1',
  245. };
  246. const data = {
  247. planyearid: student.planyearid,
  248. planid: student.planid,
  249. termid: student.termid,
  250. batchid: student.batchid,
  251. classid: student.classid,
  252. schid: student.schid,
  253. schname: student.school_name,
  254. studentid: student.id,
  255. stuname: student.name,
  256. attendInfo,
  257. };
  258. await this.model.create(data);
  259. }
  260. }
  261. }
  262. module.exports = AttendanceService;