teacher.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. res.xsscore = tea.xsscore;
  76. await res.save();
  77. }
  78. return datacheck;
  79. }
  80. // 获取导入的XLSX文件中的数据
  81. async getImportXLSXData(filepath) {
  82. console.log(filepath);
  83. const file = await this.ctx.curl(filepath);
  84. const workbook = XLSX.read(file.data);
  85. // 读取内容
  86. let exceldata = [];
  87. const sheetNames = workbook.SheetNames; // 获取表名
  88. const sheet = workbook.Sheets[sheetNames[0]]; // 通过表名得到表对象
  89. // 遍历26个字母
  90. const theadRule = [];
  91. const range = XLSX.utils.decode_range(sheet['!ref']);
  92. const col_start = range.s.c;
  93. const col_end = range.e.c;
  94. for (let i = col_start; i <= col_end; i++) {
  95. const addr = XLSX.utils.encode_col(i) + XLSX.utils.encode_row(0);
  96. theadRule.push(sheet[addr].v);
  97. }
  98. const params = XLSX.utils.sheet_to_json(sheet); // 通过工具将表对象的数据读出来并转成json
  99. if (!params) return [];
  100. const length = params.length;
  101. const _datas = [];
  102. let data = {};
  103. for (let i = 0; i < length; i++) {
  104. data = params[i];
  105. _datas.push({
  106. idnumber: data[theadRule[1]],
  107. name: data[theadRule[2]],
  108. xsscore: data[theadRule[3]],
  109. });
  110. }
  111. exceldata = [ ...exceldata, ..._datas ];
  112. return exceldata;
  113. }
  114. // 获取导入的XLSX文件中的数据
  115. async datacheck(studatas) {
  116. let errorcode = '0';
  117. const errormsg = [];
  118. for (const data of studatas) {
  119. // 判断是否为空
  120. if (!data.idnumber) {
  121. errorcode = '1';
  122. data.msg = data.msg + '身份证号不允许为空,';
  123. }
  124. if (!data.name) {
  125. errorcode = '1';
  126. data.msg = data.msg + '姓名不允许为空,';
  127. }
  128. if (!data.xsscore) {
  129. errorcode = '1';
  130. data.msg = data.msg + '评分不允许为空,';
  131. }
  132. if (errorcode === '1') {
  133. errormsg.push(data);
  134. }
  135. }
  136. return { errorcode, errormsg };
  137. }
  138. }
  139. module.exports = TeacherService;