WifiTradeHelper.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Linq;
  5. using System.Data;
  6. using MySystem;
  7. using MySystem.PxcModels;
  8. using Library;
  9. using LitJson;
  10. public class WifiTradeHelper
  11. {
  12. public readonly static WifiTradeHelper Instance = new WifiTradeHelper();
  13. private WifiTradeHelper()
  14. {
  15. }
  16. public void Start()
  17. {
  18. Thread th = new Thread(DoWorks);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. private void DoWorks()
  23. {
  24. while (true)
  25. {
  26. try
  27. {
  28. if(DateTime.Now.Day < 25 && DateTime.Now.Hour < 20)
  29. {
  30. string Month = DateTime.Now.ToString("yyyy-MM");
  31. DoSomething(Month);
  32. }
  33. Thread.Sleep(3600000);
  34. }
  35. catch (Exception ex)
  36. {
  37. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "WIFI发放交易额异常");
  38. }
  39. }
  40. }
  41. public void DoSomething(string Month)
  42. {
  43. string chk = function.ReadInstance("/wifi/trade" + Month + ".txt");
  44. if(!string.IsNullOrEmpty(chk))
  45. {
  46. return;
  47. }
  48. function.WritePage("/wifi/", "trade" + Month + ".txt", DateTime.Now.ToString());
  49. string MonthString = Month.Replace("-", "");
  50. WebCMSEntities db = new WebCMSEntities();
  51. MySystem.ReadModels.WebCMSEntities rdb = new MySystem.ReadModels.WebCMSEntities();
  52. DateTime end = DateTime.Parse(Month + "-01 00:00:00");
  53. List<string> SnNos = new List<string>();
  54. List<MySystem.ReadModels.WifiTradeRecord> trades = rdb.WifiTradeRecord.Where(m => m.Duration >= m.DoMonths && m.CreateDate < end && m.LastMonth != MonthString).ToList();
  55. List<MySystem.ReadModels.Users> users = rdb.Users.Where(m => m.AuthFlag == 1).ToList();
  56. List<string> PosSns = trades.Select(m => m.SnNo).ToList();
  57. List<PosMachinesTwo> posList = db.PosMachinesTwo.Where(m => PosSns.Contains(m.PosSn)).ToList();
  58. List<PosMerchantInfo> merList = db.PosMerchantInfo.Where(m => PosSns.Contains(m.KqMerNo)).ToList();
  59. List<TradeDaySummary> statTrade = new List<TradeDaySummary>();
  60. List<TradeDaySummaryBefore> statTradeBefore = new List<TradeDaySummaryBefore>();
  61. List<TradeDaySummaryAfter> statTradeAfter = new List<TradeDaySummaryAfter>();
  62. function.WriteLog(trades.Count.ToString(), "WIFI发放交易额日志");
  63. foreach(MySystem.ReadModels.WifiTradeRecord trade in trades)
  64. {
  65. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(trade), "WIFI发放交易额日志");
  66. if(trade.Unit == "m" && trade.Duration > 0 && !SnNos.Contains(trade.SnNo))
  67. {
  68. decimal TradeAmt = trade.TradeAmount / trade.Duration;
  69. TradeAmt = PublicFunction.NumberFormat(TradeAmt);
  70. // PosMachinesTwo pos = posList.FirstOrDefault(m => m.PosSn == trade.SnNo) ?? new PosMachinesTwo();
  71. // PosMerchantInfo mer = merList.FirstOrDefault(m => m.Id == pos.BindMerchantId) ?? new PosMerchantInfo();
  72. // StatTrade(statTrade, users, pos.BuyUserId, pos.BrandId, Month.Replace("-", ""), TradeAmt);
  73. // if (pos.BindingTime < trade.CreateDate.Value.AddMonths(-9))
  74. // {
  75. // StatTradeBefore(statTradeBefore, users, pos.BuyUserId, pos.BrandId, Month.Replace("-", ""), TradeAmt);
  76. // }
  77. // else
  78. // {
  79. // StatTradeAfter(statTradeAfter, users, pos.BuyUserId, pos.BrandId, Month.Replace("-", ""), TradeAmt);
  80. // }
  81. WifiTradeRecord edit = db.WifiTradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  82. if(edit != null)
  83. {
  84. edit.DoMonths += 1;
  85. edit.LastMonth = Month.Replace("-", "");
  86. db.SaveChanges();
  87. //推送wifi交易给java
  88. PosPushDataNewHelper.Trade(new MySystem.SpModels.TradeRecord()
  89. {
  90. TradeSnNo = edit.SnNo,
  91. MerNo = edit.SnNo,
  92. TradeAmount = TradeAmt,
  93. TradeSerialNo = Guid.NewGuid().ToString(),
  94. CreateDate = DateTime.Parse(DateTime.Now.AddMonths(-1).ToString("yyyy-MM") + "-01 01:00:00"),
  95. ProductType = edit.BrandId.ToString(),
  96. });
  97. }
  98. SnNos.Add(trade.SnNo);
  99. }
  100. }
  101. int index = 0;
  102. foreach(TradeDaySummary sub in statTrade)
  103. {
  104. index += 1;
  105. db.TradeDaySummary.Add(sub);
  106. if(index % 200 == 0)
  107. {
  108. db.SaveChanges();
  109. }
  110. }
  111. db.SaveChanges();
  112. index = 0;
  113. foreach(TradeDaySummaryBefore sub in statTradeBefore)
  114. {
  115. index += 1;
  116. db.TradeDaySummaryBefore.Add(sub);
  117. if(index % 200 == 0)
  118. {
  119. db.SaveChanges();
  120. }
  121. }
  122. db.SaveChanges();
  123. index = 0;
  124. foreach(TradeDaySummaryAfter sub in statTradeAfter)
  125. {
  126. index += 1;
  127. db.TradeDaySummaryAfter.Add(sub);
  128. if(index % 200 == 0)
  129. {
  130. db.SaveChanges();
  131. }
  132. }
  133. db.SaveChanges();
  134. db.Dispose();
  135. function.WritePage("/wifi/", "trade" + Month + "-end.txt", DateTime.Now.ToString());
  136. }
  137. // public void DoSomethingNew(string Month)
  138. // {
  139. // WebCMSEntities db = new WebCMSEntities();
  140. // MySystem.ReadModels.WebCMSEntities rdb = new MySystem.ReadModels.WebCMSEntities();
  141. // DateTime end = DateTime.Parse(Month + "-01 00:00:00").AddMonths(1);
  142. // List<string> SnNos = new List<string>();
  143. // List<MySystem.ReadModels.WifiTradeRecord> trades = rdb.WifiTradeRecord.Where(m => m.Duration >= m.DoMonths && m.CreateDate < end).ToList();
  144. // foreach(MySystem.ReadModels.WifiTradeRecord trade in trades)
  145. // {
  146. // if(trade.Unit == "m" && trade.Duration > 0 && !SnNos.Contains(trade.SnNo))
  147. // {
  148. // DoOne(db, trade.Id, Month, "");
  149. // SnNos.Add(trade.SnNo);
  150. // }
  151. // }
  152. // db.SaveChanges();
  153. // db.Dispose();
  154. // }
  155. public void DoOne(WebCMSEntities db, int Id, string Month, string OrderNo, bool time = false)
  156. {
  157. WifiTradeRecord edit = db.WifiTradeRecord.FirstOrDefault(m => m.Id == Id);
  158. if(edit != null)
  159. {
  160. int orderCount = 0;
  161. if(!string.IsNullOrEmpty(edit.MerNo))
  162. {
  163. orderCount = db.WifiTradeRecord.Count(m => m.SnNo == edit.SnNo && m.MerNo == edit.MerNo);
  164. }
  165. if(edit.LastMonth != Month.Replace("-", ""))
  166. {
  167. // decimal TradeAmt = edit.TradeAmount / edit.Duration;
  168. // TradeAmt = PublicFunction.NumberFormat(TradeAmt);
  169. // if(orderCount > 1) TradeAmt = 0.01M;
  170. decimal TradeAmt = 0.01M;
  171. edit.DoMonths += 1;
  172. edit.LastMonth = Month.Replace("-", "");
  173. //推送wifi交易给java
  174. PosPushDataNewHelper.Trade(new MySystem.SpModels.TradeRecord()
  175. {
  176. TradeSnNo = edit.SnNo,
  177. MerNo = edit.SnNo,
  178. TradeAmount = TradeAmt,
  179. TradeSerialNo = Guid.NewGuid().ToString(),
  180. CreateDate = time ? DateTime.Now : DateTime.Parse(Month + "-01 01:00:00"),
  181. ProductType = edit.BrandId.ToString(),
  182. ErrorMsg = edit.DoMonths == 1 ? edit.TradeAmount.ToString() : "0",
  183. ChannelSerial = OrderNo,
  184. });
  185. }
  186. }
  187. }
  188. public void StatTrade(List<TradeDaySummary> statTrade, List<MySystem.ReadModels.Users> users, int UserId, int BrandId, string TradeMonth, decimal TradeAmount)
  189. {
  190. MySystem.ReadModels.Users user = users.FirstOrDefault(m => m.Id == UserId) ?? new MySystem.ReadModels.Users();
  191. string ParentNav = user.ParentNav;
  192. string TradeDate = TradeMonth + "01";
  193. ParentNav += "," + UserId + ",";
  194. TradeDaySummary selfStat = statTrade.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "self");
  195. if (selfStat == null)
  196. {
  197. statTrade.Add(new TradeDaySummary()
  198. {
  199. UserId = UserId,
  200. TradeMonth = TradeMonth,
  201. TradeDate = TradeDate,
  202. BrandId = BrandId,
  203. SeoTitle = "self",
  204. HelpDirectTradeAmt = TradeAmount,
  205. });
  206. }
  207. else
  208. {
  209. selfStat.HelpDirectTradeAmt += TradeAmount;
  210. }
  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. TradeDaySummary teamStat = statTrade.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "team");
  218. if (teamStat == null)
  219. {
  220. statTrade.Add(new TradeDaySummary()
  221. {
  222. UserId = NavUserId,
  223. TradeMonth = TradeMonth,
  224. TradeDate = TradeDate,
  225. BrandId = BrandId,
  226. SeoTitle = "team",
  227. HelpNonDirectTradeAmt = TradeAmount,
  228. });
  229. }
  230. else
  231. {
  232. teamStat.HelpNonDirectTradeAmt += TradeAmount;
  233. }
  234. }
  235. }
  236. }
  237. public void StatTradeBefore(List<TradeDaySummaryBefore> statTrade, List<MySystem.ReadModels.Users> users, int UserId, int BrandId, string TradeMonth, decimal TradeAmount)
  238. {
  239. MySystem.ReadModels.Users user = users.FirstOrDefault(m => m.Id == UserId) ?? new MySystem.ReadModels.Users();
  240. string ParentNav = user.ParentNav;
  241. string TradeDate = TradeMonth + "01";
  242. ParentNav += "," + UserId + ",";
  243. TradeDaySummaryBefore selfStat = statTrade.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "self");
  244. if (selfStat == null)
  245. {
  246. statTrade.Add(new TradeDaySummaryBefore()
  247. {
  248. UserId = UserId,
  249. TradeMonth = TradeMonth,
  250. TradeDate = TradeDate,
  251. BrandId = BrandId,
  252. SeoTitle = "self",
  253. HelpDirectTradeAmt = TradeAmount,
  254. });
  255. }
  256. else
  257. {
  258. selfStat.HelpDirectTradeAmt += TradeAmount;
  259. }
  260. if (!string.IsNullOrEmpty(ParentNav))
  261. {
  262. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  263. foreach (string NavUserIdString in ParentNavList)
  264. {
  265. int NavUserId = int.Parse(NavUserIdString);
  266. TradeDaySummaryBefore teamStat = statTrade.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "team");
  267. if (teamStat == null)
  268. {
  269. statTrade.Add(new TradeDaySummaryBefore()
  270. {
  271. UserId = NavUserId,
  272. TradeMonth = TradeMonth,
  273. TradeDate = TradeDate,
  274. BrandId = BrandId,
  275. SeoTitle = "team",
  276. HelpNonDirectTradeAmt = TradeAmount,
  277. });
  278. }
  279. else
  280. {
  281. teamStat.HelpNonDirectTradeAmt += TradeAmount;
  282. }
  283. }
  284. }
  285. }
  286. public void StatTradeAfter(List<TradeDaySummaryAfter> statTrade, List<MySystem.ReadModels.Users> users, int UserId, int BrandId, string TradeMonth, decimal TradeAmount)
  287. {
  288. MySystem.ReadModels.Users user = users.FirstOrDefault(m => m.Id == UserId) ?? new MySystem.ReadModels.Users();
  289. string ParentNav = user.ParentNav;
  290. string TradeDate = TradeMonth + "01";
  291. ParentNav += "," + UserId + ",";
  292. TradeDaySummaryAfter selfStat = statTrade.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "self");
  293. if (selfStat == null)
  294. {
  295. statTrade.Add(new TradeDaySummaryAfter()
  296. {
  297. UserId = UserId,
  298. TradeMonth = TradeMonth,
  299. TradeDate = TradeDate,
  300. BrandId = BrandId,
  301. SeoTitle = "self",
  302. HelpDirectTradeAmt = TradeAmount,
  303. });
  304. }
  305. else
  306. {
  307. selfStat.HelpDirectTradeAmt += TradeAmount;
  308. }
  309. if (!string.IsNullOrEmpty(ParentNav))
  310. {
  311. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  312. foreach (string NavUserIdString in ParentNavList)
  313. {
  314. int NavUserId = int.Parse(NavUserIdString);
  315. TradeDaySummaryAfter teamStat = statTrade.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "team");
  316. if (teamStat == null)
  317. {
  318. statTrade.Add(new TradeDaySummaryAfter()
  319. {
  320. UserId = NavUserId,
  321. TradeMonth = TradeMonth,
  322. TradeDate = TradeDate,
  323. BrandId = BrandId,
  324. SeoTitle = "team",
  325. HelpNonDirectTradeAmt = TradeAmount,
  326. });
  327. }
  328. else
  329. {
  330. teamStat.HelpNonDirectTradeAmt += TradeAmount;
  331. }
  332. }
  333. }
  334. }
  335. }