SycnSpTradeWifiService.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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;
  39. if(trade.SeoTitle != "v2") TradeAmount = TradeAmount / 100;
  40. bool statFlag = false;
  41. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == trade.TradeSnNo);
  42. if (pos != null)
  43. {
  44. statFlag = db.WifiTradeRecord.Any(m => m.SnNo == trade.TradeSnNo);
  45. PxcModels.WifiTradeRecord add = db.WifiTradeRecord.Add(new PxcModels.WifiTradeRecord()
  46. {
  47. CreateDate = trade.CreateDate,
  48. UpdateDate = trade.UpdateDate,
  49. SnNo = trade.TradeSnNo, //渠道SN号
  50. TradeAmount = TradeAmount, //交易金额
  51. BrandId = pos.BrandId, //品牌
  52. Remark = trade.Remark, //备注
  53. }).Entity;
  54. }
  55. db.SpOrderNos.Add(new PxcModels.SpOrderNos()
  56. {
  57. OrderNo = trade.TradeSerialNo
  58. });
  59. db.SaveChanges();
  60. //开始统计
  61. int Months = 0;
  62. if(trade.SeoTitle == "v2")
  63. {
  64. if(trade.SerEntryMode == "1") //判断是否是押金
  65. {
  66. pos.SeoKeyword = trade.TradeAmount.ToString("f2");
  67. pos.PrizeParams = trade.TradeAmount.ToString("f2");
  68. db.SaveChanges();
  69. }
  70. else
  71. {
  72. string duration = trade.Field2; //设备套餐时长
  73. string unit = trade.Field3; //套餐单位 0:按天 1:按月
  74. if(unit == "1") Months = int.Parse(function.CheckInt(duration));
  75. else Months = int.Parse(function.CheckInt(duration)) / 30;
  76. pos.ActivationState = 1;
  77. pos.ActivationTime = DateTime.Now;
  78. PxcModels.PosMerchantInfo add = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId) ?? new PxcModels.PosMerchantInfo();
  79. add.ActiveStatus = 1;
  80. add.MerStandardDate = DateTime.Now;
  81. db.SaveChanges();
  82. //检查交易状态,并发奖励
  83. SycnSpMerchantService.Instance.WifiSendPrize(db, pos, add);
  84. //发放奖励MQ
  85. PrizeSetHelper.Do("6", pos.PosSn);
  86. }
  87. }
  88. else
  89. {
  90. if(pos.BrandId == 23 && TradeAmount == 49) Months = 1;
  91. if(pos.BrandId == 23 && TradeAmount == 79) Months = 3;
  92. if(pos.BrandId == 23 && TradeAmount == 119) Months = 12;
  93. if(pos.BrandId == 23 && TradeAmount == 199) Months = 24;
  94. if(pos.BrandId == 24 && TradeAmount == 59) Months = 1;
  95. if(pos.BrandId == 24 && TradeAmount == 159) Months = 3;
  96. if(pos.BrandId == 24 && TradeAmount == 399) Months = 12;
  97. if(pos.BrandId == 24 && TradeAmount == 699) Months = 24;
  98. if(pos.BrandId == 25 && TradeAmount == 69) Months = 1;
  99. if(pos.BrandId == 25 && TradeAmount == 199) Months = 3;
  100. if(pos.BrandId == 25 && TradeAmount == 499) Months = 12;
  101. if(pos.BrandId == 25 && TradeAmount == 799) Months = 24;
  102. if(pos.BrandId == 26 && TradeAmount == 219) Months = 1;
  103. if(pos.BrandId == 26 && TradeAmount == 449) Months = 3;
  104. if(pos.BrandId == 26 && TradeAmount == 1399) Months = 12;
  105. if(pos.BrandId == 26 && TradeAmount == 2698) Months = 24;
  106. }
  107. if(Months > 0)
  108. {
  109. decimal TradeAmt = TradeAmount / Months;
  110. TradeAmt = PublicFunction.NumberFormat(TradeAmt);
  111. string TradeMonth = DateTime.Now.AddMonths(-1).ToString("yyyyMM");
  112. if(statFlag)
  113. {
  114. PxcModels.TradeDaySummary stat = db.TradeDaySummary.Where(m => m.UserId == pos.BuyUserId && m.BrandId == pos.BrandId).OrderByDescending(m => m.Id).FirstOrDefault() ?? new PxcModels.TradeDaySummary();
  115. TradeMonth = string.IsNullOrEmpty(stat.TradeMonth) ? DateTime.Now.AddMonths(-1).ToString("yyyyMM") : stat.TradeMonth;
  116. }
  117. DateTime StartMonth = DateTime.Parse(TradeMonth.Substring(0, 4) + "-" + TradeMonth.Substring(4, 2) + "-01 00:00:00");
  118. for (int i = 1; i <= Months; i++)
  119. {
  120. StartMonth = StartMonth.AddMonths(1);
  121. StatTrade(db, pos.BuyUserId, pos.BrandId, StartMonth.ToString("yyyyMM"), TradeAmt);
  122. if (pos.BindingTime < DateTime.Now.AddMonths(-9))
  123. {
  124. StatTradeBefore(db, pos.BuyUserId, pos.BrandId, StartMonth.ToString("yyyyMM"), TradeAmt);
  125. }
  126. else
  127. {
  128. StatTradeAfter(db, pos.BuyUserId, pos.BrandId, StartMonth.ToString("yyyyMM"), TradeAmt);
  129. }
  130. }
  131. }
  132. //检查交易状态,并发奖励
  133. if(pos.ActivationState == 1)
  134. {
  135. PxcModels.PosMerchantInfo add = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId) ?? new PxcModels.PosMerchantInfo();
  136. SycnSpMerchantService.Instance.WifiSendPrize(db, pos, add);
  137. }
  138. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  139. if (edit != null)
  140. {
  141. edit.Status = 2;
  142. spdb.SaveChanges();
  143. }
  144. }
  145. if(trade.SerEntryMode == "1")
  146. {
  147. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == trade.TradeSnNo);
  148. if (pos != null)
  149. {
  150. pos.SeoKeyword = trade.TradeAmount.ToString();
  151. pos.PrizeParams = trade.TradeAmount.ToString();
  152. db.SaveChanges();
  153. }
  154. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  155. if (edit != null)
  156. {
  157. edit.Status = 2;
  158. spdb.SaveChanges();
  159. }
  160. }
  161. }
  162. spdb.Dispose();
  163. db.Dispose();
  164. }
  165. catch (Exception ex)
  166. {
  167. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP交易数据到MAIN异常");
  168. }
  169. Thread.Sleep(500);
  170. }
  171. }
  172. public void StatTrade(PxcModels.WebCMSEntities db, int UserId, int BrandId, string TradeMonth, decimal TradeAmount)
  173. {
  174. PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new PxcModels.Users();
  175. string ParentNav = user.ParentNav;
  176. string TradeDate = TradeMonth + "01";
  177. ParentNav += "," + UserId + ",";
  178. PxcModels.TradeDaySummary selfStat = db.TradeDaySummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "self");
  179. if (selfStat == null)
  180. {
  181. selfStat = db.TradeDaySummary.Add(new PxcModels.TradeDaySummary()
  182. {
  183. UserId = UserId,
  184. TradeMonth = TradeMonth,
  185. TradeDate = TradeDate,
  186. BrandId = BrandId,
  187. SeoTitle = "self",
  188. }).Entity;
  189. db.SaveChanges();
  190. }
  191. selfStat.HelpDirectTradeAmt += TradeAmount;
  192. if (!string.IsNullOrEmpty(ParentNav))
  193. {
  194. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  195. foreach (string NavUserIdString in ParentNavList)
  196. {
  197. int NavUserId = int.Parse(NavUserIdString);
  198. PxcModels.TradeDaySummary teamStat = db.TradeDaySummary.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "team");
  199. if (teamStat == null)
  200. {
  201. teamStat = db.TradeDaySummary.Add(new PxcModels.TradeDaySummary()
  202. {
  203. UserId = NavUserId,
  204. TradeMonth = TradeMonth,
  205. TradeDate = TradeDate,
  206. BrandId = BrandId,
  207. SeoTitle = "team",
  208. }).Entity;
  209. db.SaveChanges();
  210. }
  211. teamStat.HelpNonDirectTradeAmt += TradeAmount;
  212. db.SaveChanges();
  213. }
  214. }
  215. }
  216. public void StatTradeBefore(PxcModels.WebCMSEntities db, int UserId, int BrandId, string TradeMonth, decimal TradeAmount)
  217. {
  218. PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new PxcModels.Users();
  219. string ParentNav = user.ParentNav;
  220. string TradeDate = TradeMonth + "01";
  221. ParentNav += "," + UserId + ",";
  222. PxcModels.TradeDaySummaryBefore selfStat = db.TradeDaySummaryBefore.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "self");
  223. if (selfStat == null)
  224. {
  225. selfStat = db.TradeDaySummaryBefore.Add(new PxcModels.TradeDaySummaryBefore()
  226. {
  227. UserId = UserId,
  228. TradeMonth = TradeMonth,
  229. TradeDate = TradeDate,
  230. BrandId = BrandId,
  231. SeoTitle = "self",
  232. }).Entity;
  233. db.SaveChanges();
  234. }
  235. selfStat.HelpDirectTradeAmt += TradeAmount;
  236. if (!string.IsNullOrEmpty(ParentNav))
  237. {
  238. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  239. foreach (string NavUserIdString in ParentNavList)
  240. {
  241. int NavUserId = int.Parse(NavUserIdString);
  242. PxcModels.TradeDaySummaryBefore teamStat = db.TradeDaySummaryBefore.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "team");
  243. if (teamStat == null)
  244. {
  245. teamStat = db.TradeDaySummaryBefore.Add(new PxcModels.TradeDaySummaryBefore()
  246. {
  247. UserId = NavUserId,
  248. TradeMonth = TradeMonth,
  249. TradeDate = TradeDate,
  250. BrandId = BrandId,
  251. SeoTitle = "team",
  252. }).Entity;
  253. db.SaveChanges();
  254. }
  255. teamStat.HelpNonDirectTradeAmt += TradeAmount;
  256. db.SaveChanges();
  257. }
  258. }
  259. }
  260. public void StatTradeAfter(PxcModels.WebCMSEntities db, int UserId, int BrandId, string TradeMonth, decimal TradeAmount)
  261. {
  262. PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new PxcModels.Users();
  263. string ParentNav = user.ParentNav;
  264. string TradeDate = TradeMonth + "01";
  265. ParentNav += "," + UserId + ",";
  266. PxcModels.TradeDaySummaryAfter selfStat = db.TradeDaySummaryAfter.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "self");
  267. if (selfStat == null)
  268. {
  269. selfStat = db.TradeDaySummaryAfter.Add(new PxcModels.TradeDaySummaryAfter()
  270. {
  271. UserId = UserId,
  272. TradeMonth = TradeMonth,
  273. TradeDate = TradeDate,
  274. BrandId = BrandId,
  275. SeoTitle = "self",
  276. }).Entity;
  277. db.SaveChanges();
  278. }
  279. selfStat.HelpDirectTradeAmt += TradeAmount;
  280. if (!string.IsNullOrEmpty(ParentNav))
  281. {
  282. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  283. foreach (string NavUserIdString in ParentNavList)
  284. {
  285. int NavUserId = int.Parse(NavUserIdString);
  286. PxcModels.TradeDaySummaryAfter teamStat = db.TradeDaySummaryAfter.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "team");
  287. if (teamStat == null)
  288. {
  289. teamStat = db.TradeDaySummaryAfter.Add(new PxcModels.TradeDaySummaryAfter()
  290. {
  291. UserId = NavUserId,
  292. TradeMonth = TradeMonth,
  293. TradeDate = TradeDate,
  294. BrandId = BrandId,
  295. SeoTitle = "team",
  296. }).Entity;
  297. db.SaveChanges();
  298. }
  299. teamStat.HelpNonDirectTradeAmt += TradeAmount;
  300. db.SaveChanges();
  301. }
  302. }
  303. }
  304. }
  305. }