teacher.js 4.7 KB

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