'use strict'; const assert = require('assert'); const _ = require('lodash'); const { ObjectId } = require('mongoose').Types; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; class UserService extends CrudService { constructor(ctx) { super(ctx, 'user'); this.model = this.ctx.model.User; this.stuModel = this.ctx.model.Student; this.tModel = this.ctx.model.Teacher; this.schModel = this.ctx.model.School; this.hModel = this.ctx.model.Headteacher; } // 重写创建方法 async create(data) { const { name, mobile, passwd, type, gender } = data; assert(name && mobile && passwd && type, '缺少部分信息项'); assert(/^\d{11}$/i.test(mobile), 'mobile无效'); const newdata = data; newdata.passwd = { secret: passwd }; const res = await this.model.create(newdata); if (res) { // 如果是班主任的时候将用户信息填入班主任表 if (type === '1') { const headt = { name, phone: mobile, gender }; await this.hModel.create(headt); } } return res; } } module.exports = UserService;