SycnSpTradeWifiService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. if (Utils.PushTimeCheck())
  28. {
  29. List<string> SaveDataBindIds = Utils.Instance.SaveDataBindIds();
  30. WebCMSEntities spdb = new WebCMSEntities();
  31. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  32. DateTime start = DateTime.Now.AddDays(-60);
  33. DateTime end = DateTime.Now.AddMinutes(-1);
  34. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/TradeRecordId.txt")));
  35. IQueryable<TradeRecord> trades = spdb.TradeRecord.Where(m => m.Id >= StartId && m.ProductType == "23" && m.CreateDate >= start && SaveDataBindIds.Contains(m.ProductType) && m.Status == 1).OrderBy(m => m.CreateDate).Take(20);
  36. foreach (TradeRecord trade in trades.ToList())
  37. {
  38. DoQueueTrade(db, trade);
  39. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  40. if (edit != null)
  41. {
  42. edit.Status = 2;
  43. spdb.SaveChanges();
  44. }
  45. }
  46. spdb.Dispose();
  47. db.Dispose();
  48. }
  49. }
  50. catch (Exception ex)
  51. {
  52. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP交易数据到MAIN异常");
  53. }
  54. Thread.Sleep(500);
  55. }
  56. }
  57. public void DoQueueTrade(PxcModels.WebCMSEntities db, TradeRecord trade)
  58. {
  59. bool check = db.SpOrderNos.Any(m => m.OrderNo == trade.TradeSerialNo);
  60. if (!check && trade.SerEntryMode != "1")
  61. {
  62. int BrandId = int.Parse(function.CheckInt(trade.ProductType));
  63. decimal TradeAmount = trade.TradeAmount;
  64. if (trade.SeoTitle != "v2") TradeAmount = TradeAmount / 100;
  65. PxcModels.WifiTradeRecord add = db.WifiTradeRecord.Add(new PxcModels.WifiTradeRecord()
  66. {
  67. CreateDate = trade.CreateDate,
  68. UpdateDate = trade.UpdateDate,
  69. SnNo = trade.TradeSnNo, //渠道SN号
  70. MerNo = trade.MerNo, //商户号
  71. TradeAmount = TradeAmount, //交易金额
  72. BrandId = BrandId, //品牌
  73. Remark = trade.MerNo, //备注
  74. OrderNo = trade.TradeSerialNo,
  75. }).Entity;
  76. db.SpOrderNos.Add(new PxcModels.SpOrderNos()
  77. {
  78. OrderNo = trade.TradeSerialNo
  79. });
  80. db.SaveChanges();
  81. //开始统计
  82. int Months = 0;
  83. if (trade.SeoTitle == "v2")
  84. {
  85. string duration = trade.Field2; //设备套餐时长
  86. string unit = trade.Field3; //套餐单位 0:按天 1:按月
  87. if (unit == "1") Months = int.Parse(function.CheckInt(duration));
  88. else Months = int.Parse(function.CheckInt(duration)) / 30;
  89. }
  90. else
  91. {
  92. if (BrandId == 23 && TradeAmount == 49) Months = 1;
  93. else if (BrandId == 23 && TradeAmount == 79) Months = 3;
  94. else if (BrandId == 23 && TradeAmount == 119) Months = 12;
  95. else if (BrandId == 23 && TradeAmount == 199) Months = 24;
  96. else Months = trade.QueryCount;
  97. // if(BrandId == 24 && TradeAmount == 59) Months = 1;
  98. // if(BrandId == 24 && TradeAmount == 159) Months = 3;
  99. // if(BrandId == 24 && TradeAmount == 399) Months = 12;
  100. // if(BrandId == 24 && TradeAmount == 699) Months = 24;
  101. // if(BrandId == 25 && TradeAmount == 69) Months = 1;
  102. // if(BrandId == 25 && TradeAmount == 199) Months = 3;
  103. // if(BrandId == 25 && TradeAmount == 499) Months = 12;
  104. // if(BrandId == 25 && TradeAmount == 799) Months = 24;
  105. // if(BrandId == 26 && TradeAmount == 219) Months = 1;
  106. // if(BrandId == 26 && TradeAmount == 449) Months = 3;
  107. // if(BrandId == 26 && TradeAmount == 1399) Months = 12;
  108. // if(BrandId == 26 && TradeAmount == 2698) Months = 24;
  109. }
  110. string TradeMonth = DateTime.Now.ToString("yyyyMM");
  111. add.Duration = Months;
  112. add.Unit = "m";
  113. WifiTradeHelper.Instance.DoOne(db, add.Id, DateTime.Now.ToString("yyyy-MM"), trade.TradeSerialNo, true);
  114. db.SaveChanges();
  115. }
  116. if (trade.SerEntryMode == "1")
  117. {
  118. PosPushDataNewHelper.Deposit(new ActivateRecord()
  119. {
  120. SeoTitle = trade.TradeAmount.ToString(),
  121. ProductType = trade.ProductType,
  122. SnNo = trade.TradeSnNo,
  123. MerNo = trade.TradeSnNo,
  124. Id = trade.Id,
  125. });
  126. }
  127. }
  128. public void StatTrade(PxcModels.WebCMSEntities db, int UserId, int BrandId, string TradeMonth, decimal TradeAmount)
  129. {
  130. PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new PxcModels.Users();
  131. string ParentNav = user.ParentNav;
  132. string TradeDate = TradeMonth + "01";
  133. ParentNav += "," + UserId + ",";
  134. PxcModels.TradeDaySummary selfStat = db.TradeDaySummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "self");
  135. if (selfStat == null)
  136. {
  137. selfStat = db.TradeDaySummary.Add(new PxcModels.TradeDaySummary()
  138. {
  139. UserId = UserId,
  140. TradeMonth = TradeMonth,
  141. TradeDate = TradeDate,
  142. BrandId = BrandId,
  143. SeoTitle = "self",
  144. }).Entity;
  145. db.SaveChanges();
  146. }
  147. selfStat.HelpDirectTradeAmt += TradeAmount;
  148. if (!string.IsNullOrEmpty(ParentNav))
  149. {
  150. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  151. foreach (string NavUserIdString in ParentNavList)
  152. {
  153. int NavUserId = int.Parse(NavUserIdString);
  154. PxcModels.TradeDaySummary teamStat = db.TradeDaySummary.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "team");
  155. if (teamStat == null)
  156. {
  157. teamStat = db.TradeDaySummary.Add(new PxcModels.TradeDaySummary()
  158. {
  159. UserId = NavUserId,
  160. TradeMonth = TradeMonth,
  161. TradeDate = TradeDate,
  162. BrandId = BrandId,
  163. SeoTitle = "team",
  164. }).Entity;
  165. db.SaveChanges();
  166. }
  167. teamStat.HelpNonDirectTradeAmt += TradeAmount;
  168. db.SaveChanges();
  169. }
  170. }
  171. }
  172. public void StatTradeBefore(PxcModels.WebCMSEntities db, int UserId, int BrandId, string TradeMonth, decimal TradeAmount)
  173. {
  174. PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new PxcModels.Users();
  175. string ParentNav = user.ParentNav;
  176. string TradeDate = TradeMonth + "01";
  177. ParentNav += "," + UserId + ",";
  178. PxcModels.TradeDaySummaryBefore selfStat = db.TradeDaySummaryBefore.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "self");
  179. if (selfStat == null)
  180. {
  181. selfStat = db.TradeDaySummaryBefore.Add(new PxcModels.TradeDaySummaryBefore()
  182. {
  183. UserId = UserId,
  184. TradeMonth = TradeMonth,
  185. TradeDate = TradeDate,
  186. BrandId = BrandId,
  187. SeoTitle = "self",
  188. }).Entity;
  189. db.SaveChanges();
  190. }
  191. selfStat.HelpDirectTradeAmt += TradeAmount;
  192. if (!string.IsNullOrEmpty(ParentNav))
  193. {
  194. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  195. foreach (string NavUserIdString in ParentNavList)
  196. {
  197. int NavUserId = int.Parse(NavUserIdString);
  198. PxcModels.TradeDaySummaryBefore teamStat = db.TradeDaySummaryBefore.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "team");
  199. if (teamStat == null)
  200. {
  201. teamStat = db.TradeDaySummaryBefore.Add(new PxcModels.TradeDaySummaryBefore()
  202. {
  203. UserId = NavUserId,
  204. TradeMonth = TradeMonth,
  205. TradeDate = TradeDate,
  206. BrandId = BrandId,
  207. SeoTitle = "team",
  208. }).Entity;
  209. db.SaveChanges();
  210. }
  211. teamStat.HelpNonDirectTradeAmt += TradeAmount;
  212. db.SaveChanges();
  213. }
  214. }
  215. }
  216. public void StatTradeAfter(PxcModels.WebCMSEntities db, int UserId, int BrandId, string TradeMonth, decimal TradeAmount)
  217. {
  218. PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new PxcModels.Users();
  219. string ParentNav = user.ParentNav;
  220. string TradeDate = TradeMonth + "01";
  221. ParentNav += "," + UserId + ",";
  222. PxcModels.TradeDaySummaryAfter selfStat = db.TradeDaySummaryAfter.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "self");
  223. if (selfStat == null)
  224. {
  225. selfStat = db.TradeDaySummaryAfter.Add(new PxcModels.TradeDaySummaryAfter()
  226. {
  227. UserId = UserId,
  228. TradeMonth = TradeMonth,
  229. TradeDate = TradeDate,
  230. BrandId = BrandId,
  231. SeoTitle = "self",
  232. }).Entity;
  233. db.SaveChanges();
  234. }
  235. selfStat.HelpDirectTradeAmt += TradeAmount;
  236. if (!string.IsNullOrEmpty(ParentNav))
  237. {
  238. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  239. foreach (string NavUserIdString in ParentNavList)
  240. {
  241. int NavUserId = int.Parse(NavUserIdString);
  242. PxcModels.TradeDaySummaryAfter teamStat = db.TradeDaySummaryAfter.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "team");
  243. if (teamStat == null)
  244. {
  245. teamStat = db.TradeDaySummaryAfter.Add(new PxcModels.TradeDaySummaryAfter()
  246. {
  247. UserId = NavUserId,
  248. TradeMonth = TradeMonth,
  249. TradeDate = TradeDate,
  250. BrandId = BrandId,
  251. SeoTitle = "team",
  252. }).Entity;
  253. db.SaveChanges();
  254. }
  255. teamStat.HelpNonDirectTradeAmt += TradeAmount;
  256. db.SaveChanges();
  257. }
  258. }
  259. }
  260. //推送wifi每月交易到java
  261. public void PushMsgToJava(string MsgContent)
  262. {
  263. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  264. try
  265. {
  266. JsonData jsonObj = JsonMapper.ToObject(MsgContent);
  267. string PosSn = jsonObj["pos_sn"].ToString();
  268. decimal TradeAmount = decimal.Parse(jsonObj["amt"].ToString());
  269. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == PosSn) ?? new PxcModels.PosMachinesTwo();
  270. if (pos.Status > -1 && pos.BuyUserId > 0 && pos.ActivationState == 1)
  271. {
  272. PxcModels.PosMerchantInfo mer = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId) ?? new PxcModels.PosMerchantInfo();
  273. PosPushDataNewHelper.Trade(new TradeRecord()
  274. {
  275. TradeSnNo = pos.PosSn,
  276. MerNo = mer.KqMerNo,
  277. TradeSerialNo = Guid.NewGuid().ToString(),
  278. TradeAmount = TradeAmount,
  279. CreateDate = DateTime.Now,
  280. ProductType = pos.BrandId.ToString(),
  281. });
  282. }
  283. }
  284. catch (Exception ex)
  285. {
  286. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "推送wifi每月交易到java异常");
  287. }
  288. db.Dispose();
  289. }
  290. }
  291. }