'use strict'; const { CrudService } = require('naf-framework-mongoose-free/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const _ = require('lodash'); const assert = require('assert'); // class UserService extends CrudService { constructor(ctx) { super(ctx, 'user'); this.mysql = this.ctx.service.mysql; this.userRelation = this.ctx.model.UserRelation; } async query({ superior_id, ...others }, { skip = 0, limit = 0 } = {}) { assert(superior_id, '缺少查询信息'); // 通过上级id找关系 const relation = await this.userRelation.find({ superior_id }).skip(parseInt(skip)).limit(parseInt(limit)); const total = await this.userRelation.count({ superior_id }); // 确定好是哪些用户,然后拿id换用户信息 const ids = relation.map(i => i.user_id); if (ids.length <= 0) return { data: [], total }; const table = 'sys_user'; let { data } = await this.mysql.query(table, { user_id: ids, ...others }); data = data.map(i => _.omit(i, [ 'password' ])); // 将key转换成java部分的驼峰显示 data = data.map(i => { const keys = Object.keys(i); const newObject = {}; for (const key of keys) { const value = i[key]; const newKey = _.camelCase(key); newObject[newKey] = value; } return newObject; }); return { data, total }; } // 创建上下级用户关系 async create(body) { const data = await this.userRelation.create(body); return data; } // 删除上下级用户关系 async delete({ id }) { const result = await this.userRelation.deleteOne({ user_id: id }); return result; } } module.exports = UserService;