Example #1
0
        /// <summary>
        /// 查询是否存在抽奖关联数据,不存在则插入数据,并返回当前用户抽奖信息
        /// </summary>
        /// <param name="userFlag">用户标志</param>
        /// <param name="userName">用户名</param>
        /// <returns>用户抽奖分数</returns>
        public LotteryDrawModel CheckUserLotteryInfo(String userFlag, String userName)
        {
            var currentUserLotteryInfo = lotteryDrawData.FirstOrDefault(r => r.UserFlag == userFlag);

            if (currentUserLotteryInfo == null)
            {
                currentUserLotteryInfo = new LotteryDrawModel {
                    UserName = userName, UserFlag = userFlag, Points = 0, LastLotteryDrawTime = DateTime.MinValue
                };
                LotteryDrawDal.Instance.InsertInitLotteryDrawData(currentUserLotteryInfo);

                //将当前用户抽奖数据写入抽奖数据中
                try
                {
                    lotteryDrawLock.EnterWriteLock();
                    lotteryDrawData.Add(currentUserLotteryInfo);
                }
                finally
                {
                    lotteryDrawLock.ExitWriteLock();
                }
            }

            return(currentUserLotteryInfo);
        }
        /// <summary>
        /// 插入初始化的用户抽奖数据
        /// </summary>
        /// <param name="lotteryDrawModel">抽奖数据</param>
        public void InsertInitLotteryDrawData(LotteryDrawModel lotteryDrawModel)
        {
            MySqlParameter[] mySqlParameter = new MySqlParameter[]
            {
                new MySqlParameter("@userName", MySqlDbType.VarChar, 32),
                new MySqlParameter("@userflag", MySqlDbType.VarChar, 32)
            };
            mySqlParameter[0].Value = lotteryDrawModel.UserName;
            mySqlParameter[1].Value = lotteryDrawModel.UserFlag;

            MySqlHelper.ExecuteNonQuery(CommandType.Text, SqlConst.InsertLotteryDrawInfoStr, mySqlParameter);
        }
        /// <summary>
        /// 更新用户抽奖信息
        /// </summary>
        /// <param name="mySqlTransaction">事务</param>
        /// <param name="lotteryDrawModel">抽奖信息</param>
        public Int32 UpdateLotteryDrawInfo(MySqlTransaction mySqlTransaction, LotteryDrawModel lotteryDrawModel)
        {
            //参数
            MySqlParameter[] mySqlParameter = new MySqlParameter[]
            {
                new MySqlParameter("@userFlag", MySqlDbType.VarChar, 32),
                new MySqlParameter("@pointsStr", MySqlDbType.VarChar, 32),
                new MySqlParameter("@lastLotteryDrawTime", MySqlDbType.DateTime)
            };
            mySqlParameter[0].Value = lotteryDrawModel.UserFlag;
            mySqlParameter[1].Value = lotteryDrawModel.Points + 10;
            mySqlParameter[2].Value = DateTime.Now;

            //更新用户信息
            return(MySqlHelper.ExecuteNonQuery(mySqlTransaction, CommandType.Text, SqlConst.UpdateLotteryDrawInfoStr, mySqlParameter));
        }
Example #4
0
        /// <summary>
        /// 抽奖操作
        /// </summary>
        /// <param name="userFlag">用户标志</param>
        public void LotteryDraw(String userFlag)
        {
            LotteryDrawConfigBll.Instance.CheckLotteryTimeConfig();

            //获取用户信息,并判断宝石数量
            UserInfoModel userInfo = UserInfoBll.Instance.GetUserInfo(userFlag);

            UserInfoBll.Instance.CheckUserDiamond(userInfo);

            //检测用户抽奖信息,并获取当前用户的抽奖信息
            LotteryDrawModel userLotteryInfo = CheckUserLotteryInfo(userFlag, userInfo.UserName);

            //执行抽奖的数据库操作
            CommonTranDal.Instance.UserLotteryTran(userInfo, userLotteryInfo);

            //更新用户抽奖数据
            UpdateLotteryDrawData(userFlag);

            //更新用户宝石数据
            UserInfoBll.Instance.UpdateUserDiamondData(userFlag);
        }
        /// <summary>
        /// 用户抽奖更新数据操作
        /// </summary>
        /// <param name="userInfo">用户信息</param>
        /// <param name="userLotteryInfo">用户抽奖信息</param>
        /// <returns>受影响的行数</returns>
        public Int32 UserLotteryTran(UserInfoModel userInfo, LotteryDrawModel userLotteryInfo)
        {
            MySqlTransaction sqlTransaction = null;

            //打开连接,对用户更新以及用户抽奖更新进行事务处理
            using (MySqlConnection conn = new MySqlConnection(MySqlHelper.Conn))
            {
                if (conn.State != ConnectionState.Open)
                {
                    conn.Open();
                }

                try
                {
                    //开始事务
                    sqlTransaction = conn.BeginTransaction(IsolationLevel.ReadCommitted);

                    Int32 returnVal = UserInfoDal.Instance.UpdateUserInfo(sqlTransaction, userInfo);
                    returnVal += LotteryDrawDal.Instance.UpdateLotteryDrawInfo(sqlTransaction, userLotteryInfo);

                    //提交事务
                    sqlTransaction.Commit();

                    return(returnVal);
                }
                catch (Exception)
                {
                    //捕获到错误后,先回滚错误,并将错误消息抛出给顶层显示
                    if (sqlTransaction != null)
                    {
                        sqlTransaction.Rollback();
                    }

                    throw;
                }
            }
        }