'use strict'; const assert = require('assert'); const _ = require('lodash'); 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 deleteByStatus({ status }) { await this.model.deleteMany({ status }); return 'deleted'; } // 查询详情 async fetchTeacher({ id }) { // 将文件拼到查询到的数据后 return await this.model.findById(id, '+file'); } 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 }; newdata.passwd = { secret: passwd }; await this.umodel.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, teacher.openid, '您有一个新的通知', detail, date, remark); } else { await this.ctx.service.util.sendMail(teacher.email, '账号审核', detail, ''); } } } } module.exports = TeacherService;