teacher.js 5.5 KB

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