'use strict'; const { CrudService } = require('naf-framework-mongoose-free/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const _ = require('lodash'); const assert = require('assert'); // class TeamApplyService extends CrudService { constructor(ctx) { super(ctx, 'teamapply'); this.model = this.ctx.model.Race.TeamApply; this.matchSignModel = this.ctx.model.Race.MatchSign; this.matchProjectModel = this.ctx.model.Race.MatchProject; this.userModel = this.ctx.model.Race.User; this.baseUserModel = this.ctx.model.Base.User; } async beforeCreate(body) { // 检查:1.这两个人 是否报过 该比赛项目,且支付成功 // 2.该比赛项目是否是双打, type 是否为1 // 3.这两人是否有 组队申请,且申请拒绝 const { project_id, one_member_id, one_member_name, two_member_id, two_member_name } = body; // check 1 const check1 = async (project_id, user_id) => await this.matchSignModel.count({ project_id, user_id }); let num = await check1(project_id, one_member_id); if (num <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, `${one_member_name} 没有报名该比赛项目`); num = await check1(project_id, two_member_id); if (num <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, `${two_member_name} 没有报名该比赛项目`); // check2 const project = await this.matchProjectModel.findById(project_id); if (!project) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到比赛项目'); const { type } = project; if (type !== '1') throw new BusinessError(ErrorCode.DATA_INVALID, '该比赛项目不为双打,无需进行组队申请'); // check3 num = await this.checkHasTeamApply(project_id, one_member_id); if (num > 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, `${one_member_name} 在该比赛项目下已有组队申请`); num = await this.checkHasTeamApply(project_id, two_member_id); if (num > 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, `${two_member_name} 在该比赛项目下已有组队申请`); return body; } /** * 查询项目下可以组队的人 * @param {Object} query 地址栏查询条件 * @property {String} project_id 项目地址 * @property {String} user_id 项目地址 */ async findPartner({ project_id, user_id }) { // 多层嵌套populate let list = await this.matchSignModel.find({ user_id: { $ne: user_id }, pay_status: '1', project_id }).populate({ path: 'user_id', select: 'user_id', model: this.userModel, populate: { path: 'user_id', model: this.baseUserModel, select: 'name' }, }); if (list.length > 0) { list = JSON.parse(JSON.stringify(list)); const arr = []; for (const i of list) { const { user_id } = i; const num = await this.checkHasTeamApply(project_id, user_id); if (num > 0) continue; i.user_name = _.get(i, 'user_id.user_id.name'); i.user_id = _.get(i, 'user_id._id'); arr.push(i); } list = arr; } return list; } async query(filter, { skip = 0, limit, sort, desc, projection } = {}) { // 处理排序 if (sort && _.isString(sort)) { sort = { [sort]: desc ? -1 : 1 }; } else if (sort && _.isArray(sort)) { sort = sort.map(f => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {}); } let condition = _.cloneDeep(filter); condition = await this.beforeQuery(condition); condition = this.dealFilter(condition); condition = this.selfQueryDeal(condition); // 过滤出ref字段 const { refMods, populate } = await this.getRefMods(); // 带ref查询 let rs = await this.model.find(condition, projection, { skip, limit, sort }).populate(populate).exec(); rs = JSON.parse(JSON.stringify(rs)); // 整理ref数据 rs = rs.map(i => { for (const obj of refMods) { const { col, prop, type } = obj; if (!prop) continue; if (_.isArray(prop)) { for (const p of prop) { if (type === 'String') i[`${col}_${p}`] = _.get(i, `${col}.${p}`); if (type === 'Array') { const list = []; const oList = _.get(i, `${col}`); for (const d of oList) { const obj = { _id: d._id }; obj[p] = _.get(d, p); list.push(obj); } i[`${col}_${p}`] = list; } } i[col] = _.get(i, `${col}._id`); } } return i; }); rs = await this.afterQuery(filter, rs); return rs; } async count(filter) { let condition = _.cloneDeep(filter); condition = await this.beforeQuery(condition); condition = this.dealFilter(condition); condition = this.selfQueryDeal(condition); const count = await this.model.count(condition); return count; } /** * 查询该比赛项目下,这个用户是否有 有效的 组队申请 * @param {String} project_id 比赛项目 * @param {String} user_id 比赛模块用户数据id * @return {number} 组队条数 */ async checkHasTeamApply(project_id, user_id) { return await this.model.count({ project_id, $or: [{ one_member_id: user_id }, { two_member_id: user_id }], type: { $ne: '-1' } }); } selfQueryDeal(condition) { if (_.get(condition, 'user_id')) { const user_id = _.get(condition, 'user_id'); condition.$or = [{ one_member_id: user_id }, { two_member_id: user_id }]; delete condition.user_id; } return condition; } } module.exports = TeamApplyService;