Example #1
0
        public int ChangeBagStatus(RBCreateBag bag)
        {
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@BagStatus", bag.BagStatus),
                new SqlParameter("@RID", bag.RID)
            };
            var result = Dapper.Helper.SQLHelper.Execute("update CreateBag set BagStatus=@BagStatus where RID=@RID", sp, CommandType.Text);

            return(result);
        }
Example #2
0
        public IHttpActionResult InsertBag(RBCreateBag bag)
        {
            var rt = ISoftSmart.Core.IoC.IoCFactory.Instance.CurrentContainer.Resolve <IRedBag>();//使用接口

            bag.CreateTime = DateTime.Now;
            var         Code            = string.Empty;
            var         ResponseMessage = string.Empty;
            RBCreateBag Result          = null;

            bag.CreateTime = DateTime.Now;
            var db = RedisManager.Instance.GetDatabase();

            if (StackExchangeRedisExtensions.HasKey(db, CacheKey.BagKey))
            {
                lock (_locker)
                {
                    var bagcache = StackExchangeRedisExtensions.Get <List <RBCreateBag> >(db, CacheKey.BagKey);
                    bagcache.Add(bag);
                    StackExchangeRedisExtensions.Set(db, CacheKey.BagKey, bagcache, 240);
                    var res = rt.InsertBag(bag);
                    if (res > 0)
                    {
                        Code            = "SCCESS";
                        ResponseMessage = "金豆发放成功!";
                    }
                    else
                    {
                        Code            = "ERROR";
                        ResponseMessage = "金豆发放失败!";
                    }
                }
            }
            else
            {
                StackExchangeRedisExtensions.Set(db, CacheKey.BagKey, bag);
                var res = rt.InsertBag(bag);
                if (res > 0)
                {
                    Code            = "SCCESS";
                    ResponseMessage = "金豆发放成功!";
                }
                else
                {
                    Code            = "ERROR";
                    ResponseMessage = "金豆发放失败!";
                }
            }
            return(Ok(new APIResponse <RBCreateBag>
            {
                Code = Code,
                ResponseMessage = ResponseMessage,
                Result = Result
            }));
        }
Example #3
0
        public List <RBCreateBag> GetSendBag(RBCreateBag bag)
        {
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@RID", bag.RID),
            };
            var result = Dapper.Helper.SQLHelper.QueryDataSet("select * from CreateBag where  RID=@RID", sp, CommandType.Text);

            if (result == "")
            {
                return(null);
            }
            return(result.JsonDeserialize <List <RBCreateBag> >());
        }
Example #4
0
        public RBCreateBag GetBag(RBCreateBag bag)
        {
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@BagStatus", bag.BagStatus)
            };
            var result = Dapper.Helper.SQLHelper.QueryDataSet("select * from CreateBag where BagStatus=@BagStatus", sp, CommandType.Text);

            if (result == "")
            {
                return(null);
            }
            return(result.JsonDeserialize <RBCreateBag>());
        }
Example #5
0
        public List <RBCreateBag> GetBag(RBCreateBag bag)
        {
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@BagStatus", bag.BagStatus),
                new SqlParameter("@RID", bag.RID.ToString().ToUpper()),
            };
            var result = Dapper.Helper.SQLHelper.QueryDataSet("select * from CreateBag where BagStatus=@BagStatus and RID=@RID", sp, CommandType.Text);

            if (result == "")
            {
                return(null);
            }
            return(result.JsonDeserialize <List <RBCreateBag> >());
        }
Example #6
0
 RBCreateBag GenerateBag(RBCreateBag bag, out decimal curAmount)
 {
     bag.BagNum -= 1;
     curAmount   = 0;
     if (bag.BagNum != 0)
     {
         Random  ran     = new Random();
         var     Num     = Decimal.ToInt32(bag.BagAmount * 100 - bag.BagNum);
         double  RandKey = ran.Next(1, Num);
         decimal f       = (decimal)(RandKey * 0.01);
         bag.BagAmount -= f;
         curAmount      = f;
     }
     else
     {
         curAmount     = bag.BagAmount;
         bag.BagAmount = 0;
     }
     return(bag);
 }
Example #7
0
        public int InsertBag(RBCreateBag bag)
        {
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@RID", bag.RID),
                new SqlParameter("@UserId", bag.UserId),
                new SqlParameter("@BagAmount", bag.BagAmount),
                new SqlParameter("@BagNum", bag.BagNum),
                new SqlParameter("@CreateTime", bag.CreateTime),
                new SqlParameter("@BagStatus", bag.BagStatus),
                new SqlParameter("@Winner", bag.Winner),
                new SqlParameter("@WinnerAmount", bag.WinnerAmount),
                new SqlParameter("@Remark", bag.Remark),
                new SqlParameter("@UserImgUrl", bag.CurrentUserImgUrl),
                new SqlParameter("@nikename", bag.nickname),
            };
            var result = Dapper.Helper.SQLHelper.Execute(@"INSERT INTO CreateBag
                   ([RID]
                   ,[UserId]
                   ,[BagAmount]
                   ,[BagNum]
                   ,[CreateTime]
                   ,[BagStatus]
                   ,[Winner]
                   ,[WinnerAmount]
                   ,[Remark]
                   ,[UserImgUrl]
                   ,[nikename])
             VALUES
                   (@RID
                   ,@UserId
                   ,@BagAmount
                   ,@BagNum
                   ,@CreateTime
                   ,@BagStatus
                   ,@Winner
                   ,@WinnerAmount,@Remark,@UserImgUrl,@nikename)", sp, CommandType.Text);

            return(result);
        }
Example #8
0
        public int InsertBag(RBCreateBag bag)
        {
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@RID", bag.RID),
                new SqlParameter("@UserId", bag.UserId),
                new SqlParameter("@BagAmount", bag.BagAmount),
                new SqlParameter("@BagNum", bag.BagNum),
                new SqlParameter("@CreateTime", bag.CreateTime),
                new SqlParameter("@BagStatus", bag.BagStatus),
                new SqlParameter("@Winner", bag.Winner),
                new SqlParameter("@WinnerAmount", bag.WinnerAmount)
            };
            var result = Dapper.Helper.SQLHelper.QueryDataSet(@"INSERT INTO CreateBag
                   ([RID]
                   ,[UserId]
                   ,[BagAmount]
                   ,[BagNum]
                   ,[CreateTime]
                   ,[BagStatus]
                   ,[Winner]
                   ,[WinnerAmount])
             VALUES
                   (@RID
                   ,@UserId
                   ,@BagAmount
                   ,@BagNum
                   ,@CreateTime
                   ,@BagStatus
                   ,@Winner
                   ,@WinnerAmount)", sp, CommandType.Text);

            if (result == "")
            {
                return(0);
            }
            return(1);
        }
Example #9
0
        public IHttpActionResult OpenBag(RBCreateBag bag)
        {
            var rt = ISoftSmart.Core.IoC.IoCFactory.Instance.CurrentContainer.Resolve <IRedBag>();//使用接口
            //bag.CreateTime = DateTime.Now;
            //var res = await Task.Run(() =>rt.GetBag(bag));
            var         Code            = string.Empty;
            var         ResponseMessage = string.Empty;
            RBCreateBag Result          = null;

            //Task.Run(() =>
            //{
            bag.CreateTime = DateTime.Now;
            var db = RedisManager.Instance.GetDatabase();

            if (StackExchangeRedisExtensions.HasKey(db, CacheKey.BagKey))
            {
                lock (_locker)
                {
                    var bagcache = StackExchangeRedisExtensions.Get <RBCreateBag>(db, CacheKey.BagKey);
                    if (bagcache.BagNum > 0)
                    {
                        decimal curAmount  = 0;
                        var     openResult = GenerateBag(bagcache, out curAmount);
                        Code            = "SUCCESS";
                        ResponseMessage = "抢到" + curAmount + "元!";
                        Result          = openResult;
                        RedisQueueManager.Push <RBCreateBag>(CacheKey.OpenBagKey, bagcache);
                        StackExchangeRedisExtensions.Set(db, CacheKey.BagKey, bagcache);
                    }
                    else
                    {
                        Code            = "ERROR";
                        ResponseMessage = "红包抢完了!";
                        bag.BagStatus   = 1;
                        //RedisQueueManager.DoQueue<int>((s) =>
                        //{
                        //    rt.ChangeBagStatus(bag);
                        //}, CacheKey.OpenBagKey);
                        rt.ChangeBagStatus(bag);
                        StackExchangeRedisExtensions.Remove(db, CacheKey.BagKey);
                    }
                }
            }
            else
            {
                StackExchangeRedisExtensions.Set(db, CacheKey.BagKey, bag);
            }
            return(Ok(new APIResponse <RBCreateBag>
            {
                Code = Code,
                ResponseMessage = ResponseMessage,
                Result = Result
            }));
            //});
            //return Ok(new APIResponse<RBCreateBag>
            //{
            //    Code = Code,
            //    ResponseMessage = ResponseMessage,
            //    Result = Result
            //});
        }