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 list = new List(); 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("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); } } } }