teacher.js 5.4 KB

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