Esempio n. 1
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="targetUserIds">key:用户ID;value:更新倍数</param>
        /// <param name="type"></param>
        /// <param name="actionType"></param>
        /// <param name="beforeUpdate"></param>
        /// <returns></returns>
        private static bool UpdateUserPoint <T>(Dictionary <int, int> targetUserIds, string type, T actionType, bool isNormal, int nodeId, TryUpdateUserPointCallback beforeUpdate, TryUpdateUserPointCallback2 beforeUpdate2) where T : struct
        {
            using (BbsContext context = new BbsContext())
            {
                context.BeginTransaction(IsolationLevel.ReadUncommitted);
                try
                {
                    bool updatePoint = true;
                    if (beforeUpdate2 != null)
                    {
                        updatePoint = beforeUpdate2(TryUpdateUserPointState.CheckSucceed, out targetUserIds);
                    }
                    else if (beforeUpdate != null)
                    {
                        updatePoint = beforeUpdate(TryUpdateUserPointState.CheckSucceed);
                    }

                    if (updatePoint)
                    {
                        //List<int> userIDs = new List<int>();
                        //foreach (KeyValuePair<int, int> pair in targetUserIds)
                        //{
                        //    userIDs.Add(pair.Key);
                        //}
                        ////先缓存一下用户
                        //UserBO.Instance.GetUsers(userIDs);

                        bool success = true;
                        foreach (KeyValuePair <int, int> pair in targetUserIds)
                        {
                            //throw new Exception("test error");
                            success = UpdateUserPoint <T>(pair.Key, type, actionType, pair.Value, isNormal, nodeId, null);
                            if (!success)
                            {
                                break;
                            }
                        }
                        if (success)
                        {
                            context.CommitTransaction();
                            return(true);
                        }
                    }
                    context.RollbackTransaction();
                    return(false);
                }
                catch (Exception ex)
                {
                    context.RollbackTransaction();
                    throw ex;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 更新用户积分
        /// </summary>
        public static bool UpdateUsersPoints <T>(string type, bool isNormal, int nodeId, TryUpdateUserPointCallback3 <T> beforeUpdate) where T : struct
        {
            using (BbsContext context = new BbsContext())
            {
                context.BeginTransaction(IsolationLevel.ReadUncommitted);
                try
                {
                    PointResultCollection <T> pointResults;
                    bool updatePoint = true;
                    if (beforeUpdate != null)
                    {
                        updatePoint = beforeUpdate(TryUpdateUserPointState.CheckSucceed, out pointResults);
                    }
                    else
                    {
                        context.RollbackTransaction();
                        return(false);
                    }

                    if (updatePoint)
                    {
                        bool success = true;
                        foreach (PointResult <T> pointResult in pointResults)
                        {
                            //throw new Exception("test error");
                            if (nodeId == 0)
                            {
                                nodeId = pointResult.NodeID;
                            }
                            success = UpdateUserPoint <T>(pointResult.UserID, type, pointResult.actionType, pointResult.Count, isNormal, nodeId, null);
                            if (!success)
                            {
                                break;
                            }
                        }
                        if (success)
                        {
                            context.CommitTransaction();
                            return(true);
                        }
                    }
                    context.RollbackTransaction();
                    return(false);
                }
                catch (Exception ex)
                {
                    context.RollbackTransaction();
                    throw ex;
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 对同一个用户的不同动作 操作积分
        /// </summary>
        /// <typeparam name="T1">不需要传值的积分动作</typeparam>
        /// <typeparam name="T2">需要用户填值的积分动作</typeparam>
        /// <param name="userID">操作该用户的积分</param>
        /// <param name="noNeedValueActions">(key:动作类型 value:操作倍数) 如果没有用null</param>
        /// <param name="needValueActions">(key:动作类型 value:积分值) 需要用户填值的积分动作类型 如果没有用null</param>
        /// <param name="type"></param>
        /// <param name="nodeID"></param>
        /// <param name="beforeUpdate"></param>
        /// <returns></returns>
        public static bool UpdateUserPoints <T1, T2>(int userID, Dictionary <T1, int> noNeedValueActions, Dictionary <T2, int> needValueActions, string type, int nodeID, TryUpdateUserPointCallback beforeUpdate)
            where T1 : struct
            where T2 : struct
        {
            using (BbsContext context = new BbsContext())
            {
                context.BeginTransaction(IsolationLevel.ReadUncommitted);
                try
                {
                    bool success = beforeUpdate(TryUpdateUserPointState.CheckSucceed);

                    if (success == false)
                    {
                        context.RollbackTransaction();
                        return(success);
                    }

                    if (noNeedValueActions != null)
                    {
                        foreach (KeyValuePair <T1, int> pair in noNeedValueActions)
                        {
                            success = UpdateUserPoint <T1>(userID, type, pair.Key, pair.Value, true, nodeID, null);
                            if (success == false)
                            {
                                break;
                            }
                        }
                    }
                    if (success == false)
                    {
                        context.RollbackTransaction();
                        return(success);
                    }

                    if (needValueActions != null)
                    {
                        foreach (KeyValuePair <T2, int> pair in needValueActions)
                        {
                            success = UpdateUserPointValue <T2>(userID, type, pair.Key, nodeID, pair.Value, null);
                            if (success == false)
                            {
                                break;
                            }
                        }
                    }

                    if (success == false)
                    {
                        context.RollbackTransaction();
                        return(success);
                    }

                    context.CommitTransaction();
                }
                catch (Exception ex)
                {
                    context.RollbackTransaction();
                    throw ex;
                }
            }
            return(true);
        }