teamApply.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. const arr = [];
  32. for (const i of list) {
  33. const { user_id } = i;
  34. const num = await this.model.count({ $or: [{ one_member_id: user_id }, { two_member_id: user_id }] });
  35. if (num > 0) continue;
  36. i.user_name = _.get(i, 'user_id.user_id.name');
  37. i.user_id = _.get(i, 'user_id._id');
  38. arr.push(i);
  39. }
  40. list = arr;
  41. }
  42. return list;
  43. }
  44. async query(filter, { skip = 0, limit, sort, desc, projection } = {}) {
  45. // 处理排序
  46. if (sort && _.isString(sort)) {
  47. sort = { [sort]: desc ? -1 : 1 };
  48. } else if (sort && _.isArray(sort)) {
  49. sort = sort.map(f => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
  50. }
  51. let condition = _.cloneDeep(filter);
  52. condition = await this.beforeQuery(condition);
  53. condition = this.dealFilter(condition);
  54. condition = this.selfQueryDeal(condition);
  55. // 过滤出ref字段
  56. const { refMods, populate } = await this.getRefMods();
  57. // 带ref查询
  58. let rs = await this.model.find(condition, projection, { skip, limit, sort }).populate(populate).exec();
  59. rs = JSON.parse(JSON.stringify(rs));
  60. // 整理ref数据
  61. rs = rs.map(i => {
  62. for (const obj of refMods) {
  63. const { col, prop, type } = obj;
  64. if (!prop) continue;
  65. if (_.isArray(prop)) {
  66. for (const p of prop) {
  67. if (type === 'String') i[`${col}_${p}`] = _.get(i, `${col}.${p}`);
  68. if (type === 'Array') {
  69. const list = [];
  70. const oList = _.get(i, `${col}`);
  71. for (const d of oList) {
  72. const obj = { _id: d._id };
  73. obj[p] = _.get(d, p);
  74. list.push(obj);
  75. }
  76. i[`${col}_${p}`] = list;
  77. }
  78. }
  79. i[col] = _.get(i, `${col}._id`);
  80. }
  81. }
  82. return i;
  83. });
  84. rs = await this.afterQuery(filter, rs);
  85. return rs;
  86. }
  87. async count(filter) {
  88. let condition = _.cloneDeep(filter);
  89. condition = await this.beforeQuery(condition);
  90. condition = this.dealFilter(condition);
  91. condition = this.selfQueryDeal(condition);
  92. const count = await this.model.count(condition);
  93. return count;
  94. }
  95. selfQueryDeal(condition) {
  96. if (_.get(condition, 'user_id')) {
  97. const user_id = _.get(condition, 'user_id');
  98. condition.$or = [{ one_member_id: user_id }, { two_member_id: user_id }];
  99. delete condition.user_id;
  100. }
  101. return condition;
  102. }
  103. }
  104. module.exports = TeamApplyService;