SycnSpTradeWifiService.cs 15 KB

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