SycnSpTradeWifiService.cs 20 KB

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