SycnSpTradeWifiService.cs 17 KB

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