Commit 0369b72d authored by maggie's avatar maggie

添加排名查询service

parent 71bb98fe
......@@ -182,6 +182,62 @@ class UserService extends BaseService {
async updateUser(_id: string, projection: IUpdateQuery) {
return await this.userdao.update({ _id }, this.formatUpdateUserProjection(projection))
}
/**
* @desc 排行榜
* @param {查询条件} match
* @param {排序规则} sort
* @param {查询条数} limit
*/
async getRank(match: Object, sort: Object, limit: number) {
const { data } = this.context
const { activityId } = data
return await this.userdao.aggregate([
{ $match: { ...match, activityId } },
{ $sort: sort },
{ $group: { _id: null, table: { $push: '$$ROOT' } } },
{ $unwind: { path: '$table', includeArrayIndex: 'no' } },
{
$project: {
_id: 0,
openId: '$table.openId',
userNick: '$table.userNick',
no: {
$add: ['$no', 1]
}
}
},
{ $limit: limit }
])
}
/**
* @desc 获取我的排名
* @param {查询条件} match
* @param {排序规则} sort
*/
async getMyRank(match: Object, sort: Object) {
const { openId, data } = this.context
const { activityId } = data
return await this.userdao.aggregate([
{ $match: { ...match, activityId } },
{ $sort: sort },
{ $group: { _id: null, table: { $push: '$$ROOT' } } },
{ $unwind: { path: '$table', includeArrayIndex: 'no' } },
{
$project: {
_id: 0,
openId: '$table.openId',
userNick: '$table.userNick',
no: {
$add: ['$no', 1]
}
}
},
{ $match: { openId } }
])
}
/**
* updateUser projection 格式化
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment