SycnSpTradeWifiService.cs 15 KB

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