/// <summary> /// 设置用户最高级的角色。 /// </summary> /// <param name="db">事务实例。</param> /// <param name="userId">用户Id。</param> /// <returns>返回设置结果。</returns> protected virtual bool SetMaxRole(IDbTransactionContext <TRole> db, int userId) { var role = db.AsQueryable() .Select() .OrderByDescending(x => x.RoleLevel) .InnerJoin <TUserRole>((r, ur) => r.RoleId == ur.RoleId) .Where <TUserRole>(x => x.UserId == userId) .FirstOrDefault(); return(db.As <TUser>().Update(x => x.UserId == userId, new { role.RoleId, RoleName = role.Name })); }
/// <summary> /// 设置用户最高级的角色。 /// </summary> /// <param name="db">事务实例。</param> /// <param name="userId">用户Id。</param> /// <param name="cancellationToken">取消标识。</param> /// <returns>返回设置结果。</returns> protected virtual async Task <bool> SetMaxRoleAsync(IDbTransactionContext <TRole> db, int userId, CancellationToken cancellationToken = default) { var role = await db.AsQueryable() .Select() .OrderByDescending(x => x.RoleLevel) .InnerJoin <TUserRole>((r, ur) => r.RoleId == ur.RoleId) .Where <TUserRole>(x => x.UserId == userId) .FirstOrDefaultAsync(cancellationToken); return(await db.As <TUser>().UpdateAsync(x => x.UserId == userId, new { role.RoleId, RoleName = role.Name }, cancellationToken)); }
/// <summary> /// 更新用户积分。 /// </summary> /// <param name="db">数据库事务接口实例。</param> /// <param name="userId">用户Id。</param> /// <param name="score">用户积分。</param> /// <param name="remark">描述。</param> /// <param name="scoreType">积分使用类型。</param> /// <param name="cancellationToken">取消标志。</param> /// <returns>返回添加结果。</returns> public static async Task <UpdateScoreResult> UpdateScoreAsync(this IDbTransactionContext <UserScore> db, int userId, int score, string remark = null, ScoreType?scoreType = null, CancellationToken cancellationToken = default) { var userScore = await db.AsQueryable().WithNolock().Where(x => x.UserId == userId).FirstOrDefaultAsync(cancellationToken); if (userScore == null || userScore.Score < score) { return(UpdateScoreStatus.NotEnough); } var log = new ScoreLog(); log.BeforeScore = userScore.Score; log.Score = -score; userScore.Score -= score; userScore.ScoredDate = DateTimeOffset.Now; if (scoreType == null) { scoreType = score > 0 ? ScoreType.Consume : ScoreType.Recharge; } log.ScoreType = scoreType.Value; if (!await db.UpdateAsync(x => x.UserId == userId && x.RowVersion == userScore.RowVersion, new { userScore.Score, userScore.ScoredDate }, cancellationToken)) { return(UpdateScoreStatus.ScoreError); } log.AfterScore = userScore.Score; log.Remark = remark; log.UserId = userId; var sdb = db.As <ScoreLog>(); if (await sdb.CreateAsync(log, cancellationToken)) { return(log.Id); } return(UpdateScoreStatus.LogError); }