teacher.js 5.5 KB

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