staff.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 StaffService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'staff');
  10. this.model = this.ctx.model.Staff;
  11. this.umodel = this.ctx.model.User;
  12. }
  13. async update({ id }, data) {
  14. const staff = await this.model.findById(id);
  15. const { name, gender, phone, address, birthday, id_number, dept_id, level_id, sort } = data;
  16. const user = (await this.umodel.find({ userid: id }))[0];
  17. if (name) {
  18. user.name = name;
  19. staff.name = name;
  20. await user.save();
  21. }
  22. if (gender) {
  23. staff.gender = gender;
  24. }
  25. if (phone) {
  26. const _user = await this.umodel.find({ phone });
  27. if (_.isEqual(_user.length, 0) || (_.isEqual(_user.length, 1) && _.isEqual(_user[0].id, user.id))) {
  28. user.phone = phone;
  29. staff.phone = phone;
  30. await user.save();
  31. } else {
  32. throw new BusinessError(ErrorCode.DATA_EXISTED);
  33. }
  34. }
  35. if (address) {
  36. staff.address = address;
  37. }
  38. if (birthday) {
  39. staff.birthday = birthday;
  40. }
  41. if (id_number) {
  42. staff.id_number = id_number;
  43. }
  44. if (dept_id) {
  45. staff.dept_id = dept_id;
  46. }
  47. if (level_id) {
  48. staff.level_id = level_id;
  49. }
  50. if (sort) {
  51. staff.sort = sort;
  52. }
  53. const res = await staff.save();
  54. return res;
  55. }
  56. }
  57. module.exports = StaffService;