SycnSpTradeService.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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 SycnSpTradeService
  11. {
  12. public readonly static SycnSpTradeService Instance = new SycnSpTradeService();
  13. private SycnSpTradeService()
  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. List<string> BrandIds = new List<string>();
  28. BrandIds.Add("14");
  29. BrandIds.Add("17");
  30. BrandIds.Add("23");
  31. BrandIds.Add("24");
  32. BrandIds.Add("25");
  33. BrandIds.Add("26");
  34. WebCMSEntities spdb = new WebCMSEntities();
  35. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  36. DateTime start = DateTime.Now.AddDays(-10);
  37. DateTime end = DateTime.Now.AddMinutes(-3);
  38. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/TradeRecordId.txt")));
  39. IQueryable<TradeRecord> trades = spdb.TradeRecord.Where(m => m.Id >= StartId && !BrandIds.Contains(m.ProductType) && m.CreateDate >= start && m.Status == 1).OrderBy(m => m.CreateDate).Take(20);
  40. foreach (TradeRecord trade in trades.ToList())
  41. {
  42. string TradeSnNo = trade.TradeSnNo;
  43. bool op = true;
  44. bool isDeposit = false;
  45. //判断盒易付吱码交易
  46. if(trade.ProductType == "12" && trade.TradeType == "2")
  47. {
  48. PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == trade.MerNo);
  49. if(merchant != null)
  50. {
  51. TradeSnNo = merchant.KqSnNo;
  52. }
  53. }
  54. if (trade.SerEntryMode == "N" && trade.ProductType == "1") //金控押金获取
  55. {
  56. if (trade.TradeAmount == 9900 || trade.TradeAmount == 19900 || trade.TradeAmount == 29900)
  57. {
  58. op = false;
  59. PosPushDataNewHelper.Deposit(trade);
  60. }
  61. }
  62. else if (trade.SerEntryMode == "1" && trade.ProductType == "27") //金控押金获取
  63. {
  64. if (trade.TradeAmount == 9900 || trade.TradeAmount == 19900 || trade.TradeAmount == 29900)
  65. {
  66. op = false;
  67. PosPushDataNewHelper.Deposit(trade);
  68. }
  69. }
  70. else if ((trade.ProductType == "4" || trade.ProductType == "8" || trade.ProductType == "9") && (trade.Field2 == "5200" || trade.Field2 == "5800")) //乐刷海科费率0.52,0.58的不入库
  71. {
  72. op = false;
  73. }
  74. else if (trade.SerEntryMode == "1" && trade.ProductType == "10") //联动押金获取
  75. {
  76. if (trade.TradeAmount == 99 || trade.TradeAmount == 199 || trade.TradeAmount == 299)
  77. {
  78. op = false;
  79. PosPushDataNewHelper.Deposit(trade);
  80. }
  81. }
  82. else if (trade.SerEntryMode == "40" && trade.ProductType == "12") //盒易付押金获取
  83. {
  84. if (trade.TradeAmount == 99 || trade.TradeAmount == 199 || trade.TradeAmount == 299)
  85. {
  86. op = false;
  87. PosPushDataNewHelper.Deposit(trade);
  88. }
  89. }
  90. else if (trade.TradeType == "40" && trade.ProductType == "15") //联客宝押金获取
  91. {
  92. if (trade.TradeAmount == 99 || trade.TradeAmount == 199 || trade.TradeAmount == 299)
  93. {
  94. op = false;
  95. PosPushDataNewHelper.Deposit(trade);
  96. }
  97. }
  98. else if (trade.SerEntryMode == "DEPOSIT" && trade.ProductType == "30") //拉卡拉押金获取
  99. {
  100. if (trade.TradeAmount == 9900 || trade.TradeAmount == 19900 || trade.TradeAmount == 29900)
  101. {
  102. op = false;
  103. if(trade.ProductType == "30")
  104. {
  105. PxcModels.LklMerNo mer = db.LklMerNo.FirstOrDefault(m => m.ExtMerNo == trade.MerNo);
  106. if(mer != null)
  107. {
  108. trade.MerNo = mer.MerNo;
  109. }
  110. }
  111. PosPushDataNewHelper.Deposit(trade);
  112. }
  113. }
  114. if (trade.DiscountRateFlag == "True")
  115. {
  116. op = false;
  117. }
  118. if(trade.ProductType == "7" && trade.TradeStatus != "00")
  119. {
  120. op = false;
  121. }
  122. if (op)
  123. {
  124. bool check = db.SpOrderNos.Any(m => m.OrderNo == trade.TradeSerialNo);
  125. if (!check)
  126. {
  127. if(trade.ProductType == "30")
  128. {
  129. trade.MerNo = trade.Field2;
  130. }
  131. PosPushDataNewHelper.Trade(trade);
  132. }
  133. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  134. if (edit != null)
  135. {
  136. edit.Status = 2;
  137. }
  138. spdb.SaveChanges();
  139. }
  140. }
  141. spdb.Dispose();
  142. db.Dispose();
  143. }
  144. catch (Exception ex)
  145. {
  146. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP交易数据到MAIN异常");
  147. }
  148. Thread.Sleep(500);
  149. }
  150. }
  151. public void StartSim()
  152. {
  153. Thread th = new Thread(StartSimDo);
  154. th.IsBackground = true;
  155. th.Start();
  156. }
  157. public void StartSimDo()
  158. {
  159. while (true)
  160. {
  161. try
  162. {
  163. WebCMSEntities spdb = new WebCMSEntities();
  164. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  165. DateTime start = DateTime.Now.AddDays(-40);
  166. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/TradeRecordId.txt")));
  167. IQueryable<TradeRecord> trades = spdb.TradeRecord.Where(m => m.Id >= StartId && m.ProductType == "14" && m.CreateDate >= start && m.Status == 1).OrderBy(m => m.CreateDate).Take(20);
  168. foreach (TradeRecord trade in trades.ToList())
  169. {
  170. bool op = true;
  171. if (op)
  172. {
  173. PosPushDataNewHelper.Trade(trade);
  174. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  175. if (edit != null)
  176. {
  177. edit.Status = 2;
  178. }
  179. spdb.SaveChanges();
  180. }
  181. }
  182. spdb.Dispose();
  183. db.Dispose();
  184. }
  185. catch (Exception ex)
  186. {
  187. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP交易数据到MAIN异常");
  188. }
  189. Thread.Sleep(500);
  190. }
  191. }
  192. public void StartTradeStatus()
  193. {
  194. Thread th = new Thread(StartTradeStatusDo);
  195. th.IsBackground = true;
  196. th.Start();
  197. }
  198. public void StartTradeStatusDo()
  199. {
  200. while (true)
  201. {
  202. try
  203. {
  204. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  205. WebCMSEntities spdb = new WebCMSEntities();
  206. DateTime start = DateTime.Now.AddDays(-10);
  207. DateTime end = DateTime.Now.AddMinutes(-10);
  208. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/TradeRecordId.txt")));
  209. IQueryable<TradeRecord> trades = spdb.TradeRecord.Where(m => m.Id >= StartId && m.CreateDate >= start && m.CreateDate < end && m.Status == 0).OrderBy(m => m.CreateDate).Take(20);
  210. foreach (TradeRecord trade in trades.ToList())
  211. {
  212. int Status = 1;
  213. bool op = db.PosMachinesTwo.Any(m => m.PosSn == trade.TradeSnNo);
  214. if(!op)
  215. {
  216. Status = -1;
  217. }
  218. else
  219. {
  220. op = db.PosMerchantInfo.Any(m => m.KqSnNo == trade.TradeSnNo);
  221. if(!op)
  222. {
  223. Status = -2;
  224. }
  225. }
  226. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  227. if (edit != null)
  228. {
  229. edit.Status = Status;
  230. spdb.SaveChanges();
  231. }
  232. }
  233. db.Dispose();
  234. spdb.Dispose();
  235. }
  236. catch (Exception ex)
  237. {
  238. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "恢复原始交易没有统计上的数据异常");
  239. }
  240. Thread.Sleep(10000);
  241. }
  242. }
  243. //机具激活逻辑
  244. public void ActPos(PxcModels.WebCMSEntities db, PxcModels.PosMachinesTwo pos, decimal CheckMoney, int CheckDays, bool PrizeFlag = true, string ActivationTime = "")
  245. {
  246. DateTime now = DateTime.Now;
  247. if(!string.IsNullOrEmpty(ActivationTime))
  248. {
  249. now = DateTime.Parse(ActivationTime);
  250. }
  251. DateTime TransferTime = pos.TransferTime == null ? DateTime.Parse("2050-01-01") : pos.TransferTime.Value;
  252. int minute = 30;
  253. if(pos.BrandId == 14 && pos.PosSn.StartsWith("CS"))
  254. {
  255. minute = 60 * 24 * 30;
  256. }
  257. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:贷记卡是否刷满" + CheckMoney + ";贷记卡交易:" + pos.CreditTrade + "");
  258. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:是否有机具所属人;机具所属人Id:" + pos.BuyUserId + "");
  259. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:是否未激活;机具激活状态:" + pos.ActivationState + "");
  260. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:划拨时间不能超过绑定时间" + minute + "分钟;划拨时间:" + TransferTime.ToString() + ",绑定时间:" + pos.BindingTime.Value.ToString());
  261. if (pos.CreditTrade >= CheckMoney && pos.BuyUserId > 0 && pos.ActivationState == 0 && TransferTime.AddMinutes(-minute) < pos.BindingTime)
  262. {
  263. Utils.Instance.PrizeRecord(1, pos.PosSn, "满足条件");
  264. pos.ActivationState = 1;
  265. pos.ActivationTime = now;
  266. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:是否营训");
  267. if(pos.CardType == 101)
  268. {
  269. Utils.Instance.PrizeRecord(1, pos.PosSn, "是营训");
  270. pos.IsPurchase = 1;
  271. Utils.Instance.PrizeRecord(1, pos.PosSn, "取消循环资格");
  272. }
  273. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:是否关联商户信息");
  274. PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  275. if (merchant != null)
  276. {
  277. merchant.ActiveStatus = 1;
  278. merchant.MerStandardDate = now;
  279. db.SaveChanges();
  280. if(pos.BrandId != 14)
  281. {
  282. //发放奖励MQ
  283. PrizeSetHelper.Do("1", pos.PosSn);
  284. }
  285. Utils.Instance.PrizeRecord(1, pos.PosSn, "已关联");
  286. //发放开机奖励
  287. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:绑定时间不超过" + CheckDays + "天;当前绑定时间:" + pos.BindingTime.ToString() + "");
  288. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:非营训;当前类型:" + pos.CardType);
  289. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:奖励开关;当前:" + PrizeFlag);
  290. if(pos.BindingTime > now.AddDays(-CheckDays) && pos.CardType < 100 && PrizeFlag)
  291. {
  292. Utils.Instance.PrizeRecord(1, pos.PosSn, "满足条件,发放开机奖励");
  293. RedisDbconn.Instance.AddList("OpenRewardQueue", pos.Id.ToString());
  294. if(pos.BrandId == 14)
  295. {
  296. Utils.Instance.PrizeRecord(1, pos.PosSn, "满足条件,发放运营中心奖励");
  297. RedisDbconn.Instance.AddList("OperatePrizeQueue", pos.Id);
  298. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:大盟主标记;当前大盟主Id:" + pos.LeaderUserId);
  299. PxcModels.Leaders leader = db.Leaders.FirstOrDefault(m => m.Id == pos.LeaderUserId) ?? new PxcModels.Leaders();
  300. DateTime ExpiredDate = leader.ExpiredDate == null ? DateTime.Parse("1900-01-01") : leader.ExpiredDate.Value;
  301. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:盟主是否过期;过期时间:" + pos.LeaderUserId);
  302. if(pos.LeaderUserId > 0 && ExpiredDate > now)
  303. {
  304. Utils.Instance.PrizeRecord(1, pos.PosSn, "满足条件,发放大盟主奖励");
  305. RedisDbconn.Instance.AddList("LeaderPrizeQueue", pos.Id);
  306. }
  307. }
  308. }
  309. }
  310. //发放大盟主奖励
  311. decimal Deposit = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  312. if(pos.BrandId != 14)
  313. {
  314. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:有押金;当前押金:" + Deposit);
  315. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:大盟主标记;当前大盟主Id:" + pos.LeaderUserId);
  316. PxcModels.Leaders leader = db.Leaders.FirstOrDefault(m => m.Id == pos.LeaderUserId) ?? new PxcModels.Leaders();
  317. DateTime ExpiredDate = leader.ExpiredDate == null ? DateTime.Parse("1900-01-01") : leader.ExpiredDate.Value;
  318. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:盟主是否过期;过期时间:" + pos.LeaderUserId);
  319. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:绑定时间不超过" + CheckDays + "天;绑定时间:" + pos.BindingTime.Value.ToString() + "");
  320. }
  321. if (Deposit > 0 && pos.LeaderUserId > 0 && db.Leaders.Any(m => m.Id == pos.LeaderUserId && m.ExpiredDate > now) && pos.BindingTime > now.AddDays(-CheckDays) && pos.CardType < 100 && PrizeFlag)
  322. {
  323. Utils.Instance.PrizeRecord(1, pos.PosSn, "满足条件,发放大盟主奖励");
  324. RedisDbconn.Instance.AddList("LeaderPrizeQueue", pos.Id);
  325. }
  326. //发放运营中心奖励
  327. if (Deposit > 0 && pos.BindingTime > now.AddDays(-CheckDays) && pos.CardType < 100 && PrizeFlag)
  328. {
  329. Utils.Instance.PrizeRecord(1, pos.PosSn, "满足条件,发放运营中心奖励");
  330. RedisDbconn.Instance.AddList("OperatePrizeQueue", pos.Id);
  331. }
  332. if(pos.CardType < 100)
  333. {
  334. //统计激活数
  335. RedisDbconn.Instance.AddList("StatActQueue", "{\"TradeDate\":\"" + now.ToString("yyyyMMdd") + "\",\"UserId\":\"" + pos.BuyUserId + "\",\"BrandId\":\"" + pos.BrandId + "\"}");
  336. //运营中心额度变更
  337. OpAmountItem item = new OpAmountItem()
  338. {
  339. UserId = pos.OpId,
  340. OperateType = 1,
  341. ChangeType = 9,
  342. Remark = "机具激活",
  343. UseAmount = pos.OpReserve1 + pos.OpReserve2 + pos.OpReserve3,
  344. UseValidForGetAmount = pos.OpReserve2,
  345. UseTotalAmt = pos.OpReserve1,
  346. UseValidAmount = pos.OpReserve3,
  347. DataType = 2,
  348. DataId = pos.Id,
  349. };
  350. RedisDbconn.Instance.AddList("OperateAmountRecordServiceQueue", item);
  351. }
  352. //清理预扣款
  353. RedisDbconn.Instance.AddList("PosWithholdQueue", "{\"UserId\":\"" + pos.BuyUserId + "\",\"SnNo\":\"" + pos.PosSn + "\"}");
  354. //推送激活数据
  355. if(Deposit == 0) Utils.Instance.SendActData(pos);
  356. }
  357. else if (pos.ActivationState == 1 && pos.BrandId == 14)
  358. {
  359. if(pos.BindingTime > now.AddDays(-CheckDays) && pos.CardType < 100 && PrizeFlag)
  360. {
  361. RedisDbconn.Instance.AddList("OpenRewardQueue", pos.Id.ToString());
  362. if(pos.BrandId == 14)
  363. {
  364. RedisDbconn.Instance.AddList("OperatePrizeQueue", pos.Id);
  365. PxcModels.Leaders leader = db.Leaders.FirstOrDefault(m => m.Id == pos.LeaderUserId) ?? new PxcModels.Leaders();
  366. DateTime ExpiredDate = leader.ExpiredDate == null ? DateTime.Parse("1900-01-01") : leader.ExpiredDate.Value;
  367. if(pos.LeaderUserId > 0 && ExpiredDate > now)
  368. {
  369. RedisDbconn.Instance.AddList("LeaderPrizeQueue", pos.Id);
  370. }
  371. }
  372. }
  373. }
  374. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:已激活;当前状态:" + pos.ActivationState);
  375. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:绑定后台30天到60天之间;绑定时间:" + pos.BindingTime.Value.ToString() + "");
  376. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:贷记卡是否刷满10000;贷记卡交易:" + pos.CreditTrade + "");
  377. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:非营训;当前类型:" + pos.CardType);
  378. if(pos.ActivationState == 1 && pos.BindingTime.Value.AddDays(30) <= DateTime.Now && pos.BindingTime.Value.AddDays(60) > DateTime.Now && pos.CreditTrade >= 10000 && pos.CardType < 100)
  379. {
  380. Utils.Instance.PrizeRecord(1, pos.PosSn, "满足条件,发放盟主开机奖励");
  381. RedisDbconn.Instance.AddList("LeaderRewardQueue", pos.Id.ToString());
  382. //发放奖励MQ
  383. PrizeSetHelper.Do("1", pos.PosSn);
  384. }
  385. }
  386. //立刷格外奖励
  387. public void LiShuaActPrize(PxcModels.WebCMSEntities db, PxcModels.PosMachinesTwo pos, int prize)
  388. {
  389. PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  390. if(merchant != null)
  391. {
  392. PxcModels.Users getUser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new PxcModels.Users();
  393. int BuyTopUserId = 0;
  394. string ParentNav = getUser.ParentNav;
  395. if (!string.IsNullOrEmpty(ParentNav))
  396. {
  397. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  398. if (ParentNavList.Length > 1)
  399. {
  400. BuyTopUserId = int.Parse(ParentNavList[1]);
  401. }
  402. else if (ParentNavList.Length == 1)
  403. {
  404. BuyTopUserId = int.Parse(ParentNavList[0]);
  405. }
  406. }
  407. if(prize == 50)
  408. {
  409. StatService.Instance.doActiveReward(db, merchant, pos, getUser.Id, getUser.ParentNav, BuyTopUserId, prize);
  410. }
  411. if(prize == 40)
  412. {
  413. StatService.Instance.doActiveReward(db, merchant, pos, getUser.Id, getUser.ParentNav, BuyTopUserId, prize, 12);
  414. }
  415. if(prize == 30)
  416. {
  417. StatService.Instance.doActiveReward(db, merchant, pos, getUser.Id, getUser.ParentNav, BuyTopUserId, prize, 133);
  418. }
  419. }
  420. }
  421. private int GetLeShuaHaiKe(int UserId, int BrandId)
  422. {
  423. if(DateTime.Now >= DateTime.Parse("2023-08-01 00:00:00"))
  424. {
  425. return 0;
  426. }
  427. int ActStatus = 1;
  428. try
  429. {
  430. string Date = DateTime.Now.ToString("yyyyMMdd");
  431. int checkNum = 0;
  432. if(Date == "20230720") checkNum = 12;
  433. if(Date == "20230721") checkNum = 11;
  434. if(Date == "20230722") checkNum = 10;
  435. if(Date == "20230723") checkNum = 9;
  436. if(Date == "20230724") checkNum = 8;
  437. if(Date == "20230725") checkNum = 7;
  438. if(Date == "20230726") checkNum = 6;
  439. if(Date == "20230727") checkNum = 5;
  440. if(Date == "20230728") checkNum = 4;
  441. if(Date == "20230729") checkNum = 3;
  442. if(Date == "20230730") checkNum = 2;
  443. if(Date == "20230731") checkNum = 1;
  444. int check = int.Parse(function.CheckInt(RedisDbconn.Instance.Get<string>("lscut:" + UserId + ":" + BrandId + ":" + Date)));
  445. if(check <= checkNum)
  446. {
  447. check += 1;
  448. RedisDbconn.Instance.Set("lscut:" + UserId + ":" + BrandId + ":" + Date, check.ToString());
  449. }
  450. else
  451. {
  452. ActStatus = -1;
  453. RedisDbconn.Instance.Set("lscut:" + UserId + ":" + BrandId + ":" + Date, "0");
  454. }
  455. }
  456. catch(Exception ex)
  457. {
  458. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "乐刷海科计数异常");
  459. }
  460. return ActStatus;
  461. }
  462. }
  463. }