SycnSpTradeWifiService.cs 19 KB

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