/// <summary> /// 用户购买礼物 赠送主播 /// </summary> /// <returns></returns> public Result <long> Cost(int userId, int roomId, int giftId) { var result = new Result <long>(); #region check params if (userId <= 0) { result.Msg = "用户ID无效"; return(result); } if (roomId <= 0) { result.Msg = "直播房间号无效"; return(result); } if (giftId <= 0) { result.Msg = "礼物信息无效"; return(result); } #endregion #region 数据验证 var giftList = _giftService.GetGiftList(); if (giftList == null || !giftList.Any()) { result.Msg = "礼物配置信息无效"; return(result); } var gift = giftList.FirstOrDefault(m => m.id == giftId); if (gift == null) { result.Msg = "赠送的礼物无效"; return(result); } //扣除用户的金币 var userAccount = this.CurrentRepository.Get(m => m.UserId == userId).FirstOrDefault(); if (userAccount == null) { result.Msg = "用户信息无效"; return(result); } if (userAccount.Gold <= 0) { result.Msg = "金币数量不足"; return(result); } var roomInfo = this._roomService.Get(m => m.ID == roomId && m.Status != (int)UserBaseStausEnum.Normal).FirstOrDefault(); if (roomInfo == null) { result.Msg = "当前直播间无效"; return(result); } if (roomInfo.UserId == userId) { result.Msg = "不可以给自己送礼物"; return(result); } var liveAccount = this.CurrentRepository.Get(m => m.UserId == roomInfo.UserId).FirstOrDefault(); if (liveAccount == null) { result.Msg = "主播信息无效"; return(result); } #endregion var now = DateTime.Now; var giving = gift.gold; #region 用户账户变动信息 //用户账户变动 var goldByUser = userAccount.Gold; userAccount.Gold -= giving; userAccount.Contribution += giving; userAccount.UpdateTime = now; var result1 = this.CurrentRepository.Update(userAccount); if (result1 <= 0) { result.Msg = $"用户送出礼物给主播[{liveAccount.UserId}],扣除金币失败"; return(result); } //金币变动记录 var result2 = this._goldRecordService.Add(new GoldRecord() { UserId = userId, ChangeType = (int)GoldChangeTypeEnum.Reduce, GoldBefore = goldByUser, GoldAfter = userAccount.Gold, Remark = $"用户送出礼物给主播[{liveAccount.UserId}],减少金币[{giving}]", InsertTime = now, UpdateTime = now }); if (result2.Code == ResultEnum.Error || result2.Data.ID <= 0) { result.Msg = $"用户送出礼物给主播[{liveAccount.UserId}],增加金币变动记录失败"; return(result); } #endregion #region 主播账户变动信息 //用户账户变动 var integral = liveAccount.Integral; liveAccount.Integral += giving; liveAccount.UpdateTime = now; var result3 = this.CurrentRepository.Update(liveAccount); if (result3 <= 0) { result.Msg = $"主播获得粉丝[{userId}]的礼物,增加积分失败"; return(result); } //积分变动记录 var result4 = this._integralRecordService.Add(new IntegralRecord() { UserId = roomInfo.UserId, ChangeType = (int)IntegralChangeTypeEnum.IncreaseByGetGift, IntegralBefore = integral, IntegralAfter = liveAccount.Integral, Remark = $"主播获得粉丝[{userId}]的礼物,增加积分{giving}", InsertTime = now, UpdateTime = now }); if (result4.Code == ResultEnum.Error || result4.Data.ID <= 0) { result.Msg = $"主播获得粉丝[{userId}]的礼物,增加积分变动记录失败"; return(result); } #endregion result.Data = userAccount.Gold; result.Code = ResultEnum.Success; return(result); }