123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- '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 HeadteacherService extends CrudService {
- constructor(ctx) {
- super(ctx, 'headteacher');
- this.model = this.ctx.model.Headteacher;
- this.umodel = this.ctx.model.User;
- }
- async create(data) {
- const { name, phone } = data;
- const user = await this.umodel.findOne({ mobile: phone });
- if (user) {
- throw new BusinessError(ErrorCode.DATA_EXIST, '电话已经存在');
- }
- console.log(data);
- const res = await this.model.create(data);
- const newdata = { name, mobile: phone, type: '1', uid: res.id };
- newdata.passwd = { secret: '12345678' };
- await this.umodel.create(newdata);
- }
- async update({ id }, data) {
- const { name, phone, openid } = data;
- const user = await this.umodel.findOne({ mobile: phone });
- const res = await this.model.update({ _id: ObjectId(id) }, data);
- if (res) {
- const _user = await this.umodel.findOne({ uid: id, type: '1' });
- if (_user) {
- _user.mobile = phone;
- _user.name = name;
- if (openid) {
- _user.openid = openid;
- }
- _user.save();
- }
- }
- return res;
- }
- async delete({ id }) {
- const res = await this.model.findByIdAndDelete(id);
- const _user = await this.umodel.findOne({ uid: id, thpe: '1' });
- _user.delete();
- return res;
- }
- }
- module.exports = HeadteacherService;
|