headteacher.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. 'use strict';
  2. const assert = require('assert');
  3. const { trimData } = require('naf-core').Util;
  4. const _ = require('lodash');
  5. const { ObjectId } = require('mongoose').Types;
  6. const { CrudService } = require('naf-framework-mongoose/lib/service');
  7. const { BusinessError, ErrorCode } = require('naf-core').Error;
  8. class HeadteacherService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'headteacher');
  11. this.model = this.ctx.model.Headteacher;
  12. this.umodel = this.ctx.model.User;
  13. this.yjy = this.ctx.model.Yjyconnect;
  14. }
  15. async query(filter, { skip, limit, sort, desc, projection } = {}) {
  16. // 处理排序
  17. if (sort && _.isString(sort)) {
  18. sort = { [sort]: desc ? -1 : 1 };
  19. } else if (sort && _.isArray(sort)) {
  20. sort = sort.map(f => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
  21. }
  22. let rs = await this.model.find(trimData(filter), projection, { skip, limit, sort }).lean();
  23. if (rs.length > 0) rs = JSON.parse(JSON.stringify(rs));
  24. const ids = rs.map(i => ObjectId(i._id).toString());
  25. const users = await this.umodel.find({ uid: ids }).lean();
  26. const uids = users.map(i => ObjectId(i._id).toString());
  27. const yjyUsers = await this.yjy.find({ suid: uids }).lean();
  28. for (const i of rs) {
  29. const { _id: did } = i;
  30. i.id = did;
  31. const user = users.find(f => ObjectId(did).equals(f.uid));
  32. if (!user) {
  33. i.is_bind = false;
  34. continue;
  35. }
  36. const yjyUser = yjyUsers.find(f => ObjectId(user._id).equals(f.suid));
  37. if (yjyUser) i.is_bind = true;
  38. else i.is_bind = false;
  39. }
  40. return rs;
  41. }
  42. async create(data) {
  43. const { name, mobile } = data;
  44. const user = await this.umodel.findOne({ mobile });
  45. if (user) {
  46. throw new BusinessError(ErrorCode.DATA_EXIST, '电话已经存在');
  47. }
  48. const res = await this.model.create(data);
  49. const newdata = { name, mobile, type: '1', uid: res.id };
  50. newdata.passwd = { secret: '12345678' };
  51. await this.umodel.create(newdata);
  52. return await this.model.findById(res.id || res._id);
  53. }
  54. async update({ id }, data) {
  55. const { name, mobile, openid } = data;
  56. // 修改后的手机号查找是否已存在
  57. const have_mobile = await this.umodel.count({ mobile, uid: { $ne: id } });
  58. if (have_mobile > 0) throw new BusinessError(ErrorCode.DATA_EXIST, '电话已经存在');
  59. const res = await this.model.update({ _id: ObjectId(id) }, data);
  60. if (res) {
  61. const _user = await this.umodel.findOne({ uid: id, type: '1' });
  62. if (_user) {
  63. _user.mobile = mobile;
  64. _user.name = name;
  65. if (openid) {
  66. _user.openid = openid;
  67. }
  68. _user.save();
  69. }
  70. }
  71. return res;
  72. }
  73. async delete({ id }) {
  74. await this.model.findByIdAndDelete(id);
  75. await this.umodel.deleteOne({ uid: id, type: '1' });
  76. return 'deleted';
  77. }
  78. }
  79. module.exports = HeadteacherService;