123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- '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');
- // 只属于这个项目的工具service
- class SpmService extends CrudService {
- constructor(ctx) {
- super(ctx, 'spm');
- this.org = this.ctx.model.Organization;
- this.personal = this.ctx.model.Personal;
- }
- /**
- * 入口,根据method,分配到函数中进行处理,返回结果
- * @param {Object} {method} method:在这个service的函数
- * @param {*} body 条件
- */
- async index({ method }, body) {
- return await this[method](body);
- }
- /**
- * 获取用户信息,个人(专家)/企业都有可能
- * @param {Object} condition 条件
- */
- async getAllUser(condition) {
- const { ids } = condition;
- const projection = 'name phone';
- // 直接先用ids在企业表中查询
- let arr = [];
- const org = await this.org.find({ _id: ids }, projection);
- arr = arr.concat(org);
- if (org.length !== ids.length) {
- // 还有个人用户,再查
- const personal = await this.personal.find({ _id: ids }, projection);
- arr = arr.concat(personal);
- }
- return arr;
- }
- /**
- * 根据条件查找用户
- * @param {Object} condition 查询条件
- */
- async getUser(condition) {
- let res;
- // 先查是不是企业
- const org = await this.org.findOne(condition);
- if (org) res = org;
- else {
- const personal = await this.personal.findOne(condition);
- if (personal) res = personal;
- }
- return res;
- }
- /**
- * 直接操作model做什么
- * @param {Object} condition 条件
- * @property {String} model 表
- * @property {String} method mongoose的函数
- * @property {Object} query 查询条件
- * @property {Object} body 添加/修改数据
- * @property {String/Object} projection 查询使用,选取指定字段
- */
- async useModel(condition = {}) {
- const { method, query = {}, body = {}, projection } = condition;
- let { model } = condition;
- model = _.capitalize(model);
- assert(model, '缺少指定表');
- assert(method, '缺少使用函数');
- // 区分method的情况
- let res;
- // 添加/批量添加
- if (method === 'create' || method === 'insertMany') {
- res = await this.ctx.model[model][method](body);
- }
- // 修改(updateOne)/批量修改(updateMany)
- if (method.includes('update')) {
- res = await this.ctx.model[model][method](query, body);
- }
- // 删除(deleteOne)/批量删除(deleteMany)
- if (method.includes('delete')) {
- res = await this.ctx.model[model][method](query);
- }
- // 查询(find,findById,findOne),反正都是只要query
- if (method.includes('find')) {
- res = await this.ctx.model[model][method](query, projection);
- }
- // 再下来,就是聚合了,聚合就算了吧.这个还跨项目写就没啥必要了
- return res;
- }
- /**
- * 调用service
- * @param {Object} condition 条件
- * @property {String} model 表
- * @property {String} method mongoose的函数
- */
- async useService(condition) {
- const { service, method, body } = condition;
- assert(service, '缺少指定表');
- assert(method, '缺少使用函数');
- const arr = service.split('.');
- let serve = this.ctx.service;
- for (const i of arr) {
- serve = serve[i];
- }
- return await serve[method](body);
- }
- }
- module.exports = SpmService;
|