teacher.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const XLSX = require('xlsx');
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. const stringRandom = require('string-random');
  8. class TeacherService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'teacher');
  11. this.model = this.ctx.model.Teacher;
  12. this.umodel = this.ctx.model.User;
  13. }
  14. // 根据状态删除教师信息
  15. async deleteByStatus({ status }) {
  16. await this.model.deleteMany({ status });
  17. return 'deleted';
  18. }
  19. // 查询详情
  20. async fetchTeacher({ id }) {
  21. // 将文件拼到查询到的数据后
  22. return await this.model.findById(id, '+file');
  23. }
  24. // 批量查询教师
  25. async fetchteachers({ ids }) {
  26. return await this.model.find({ _id: { $in: ids } });
  27. }
  28. // 检查教师
  29. async checkarrange({ id, date }) {
  30. }
  31. async status(data) {
  32. const { teachersid, zlscore, msscore, status, remark } = data;
  33. for (const teacherid of teachersid) {
  34. const teacher = await this.model.findById(teacherid);
  35. teacher.status = status;
  36. if (zlscore) {
  37. teacher.zlscore = zlscore;
  38. }
  39. if (msscore) {
  40. teacher.msscore = msscore;
  41. }
  42. await teacher.save();
  43. let detail = '';
  44. if (status === '1') {
  45. const passwd = stringRandom();
  46. detail = '您的账号身份已确认,密码为:' + passwd + '请尽快登录账号上传课件资料附件';
  47. // 状态更新后创建教师用户
  48. const newdata = { name: teacher.name, mobile: teacher.phone, type: '3', uid: teacher.id, gender: teacher.gender, passwd };
  49. await this.ctx.service.user.create(newdata);
  50. } else if (status === '4') {
  51. detail = '您已通过审核被正式录入教师库';
  52. }
  53. const date = await this.ctx.service.util.updatedate();
  54. console.log(teacher.id);
  55. const user = await this.umodel.findOne({ uid: teacher.id, type: '3' });
  56. console.log(user);
  57. if (user && user.openid) {
  58. await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, user.openid, '您有一个新的通知', detail, date, remark);
  59. } else {
  60. await this.ctx.service.util.sendMail(teacher.email, '账号审核', detail, '');
  61. }
  62. }
  63. }
  64. // 教室分数上传
  65. async teaimport(data) {
  66. const { filepath } = data;
  67. assert(filepath, 'filepath不能为空');
  68. // 取得excle中数据
  69. const _filepath = this.ctx.app.config.baseUrl + filepath;
  70. console.log(_filepath);
  71. const teadatas = await this.getImportXLSXData(
  72. _filepath
  73. );
  74. // 将得到的数据校验
  75. const datacheck = await this.datacheck(teadatas);
  76. if (datacheck.errorcode === '1') {
  77. return datacheck;
  78. }
  79. // 将数据存入数据库中
  80. for (const tea of teadatas) {
  81. const res = await this.model.findOne({ idnumber: tea.idnumber, name: tea.name });
  82. if (res) {
  83. res.xsscore = tea.xsscore;
  84. await res.save();
  85. }
  86. }
  87. return datacheck;
  88. }
  89. // 获取导入的XLSX文件中的数据
  90. async getImportXLSXData(filepath) {
  91. console.log(filepath);
  92. const file = await this.ctx.curl(filepath);
  93. const workbook = XLSX.read(file.data);
  94. // 读取内容
  95. let exceldata = [];
  96. const sheetNames = workbook.SheetNames; // 获取表名
  97. const sheet = workbook.Sheets[sheetNames[0]]; // 通过表名得到表对象
  98. // 遍历26个字母
  99. const theadRule = [];
  100. const range = XLSX.utils.decode_range(sheet['!ref']);
  101. const col_start = range.s.c;
  102. const col_end = range.e.c;
  103. for (let i = col_start; i <= col_end; i++) {
  104. const addr = XLSX.utils.encode_col(i) + XLSX.utils.encode_row(0);
  105. theadRule.push(sheet[addr].v);
  106. }
  107. const params = XLSX.utils.sheet_to_json(sheet); // 通过工具将表对象的数据读出来并转成json
  108. if (!params) return [];
  109. const length = params.length;
  110. const _datas = [];
  111. let data = {};
  112. for (let i = 0; i < length; i++) {
  113. data = params[i];
  114. _datas.push({
  115. idnumber: data[theadRule[1]],
  116. name: data[theadRule[2]],
  117. xsscore: data[theadRule[3]],
  118. });
  119. }
  120. exceldata = [ ...exceldata, ..._datas ];
  121. return exceldata;
  122. }
  123. // 获取导入的XLSX文件中的数据
  124. async datacheck(studatas) {
  125. let errorcode = '0';
  126. const errormsg = [];
  127. for (const data of studatas) {
  128. // 判断是否为空
  129. if (!data.idnumber) {
  130. errorcode = '1';
  131. data.msg = data.msg + '身份证号不允许为空,';
  132. }
  133. if (!data.name) {
  134. errorcode = '1';
  135. data.msg = data.msg + '姓名不允许为空,';
  136. }
  137. if (!data.xsscore) {
  138. errorcode = '1';
  139. data.msg = data.msg + '评分不允许为空,';
  140. }
  141. if (errorcode === '1') {
  142. errormsg.push(data);
  143. }
  144. }
  145. return { errorcode, errormsg };
  146. }
  147. }
  148. module.exports = TeacherService;