teamApply.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. //
  7. class TeamApplyService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'teamapply');
  10. this.model = this.ctx.model.Race.TeamApply;
  11. this.matchSignModel = this.ctx.model.Race.MatchSign;
  12. }
  13. /**
  14. * 查询项目下可以组队的人
  15. * @param {Object} query 地址栏查询条件
  16. * @property {String} project_id 项目地址
  17. * @property {String} user_id 项目地址
  18. */
  19. async findPartner({ project_id, user_id }) {
  20. // 多层嵌套populate
  21. const conn = this.app.mongooseDB.get('base');
  22. const schema = _.get(this.ctx.model, 'Base.User.schema');
  23. const m = conn.model('User', schema);
  24. let list = await this.matchSignModel.find({ user_id: { $ne: user_id }, pay_status: '1', project_id }).populate({
  25. path: 'user_id',
  26. select: 'user_id',
  27. populate: { path: 'user_id', model: m, select: 'name' },
  28. });
  29. if (list.length > 0) {
  30. list = JSON.parse(JSON.stringify(list));
  31. list = list.map(i => {
  32. i.user_name = _.get(i, 'user_id.user_id.name');
  33. i.user_id = _.get(i, 'user_id._id');
  34. return i;
  35. });
  36. }
  37. return list;
  38. }
  39. async query(filter, { skip = 0, limit, sort, desc, projection } = {}) {
  40. // 处理排序
  41. if (sort && _.isString(sort)) {
  42. sort = { [sort]: desc ? -1 : 1 };
  43. } else if (sort && _.isArray(sort)) {
  44. sort = sort.map(f => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
  45. }
  46. let condition = _.cloneDeep(filter);
  47. condition = await this.beforeQuery(condition);
  48. condition = this.dealFilter(condition);
  49. condition = this.selfQueryDeal(condition);
  50. console.log(condition);
  51. // 过滤出ref字段
  52. const { refMods, populate } = await this.getRefMods();
  53. // 带ref查询
  54. let rs = await this.model.find(condition, projection, { skip, limit, sort }).populate(populate).exec();
  55. rs = JSON.parse(JSON.stringify(rs));
  56. // 整理ref数据
  57. rs = rs.map(i => {
  58. for (const obj of refMods) {
  59. const { col, prop, type } = obj;
  60. if (!prop) continue;
  61. if (_.isArray(prop)) {
  62. for (const p of prop) {
  63. if (type === 'String') i[`${col}_${p}`] = _.get(i, `${col}.${p}`);
  64. if (type === 'Array') {
  65. const list = [];
  66. const oList = _.get(i, `${col}`);
  67. for (const d of oList) {
  68. const obj = { _id: d._id };
  69. obj[p] = _.get(d, p);
  70. list.push(obj);
  71. }
  72. i[`${col}_${p}`] = list;
  73. }
  74. }
  75. i[col] = _.get(i, `${col}._id`);
  76. }
  77. }
  78. return i;
  79. });
  80. rs = await this.afterQuery(filter, rs);
  81. return rs;
  82. }
  83. selfQueryDeal(condition) {
  84. if (_.get(condition, 'user_id')) {
  85. const user_id = _.get(condition, 'user_id');
  86. condition.$or = [{ one_member_id: user_id }, { two_member_id: user_id }];
  87. delete condition.user_id;
  88. }
  89. return condition;
  90. }
  91. }
  92. module.exports = TeamApplyService;