123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- 'use strict';
- const assert = require('assert');
- const _ = require('lodash');
- const XLSX = require('xlsx');
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const stringRandom = require('string-random');
- class TeacherService extends CrudService {
- constructor(ctx) {
- super(ctx, 'teacher');
- this.model = this.ctx.model.Teacher;
- this.umodel = this.ctx.model.User;
- }
- async query(info, { skip, limit }) {
- let res = await this.model
- .find(info)
- .skip(Number(skip))
- .limit(Number(limit));
- res = JSON.parse(JSON.stringify(res));
- for (const tea of res) {
- const r = await this.umodel.findOne({ uid: tea._id });
- if (r) tea.openid = _.get(r, 'openid');
- }
- return res;
- }
- // 根据状态删除教师信息
- async deleteByStatus({ status }) {
- await this.model.deleteMany({ status });
- return 'deleted';
- }
- // 查询详情
- async fetchTeacher({ id }) {
- // 将文件拼到查询到的数据后
- return await this.model.findById(id, '+file');
- }
- // 批量查询教师
- async fetchteachers({ ids }) {
- return await this.model.find({ _id: { $in: ids } });
- }
- // 检查教师
- async checkarrange({ id, date }) {}
- async status(data) {
- const { teachersid, zlscore, msscore, status, remark } = data;
- for (const teacherid of teachersid) {
- const teacher = await this.model.findById(teacherid);
- teacher.status = status;
- if (zlscore) {
- teacher.zlscore = zlscore;
- }
- if (msscore) {
- teacher.msscore = msscore;
- }
- await teacher.save();
- let detail = '';
- if (status === '1') {
- const passwd = stringRandom();
- detail =
- '您的账号身份已确认,密码为:' +
- passwd +
- '请尽快登录账号上传课件资料附件';
- // 状态更新后创建教师用户
- const newdata = {
- name: teacher.name,
- mobile: teacher.phone,
- type: '3',
- uid: teacher.id,
- gender: teacher.gender,
- passwd,
- };
- await this.ctx.service.user.create(newdata);
- } else if (status === '4') {
- detail = '您已通过审核被正式录入教师库';
- }
- const date = await this.ctx.service.util.updatedate();
- const user = await this.umodel.findOne({ uid: teacher.id, type: '3' });
- if (user && user.openid) {
- await this.ctx.service.weixin.sendTemplateMsg(
- this.ctx.app.config.REVIEW_TEMPLATE_ID,
- user.openid,
- '您有一个新的通知',
- detail,
- date,
- remark
- );
- } else {
- await this.ctx.service.util.sendMail(
- teacher.email,
- '账号审核',
- detail,
- ''
- );
- }
- }
- }
- // 教室分数上传
- async teaimport(data) {
- const { filepath } = data;
- assert(filepath, 'filepath不能为空');
- // 取得excle中数据
- const _filepath = this.ctx.app.config.baseUrl + filepath;
- const teadatas = await this.getImportXLSXData(_filepath);
- // 将得到的数据校验
- const datacheck = await this.datacheck(teadatas);
- if (datacheck.errorcode === '1') {
- return datacheck;
- }
- // 将数据存入数据库中
- for (const tea of teadatas) {
- const res = await this.model.findOne({
- idnumber: tea.idnumber,
- name: tea.name,
- });
- if (res) {
- res.xsscore = tea.xsscore;
- await res.save();
- }
- }
- return datacheck;
- }
- // 获取导入的XLSX文件中的数据
- async getImportXLSXData(filepath) {
- const file = await this.ctx.curl(filepath);
- const workbook = XLSX.read(file.data);
- // 读取内容
- let exceldata = [];
- const sheetNames = workbook.SheetNames; // 获取表名
- const sheet = workbook.Sheets[sheetNames[0]]; // 通过表名得到表对象
- // 遍历26个字母
- const theadRule = [];
- const range = XLSX.utils.decode_range(sheet['!ref']);
- const col_start = range.s.c;
- const col_end = range.e.c;
- for (let i = col_start; i <= col_end; i++) {
- const addr = XLSX.utils.encode_col(i) + XLSX.utils.encode_row(0);
- theadRule.push(sheet[addr].v);
- }
- const params = XLSX.utils.sheet_to_json(sheet); // 通过工具将表对象的数据读出来并转成json
- if (!params) return [];
- const length = params.length;
- const _datas = [];
- let data = {};
- for (let i = 0; i < length; i++) {
- data = params[i];
- _datas.push({
- idnumber: data[theadRule[1]],
- name: data[theadRule[2]],
- xsscore: data[theadRule[3]],
- });
- }
- exceldata = [ ...exceldata, ..._datas ];
- return exceldata;
- }
- // 获取导入的XLSX文件中的数据
- async datacheck(studatas) {
- let errorcode = '0';
- const errormsg = [];
- for (const data of studatas) {
- // 判断是否为空
- if (!data.idnumber) {
- errorcode = '1';
- data.msg = data.msg + '身份证号不允许为空,';
- }
- if (!data.name) {
- errorcode = '1';
- data.msg = data.msg + '姓名不允许为空,';
- }
- if (!data.xsscore) {
- errorcode = '1';
- data.msg = data.msg + '评分不允许为空,';
- }
- if (errorcode === '1') {
- errormsg.push(data);
- }
- }
- return { errorcode, errormsg };
- }
- }
- module.exports = TeacherService;
|