SycnSpTradeWifiService.cs 8.5 KB

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