SycnSpTradeWifiService.cs 17 KB

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