/// <summary> /// 抢红包 /// </summary> /// <param name="username"></param> /// <param name="redId"></param> /// <returns></returns> public double receiver(string username, string redId) { double val = 0; if (redPackageRepository != null) { redPackageRepository.Get(redId); } else { val = RandomRealContainer.Dequeue(); } Console.WriteLine("这个用户:{0},领取了一个红包:{1}", username, val); return(val); }
/// <summary> /// 生成红包,为RealContainer赋值 /// </summary> public void GenerateRedPackage() { //求固定红包的概率 int fixCount = (int)Math.Floor(Count * FixMoneyLevel / 100.0); //求固定红包的数量,如果固定红包没那么多,会把名额留给随机红包 if (fixCount > FixMoney.Count) { fixCount = FixMoney.Count; } //生成固定金额的红包 for (int i = 0; i < fixCount; i++) { if (Money > 0) { Random rd = new Random(); int randomIndex = rd.Next(0, FixMoney.Count - 1); double val = FixMoney[randomIndex]; if (Money - val >= 0) { this.RealContainer.Add(val); FixMoney.Remove(val); Money -= val; SendMoney.Add(val); } else { fixCount--; } } } //求随机红包的数量 int randomCount = Count - fixCount; Console.WriteLine("需要产生随机红包的金额:{0}", Money); //产生随机红包 randNum(randomCount); //为红包池进行随机排序 RealContainer = RealContainer.OrderBy(x => Guid.NewGuid()).ToList(); //添加到随机队列 foreach (var item in RealContainer) { RandomRealContainer.Enqueue(item); } Console.WriteLine("红包金额剩余_balance:{0}", Money); }