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