123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- '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 StaffService extends CrudService {
- constructor(ctx) {
- super(ctx, 'staff');
- this.model = this.ctx.model.Staff;
- this.umodel = this.ctx.model.User;
- }
- async update({ id }, data) {
- const staff = await this.model.findById(id);
- const { name, gender, phone, address, birthday, id_number, dept_id, level_id, sort } = data;
- const user = (await this.umodel.find({ userid: id }))[0];
- if (name) {
- user.name = name;
- staff.name = name;
- await user.save();
- }
- if (gender) {
- staff.gender = gender;
- }
- if (phone) {
- const _user = await this.umodel.find({ phone });
- if (_.isEqual(_user.length, 0) || (_.isEqual(_user.length, 1) && _.isEqual(_user[0].id, user.id))) {
- user.phone = phone;
- staff.phone = phone;
- await user.save();
- } else {
- throw new BusinessError(ErrorCode.DATA_EXISTED);
- }
- }
- if (address) {
- staff.address = address;
- }
- if (birthday) {
- staff.birthday = birthday;
- }
- if (id_number) {
- staff.id_number = id_number;
- }
- if (dept_id) {
- staff.dept_id = dept_id;
- }
- if (level_id) {
- staff.level_id = level_id;
- }
- if (sort) {
- staff.sort = sort;
- }
- const res = await staff.save();
- return res;
- }
- }
- module.exports = StaffService;
|