SycnSpTradeWifiService.cs 8.0 KB

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