teamApply.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. this.matchProjectModel = this.ctx.model.Race.MatchProject;
  13. this.userModel = this.ctx.model.Race.User;
  14. this.baseUserModel = this.ctx.model.Base.User;
  15. }
  16. async beforeCreate(body) {
  17. // 检查:1.这两个人 是否报过 该比赛项目,且支付成功
  18. // 2.该比赛项目是否是双打, type 是否为1
  19. // 3.这两人是否有 组队申请,且申请拒绝
  20. const { project_id, one_member_id, one_member_name, two_member_id, two_member_name } = body;
  21. // check 1
  22. const check1 = async (project_id, user_id) => await this.matchSignModel.count({ project_id, user_id });
  23. let num = await check1(project_id, one_member_id);
  24. if (num <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, `${one_member_name} 没有报名该比赛项目`);
  25. num = await check1(project_id, two_member_id);
  26. if (num <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, `${two_member_name} 没有报名该比赛项目`);
  27. // check2
  28. const project = await this.matchProjectModel.findById(project_id);
  29. if (!project) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到比赛项目');
  30. const { type } = project;
  31. if (type !== '1') throw new BusinessError(ErrorCode.DATA_INVALID, '该比赛项目不为双打,无需进行组队申请');
  32. // check3
  33. num = await this.checkHasTeamApply(project_id, one_member_id);
  34. if (num > 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, `${one_member_name} 在该比赛项目下已有组队申请`);
  35. num = await this.checkHasTeamApply(project_id, two_member_id);
  36. if (num > 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, `${two_member_name} 在该比赛项目下已有组队申请`);
  37. return body;
  38. }
  39. /**
  40. * 查询项目下可以组队的人
  41. * @param {Object} query 地址栏查询条件
  42. * @property {String} project_id 项目地址
  43. * @property {String} user_id 项目地址
  44. */
  45. async findPartner({ project_id, user_id }) {
  46. // 多层嵌套populate
  47. let list = await this.matchSignModel.find({ user_id: { $ne: user_id }, pay_status: '1', project_id }).populate({
  48. path: 'user_id',
  49. select: 'user_id',
  50. model: this.userModel,
  51. populate: { path: 'user_id', model: this.baseUserModel, select: 'name' },
  52. });
  53. if (list.length > 0) {
  54. list = JSON.parse(JSON.stringify(list));
  55. const arr = [];
  56. for (const i of list) {
  57. const { user_id } = i;
  58. const num = await this.checkHasTeamApply(project_id, user_id);
  59. if (num > 0) continue;
  60. i.user_name = _.get(i, 'user_id.user_id.name');
  61. i.user_id = _.get(i, 'user_id._id');
  62. arr.push(i);
  63. }
  64. list = arr;
  65. }
  66. return list;
  67. }
  68. async query(filter, { skip = 0, limit, sort, desc, projection } = {}) {
  69. // 处理排序
  70. if (sort && _.isString(sort)) {
  71. sort = { [sort]: desc ? -1 : 1 };
  72. } else if (sort && _.isArray(sort)) {
  73. sort = sort.map(f => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
  74. }
  75. let condition = _.cloneDeep(filter);
  76. condition = await this.beforeQuery(condition);
  77. condition = this.dealFilter(condition);
  78. condition = this.selfQueryDeal(condition);
  79. // 过滤出ref字段
  80. const { refMods, populate } = await this.getRefMods();
  81. // 带ref查询
  82. let rs = await this.model.find(condition, projection, { skip, limit, sort }).populate(populate).exec();
  83. rs = JSON.parse(JSON.stringify(rs));
  84. // 整理ref数据
  85. rs = rs.map(i => {
  86. for (const obj of refMods) {
  87. const { col, prop, type } = obj;
  88. if (!prop) continue;
  89. if (_.isArray(prop)) {
  90. for (const p of prop) {
  91. if (type === 'String') i[`${col}_${p}`] = _.get(i, `${col}.${p}`);
  92. if (type === 'Array') {
  93. const list = [];
  94. const oList = _.get(i, `${col}`);
  95. for (const d of oList) {
  96. const obj = { _id: d._id };
  97. obj[p] = _.get(d, p);
  98. list.push(obj);
  99. }
  100. i[`${col}_${p}`] = list;
  101. }
  102. }
  103. i[col] = _.get(i, `${col}._id`);
  104. }
  105. }
  106. return i;
  107. });
  108. rs = await this.afterQuery(filter, rs);
  109. return rs;
  110. }
  111. async count(filter) {
  112. let condition = _.cloneDeep(filter);
  113. condition = await this.beforeQuery(condition);
  114. condition = this.dealFilter(condition);
  115. condition = this.selfQueryDeal(condition);
  116. const count = await this.model.count(condition);
  117. return count;
  118. }
  119. /**
  120. * 查询该比赛项目下,这个用户是否有 有效的 组队申请
  121. * @param {String} project_id 比赛项目
  122. * @param {String} user_id 比赛模块用户数据id
  123. * @return {number} 组队条数
  124. */
  125. async checkHasTeamApply(project_id, user_id) {
  126. return await this.model.count({ project_id, $or: [{ one_member_id: user_id }, { two_member_id: user_id }], type: { $ne: '-1' } });
  127. }
  128. selfQueryDeal(condition) {
  129. if (_.get(condition, 'user_id')) {
  130. const user_id = _.get(condition, 'user_id');
  131. condition.$or = [{ one_member_id: user_id }, { two_member_id: user_id }];
  132. delete condition.user_id;
  133. }
  134. return condition;
  135. }
  136. }
  137. module.exports = TeamApplyService;