headteacher.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. class HeadteacherService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'headteacher');
  10. this.model = this.ctx.model.Headteacher;
  11. this.umodel = this.ctx.model.User;
  12. }
  13. async create(data) {
  14. const { name, phone } = data;
  15. const user = await this.umodel.findOne({ mobile: phone });
  16. if (user) {
  17. throw new BusinessError(ErrorCode.DATA_EXIST, '电话已经存在');
  18. }
  19. console.log(data);
  20. const res = await this.model.create(data);
  21. const newdata = { name, mobile: phone, type: '1', uid: res.id };
  22. newdata.passwd = { secret: '12345678' };
  23. await this.umodel.create(newdata);
  24. }
  25. async update({ id }, data) {
  26. const { name, phone, openid } = data;
  27. const user = await this.umodel.findOne({ mobile: phone });
  28. const res = await this.model.update({ _id: ObjectId(id) }, data);
  29. if (res) {
  30. const _user = await this.umodel.findOne({ uid: id, type: '1' });
  31. if (_user) {
  32. _user.mobile = phone;
  33. _user.name = name;
  34. if (openid) {
  35. _user.openid = openid;
  36. }
  37. _user.save();
  38. }
  39. }
  40. return res;
  41. }
  42. async delete({ id }) {
  43. const res = await this.model.findByIdAndDelete(id);
  44. const _user = await this.umodel.findOne({ uid: id, thpe: '1' });
  45. _user.delete();
  46. return res;
  47. }
  48. }
  49. module.exports = HeadteacherService;