teacher.js 4.9 KB

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