/// <summary>
        /// 生成奖金
        /// </summary>
        /// <param name="pk"></param>
        public void GenerateBonus(PK pk)
        {
            using (var db = new RacingDbContext())
            {
                var betService = new BetService();
                var bets       = betService.ConvertRanksToBets(pk.Ranks);
                var pkRates    = new PKRateService().GetPKRates(pk.PKId);

                // 按 名次/大小单双+车号 循环
                foreach (var bet in bets)
                {
                    // 奖金
                    var bonuses = new List <PKBonus>();

                    // 名次+车号 的下注数据
                    var dbBets = betService.GetBets(pk.PKId, bet.Rank, bet.Num);
                    foreach (var dbBet in dbBets)
                    {
                        var pkRate = pkRates.Where(r => r.Rank == dbBet.Rank && r.Num == dbBet.Num).First();

                        bonuses.Add(new PKBonus
                        {
                            BetId            = dbBet.BetId,
                            PKId             = pk.PKId,
                            UserId           = dbBet.UserId,
                            Rank             = dbBet.Rank,
                            Num              = dbBet.Num,
                            BonusType        = Data.Enums.BonusType.Bonus,
                            Amount           = Math.Round(dbBet.Amount * pkRate.Rate, 4),
                            IsSettlementDone = true // 直接设置成已结算
                        });
                    }

                    if (bonuses.Count > 0)
                    {
                        // 保存奖金
                        db.PKBonus.AddRange(bonuses);
                        db.SaveChanges();

                        // 奖金加到余额
                        var userExtensionService = new UserExtensionService();
                        foreach (var bonus in bonuses)
                        {
                            userExtensionService.AddAmount(bonus.UserId, bonus.Amount);
                        }
                    }
                }
            }
        }
