teamApply.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. // 过滤出ref字段
  51. const { refMods, populate } = await this.getRefMods();
  52. // 带ref查询
  53. let rs = await this.model.find(condition, projection, { skip, limit, sort }).populate(populate).exec();
  54. rs = JSON.parse(JSON.stringify(rs));
  55. // 整理ref数据
  56. rs = rs.map(i => {
  57. for (const obj of refMods) {
  58. const { col, prop, type } = obj;
  59. if (!prop) continue;
  60. if (_.isArray(prop)) {
  61. for (const p of prop) {
  62. if (type === 'String') i[`${col}_${p}`] = _.get(i, `${col}.${p}`);
  63. if (type === 'Array') {
  64. const list = [];
  65. const oList = _.get(i, `${col}`);
  66. for (const d of oList) {
  67. const obj = { _id: d._id };
  68. obj[p] = _.get(d, p);
  69. list.push(obj);
  70. }
  71. i[`${col}_${p}`] = list;
  72. }
  73. }
  74. i[col] = _.get(i, `${col}._id`);
  75. }
  76. }
  77. return i;
  78. });
  79. rs = await this.afterQuery(filter, rs);
  80. return rs;
  81. }
  82. async count(filter) {
  83. let condition = _.cloneDeep(filter);
  84. condition = await this.beforeQuery(condition);
  85. condition = this.dealFilter(condition);
  86. condition = this.selfQueryDeal(condition);
  87. const count = await this.model.count(condition);
  88. return count;
  89. }
  90. selfQueryDeal(condition) {
  91. if (_.get(condition, 'user_id')) {
  92. const user_id = _.get(condition, 'user_id');
  93. condition.$or = [{ one_member_id: user_id }, { two_member_id: user_id }];
  94. delete condition.user_id;
  95. }
  96. return condition;
  97. }
  98. }
  99. module.exports = TeamApplyService;