/// <returns>
        ///  0: valid
        /// -1: user doesn't exist
        /// </returns>
        public override int LockUser(int userId, bool locked)
        {
            using (var context = new RacingDbContext())
            {
                var user = context.User.FirstOrDefault(u => u.UserId == userId);

                if (user == null)
                {
                    return(-1);
                }

                user.IsLocked = locked;
                if (locked)
                {
                    user.LockedDate = DateTime.Now;
                }
                else
                {
                    user.LockedDate = null;
                }

                context.SaveChanges();
            }

            return(0);
        }
Exemple #2
0
 public void UpdateRates(RateType type, List <Rate> rates)
 {
     using (var db = new RacingDbContext())
     {
         var dbRates = db.Rate.Where(r => r.RateType == type).OrderBy(r => r.Rank).ToList();
         foreach (var dbRate in dbRates)
         {
             var rate = rates.Where(r => r.RateId == dbRate.RateId).FirstOrDefault();
             if (rate != null)
             {
                 dbRate.Rate1  = rate.Rate1;
                 dbRate.Rate2  = rate.Rate2;
                 dbRate.Rate3  = rate.Rate3;
                 dbRate.Rate4  = rate.Rate4;
                 dbRate.Rate5  = rate.Rate5;
                 dbRate.Rate6  = rate.Rate6;
                 dbRate.Rate7  = rate.Rate7;
                 dbRate.Rate8  = rate.Rate8;
                 dbRate.Rate9  = rate.Rate9;
                 dbRate.Rate10 = rate.Rate10;
                 dbRate.Big    = rate.Big;
                 dbRate.Small  = rate.Small;
                 dbRate.Odd    = rate.Odd;
                 dbRate.Even   = rate.Even;
             }
         }
         db.SaveChanges();
     }
 }
