1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- '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, mobile } = data;
- const user = await this.umodel.findOne({ mobile });
- if (user) {
- throw new BusinessError(ErrorCode.DATA_EXIST, '电话已经存在');
- }
- const res = await this.model.create(data);
- const newdata = { name, mobile, type: '1', uid: res.id };
- newdata.passwd = { secret: '12345678' };
- await this.umodel.create(newdata);
- }
- async update({ id }, data) {
- const { name, mobile, openid } = data;
- // 修改后的手机号查找是否已存在
- console.log(mobile);
- const have_mobile = await this.umodel.count({ mobile, uid: { $ne: id } });
- if (have_mobile > 0) throw new BusinessError(ErrorCode.DATA_EXIST, '电话已经存在');
- 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 = mobile;
- _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;
|