123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- using System;
- using System.Collections.Generic;
- using System.Threading;
- using System.Linq;
- using System.Data;
- using MySystem;
- using MySystem.PxcModels;
- using Library;
- public class RedPackageHelper
- {
- public readonly static RedPackageHelper Instance = new RedPackageHelper();
- private RedPackageHelper()
- {
- }
- public void Start()
- {
- Thread th = new Thread(DoWorks);
- th.IsBackground = true;
- th.Start();
- }
- private void DoWorks()
- {
- while (true)
- {
- if (DateTime.Now.Hour >= 3)
- {
- string ActivityDate = DateTime.Now.ToString("yyyyMMdd");
- string check = function.ReadInstance("/Activity/RedPackage/" + ActivityDate + ".txt");
- if (string.IsNullOrEmpty(check))
- {
- function.WritePage("/Activity/RedPackage/", ActivityDate + ".txt", DateTime.Now.ToString());
- function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "生成指定未领取红包日志");
- WebCMSEntities db = new WebCMSEntities();
- try
- {
- // decimal TotalPrize = 888.88M;
- DateTime start = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 22:00:00");
- DateTime today = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00");
- string PreDate = DateTime.Now.AddDays(-1).ToString("yyyyMMdd");
- List<decimal> list = new List<decimal>();
- list.Add(88.88M);
- list.Add(40.76M);
- list.Add(38.88M);
- list.Add(18.88M);
- list.Add(15.88M);
- list.Add(11.88M);
- list.Add(8.88M);
- list.Add(8.88M);
- list.Add(8.88M);
- list.Add(8.88M);
- list.Add(8.88M);
- list.Add(8.88M);
- list.Add(8.88M);
- list.Add(8.88M);
- list.Add(8.88M);
- list.Add(8.88M);
- list.Add(8.88M);
- list.Add(8.88M);
- list.Add(8.88M);
- list.Add(8.88M);
- list.Add(8.88M);
- list.Add(8.88M);
- list.Add(8.88M);
- list.Add(8.88M);
- list.Add(8.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(5.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(3.88M);
- list.Add(1.88M);
- list.Add(1.88M);
- list.Add(1.88M);
- list.Add(1.88M);
- list.Add(1.88M);
- list.Add(1.88M);
- list.Add(1.88M);
- list.Add(1.88M);
- list.Add(1.88M);
- list.Add(1.88M);
- list.Add(1.88M);
- list.Add(1.88M);
- list.Add(1.88M);
- list.Add(1.88M);
- list.Add(1.88M);
- list.Add(1.88M);
- list.Add(1.88M);
- list.Add(1.88M);
- list.Add(1.88M);
- list.Add(1.88M);
- // TotalPrize -= 88.88M;
- // for (int i = 1; i < 150; i++)
- // {
- // list.Add(0.88M);
- // TotalPrize -= 0.88M;
- // }
- // for (int i = 0; i < 150; i++)
- // {
- // if (TotalPrize > 0)
- // {
- // int BigPrize = function.get_Random(1, 150);
- // decimal PrizeAmt = (decimal)function.get_Random(88, 1000) / 100M;
- // if (TotalPrize > PrizeAmt)
- // {
- // list[BigPrize] += PrizeAmt;
- // TotalPrize -= PrizeAmt;
- // }
- // else
- // {
- // list[BigPrize] += TotalPrize;
- // TotalPrize = 0;
- // }
- // }
- // else
- // {
- // break;
- // }
- // }
- foreach (decimal PrizeAmt in list)
- {
- db.ActivityRedPackageStock.Add(new ActivityRedPackageStock()
- {
- CreateDate = DateTime.Now,
- PrizeAmt = PrizeAmt,
- ActivityDate = ActivityDate,
- });
- }
- db.SaveChanges();
- }
- catch (Exception ex)
- {
- function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "生成指定未领取红包异常");
- }
- db.Dispose();
- function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "生成指定未领取红包日志");
- }
- }
- Thread.Sleep(60000);
- }
- }
- public void StartStatTop10()
- {
- Thread th = new Thread(StartStatTop10Do);
- th.IsBackground = true;
- th.Start();
- }
- private void StartStatTop10Do()
- {
- while (true)
- {
- OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
- OtherMySqlConn.op("delete from ActivityRedPackageTop10");
- using (WebCMSEntities db = new WebCMSEntities())
- {
- DataTable dt = OtherMySqlConn.dtable("select UserId,sum(PrizeAmt) from ActivityRedPackageJoins group by UserId order by sum(PrizeAmt) desc");
- int index = 0;
- foreach (DataRow dr in dt.Rows)
- {
- index += 1;
- int UserId = int.Parse(dr["UserId"].ToString());
- decimal PrizeAmt = decimal.Parse(dr[1].ToString());
- db.ActivityRedPackageTop10.Add(new ActivityRedPackageTop10()
- {
- Sort = index,
- UserId = UserId,
- PrizeAmt = PrizeAmt,
- Kind = 1,
- });
- }
- dt = OtherMySqlConn.dtable("select UserId,count(Id) from ActivityRedPackageJoins group by UserId order by count(Id) desc");
- index = 0;
- foreach (DataRow dr in dt.Rows)
- {
- index += 1;
- int UserId = int.Parse(dr["UserId"].ToString());
- int PrizeCount = int.Parse(dr[1].ToString());
- db.ActivityRedPackageTop10.Add(new ActivityRedPackageTop10()
- {
- Sort = index,
- UserId = UserId,
- TimeNum = PrizeCount,
- Kind = 2,
- });
- }
- db.SaveChanges();
- }
- Thread.Sleep(60000);
- }
- }
- public void StartSendPrize()
- {
- Thread th = new Thread(StartSendPrizeDo);
- th.IsBackground = true;
- th.Start();
- }
- private void StartSendPrizeDo()
- {
- while (true)
- {
- string content = RedisDbconn.Instance.RPop<string>("RedPrizeQueue");
- if (!string.IsNullOrEmpty(content))
- {
- try
- {
- using (WebCMSEntities db = new WebCMSEntities())
- {
- int Id = int.Parse(function.CheckInt(content));
- ActivityRedPackageJoins item = db.ActivityRedPackageJoins.FirstOrDefault(m => m.Id == Id);
- if (item != null)
- {
- int ParentUserId = item.UserId;
- UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == ParentUserId);
- if (account == null)
- {
- account = db.UserAccount.Add(new UserAccount()
- {
- Id = ParentUserId,
- UserId = ParentUserId,
- }).Entity;
- db.SaveChanges();
- }
- decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
- decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
- decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
- account.BalanceAmount += item.PrizeAmt;
- account.TotalAmount += item.PrizeAmt;
- decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
- decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
- decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
- UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
- {
- CreateDate = DateTime.Now,
- UpdateDate = DateTime.Now,
- UserId = ParentUserId, //创客
- ChangeType = 31, //变动类型
- ChangeAmount = item.PrizeAmt, //变更金额
- BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
- AfterTotalAmount = AfterTotalAmount, //变更后总金额
- BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
- AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
- BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
- AfterBalanceAmount = AfterBalanceAmount, //变更后余额
- QueryCount = item.Id,
- Remark = "开机抢红包活动",
- }).Entity;
- db.SaveChanges();
- }
- }
- }
- catch (Exception ex)
- {
- function.WriteLog(ex.ToString(), "领取红包奖励异常");
- }
- Thread.Sleep(200);
- }
- else
- {
- Thread.Sleep(60000);
- }
- }
- }
- }
|