Exemple #3
0
        /// <summary>
        /// 更新用户扩展 UserExtension
        /// </summary>
        /// <param name="user"></param>
        /// <param name="roleId"></param>
        public void UpdateUserExtension(User user, int roleId)
        {
            using (var db = new RacingDbContext())
            {
                var userExtension = db.UserExtension.Where(u => u.UserId == user.UserId).FirstOrDefault();
                if (userExtension != null)
                {
                    switch (roleId)
                    {
                    case RoleConst.Role_Id_General_Agent:
                        userExtension.GeneralAgentUserId = user.UserId;
                        userExtension.AgentUserId        = user.UserId;
                        break;

                    case RoleConst.Role_Id_Agent:
                        userExtension.GeneralAgentUserId = user.ParentUserId;
                        userExtension.AgentUserId        = user.UserId;
                        break;

                    case RoleConst.Role_Id_Member:
                        userExtension.AgentUserId = user.ParentUserId;
                        if (user.ParentUserId.HasValue)
                        {
                            userExtension.GeneralAgentUserId = db.User.Where(agent => agent.UserId == user.ParentUserId).Select(u => u.ParentUserId).First();
                        }
                        break;
                    }
                    db.SaveChanges();
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// 更新父User.UserExtension的AgentCount/MemberCount
        /// </summary>
        /// <param name="user">当前用户</param>
        /// <param name="roleId">当前用户的角色Id</param>
        /// <param name="userOperation">操作: 增/删/改</param>
        public void UpdateParentUserExtension(User user, int roleId, UserOperation userOperation)
        {
            using (var db = new RacingDbContext())
            {
                var parentUserExtension = db.UserExtension
                                          .Include(nameof(UserExtension.User))
                                          .Where(u => u.UserId == user.ParentUserId).FirstOrDefault();

                if (parentUserExtension != null)
                {
                    switch (userOperation)
                    {
                    case UserOperation.Add:     //新增用户
                        switch (roleId)
                        {
                        case RoleConst.Role_Id_General_Agent:           //新增用户是总代理
                            parentUserExtension.AgentCount = parentUserExtension.AgentCount + 1;
                            break;

                        case RoleConst.Role_Id_Agent:           //新增用户是代理
                            parentUserExtension.AgentCount = parentUserExtension.AgentCount + 1;
                            break;

                        case RoleConst.Role_Id_Member:           //新增用户是会员
                                                                 //更新代理
                            parentUserExtension.MemberCount = parentUserExtension.MemberCount + 1;
                            //更新总代理
                            UpdateParentUserExtension(parentUserExtension.User, RoleConst.Role_Id_Member, UserOperation.Add);
                            break;
                        }
                        break;

                    case UserOperation.Edit:     //修改用户
                        break;

                    case UserOperation.Delete:     //删除用户
                        switch (roleId)
                        {
                        case RoleConst.Role_Id_General_Agent:           //删除用户是总代理
                            parentUserExtension.AgentCount = parentUserExtension.AgentCount - 1;
                            break;

                        case RoleConst.Role_Id_Agent:           //删除用户是代理
                            parentUserExtension.AgentCount  = parentUserExtension.AgentCount - 1;
                            parentUserExtension.MemberCount = parentUserExtension.MemberCount - user.UserExtension.MemberCount;
                            break;

                        case RoleConst.Role_Id_Member:           //删除用户是会员
                            parentUserExtension.MemberCount = parentUserExtension.MemberCount - 1;
                            //更新总代理
                            UpdateParentUserExtension(parentUserExtension.User, RoleConst.Role_Id_Member, UserOperation.Delete);
                            break;
                        }
                        break;
                    }

                    db.SaveChanges();
                }
            }
        }
 /// <summary>
 /// 减少账户金额
 /// </summary>
 /// <param name="userId"></param>
 /// <param name="amount"></param>
 public void MinusAmount(int userId, decimal amount)
 {
     using (var db = new RacingDbContext())
     {
         var userExtend = db.UserExtension.Where(u => u.UserId == userId).FirstOrDefault();
         userExtend.Amount = userExtend.Amount - amount;
         db.SaveChanges();
     }
 }
Exemple #6
0
 /// <summary>
 /// 每局结束后, 更新已结算标志 IsSettlementDone
 /// </summary>
 public void UpdateSettlementDone()
 {
     using (var db = new RacingDbContext())
     {
         var dbBets = db.Bet.Where(b => !b.IsSettlementDone && DbFunctions.DiffSeconds(b.PK.EndTime, DateTime.Now) > 0).ToList();
         dbBets.ForEach(b => b.IsSettlementDone = true);
         db.SaveChanges();
     }
 }
Exemple #7
0
 public void ChangePassword(string userName, string password)
 {
     using (var db = new RacingDbContext())
     {
         var user = db.User.Where(u => u.UserName == userName).First();
         user.Password = CryptoUtils.Encrypt(password);
         db.SaveChanges();
     }
 }
Exemple #8
0
 public void SaveEmail(int userId, string email)
 {
     using (var db = new RacingDbContext())
     {
         var user = db.User.Where(u => u.UserId == userId).First();
         user.Email = email;
         db.SaveChanges();
     }
 }
        protected virtual void AuditLogin(int userId)
        {
            using (var context = new RacingDbContext())
            {
                var user = context.User.Where(u => u.UserId == userId).FirstOrDefault();
                user.LastLoginDate = DateTime.Now;

                context.SaveChanges();
            }
        }
 public void UpdateIsSynced(long orderNo, bool isSynced)
 {
     using (var db = new RacingDbContext())
     {
         var betItems = db.BetItem.Where(b => b.OrderNo == orderNo).ToList();
         foreach (var item in betItems)
         {
             item.IsSynced = isSynced;
         }
         db.SaveChanges();
     }
 }
Exemple #11
0
 /// <summary>
 /// 更新 名次同步标志, 防止多次计算
 /// </summary>
 public void UpdateIsRanksSynced(int pkId, bool isRanksSynced)
 {
     using (var db = new RacingDbContext())
     {
         var pk = db.PK.Where(p => p.PKId == pkId).FirstOrDefault();
         if (pk != null)
         {
             pk.IsRanksSynced = isRanksSynced;
             db.SaveChanges();
         }
     }
 }
 public void Update(string name, string val)
 {
     using (var db = new RacingDbContext())
     {
         var appConfig = db.AppConfig.Where(a => a.Name == name).FirstOrDefault();
         if (appConfig != null)
         {
             appConfig.Value = val;
             db.SaveChanges();
         }
     }
 }
 public void Delete(string name)
 {
     using (var db = new RacingDbContext())
     {
         var appConfig = db.AppConfig.Where(a => a.Name == name).FirstOrDefault();
         if (appConfig != null)
         {
             db.AppConfig.Remove(appConfig);
             db.SaveChanges();
         }
     }
 }
        public AppConfig Add(string name, string val)
        {
            using (var db = new RacingDbContext())
            {
                var appConfig = new AppConfig();
                appConfig.Name  = name;
                appConfig.Value = val;

                appConfig = db.AppConfig.Add(appConfig);
                db.SaveChanges();

                return(appConfig);
            }
        }
        /// <summary>
        /// 保存 找回密码时系统生成的验证码
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="validateCode"></param>
        public void SaveValidateCode(string userName, string validateCode)
        {
            using (var db = new RacingDbContext())
            {
                var userExtention = db.UserExtension.Where(u => u.User.UserName == userName).FirstOrDefault();
                if (userExtention != null)
                {
                    userExtention.ValidateCodeForForgetPwd = validateCode;
                    userExtention.ValidateCodeCreateDate   = DateTime.Now;

                    db.SaveChanges();
                }
            }
        }
        /// <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);
                        }
                    }
                }
            }
        }
