SycnSpTradeWifiService.cs 14 KB

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