SycnSpTradeWifiService.cs 14 KB

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