Beispiel #2
0
        public BetStatisticModel GetBetStatistic()
        {
            using (var db = new RacingDbContext())
            {
                var model = new BetStatisticModel();

                var pk = new PKService().GetCurrentPK();
                //var pk = new PKService().GetPK(2129);
                if (pk == null)
                {
                    return(null);
                }
                var pkModel = new PKService().ConvertToPKModel(pk);
                var pkRates = new PKRateService().GetPKRates(pk.PKId);

                var sql = "SELECT [Rank], Num, sum(Amount) Amount \n"
                          + "    FROM [Racing.Moto].[dbo].[Bet] WHERE PKId = {0} \n"
                          + "    GROUP BY Num, [Rank]";

                var dbBetAmounts = db.Database.SqlQuery <BetAmountModel>(string.Format(sql, pk.PKId)).ToList();
                model.BetAmountsAll = dbBetAmounts;

                //pk
                model.PKModel = pkModel;
                //pkRates
                model.PKRates     = pkRates;
                model.TotalAmount = dbBetAmounts.Count > 0 ? dbBetAmounts.Sum(a => a.Amount) : 0;

                // betAmountRankModels
                //var betAmountRankModels = new List<BetAmountRankModel>();
                //for (var rank = 1; rank <= 10; rank++)
                //{
                //    betAmountRankModels.Add(new BetAmountRankModel
                //    {
                //        Rank = rank,
                //        BetAmounts = GetBetAmounts(dbBetAmounts, pkRates, rank)
                //    });
                //}
                //model.BetAmountRankModels = betAmountRankModels;

                model.BetAmounts1  = GetBetAmounts(dbBetAmounts, pkRates, 1);
                model.BetAmounts2  = GetBetAmounts(dbBetAmounts, pkRates, 2);
                model.BetAmounts3  = GetBetAmounts(dbBetAmounts, pkRates, 3);
                model.BetAmounts4  = GetBetAmounts(dbBetAmounts, pkRates, 4);
                model.BetAmounts5  = GetBetAmounts(dbBetAmounts, pkRates, 5);
                model.BetAmounts6  = GetBetAmounts(dbBetAmounts, pkRates, 6);
                model.BetAmounts7  = GetBetAmounts(dbBetAmounts, pkRates, 7);
                model.BetAmounts8  = GetBetAmounts(dbBetAmounts, pkRates, 8);
                model.BetAmounts9  = GetBetAmounts(dbBetAmounts, pkRates, 9);
                model.BetAmounts10 = GetBetAmounts(dbBetAmounts, pkRates, 10);

                //總投注額
                //model.BetAmount1 = model.BetAmounts1.Sum(a => a.Amount);
                //model.BetAmount2 = model.BetAmounts2.Sum(a => a.Amount);
                //model.BetAmount3 = model.BetAmounts3.Sum(a => a.Amount);
                //model.BetAmount4 = model.BetAmounts4.Sum(a => a.Amount);
                //model.BetAmount5 = model.BetAmounts5.Sum(a => a.Amount);
                //model.BetAmount6 = model.BetAmounts6.Sum(a => a.Amount);
                //model.BetAmount7 = model.BetAmounts7.Sum(a => a.Amount);
                //model.BetAmount8 = model.BetAmounts8.Sum(a => a.Amount);
                //model.BetAmount9 = model.BetAmounts9.Sum(a => a.Amount);
                //model.BetAmount10 = model.BetAmounts10.Sum(a => a.Amount);

                //最高盈利
                model.MaxProfit = model.TotalAmount * (1 - AppConfigCache.Rate_Rebate_A);
                //model.MaxProfit1 = model.BetAmount1 * (1 - AppConfigCache.Rate_Rebate_A);
                //model.MaxProfit2 = model.BetAmount2 * (1 - AppConfigCache.Rate_Rebate_A);
                //model.MaxProfit3 = model.BetAmount3 * (1 - AppConfigCache.Rate_Rebate_A);
                //model.MaxProfit4 = model.BetAmount4 * (1 - AppConfigCache.Rate_Rebate_A);
                //model.MaxProfit5 = model.BetAmount5 * (1 - AppConfigCache.Rate_Rebate_A);
                //model.MaxProfit6 = model.BetAmount6 * (1 - AppConfigCache.Rate_Rebate_A);
                //model.MaxProfit7 = model.BetAmount7 * (1 - AppConfigCache.Rate_Rebate_A);
                //model.MaxProfit8 = model.BetAmount8 * (1 - AppConfigCache.Rate_Rebate_A);
                //model.MaxProfit9 = model.BetAmount9 * (1 - AppConfigCache.Rate_Rebate_A);
                //model.MaxProfit10 = model.BetAmount10 * (1 - AppConfigCache.Rate_Rebate_A);

                //最高虧損
                model.MaxLoss = dbBetAmounts.Sum(a => a.Amount * a.PKRate) - model.TotalAmount;
                //model.MaxLoss1 = model.BetAmounts1.Sum(m => m.Amount * m.PKRate) - model.BetAmount1;
                //model.MaxLoss2 = model.BetAmounts2.Sum(m => m.Amount * m.PKRate) - model.BetAmount2;
                //model.MaxLoss3 = model.BetAmounts3.Sum(m => m.Amount * m.PKRate) - model.BetAmount3;
                //model.MaxLoss4 = model.BetAmounts4.Sum(m => m.Amount * m.PKRate) - model.BetAmount4;
                //model.MaxLoss5 = model.BetAmounts5.Sum(m => m.Amount * m.PKRate) - model.BetAmount5;
                //model.MaxLoss6 = model.BetAmounts6.Sum(m => m.Amount * m.PKRate) - model.BetAmount6;
                //model.MaxLoss7 = model.BetAmounts7.Sum(m => m.Amount * m.PKRate) - model.BetAmount7;
                //model.MaxLoss8 = model.BetAmounts8.Sum(m => m.Amount * m.PKRate) - model.BetAmount8;
                //model.MaxLoss9 = model.BetAmounts9.Sum(m => m.Amount * m.PKRate) - model.BetAmount9;
                //model.MaxLoss10 = model.BetAmounts10.Sum(m => m.Amount * m.PKRate) - model.BetAmount10;

                //RankAmountModels
                model.RankAmounts = GetRankAmountModels(dbBetAmounts);

                return(model);
            }
        }