teacher.js 5.8 KB

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