Beispiel #1
0
        public async Task <ResponseMessage> LotteryBet(Models.UserInfo user, [FromBody] LotteryBetRequest request)
        {
            Logger.Trace($"彩票开始开奖");
            var response = new ResponseMessage();

            if (!ModelState.IsValid)
            {
                response.Code    = ResponseCodeDefines.ModelStateInvalid;
                response.Message = "模型验证失败" + ModelState.GetAllErrors();
                Logger.Warn("彩票投注型验证失败:\r\n{0}", response.Message ?? "");
                return(response);
            }
            var prefixs = new string[] { "lottery" };
            var key     = "lottery";

            try
            {
                // 防止同时重复提交
                await _cache.LockSubmit(prefixs, key, "lottery", HttpContext.RequestAborted);

                response = await _giftManager.LotteryBetAsync(user, request, HttpContext.RequestAborted);

                if (response.Code != ResponseCodeDefines.SuccessCode)
                {
                    Logger.Warn($"彩票投注,失败:{JsonHelper.ToJson(response)}");
                }
            }
            catch (Exception e)
            {
                response.Code    = ResponseCodeDefines.ServiceError;
                response.Message = e.Message;
                Logger.Error($"彩票投注,报错:{e.Message}\r\n{e.StackTrace}");
            }
            finally
            {
                // 成功失败都要移除
                await _cache.UnlockSubmit(prefixs, key);
            }
            return(response);
        }
Beispiel #2
0
        /// <summary>
        /// 彩票投注
        /// </summary>
        /// <param name="user"></param>
        /// <param name="request"></param>
        /// <param name="requestAborted"></param>
        /// <returns></returns>
        public async Task <ResponseMessage> LotteryBetAsync(UserInfo user, LotteryBetRequest request, CancellationToken requestAborted)
        {
            var response   = new ResponseMessage();
            var periodInfo = await _allocateStore.GetScorePeriods().Where(w => w.State == 1 && !w.IsDelete).FirstOrDefaultAsync();

            if (periodInfo == null)
            {
                response.Code    = ResponseCodeDefines.ModelStateInvalid;
                response.Message = "没有进行中的赛季,暂时不能投注";
                return(response);
            }
            if (periodInfo.Id != request.PeriodId)
            {
                response.Code    = ResponseCodeDefines.ModelStateInvalid;
                response.Message = "请到主页先选择进行中的赛季。";
                return(response);
            }

            if (request.BetNumberList.Any(w => w.BetNumber.Count != 6))
            {
                response.Code    = ResponseCodeDefines.ModelStateInvalid;
                response.Message = "投注号码格式不正确";
                return(response);
            }
            foreach (var item in request.BetNumberList)
            {
                if (item.BetNumber.Distinct().Count() != item.BetNumber.Count)
                {
                    response.Code    = ResponseCodeDefines.ModelStateInvalid;
                    response.Message = "同一注号码中不能重复选号";
                    return(response);
                }
                if (item.BetScore <= 0)
                {
                    response.Code    = ResponseCodeDefines.ModelStateInvalid;
                    response.Message = "投注K币数必须大于零";
                    return(response);
                }
                if (item.BetScore > 500)
                {
                    response.Code    = ResponseCodeDefines.ModelStateInvalid;
                    response.Message = "投注K币数不能大于500K";
                    return(response);
                }
                item.BetNumber = item.BetNumber.OrderBy(o => o).ToList();//排序
            }

            var scoreInfo = await _allocateStore.GetScoreInfos().Where(w => !w.IsDelete && w.PeriodId == request.PeriodId && w.UserId == user.Id).FirstOrDefaultAsync();

            if (scoreInfo == null)
            {
                response.Code    = ResponseCodeDefines.ModelStateInvalid;
                response.Message = "未查询到您的积分信息";
                return(response);
            }
            if (scoreInfo.ConsumableScore < request.BetNumberList.Sum(s => s.BetScore))
            {
                response.Code    = ResponseCodeDefines.ModelStateInvalid;
                response.Message = "您的可投注的K币不足,赶紧去赚点K币吧";
                return(response);
            }

            var nowTime       = DateTime.Now;
            var numberPeriods = await GetNumberPeriods();//投注期数

            var loteryUserList = request.BetNumberList.Select(s => new LotteryUser
            {
                Id            = Guid.NewGuid().ToString(),
                PeriodId      = request.PeriodId,
                UserId        = user.Id,
                NumberPeriods = numberPeriods,
                Number        = string.Join(',', s.BetNumber),
                Score         = s.BetScore,
                CreateTime    = nowTime
            }).ToList();

            //事务保存
            using (var trans = await _transaction.BeginTransaction())
            {
                try
                {
                    //事务里面重新查一次
                    scoreInfo = await _allocateStore.GetScoreInfos().Where(w => !w.IsDelete && w.PeriodId == request.PeriodId && w.UserId == user.Id).FirstOrDefaultAsync();

                    if (scoreInfo == null)
                    {
                        response.Code    = ResponseCodeDefines.ModelStateInvalid;
                        response.Message = "未查询到您的积分信息";
                        return(response);
                    }
                    scoreInfo.ConsumableScore = scoreInfo.ConsumableScore - request.BetNumberList.Sum(s => s.BetScore).GetValueOrDefault(0);
                    await _allocateStore.UpdateScoreInfo(scoreInfo, requestAborted);

                    await _giftStore.AddLotteryUserList(loteryUserList, requestAborted);

                    //添加够买彩票动态动态
                    await _dynamicHelper.AddDynamicContent(
                        DynamicType.BuyLottery,
                        request.PeriodId,
                        scoreInfo.Id,
                        null,
                        null,
                        user.UserName,
                        user.GroupName,
                        user.Id,
                        numberPeriods,
                        loteryUserList.Sum(s => s.Score.Value),
                        null,
                        null
                        );

                    trans.Commit();
                }
                catch (Exception e)
                {
                    trans.Rollback();
                    throw new Exception("保存事务失败", e);
                }
            }
            // 触发信息变化
            await _sendClientMessageManager.SendInfos(new List <Dto.Common.SendClientType>()
            {
                Dto.Common.SendClientType.User,
                Dto.Common.SendClientType.Dynamic
            });

            return(response);
        }