SycnSpTradeWifiService.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MySystem.SpModels;
  5. using Library;
  6. using LitJson;
  7. using System.Threading;
  8. namespace MySystem
  9. {
  10. public class SycnSpTradeWifiService
  11. {
  12. public readonly static SycnSpTradeWifiService Instance = new SycnSpTradeWifiService();
  13. private SycnSpTradeWifiService()
  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. while (true)
  24. {
  25. try
  26. {
  27. List<string> BrandIds = new List<string>();
  28. WebCMSEntities spdb = new WebCMSEntities();
  29. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  30. DateTime start = DateTime.Now.AddDays(-10);
  31. DateTime end = DateTime.Now.AddMinutes(-1);
  32. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/TradeRecordId.txt")));
  33. IQueryable<TradeRecord> trades = spdb.TradeRecord.Where(m => m.Id >= StartId && m.ProductType == "23" && m.CreateDate >= start && m.Status == 1).OrderBy(m => m.CreateDate).Take(20);
  34. foreach (TradeRecord trade in trades.ToList())
  35. {
  36. bool check = db.SpOrderNos.Any(m => m.OrderNo == trade.TradeSerialNo);
  37. if (!check)
  38. {
  39. decimal TradeAmount = trade.TradeAmount / 100;
  40. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == trade.TradeSnNo);
  41. if (pos != null)
  42. {
  43. PxcModels.WifiTradeRecord add = db.WifiTradeRecord.Add(new PxcModels.WifiTradeRecord()
  44. {
  45. CreateDate = trade.CreateDate,
  46. UpdateDate = trade.UpdateDate,
  47. SnNo = trade.TradeSnNo, //渠道SN号
  48. TradeAmount = TradeAmount, //交易金额
  49. BrandId = pos.BrandId, //品牌
  50. Remark = trade.Remark, //备注
  51. }).Entity;
  52. }
  53. db.SpOrderNos.Add(new PxcModels.SpOrderNos()
  54. {
  55. OrderNo = trade.TradeSerialNo
  56. });
  57. db.SaveChanges();
  58. //开始统计
  59. int Months = 0;
  60. if(pos.BrandId == 23 && TradeAmount == 49) Months = 1;
  61. if(pos.BrandId == 23 && TradeAmount == 79) Months = 3;
  62. if(pos.BrandId == 23 && TradeAmount == 119) Months = 12;
  63. if(pos.BrandId == 23 && TradeAmount == 199) Months = 24;
  64. if(pos.BrandId == 24 && TradeAmount == 59) Months = 1;
  65. if(pos.BrandId == 24 && TradeAmount == 159) Months = 3;
  66. if(pos.BrandId == 24 && TradeAmount == 399) Months = 12;
  67. if(pos.BrandId == 24 && TradeAmount == 699) Months = 24;
  68. if(pos.BrandId == 25 && TradeAmount == 69) Months = 1;
  69. if(pos.BrandId == 25 && TradeAmount == 199) Months = 3;
  70. if(pos.BrandId == 25 && TradeAmount == 499) Months = 12;
  71. if(pos.BrandId == 25 && TradeAmount == 799) Months = 24;
  72. if(pos.BrandId == 26 && TradeAmount == 219) Months = 1;
  73. if(pos.BrandId == 26 && TradeAmount == 449) Months = 3;
  74. if(pos.BrandId == 26 && TradeAmount == 1399) Months = 12;
  75. if(pos.BrandId == 26 && TradeAmount == 2698) Months = 24;
  76. if(Months > 0)
  77. {
  78. decimal TradeAmt = TradeAmount / Months;
  79. PxcModels.TradeDaySummary stat = db.TradeDaySummary.Where(m => m.UserId == pos.BuyUserId && m.BrandId == pos.BrandId).OrderByDescending(m => m.Id).FirstOrDefault() ?? new PxcModels.TradeDaySummary();
  80. string TradeMonth = string.IsNullOrEmpty(stat.TradeMonth) ? DateTime.Now.AddMonths(-1).ToString("yyyyMM") : stat.TradeMonth;
  81. DateTime StartMonth = DateTime.Parse(TradeMonth.Substring(0, 4) + "-" + TradeMonth.Substring(4, 2) + "-01 00:00:00");
  82. for (int i = 1; i <= Months; i++)
  83. {
  84. StartMonth = StartMonth.AddMonths(1);
  85. StatTrade(db, pos.BuyUserId, pos.BrandId, TradeMonth, TradeAmt);
  86. }
  87. }
  88. }
  89. }
  90. spdb.Dispose();
  91. db.Dispose();
  92. }
  93. catch (Exception ex)
  94. {
  95. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP交易数据到MAIN异常");
  96. }
  97. Thread.Sleep(500);
  98. }
  99. }
  100. public void StatTrade(PxcModels.WebCMSEntities db, int UserId, int BrandId, string TradeMonth, decimal TradeAmount)
  101. {
  102. PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new PxcModels.Users();
  103. string ParentNav = user.ParentNav;
  104. string TradeDate = TradeMonth + "01";
  105. ParentNav += "," + UserId + ",";
  106. PxcModels.TradeDaySummary selfStat = db.TradeDaySummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "self");
  107. if (selfStat == null)
  108. {
  109. selfStat = db.TradeDaySummary.Add(new PxcModels.TradeDaySummary()
  110. {
  111. UserId = UserId,
  112. TradeMonth = TradeMonth,
  113. TradeDate = TradeDate,
  114. BrandId = BrandId,
  115. SeoTitle = "self",
  116. }).Entity;
  117. db.SaveChanges();
  118. }
  119. selfStat.HelpDirectTradeAmt += TradeAmount;
  120. if (!string.IsNullOrEmpty(ParentNav))
  121. {
  122. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  123. foreach (string NavUserIdString in ParentNavList)
  124. {
  125. int NavUserId = int.Parse(NavUserIdString);
  126. PxcModels.TradeDaySummary teamStat = db.TradeDaySummary.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "team");
  127. if (teamStat == null)
  128. {
  129. teamStat = db.TradeDaySummary.Add(new PxcModels.TradeDaySummary()
  130. {
  131. UserId = NavUserId,
  132. TradeMonth = TradeMonth,
  133. TradeDate = TradeDate,
  134. BrandId = BrandId,
  135. SeoTitle = "team",
  136. }).Entity;
  137. db.SaveChanges();
  138. }
  139. teamStat.HelpNonDirectTradeAmt += TradeAmount;
  140. db.SaveChanges();
  141. }
  142. }
  143. }
  144. }
  145. }