SycnSpTradeWifiService.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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, StartMonth.ToString("yyyyMM"), TradeAmt);
  86. }
  87. }
  88. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  89. if (edit != null)
  90. {
  91. edit.Status = 2;
  92. spdb.SaveChanges();
  93. }
  94. }
  95. }
  96. spdb.Dispose();
  97. db.Dispose();
  98. }
  99. catch (Exception ex)
  100. {
  101. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP交易数据到MAIN异常");
  102. }
  103. Thread.Sleep(500);
  104. }
  105. }
  106. public void StatTrade(PxcModels.WebCMSEntities db, int UserId, int BrandId, string TradeMonth, decimal TradeAmount)
  107. {
  108. PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new PxcModels.Users();
  109. string ParentNav = user.ParentNav;
  110. string TradeDate = TradeMonth + "01";
  111. ParentNav += "," + UserId + ",";
  112. PxcModels.TradeDaySummary selfStat = db.TradeDaySummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "self");
  113. if (selfStat == null)
  114. {
  115. selfStat = db.TradeDaySummary.Add(new PxcModels.TradeDaySummary()
  116. {
  117. UserId = UserId,
  118. TradeMonth = TradeMonth,
  119. TradeDate = TradeDate,
  120. BrandId = BrandId,
  121. SeoTitle = "self",
  122. }).Entity;
  123. db.SaveChanges();
  124. }
  125. selfStat.HelpDirectTradeAmt += TradeAmount;
  126. if (!string.IsNullOrEmpty(ParentNav))
  127. {
  128. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  129. foreach (string NavUserIdString in ParentNavList)
  130. {
  131. int NavUserId = int.Parse(NavUserIdString);
  132. PxcModels.TradeDaySummary teamStat = db.TradeDaySummary.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "team");
  133. if (teamStat == null)
  134. {
  135. teamStat = db.TradeDaySummary.Add(new PxcModels.TradeDaySummary()
  136. {
  137. UserId = NavUserId,
  138. TradeMonth = TradeMonth,
  139. TradeDate = TradeDate,
  140. BrandId = BrandId,
  141. SeoTitle = "team",
  142. }).Entity;
  143. db.SaveChanges();
  144. }
  145. teamStat.HelpNonDirectTradeAmt += TradeAmount;
  146. db.SaveChanges();
  147. }
  148. }
  149. }
  150. }
  151. }