SycnSpTradeWifiServiceTmp.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MySystem.ReadModels;
  5. using Library;
  6. using LitJson;
  7. using System.Threading;
  8. namespace MySystem
  9. {
  10. public class SycnSpTradeWifiServiceTmp
  11. {
  12. public readonly static SycnSpTradeWifiServiceTmp Instance = new SycnSpTradeWifiServiceTmp();
  13. private SycnSpTradeWifiServiceTmp()
  14. { }
  15. public void Start()
  16. {
  17. Thread th = new Thread(StartDo);
  18. th.IsBackground = true;
  19. th.Start();
  20. }
  21. public void StartDo()
  22. {
  23. try
  24. {
  25. WebCMSEntities db = new WebCMSEntities();
  26. DateTime start = DateTime.Parse("2024-08-01 00:00:00");
  27. List<WifiTradeRecord> trades = db.WifiTradeRecord.Where(m => m.CreateDate >= start).ToList();
  28. List<string> SnNos = trades.Select(m => m.SnNo).ToList();
  29. List<PosMachinesTwo> poslist = db.PosMachinesTwo.Where(m => SnNos.Contains(m.PosSn)).ToList();
  30. foreach(WifiTradeRecord trade in trades)
  31. {
  32. decimal TradeAmount = trade.TradeAmount;
  33. bool statFlag = false;
  34. PosMachinesTwo pos = poslist.FirstOrDefault(m => m.PosSn == trade.SnNo);
  35. if (pos != null)
  36. {
  37. statFlag = db.WifiTradeRecord.Count(m => m.SnNo == trade.SnNo) > 1;
  38. }
  39. //开始统计
  40. int Months = 0;
  41. if(pos.BrandId == 23 && TradeAmount == 49) Months = 1;
  42. if(pos.BrandId == 23 && TradeAmount == 79) Months = 3;
  43. if(pos.BrandId == 23 && TradeAmount == 119) Months = 12;
  44. if(pos.BrandId == 23 && TradeAmount == 199) Months = 24;
  45. if(pos.BrandId == 24 && TradeAmount == 59) Months = 1;
  46. if(pos.BrandId == 24 && TradeAmount == 159) Months = 3;
  47. if(pos.BrandId == 24 && TradeAmount == 399) Months = 12;
  48. if(pos.BrandId == 24 && TradeAmount == 699) Months = 24;
  49. if(pos.BrandId == 25 && TradeAmount == 69) Months = 1;
  50. if(pos.BrandId == 25 && TradeAmount == 199) Months = 3;
  51. if(pos.BrandId == 25 && TradeAmount == 499) Months = 12;
  52. if(pos.BrandId == 25 && TradeAmount == 799) Months = 24;
  53. if(pos.BrandId == 26 && TradeAmount == 219) Months = 1;
  54. if(pos.BrandId == 26 && TradeAmount == 449) Months = 3;
  55. if(pos.BrandId == 26 && TradeAmount == 1399) Months = 12;
  56. if(pos.BrandId == 26 && TradeAmount == 2698) Months = 24;
  57. if(Months > 0)
  58. {
  59. decimal TradeAmt = TradeAmount / Months;
  60. TradeAmt = PublicFunction.NumberFormat(TradeAmt);
  61. string TradeMonth = trade.CreateDate.Value.AddMonths(-1).ToString("yyyyMM");
  62. if(statFlag)
  63. {
  64. TradeDaySummary stat = db.TradeDaySummary.Where(m => m.UserId == pos.BuyUserId && m.BrandId == pos.BrandId).OrderByDescending(m => m.Id).FirstOrDefault() ?? new TradeDaySummary();
  65. TradeMonth = string.IsNullOrEmpty(stat.TradeMonth) ? trade.CreateDate.Value.AddMonths(-1).ToString("yyyyMM") : stat.TradeMonth;
  66. }
  67. DateTime StartMonth = DateTime.Parse(TradeMonth.Substring(0, 4) + "-" + TradeMonth.Substring(4, 2) + "-01 00:00:00");
  68. for (int i = 1; i <= Months; i++)
  69. {
  70. StartMonth = StartMonth.AddMonths(1);
  71. StatTrade(pos.BuyUserId, pos.BrandId, StartMonth.ToString("yyyyMM"), TradeAmt);
  72. }
  73. }
  74. }
  75. db.Dispose();
  76. }
  77. catch (Exception ex)
  78. {
  79. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步WIFI数据异常");
  80. }
  81. }
  82. public void StatTrade(int UserId, int BrandId, string TradeMonth, decimal TradeAmount)
  83. {
  84. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  85. PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new PxcModels.Users();
  86. string ParentNav = user.ParentNav;
  87. string TradeDate = TradeMonth + "01";
  88. ParentNav += "," + UserId + ",";
  89. PxcModels.TradeDaySummary selfStat = db.TradeDaySummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "self");
  90. if (selfStat == null)
  91. {
  92. selfStat = db.TradeDaySummary.Add(new PxcModels.TradeDaySummary()
  93. {
  94. UserId = UserId,
  95. TradeMonth = TradeMonth,
  96. TradeDate = TradeDate,
  97. BrandId = BrandId,
  98. SeoTitle = "self",
  99. }).Entity;
  100. db.SaveChanges();
  101. }
  102. selfStat.HelpDirectTradeAmt += TradeAmount;
  103. if (!string.IsNullOrEmpty(ParentNav))
  104. {
  105. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  106. foreach (string NavUserIdString in ParentNavList)
  107. {
  108. int NavUserId = int.Parse(NavUserIdString);
  109. PxcModels.TradeDaySummary teamStat = db.TradeDaySummary.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "team");
  110. if (teamStat == null)
  111. {
  112. teamStat = db.TradeDaySummary.Add(new PxcModels.TradeDaySummary()
  113. {
  114. UserId = NavUserId,
  115. TradeMonth = TradeMonth,
  116. TradeDate = TradeDate,
  117. BrandId = BrandId,
  118. SeoTitle = "team",
  119. }).Entity;
  120. db.SaveChanges();
  121. }
  122. teamStat.HelpNonDirectTradeAmt += TradeAmount;
  123. db.SaveChanges();
  124. }
  125. }
  126. db.Dispose();
  127. }
  128. }
  129. }