spm.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. // 只属于这个项目的工具service
  7. class SpmService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'spm');
  10. this.org = this.ctx.model.Organization;
  11. this.personal = this.ctx.model.Personal;
  12. }
  13. /**
  14. * 入口,根据method,分配到函数中进行处理,返回结果
  15. * @param {Object} {method} method:在这个service的函数
  16. * @param {*} body 条件
  17. */
  18. async index({ method }, body) {
  19. return await this[method](body);
  20. }
  21. /**
  22. * 获取用户信息,个人(专家)/企业都有可能
  23. * @param {Object} condition 条件
  24. */
  25. async getAllUser(condition) {
  26. const { ids } = condition;
  27. const projection = 'name phone';
  28. // 直接先用ids在企业表中查询
  29. let arr = [];
  30. const org = await this.org.find({ _id: ids }, projection);
  31. arr = arr.concat(org);
  32. if (org.length !== ids.length) {
  33. // 还有个人用户,再查
  34. const personal = await this.personal.find({ _id: ids }, projection);
  35. arr = arr.concat(personal);
  36. }
  37. return arr;
  38. }
  39. /**
  40. * 根据条件查找用户
  41. * @param {Object} condition 查询条件
  42. */
  43. async getUser(condition) {
  44. let res;
  45. // 先查是不是企业
  46. const org = await this.org.findOne(condition);
  47. if (org) res = org;
  48. else {
  49. const personal = await this.personal.findOne(condition);
  50. if (personal) res = personal;
  51. }
  52. return res;
  53. }
  54. /**
  55. * 直接操作model做什么
  56. * @param {Object} condition 条件
  57. * @property {String} model 表
  58. * @property {String} method mongoose的函数
  59. * @property {Object} query 查询条件
  60. * @property {Object} body 添加/修改数据
  61. * @property {String/Object} projection 查询使用,选取指定字段
  62. */
  63. async useModel(condition = {}) {
  64. const { method, query = {}, body = {}, projection } = condition;
  65. let { model } = condition;
  66. model = _.capitalize(model);
  67. assert(model, '缺少指定表');
  68. assert(method, '缺少使用函数');
  69. // 区分method的情况
  70. let res;
  71. // 添加/批量添加
  72. if (method === 'create' || method === 'insertMany') {
  73. res = await this.ctx.model[model][method](body);
  74. }
  75. // 修改(updateOne)/批量修改(updateMany)
  76. if (method.includes('update')) {
  77. res = await this.ctx.model[model][method](query, body);
  78. }
  79. // 删除(deleteOne)/批量删除(deleteMany)
  80. if (method.includes('delete')) {
  81. res = await this.ctx.model[model][method](query);
  82. }
  83. // 查询(find,findById,findOne),反正都是只要query
  84. if (method.includes('find')) {
  85. res = await this.ctx.model[model][method](query, projection);
  86. }
  87. // 再下来,就是聚合了,聚合就算了吧.这个还跨项目写就没啥必要了
  88. return res;
  89. }
  90. /**
  91. * 调用service
  92. * @param {Object} condition 条件
  93. * @property {String} model 表
  94. * @property {String} method mongoose的函数
  95. */
  96. async useService(condition) {
  97. const { service, method, body } = condition;
  98. assert(service, '缺少指定表');
  99. assert(method, '缺少使用函数');
  100. const arr = service.split('.');
  101. let serve = this.ctx.service;
  102. for (const i of arr) {
  103. serve = serve[i];
  104. }
  105. return await serve[method](body);
  106. }
  107. }
  108. module.exports = SpmService;