Exemple #17
0
        public PK AddPK(DateTime beginTime)
        {
            using (var db = new RacingDbContext())
            {
                var pk = new PK
                {
                    CreateTime     = DateTime.Now,
                    BeginTime      = beginTime,
                    EndTime        = DateTime.Now.AddSeconds(AppConfigCache.Racing_Total_Seconds),
                    OpeningSeconds = AppConfigCache.Racing_Opening_Seconds,
                    CloseSeconds   = AppConfigCache.Racing_Close_Seconds,
                    GameSeconds    = AppConfigCache.Racing_Game_Seconds,
                    LotterySeconds = AppConfigCache.Racing_Lottery_Seconds,
                    //PKRates = pkRates //pkRates 过多, 插入时间很长, 必须和PK新增分开
                };

                db.PK.Add(pk);
                db.SaveChanges();

                // pkRates 过多, 插入时间很长, 必须和PK新增分开
                var pkRates = new List <PKRate>();
                foreach (var rate in RateCache.GetRatesByType(Data.Enums.RateType.Arena))
                {
                    for (var num = 1; num <= 14; num++)
                    {
                        pkRates.Add(new PKRate {
                            PKId = pk.PKId, Rank = rate.Rank, Num = num, Rate = RateService.GetRate(rate, num)
                        });
                    }
                }
                db.PKRate.AddRange(pkRates);
                db.SaveChanges();

                return(pk);
            }
        }
        public bool CheckValidateCodeForForgetPwd(string userName, string code)
        {
            using (var db = new RacingDbContext())
            {
                var extention = db.UserExtension.Where(u => u.User.UserName == userName && u.ValidateCodeForForgetPwd == code).FirstOrDefault();
                if (extention != null)
                {
                    extention.ValidateCodeForForgetPwd = null;
                    extention.ValidateCodeCreateDate   = null;

                    db.SaveChanges();
                }
                return(extention != null);
            }
        }
Exemple #19
0
 public void UpdateUserRebates(int userId, List <UserRebate> userRebates)
 {
     using (var db = new RacingDbContext())
     {
         var dbRebates = db.UserRebate.Where(u => u.UserId == userId).OrderBy(u => u.RebateNo).ToList();
         foreach (var dbRebate in dbRebates)
         {
             var rebate = userRebates.Where(r => r.RebateNo == dbRebate.RebateNo).FirstOrDefault();
             dbRebate.RebateTypeA  = rebate.RebateTypeA;
             dbRebate.RebateTypeB  = rebate.RebateTypeB;
             dbRebate.RebateTypeC  = rebate.RebateTypeC;
             dbRebate.MaxPKAmount  = rebate.MaxPKAmount;
             dbRebate.MaxBetAmount = rebate.MaxBetAmount;
         }
         db.SaveChanges();
     }
 }
