SycnSpTradeWifiService.cs 14 KB

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