'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 MatchProjectService extends CrudService { constructor(ctx) { super(ctx, 'matchproject'); this.model = this.ctx.model.Race.MatchProject; this.matchModel = this.ctx.model.Race.Match; } async beforeCreate(data) { const r = await this.canOpera(data); if (!r) throw new BusinessError(ErrorCode.SERVICE_FAULT, '当前赛事不处于可更改赛事相关信息状态'); return data; } async beforeUpdate(filter, update) { const r = await this.canOpera({ ...filter, ...update }); if (!r) throw new BusinessError(ErrorCode.SERVICE_FAULT, '当前赛事不处于可更改赛事相关信息状态'); return { filter, update }; } async beforeDelete(filter) { const r = await this.canOpera(filter); if (!r) throw new BusinessError(ErrorCode.SERVICE_FAULT, '当前赛事不处于可更改赛事相关信息状态'); return filter; } /** * 根据该赛事状态,判断是否可以操作 * 赛事状态为 0 可以操作, 其余状态不能操作 * @param {Object} data 数据 * @return {Boolean} 是否可以操作 */ async canOpera(data) { let match_id = _.get(data, 'match_id'); if (!match_id) { const id = data.id || data._id; if (!id) throw new BusinessError(ErrorCode.DATA_INVALID, '缺少检查可操作条件'); const d = await this.model.findById(id); match_id = _.get(d, 'match_id'); } const num = await this.matchModel.count({ _id: match_id, status: '0' }); return num > 0; } } module.exports = MatchProjectService;