SycnSpTradeWifiService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 && trade.SerEntryMode != "1")
  37. {
  38. decimal TradeAmount = trade.TradeAmount / 100;
  39. bool statFlag = false;
  40. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == trade.TradeSnNo);
  41. if (pos != null)
  42. {
  43. statFlag = db.WifiTradeRecord.Any(m => m.SnNo == trade.TradeSnNo);
  44. PxcModels.WifiTradeRecord add = db.WifiTradeRecord.Add(new PxcModels.WifiTradeRecord()
  45. {
  46. CreateDate = trade.CreateDate,
  47. UpdateDate = trade.UpdateDate,
  48. SnNo = trade.TradeSnNo, //渠道SN号
  49. TradeAmount = TradeAmount, //交易金额
  50. BrandId = pos.BrandId, //品牌
  51. Remark = trade.Remark, //备注
  52. }).Entity;
  53. }
  54. db.SpOrderNos.Add(new PxcModels.SpOrderNos()
  55. {
  56. OrderNo = trade.TradeSerialNo
  57. });
  58. db.SaveChanges();
  59. //开始统计
  60. int Months = 0;
  61. if(trade.SeoTitle == "v2")
  62. {
  63. string duration = trade.Field2; //设备套餐时长
  64. string unit = trade.Field3; //套餐单位 0:按天 1:按月
  65. if(unit == "1") Months = int.Parse(function.CheckInt(duration));
  66. }
  67. else
  68. {
  69. if(pos.BrandId == 23 && TradeAmount == 49) Months = 1;
  70. if(pos.BrandId == 23 && TradeAmount == 79) Months = 3;
  71. if(pos.BrandId == 23 && TradeAmount == 119) Months = 12;
  72. if(pos.BrandId == 23 && TradeAmount == 199) Months = 24;
  73. if(pos.BrandId == 24 && TradeAmount == 59) Months = 1;
  74. if(pos.BrandId == 24 && TradeAmount == 159) Months = 3;
  75. if(pos.BrandId == 24 && TradeAmount == 399) Months = 12;
  76. if(pos.BrandId == 24 && TradeAmount == 699) Months = 24;
  77. if(pos.BrandId == 25 && TradeAmount == 69) Months = 1;
  78. if(pos.BrandId == 25 && TradeAmount == 199) Months = 3;
  79. if(pos.BrandId == 25 && TradeAmount == 499) Months = 12;
  80. if(pos.BrandId == 25 && TradeAmount == 799) Months = 24;
  81. if(pos.BrandId == 26 && TradeAmount == 219) Months = 1;
  82. if(pos.BrandId == 26 && TradeAmount == 449) Months = 3;
  83. if(pos.BrandId == 26 && TradeAmount == 1399) Months = 12;
  84. if(pos.BrandId == 26 && TradeAmount == 2698) Months = 24;
  85. }
  86. if(Months > 0)
  87. {
  88. decimal TradeAmt = TradeAmount / Months;
  89. TradeAmt = PublicFunction.NumberFormat(TradeAmt);
  90. string TradeMonth = DateTime.Now.AddMonths(-1).ToString("yyyyMM");
  91. if(statFlag)
  92. {
  93. PxcModels.TradeDaySummary stat = db.TradeDaySummary.Where(m => m.UserId == pos.BuyUserId && m.BrandId == pos.BrandId).OrderByDescending(m => m.Id).FirstOrDefault() ?? new PxcModels.TradeDaySummary();
  94. TradeMonth = string.IsNullOrEmpty(stat.TradeMonth) ? DateTime.Now.AddMonths(-1).ToString("yyyyMM") : stat.TradeMonth;
  95. }
  96. DateTime StartMonth = DateTime.Parse(TradeMonth.Substring(0, 4) + "-" + TradeMonth.Substring(4, 2) + "-01 00:00:00");
  97. for (int i = 1; i <= Months; i++)
  98. {
  99. StartMonth = StartMonth.AddMonths(1);
  100. StatTrade(db, pos.BuyUserId, pos.BrandId, StartMonth.ToString("yyyyMM"), TradeAmt);
  101. }
  102. }
  103. //检查交易状态,并发奖励
  104. if(pos.ActivationState == 1)
  105. {
  106. PxcModels.PosMerchantInfo add = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId) ?? new PxcModels.PosMerchantInfo();
  107. SycnSpMerchantService.Instance.WifiSendPrize(db, pos, add);
  108. }
  109. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  110. if (edit != null)
  111. {
  112. edit.Status = 2;
  113. spdb.SaveChanges();
  114. }
  115. }
  116. if(trade.SerEntryMode == "1")
  117. {
  118. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == trade.TradeSnNo);
  119. if (pos != null)
  120. {
  121. pos.SeoKeyword = trade.TradeAmount.ToString();
  122. pos.PrizeParams = trade.TradeAmount.ToString();
  123. db.SaveChanges();
  124. }
  125. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  126. if (edit != null)
  127. {
  128. edit.Status = 2;
  129. spdb.SaveChanges();
  130. }
  131. }
  132. }
  133. spdb.Dispose();
  134. db.Dispose();
  135. }
  136. catch (Exception ex)
  137. {
  138. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP交易数据到MAIN异常");
  139. }
  140. Thread.Sleep(500);
  141. }
  142. }
  143. public void StatTrade(PxcModels.WebCMSEntities db, int UserId, int BrandId, string TradeMonth, decimal TradeAmount)
  144. {
  145. PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new PxcModels.Users();
  146. string ParentNav = user.ParentNav;
  147. string TradeDate = TradeMonth + "01";
  148. ParentNav += "," + UserId + ",";
  149. PxcModels.TradeDaySummary selfStat = db.TradeDaySummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "self");
  150. if (selfStat == null)
  151. {
  152. selfStat = db.TradeDaySummary.Add(new PxcModels.TradeDaySummary()
  153. {
  154. UserId = UserId,
  155. TradeMonth = TradeMonth,
  156. TradeDate = TradeDate,
  157. BrandId = BrandId,
  158. SeoTitle = "self",
  159. }).Entity;
  160. db.SaveChanges();
  161. }
  162. selfStat.HelpDirectTradeAmt += TradeAmount;
  163. if (!string.IsNullOrEmpty(ParentNav))
  164. {
  165. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  166. foreach (string NavUserIdString in ParentNavList)
  167. {
  168. int NavUserId = int.Parse(NavUserIdString);
  169. PxcModels.TradeDaySummary teamStat = db.TradeDaySummary.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "team");
  170. if (teamStat == null)
  171. {
  172. teamStat = db.TradeDaySummary.Add(new PxcModels.TradeDaySummary()
  173. {
  174. UserId = NavUserId,
  175. TradeMonth = TradeMonth,
  176. TradeDate = TradeDate,
  177. BrandId = BrandId,
  178. SeoTitle = "team",
  179. }).Entity;
  180. db.SaveChanges();
  181. }
  182. teamStat.HelpNonDirectTradeAmt += TradeAmount;
  183. db.SaveChanges();
  184. }
  185. }
  186. }
  187. }
  188. }