SycnSpTradeWifiService.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. add.Duration = Months;
  111. add.DoMonths = 1;
  112. add.Unit = "m";
  113. db.SaveChanges();
  114. // if(Months > 0)
  115. // {
  116. decimal TradeAmt = TradeAmount / Months;
  117. TradeAmt = PublicFunction.NumberFormat(TradeAmt);
  118. string TradeMonth = DateTime.Now.ToString("yyyyMM");
  119. // if(statFlag)
  120. // {
  121. // PxcModels.TradeDaySummary stat = db.TradeDaySummary.Where(m => m.UserId == pos.BuyUserId && m.BrandId == pos.BrandId).OrderByDescending(m => m.Id).FirstOrDefault() ?? new PxcModels.TradeDaySummary();
  122. // TradeMonth = string.IsNullOrEmpty(stat.TradeMonth) ? DateTime.Now.AddMonths(-1).ToString("yyyyMM") : stat.TradeMonth;
  123. // }
  124. // DateTime StartMonth = DateTime.Parse(TradeMonth.Substring(0, 4) + "-" + TradeMonth.Substring(4, 2) + "-01 00:00:00");
  125. // for (int i = 1; i <= Months; i++)
  126. // {
  127. // StartMonth = StartMonth.AddMonths(1);
  128. StatTrade(db, pos.BuyUserId, pos.BrandId, TradeMonth, TradeAmt);
  129. if (pos.BindingTime < DateTime.Now.AddMonths(-9))
  130. {
  131. StatTradeBefore(db, pos.BuyUserId, pos.BrandId, TradeMonth, TradeAmt);
  132. }
  133. else
  134. {
  135. StatTradeAfter(db, pos.BuyUserId, pos.BrandId, TradeMonth, TradeAmt);
  136. }
  137. //推送延时队列
  138. // TimeSpan ts = DateTime.Now - StartMonth;
  139. // RabbitMQClientV2.Instance.SendMsg("{\"pos_sn\":\"" + pos.PosSn + "\",\"amt\":\"" + TradeAmt + "\"}", "KXS_WIFI_TRADE_QUEUE", (uint)ts.TotalMilliseconds);
  140. // }
  141. // }
  142. //检查交易状态,并发奖励
  143. if(pos.ActivationState == 1)
  144. {
  145. PxcModels.PosMerchantInfo mer = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId) ?? new PxcModels.PosMerchantInfo();
  146. SycnSpMerchantService.Instance.WifiSendPrize(db, pos, mer);
  147. }
  148. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  149. if (edit != null)
  150. {
  151. edit.Status = 2;
  152. spdb.SaveChanges();
  153. }
  154. }
  155. if(trade.SerEntryMode == "1")
  156. {
  157. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == trade.TradeSnNo);
  158. if (pos != null)
  159. {
  160. pos.SeoKeyword = trade.TradeAmount.ToString();
  161. pos.PrizeParams = trade.TradeAmount.ToString();
  162. db.SaveChanges();
  163. }
  164. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  165. if (edit != null)
  166. {
  167. edit.Status = 2;
  168. spdb.SaveChanges();
  169. }
  170. }
  171. else
  172. {
  173. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  174. if (edit != null)
  175. {
  176. edit.Status = 2;
  177. spdb.SaveChanges();
  178. }
  179. }
  180. }
  181. spdb.Dispose();
  182. db.Dispose();
  183. }
  184. catch (Exception ex)
  185. {
  186. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP交易数据到MAIN异常");
  187. }
  188. Thread.Sleep(500);
  189. }
  190. }
  191. public void StatTrade(PxcModels.WebCMSEntities db, int UserId, int BrandId, string TradeMonth, decimal TradeAmount)
  192. {
  193. PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new PxcModels.Users();
  194. string ParentNav = user.ParentNav;
  195. string TradeDate = TradeMonth + "01";
  196. ParentNav += "," + UserId + ",";
  197. PxcModels.TradeDaySummary selfStat = db.TradeDaySummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "self");
  198. if (selfStat == null)
  199. {
  200. selfStat = db.TradeDaySummary.Add(new PxcModels.TradeDaySummary()
  201. {
  202. UserId = UserId,
  203. TradeMonth = TradeMonth,
  204. TradeDate = TradeDate,
  205. BrandId = BrandId,
  206. SeoTitle = "self",
  207. }).Entity;
  208. db.SaveChanges();
  209. }
  210. selfStat.HelpDirectTradeAmt += TradeAmount;
  211. if (!string.IsNullOrEmpty(ParentNav))
  212. {
  213. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  214. foreach (string NavUserIdString in ParentNavList)
  215. {
  216. int NavUserId = int.Parse(NavUserIdString);
  217. PxcModels.TradeDaySummary teamStat = db.TradeDaySummary.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "team");
  218. if (teamStat == null)
  219. {
  220. teamStat = db.TradeDaySummary.Add(new PxcModels.TradeDaySummary()
  221. {
  222. UserId = NavUserId,
  223. TradeMonth = TradeMonth,
  224. TradeDate = TradeDate,
  225. BrandId = BrandId,
  226. SeoTitle = "team",
  227. }).Entity;
  228. db.SaveChanges();
  229. }
  230. teamStat.HelpNonDirectTradeAmt += TradeAmount;
  231. db.SaveChanges();
  232. }
  233. }
  234. }
  235. public void StatTradeBefore(PxcModels.WebCMSEntities db, int UserId, int BrandId, string TradeMonth, decimal TradeAmount)
  236. {
  237. PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new PxcModels.Users();
  238. string ParentNav = user.ParentNav;
  239. string TradeDate = TradeMonth + "01";
  240. ParentNav += "," + UserId + ",";
  241. PxcModels.TradeDaySummaryBefore selfStat = db.TradeDaySummaryBefore.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "self");
  242. if (selfStat == null)
  243. {
  244. selfStat = db.TradeDaySummaryBefore.Add(new PxcModels.TradeDaySummaryBefore()
  245. {
  246. UserId = UserId,
  247. TradeMonth = TradeMonth,
  248. TradeDate = TradeDate,
  249. BrandId = BrandId,
  250. SeoTitle = "self",
  251. }).Entity;
  252. db.SaveChanges();
  253. }
  254. selfStat.HelpDirectTradeAmt += TradeAmount;
  255. if (!string.IsNullOrEmpty(ParentNav))
  256. {
  257. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  258. foreach (string NavUserIdString in ParentNavList)
  259. {
  260. int NavUserId = int.Parse(NavUserIdString);
  261. PxcModels.TradeDaySummaryBefore teamStat = db.TradeDaySummaryBefore.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "team");
  262. if (teamStat == null)
  263. {
  264. teamStat = db.TradeDaySummaryBefore.Add(new PxcModels.TradeDaySummaryBefore()
  265. {
  266. UserId = NavUserId,
  267. TradeMonth = TradeMonth,
  268. TradeDate = TradeDate,
  269. BrandId = BrandId,
  270. SeoTitle = "team",
  271. }).Entity;
  272. db.SaveChanges();
  273. }
  274. teamStat.HelpNonDirectTradeAmt += TradeAmount;
  275. db.SaveChanges();
  276. }
  277. }
  278. }
  279. public void StatTradeAfter(PxcModels.WebCMSEntities db, int UserId, int BrandId, string TradeMonth, decimal TradeAmount)
  280. {
  281. PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new PxcModels.Users();
  282. string ParentNav = user.ParentNav;
  283. string TradeDate = TradeMonth + "01";
  284. ParentNav += "," + UserId + ",";
  285. PxcModels.TradeDaySummaryAfter selfStat = db.TradeDaySummaryAfter.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "self");
  286. if (selfStat == null)
  287. {
  288. selfStat = db.TradeDaySummaryAfter.Add(new PxcModels.TradeDaySummaryAfter()
  289. {
  290. UserId = UserId,
  291. TradeMonth = TradeMonth,
  292. TradeDate = TradeDate,
  293. BrandId = BrandId,
  294. SeoTitle = "self",
  295. }).Entity;
  296. db.SaveChanges();
  297. }
  298. selfStat.HelpDirectTradeAmt += TradeAmount;
  299. if (!string.IsNullOrEmpty(ParentNav))
  300. {
  301. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  302. foreach (string NavUserIdString in ParentNavList)
  303. {
  304. int NavUserId = int.Parse(NavUserIdString);
  305. PxcModels.TradeDaySummaryAfter teamStat = db.TradeDaySummaryAfter.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "team");
  306. if (teamStat == null)
  307. {
  308. teamStat = db.TradeDaySummaryAfter.Add(new PxcModels.TradeDaySummaryAfter()
  309. {
  310. UserId = NavUserId,
  311. TradeMonth = TradeMonth,
  312. TradeDate = TradeDate,
  313. BrandId = BrandId,
  314. SeoTitle = "team",
  315. }).Entity;
  316. db.SaveChanges();
  317. }
  318. teamStat.HelpNonDirectTradeAmt += TradeAmount;
  319. db.SaveChanges();
  320. }
  321. }
  322. }
  323. //推送wifi每月交易到java
  324. public void PushMsgToJava(string MsgContent)
  325. {
  326. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  327. try
  328. {
  329. JsonData jsonObj = JsonMapper.ToObject(MsgContent);
  330. string PosSn = jsonObj["pos_sn"].ToString();
  331. decimal TradeAmount = decimal.Parse(jsonObj["amt"].ToString());
  332. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == PosSn) ?? new PxcModels.PosMachinesTwo();
  333. if(pos.Status > -1 && pos.BuyUserId > 0 && pos.ActivationState == 1)
  334. {
  335. PxcModels.PosMerchantInfo mer = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId) ?? new PxcModels.PosMerchantInfo();
  336. PosPushDataNewHelper.Trade(new TradeRecord()
  337. {
  338. TradeSnNo = pos.PosSn,
  339. MerNo = mer.KqMerNo,
  340. TradeSerialNo = Guid.NewGuid().ToString(),
  341. TradeAmount = TradeAmount,
  342. CreateDate = DateTime.Now,
  343. ProductType = pos.BrandId.ToString(),
  344. });
  345. }
  346. }
  347. catch(Exception ex)
  348. {
  349. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "推送wifi每月交易到java异常");
  350. }
  351. db.Dispose();
  352. }
  353. }
  354. }