Пример #1
0
        /// <summary>
        /// 购买金币
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        private async Task <RpsData <RpsBuyGood> > BuyJbGoods(ParamBuyGoods param)
        {
            var userPay = _context.UserPayHistories.Add(new UserPayHistory()
            {
                CreateTime = DateTime.Now,
                MallId     = param.MallId,
                Name       = param.Name,
                Price      = param.Price,
                Status     = (int)PayStatusEnum.正在处理,
                UserId     = param.Id
            });
            await _context.SaveChangesAsync();

            userPay.Entity.Status = (int)PayStatusEnum.已处理;
            userPay.Property(x => x.Status).IsModified = true;
            var mall = await _context.Malls.AsNoTracking().SingleAsync(x => x.Id == param.MallId);

            var userGoods = await _context.UserGoods
                            .SingleAsync(x => x.UserId == param.Id && x.GoodsType == MallGoodsTypeEnum.金币);

            if (mall.LimitedNum != 0)
            {
                var countHis = await _context.BuyGoodHistories
                               .WhereIf(mall.LimitedPeriod > 1, x => x.CreateDate.Date >= DateTime.Today.AddDays(1 - mall.LimitedPeriod))
                               .WhereIf(mall.LimitedPeriod == 1, x => x.CreateDate.Date >= DateTime.Today)
                               .CountAsync(x => x.UserId == param.Id);

                if (mall.LimitedNum <= countHis)
                {
                    return(RpsData <RpsBuyGood> .Error("超过每日限购次数", 2));
                }
            }

            userGoods.Num += mall.Num + mall.RewardNum;

            var buyEntity = _context.BuyGoodHistories.Add(new BuyGoodHistory()
            {
                MallId     = param.MallId,
                CreateDate = DateTime.Now,
                Num        = userGoods.Num,
                Type       = BuyTypeEnum.购买,
                UserId     = param.Id
            }).Entity;
            bool result = await _context.SaveChangesAsync() > 0;

            if (result)
            {
                return(RpsData <RpsBuyGood> .Ok(new RpsBuyGood()
                {
                    Id = buyEntity.Id,
                    Name = mall.Name,
                    Num = userGoods.Num,
                    Type = mall.Type
                }));
            }
            else
            {
                return(RpsData <RpsBuyGood> .Error("购买失败"));
            }
        }
Пример #2
0
 public async Task <IActionResult> BuyGoods([FromBody] ParamBuyGoods buyGoods)
 {
     return(await ActionWrapAsync(async() =>
     {
         ResultData <RpsData <RpsBuyGood> > result = new ResultData <RpsData <RpsBuyGood> >();
         var param = HttpContext.User.GetUserBase(buyGoods);
         result.Data = await _gameService.BuyGoods(param);
         return result;
     }));
 }
Пример #3
0
 public async Task <RpsData <RpsBuyGood> > BuyGoods(ParamBuyGoods param)
 {
     if (param.Type == MallGoodsTypeEnum.金币)
     {
         return(await BuyJbGoods(param));
     }
     else
     {
         return(await BuyDjGoods(param));
     }
 }
Пример #4
0
        /// <summary>
        /// 购买道具
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        private async Task <RpsData <RpsBuyGood> > BuyDjGoods(ParamBuyGoods param)
        {
            var mall = await _context.Malls.AsNoTracking().SingleAsync(x => x.Id == param.MallId);

            var userGoods = await _context.UserGoods.AnyAsync(x => x.UserId == param.Id && x.GoodsType == mall.Type) ?
                            await _context.UserGoods
                            .SingleAsync(x => x.UserId == param.Id && x.GoodsType == mall.Type) :
                            _context.UserGoods.Add(new UserGood()
            {
                GoodsType = mall.Type, Num = 0, UserId = param.Id
            }).Entity;

            var userJB = await _context.UserGoods.AnyAsync(x => x.UserId == param.Id && x.GoodsType == MallGoodsTypeEnum.金币) ?
                         await _context.UserGoods.SingleAsync(x => x.UserId == param.Id && x.GoodsType == MallGoodsTypeEnum.金币)
                : _context.UserGoods.Add(new UserGood()
            {
                GoodsType = MallGoodsTypeEnum.金币, Num = 0, UserId = param.Id
            }).Entity;

            if (userJB.Num < mall.Price)
            {
                return(RpsData <RpsBuyGood> .Error("金币不够,请充值"));
            }

            userGoods.Num += mall.Num + mall.RewardNum;
            userJB.Num    -= mall.Price;
            var buyEntity = _context.BuyGoodHistories.Add(new BuyGoodHistory()
            {
                MallId     = param.MallId,
                Type       = BuyTypeEnum.购买,
                CreateDate = DateTime.Now,
                Num        = userGoods.Num,
                UserId     = param.Id
            }).Entity;
            bool result = await _context.SaveChangesAsync() > 0;

            if (result)
            {
                return(RpsData <RpsBuyGood> .Ok(new RpsBuyGood()
                {
                    Id = buyEntity.Id,
                    Name = mall.Name,
                    Num = userGoods.Num,
                    Type = mall.Type
                }));
            }
            else
            {
                return(RpsData <RpsBuyGood> .Error("购买失败"));
            }
        }
Пример #5
0
 public async Task <RpsData <RpsBuyGood> > BuyGoods(ParamBuyGoods param)
 {
     return(await _gameRepository.BuyGoods(param));
 }