SycnSpTradeWifiService.cs 15 KB

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