Exemple #20
0
        /// <summary>
        /// 更新名次
        /// </summary>
        public bool UpdateRanks(int pkId, string ranks)
        {
            using (var db = new RacingDbContext())
            {
                var dbPK = db.PK.Where(pk => pk.PKId == pkId).FirstOrDefault();
                if (dbPK != null && string.IsNullOrEmpty(dbPK.Ranks))
                {
                    dbPK.Ranks = ranks;

                    db.SaveChanges();

                    return(true);
                }

                return(false);
            }
        }
        /// <returns>
        ///  0: valid
        /// -1: user already exists
        /// </returns>
        public override int CreateUser(User user)
        {
            using (var context = new RacingDbContext())
            {
                if (context.User.Any(u => u.UserName == user.UserName))
                {
                    return(-1);
                }

                User dbUser = new User
                {
                    UserName   = user.UserName,
                    Email      = user.Email,
                    Password   = string.IsNullOrEmpty(user.Password) ? null : (HashedPassword ? CryptoUtils.ComputeHash(user.Password) : CryptoUtils.Encrypt(user.Password)),
                    Enabled    = user.Enabled,
                    IsLocked   = user.IsLocked,
                    LockedDate = user.IsLocked ? new Nullable <DateTime>(DateTime.Now) : null,
                    FailedPasswordAttemptCount       = 0,
                    FailedPasswordAttemptWindowStart = DateTime.Parse("1900-01-01"),
                    CreateDate    = DateTime.Now,
                    LastLoginDate = DateTime.Now
                };

                context.User.Add(dbUser);

                if (user.UserRoles != null)
                {
                    dbUser.UserRoles = new List <UserRole>();
                    foreach (var ur in user.UserRoles)
                    {
                        dbUser.UserRoles.Add(new UserRole
                        {
                            User   = dbUser,
                            RoleId = ur.RoleId
                        });
                    }
                }

                context.SaveChanges();

                user.UserId = dbUser.UserId;
            }

            return(0);
        }
        /// <returns>
        ///  0: valid
        /// -1: user doesn't exist
        /// </returns>
        public override int EnableUser(int userId, bool enabled)
        {
            using (var context = new RacingDbContext())
            {
                var user = context.User.FirstOrDefault(u => u.UserId == userId);

                if (user == null)
                {
                    return(-1);
                }

                user.Enabled = enabled;

                context.SaveChanges();
            }

            return(0);
        }
Exemple #23
0
        /// <summary>
        /// 批量修改赔率
        /// </summary>
        /// <param name="type">0:竞技场, 1: 娱乐场a, 2: 娱乐场b, 3: 娱乐场c</param>
        /// <param name="batchType">1:名次,2:大小,3:单双,4:全部</param>
        /// <param name="rate">赔率</param>
        public void UpdateBatchRates(RateType type, BatchRateType batchType, decimal rate)
        {
            using (var db = new RacingDbContext())
            {
                var dbRates = db.Rate.Where(r => r.RateType == type).OrderBy(r => r.Rank).ToList();

                foreach (var dbRate in dbRates)
                {
                    #region  次 or 全部
                    if (batchType == BatchRateType.Rank || batchType == BatchRateType.All)
                    {
                        dbRate.Rate1  = rate;
                        dbRate.Rate2  = rate;
                        dbRate.Rate3  = rate;
                        dbRate.Rate4  = rate;
                        dbRate.Rate5  = rate;
                        dbRate.Rate6  = rate;
                        dbRate.Rate7  = rate;
                        dbRate.Rate8  = rate;
                        dbRate.Rate9  = rate;
                        dbRate.Rate10 = rate;
                    }
                    #endregion

                    #region 大小 or 全部
                    if (batchType == BatchRateType.BigSmall || batchType == BatchRateType.All)
                    {
                        dbRate.Big   = rate;
                        dbRate.Small = rate;
                    }
                    #endregion

                    #region 大小 or 全部
                    if (batchType == BatchRateType.OddEven || batchType == BatchRateType.All)
                    {
                        dbRate.Odd  = rate;
                        dbRate.Even = rate;
                    }
                    #endregion
                }

                db.SaveChanges();
            }
        }
        public UserExtension GetUserExtension(int userId)
        {
            using (var db = new RacingDbContext())
            {
                var userExtend = db.UserExtension.Where(u => u.UserId == userId).FirstOrDefault();
                if (userExtend == null)
                {
                    userExtend = new UserExtension
                    {
                        UserId = userId,
                        Amount = 0
                    };

                    db.UserExtension.Add(userExtend);
                    db.SaveChanges();
                }
                return(userExtend);
            }
        }
        /// <returns>
        ///  0: valid
        ///  1: user doesn't exist
        /// </returns>
        public override int ChangePassword(string username, string newPassword)
        {
            using (var context = new RacingDbContext())
            {
                var user = context.User.FirstOrDefault(u => u.UserName == username);

                if (user == null)
                {
                    return(1);
                }

                var encryptNewPassword = HashedPassword ? CryptoUtils.ComputeHash(newPassword) : CryptoUtils.Encrypt(newPassword);
                user.Password = encryptNewPassword;

                context.SaveChanges();
            }

            return(0);
        }
