/// <summary> /// 获得积分 /// </summary> /// <remarks>请在Transaction中使用</remarks> /// <returns></returns> public async Task GainAsync(long UserId, EmUserPointType UserPointType, int Points, EmUserPointBusType BusType, long BusId, string BusDesc, long?OperatorId) { using (var dapper = DapperFactory.CreateWithTrans()) { try { await _GainAsync(dapper, UserId, UserPointType, Points, BusType, BusId, BusDesc, OperatorId); dapper.Commit(); } catch (Exception) { dapper.Rollback(); throw; } } }
/// <summary> /// 调整积分 /// </summary> public async Task AdjustAsync(long[] UserId, EmUserPointType UserPointType, int Points, EmUserPointBusType BusType, long BusId, string BusDesc, long?OperatorId) { if (UserId == null || UserId.Length == 0) { throw new KuArgNullException("请选择用户!"); } if (Points == 0) { throw new KuArgNullException("调整积分不能为0!"); } if (Points < -9999 || Points > 9999) { throw new KuArgNullException("调整积分超出范围!"); } using (var dapper = DapperFactory.CreateWithTrans()) { try { if (Points > 0) { foreach (var id in UserId) { await _GainAsync(dapper, id, UserPointType, Points, BusType, BusId, BusDesc, OperatorId); } } else { foreach (var id in UserId) { await _ConsumeAsync(dapper, id, UserPointType, Math.Abs(Points), BusType, BusId, BusDesc, OperatorId); } } dapper.Commit(); } catch (Exception) { dapper.Rollback(); throw; } } }
private async Task _GainAsync(IDapper dapper, long UserId, EmUserPointType UserPointType, int Points, EmUserPointBusType BusType, long BusId, string BusDesc, long?OperatorId) { if (Points <= 0) { throw new KuArgNullException("积分值必须大于0!"); } if (Points > 9999) { throw new KuArgNullException("调整积分超出范围!"); } //取得用户数据 var user = await dapper.QueryOneAsync <User>(new { Id = UserId }); if (user == null) { throw new KuArgNullException("无法取得用户数据!"); } //取得用户积分数据 var userPoint = await dapper.QueryOneAsync <UserPoint>(new { UserId = UserId, Type = UserPointType, IsDeleted = false }); if (userPoint == null) { //创建新用户积分数据 userPoint = new UserPoint { Id = ID.NewID(), UserId = UserId, Type = UserPointType, Points = Points, ExpiredPoints = 0, UsablePoints = Points, UsedPoints = 0, IsDeleted = false, CreateTime = DateTime.Now }; await dapper.InsertAsync(userPoint); } else { //积分计算 var data = new { UsablePoints = userPoint.UsablePoints + Points, Points = userPoint.Points + Points }; await dapper.UpdateAsync <UserPoint>(data, new { Id = userPoint.Id }); } //log UserPointRecord record = new UserPointRecord { Id = ID.NewID(), UserPointId = userPoint.Id, UserId = UserId, ChangeType = EmUserPointChangeType.Gain, Points = Points, BusType = BusType, BusId = BusId, BusDesc = BusDesc, OperatorId = OperatorId, IsDeleted = false, CreateTime = DateTime.Now }; await dapper.InsertAsync(record); }