Exemple #26
0
        public void LockUser(int userId, bool locked)
        {
            using (var db = new RacingDbContext())
            {
                var dbUser = db.User.Where(u => u.UserId == userId).FirstOrDefault();
                if (dbUser != null)
                {
                    dbUser.IsLocked = locked;
                    if (locked)
                    {
                        dbUser.LockedDate = DateTime.Now;
                    }
                    else
                    {
                        dbUser.LockedDate = null;
                    }

                    db.SaveChanges();
                }
            }
        }
Exemple #27
0
        public void SavePost(Post post)
        {
            using (var db = new RacingDbContext())
            {
                if (post.PostId > 0)
                {
                    var dbPost = db.Post.Where(p => p.PostId == post.PostId).FirstOrDefault();
                    dbPost.Title       = post.Title;
                    dbPost.PostContent = post.PostContent;
                }
                else
                {
                    post.CreateTime = DateTime.Now;
                    post.PostStatus = Data.Enums.PostStatus.Pass;

                    db.Post.Add(post);
                }

                db.SaveChanges();
            }
        }
        /// <summary>
        /// 增加账户金额
        /// </summary>
        public void AddAmount(int userId, decimal amount)
        {
            using (var db = new RacingDbContext())
            {
                var userExtend = db.UserExtension.Where(u => u.UserId == userId).FirstOrDefault();
                if (userExtend == null)
                {
                    userExtend = new UserExtension
                    {
                        UserId = userId,
                        Amount = amount
                    };

                    db.UserExtension.Add(userExtend);
                }
                else
                {
                    userExtend.Amount += amount;
                }
                db.SaveChanges();
            }
        }
Exemple #29
0
        public void EnableUser(int userId, bool enabled)
        {
            using (var db = new RacingDbContext())
            {
                var dbUser = db.User.Include(nameof(User.UserRoles)).Where(u => u.UserId == userId).FirstOrDefault();
                if (dbUser != null)
                {
                    dbUser.Enabled = enabled;

                    db.SaveChanges();


                    // 更新父User.UserExtension的AgentCount/MemberCount
                    if (enabled)
                    {
                        UpdateParentUserExtension(dbUser, dbUser.UserRoles.First().RoleId, UserOperation.Add);
                    }
                    else
                    {
                        UpdateParentUserExtension(dbUser, dbUser.UserRoles.First().RoleId, UserOperation.Delete);
                    }
                }
            }
        }
        /// <returns>
        ///  0: valid
        /// -1: user doesn't exist
        /// </returns>
        public override int UpdateUser(User user, bool updateRoles)
        {
            using (var context = new RacingDbContext())
            {
                var dbUser = GetUser(user.UserId, false);

                if (dbUser == null)
                {
                    return(-1);
                }

                if (!string.IsNullOrEmpty(user.Password))
                {
                    dbUser.Password = HashedPassword ? CryptoUtils.ComputeHash(user.Password) : CryptoUtils.Encrypt(user.Password);
                }

                if (dbUser.IsLocked != user.IsLocked)
                {
                    if (user.IsLocked)
                    {
                        dbUser.IsLocked   = true;
                        dbUser.LockedDate = DateTime.Now;
                    }
                    else
                    {
                        dbUser.IsLocked   = false;
                        dbUser.LockedDate = null;
                        dbUser.FailedPasswordAttemptCount = 0;
                    }
                }
                dbUser.Enabled = user.Enabled;

                if (updateRoles)
                {
                    //if (dbUser.UserRoles == null) dbUser.UserRoles = new List<UserRole>();
                    //else dbUser.UserRoles.Clear();

                    var userRoles = context.UserRole.Where(ur => ur.UserId == user.UserId).ToList();
                    foreach (var userRole in userRoles)
                    {
                        context.UserRole.Remove(userRole);
                    }

                    foreach (var ur in user.UserRoles)
                    {
                        //dbUser.UserRoles.Add(new UserRole
                        //{
                        //    User = dbUser,
                        //    RoleId = ur.RoleId,
                        //    CreateDate = DateTime.Now
                        //});
                        context.UserRole.Add(new UserRole
                        {
                            UserId = ur.UserId,
                            RoleId = ur.RoleId
                        });
                    }
                }

                context.SaveChanges();
            }

            return(0);
        }