SycnSpTradeService.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. }
  134. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  135. if (edit != null)
  136. {
  137. edit.Status = 2;
  138. }
  139. spdb.SaveChanges();
  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. PosPushDataNewHelper.Trade(trade);
  171. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  172. if (edit != null)
  173. {
  174. edit.Status = 2;
  175. }
  176. spdb.SaveChanges();
  177. }
  178. spdb.Dispose();
  179. db.Dispose();
  180. }
  181. catch (Exception ex)
  182. {
  183. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP交易数据到MAIN异常");
  184. }
  185. Thread.Sleep(500);
  186. }
  187. }
  188. public void StartTradeStatus()
  189. {
  190. Thread th = new Thread(StartTradeStatusDo);
  191. th.IsBackground = true;
  192. th.Start();
  193. }
  194. public void StartTradeStatusDo()
  195. {
  196. while (true)
  197. {
  198. try
  199. {
  200. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  201. WebCMSEntities spdb = new WebCMSEntities();
  202. DateTime start = DateTime.Now.AddDays(-10);
  203. DateTime end = DateTime.Now.AddMinutes(-10);
  204. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/TradeRecordId.txt")));
  205. 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);
  206. foreach (TradeRecord trade in trades.ToList())
  207. {
  208. int Status = 1;
  209. bool op = db.PosMachinesTwo.Any(m => m.PosSn == trade.TradeSnNo);
  210. if(!op)
  211. {
  212. Status = -1;
  213. }
  214. else
  215. {
  216. op = db.PosMerchantInfo.Any(m => m.KqSnNo == trade.TradeSnNo);
  217. if(!op)
  218. {
  219. Status = -2;
  220. }
  221. }
  222. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  223. if (edit != null)
  224. {
  225. edit.Status = Status;
  226. spdb.SaveChanges();
  227. }
  228. }
  229. db.Dispose();
  230. spdb.Dispose();
  231. }
  232. catch (Exception ex)
  233. {
  234. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "恢复原始交易没有统计上的数据异常");
  235. }
  236. Thread.Sleep(10000);
  237. }
  238. }
  239. //机具激活逻辑
  240. public void ActPos(PxcModels.WebCMSEntities db, PxcModels.PosMachinesTwo pos, decimal CheckMoney, int CheckDays, bool PrizeFlag = true, string ActivationTime = "")
  241. {
  242. DateTime now = DateTime.Now;
  243. if(!string.IsNullOrEmpty(ActivationTime))
  244. {
  245. now = DateTime.Parse(ActivationTime);
  246. }
  247. DateTime TransferTime = pos.TransferTime == null ? DateTime.Parse("2050-01-01") : pos.TransferTime.Value;
  248. int minute = 30;
  249. if(pos.BrandId == 14 && pos.PosSn.StartsWith("CS"))
  250. {
  251. minute = 60 * 24 * 30;
  252. }
  253. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:贷记卡是否刷满" + CheckMoney + ";贷记卡交易:" + pos.CreditTrade + "");
  254. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:是否有机具所属人;机具所属人Id:" + pos.BuyUserId + "");
  255. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:是否未激活;机具激活状态:" + pos.ActivationState + "");
  256. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:划拨时间不能超过绑定时间" + minute + "分钟;划拨时间:" + TransferTime.ToString() + ",绑定时间:" + pos.BindingTime.Value.ToString());
  257. if (pos.CreditTrade >= CheckMoney && pos.BuyUserId > 0 && pos.ActivationState == 0 && TransferTime.AddMinutes(-minute) < pos.BindingTime)
  258. {
  259. Utils.Instance.PrizeRecord(1, pos.PosSn, "满足条件");
  260. pos.ActivationState = 1;
  261. pos.ActivationTime = now;
  262. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:是否营训");
  263. if(pos.CardType == 101)
  264. {
  265. Utils.Instance.PrizeRecord(1, pos.PosSn, "是营训");
  266. pos.IsPurchase = 1;
  267. Utils.Instance.PrizeRecord(1, pos.PosSn, "取消循环资格");
  268. }
  269. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:是否关联商户信息");
  270. PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  271. if (merchant != null)
  272. {
  273. merchant.ActiveStatus = 1;
  274. merchant.MerStandardDate = now;
  275. db.SaveChanges();
  276. if(pos.BrandId != 14)
  277. {
  278. //发放奖励MQ
  279. PrizeSetHelper.Do("1", pos.PosSn);
  280. }
  281. Utils.Instance.PrizeRecord(1, pos.PosSn, "已关联");
  282. //发放开机奖励
  283. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:绑定时间不超过" + CheckDays + "天;当前绑定时间:" + pos.BindingTime.ToString() + "");
  284. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:非营训;当前类型:" + pos.CardType);
  285. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:奖励开关;当前:" + PrizeFlag);
  286. if(pos.BindingTime > now.AddDays(-CheckDays) && pos.CardType < 100 && PrizeFlag)
  287. {
  288. Utils.Instance.PrizeRecord(1, pos.PosSn, "满足条件,发放开机奖励");
  289. RedisDbconn.Instance.AddList("OpenRewardQueue", pos.Id.ToString());
  290. if(pos.BrandId == 14)
  291. {
  292. Utils.Instance.PrizeRecord(1, pos.PosSn, "满足条件,发放运营中心奖励");
  293. RedisDbconn.Instance.AddList("OperatePrizeQueue", pos.Id);
  294. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:大盟主标记;当前大盟主Id:" + pos.LeaderUserId);
  295. PxcModels.Leaders leader = db.Leaders.FirstOrDefault(m => m.Id == pos.LeaderUserId) ?? new PxcModels.Leaders();
  296. DateTime ExpiredDate = leader.ExpiredDate == null ? DateTime.Parse("1900-01-01") : leader.ExpiredDate.Value;
  297. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:盟主是否过期;过期时间:" + pos.LeaderUserId);
  298. if(pos.LeaderUserId > 0 && ExpiredDate > now)
  299. {
  300. Utils.Instance.PrizeRecord(1, pos.PosSn, "满足条件,发放大盟主奖励");
  301. RedisDbconn.Instance.AddList("LeaderPrizeQueue", pos.Id);
  302. }
  303. }
  304. }
  305. }
  306. //发放大盟主奖励
  307. decimal Deposit = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  308. if(pos.BrandId != 14)
  309. {
  310. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:有押金;当前押金:" + Deposit);
  311. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:大盟主标记;当前大盟主Id:" + pos.LeaderUserId);
  312. PxcModels.Leaders leader = db.Leaders.FirstOrDefault(m => m.Id == pos.LeaderUserId) ?? new PxcModels.Leaders();
  313. DateTime ExpiredDate = leader.ExpiredDate == null ? DateTime.Parse("1900-01-01") : leader.ExpiredDate.Value;
  314. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:盟主是否过期;过期时间:" + pos.LeaderUserId);
  315. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:绑定时间不超过" + CheckDays + "天;绑定时间:" + pos.BindingTime.Value.ToString() + "");
  316. }
  317. 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)
  318. {
  319. Utils.Instance.PrizeRecord(1, pos.PosSn, "满足条件,发放大盟主奖励");
  320. RedisDbconn.Instance.AddList("LeaderPrizeQueue", pos.Id);
  321. }
  322. //发放运营中心奖励
  323. if (Deposit > 0 && pos.BindingTime > now.AddDays(-CheckDays) && pos.CardType < 100 && PrizeFlag)
  324. {
  325. Utils.Instance.PrizeRecord(1, pos.PosSn, "满足条件,发放运营中心奖励");
  326. RedisDbconn.Instance.AddList("OperatePrizeQueue", pos.Id);
  327. }
  328. if(pos.CardType < 100)
  329. {
  330. //统计激活数
  331. RedisDbconn.Instance.AddList("StatActQueue", "{\"TradeDate\":\"" + now.ToString("yyyyMMdd") + "\",\"UserId\":\"" + pos.BuyUserId + "\",\"BrandId\":\"" + pos.BrandId + "\"}");
  332. //运营中心额度变更
  333. OpAmountItem item = new OpAmountItem()
  334. {
  335. UserId = pos.OpId,
  336. OperateType = 1,
  337. ChangeType = 9,
  338. Remark = "机具激活",
  339. UseAmount = pos.OpReserve1 + pos.OpReserve2 + pos.OpReserve3,
  340. UseValidForGetAmount = pos.OpReserve2,
  341. UseTotalAmt = pos.OpReserve1,
  342. UseValidAmount = pos.OpReserve3,
  343. DataType = 2,
  344. DataId = pos.Id,
  345. };
  346. RedisDbconn.Instance.AddList("OperateAmountRecordServiceQueue", item);
  347. }
  348. //清理预扣款
  349. RedisDbconn.Instance.AddList("PosWithholdQueue", "{\"UserId\":\"" + pos.BuyUserId + "\",\"SnNo\":\"" + pos.PosSn + "\"}");
  350. //推送激活数据
  351. if(Deposit == 0) Utils.Instance.SendActData(pos);
  352. }
  353. else if (pos.ActivationState == 1 && pos.BrandId == 14)
  354. {
  355. if(pos.BindingTime > now.AddDays(-CheckDays) && pos.CardType < 100 && PrizeFlag)
  356. {
  357. RedisDbconn.Instance.AddList("OpenRewardQueue", pos.Id.ToString());
  358. if(pos.BrandId == 14)
  359. {
  360. RedisDbconn.Instance.AddList("OperatePrizeQueue", pos.Id);
  361. PxcModels.Leaders leader = db.Leaders.FirstOrDefault(m => m.Id == pos.LeaderUserId) ?? new PxcModels.Leaders();
  362. DateTime ExpiredDate = leader.ExpiredDate == null ? DateTime.Parse("1900-01-01") : leader.ExpiredDate.Value;
  363. if(pos.LeaderUserId > 0 && ExpiredDate > now)
  364. {
  365. RedisDbconn.Instance.AddList("LeaderPrizeQueue", pos.Id);
  366. }
  367. }
  368. }
  369. }
  370. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:已激活;当前状态:" + pos.ActivationState);
  371. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:绑定后台30天到60天之间;绑定时间:" + pos.BindingTime.Value.ToString() + "");
  372. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:贷记卡是否刷满10000;贷记卡交易:" + pos.CreditTrade + "");
  373. Utils.Instance.PrizeRecord(1, pos.PosSn, "条件:非营训;当前类型:" + pos.CardType);
  374. if(pos.ActivationState == 1 && pos.BindingTime.Value.AddDays(30) <= DateTime.Now && pos.BindingTime.Value.AddDays(60) > DateTime.Now && pos.CreditTrade >= 10000 && pos.CardType < 100)
  375. {
  376. Utils.Instance.PrizeRecord(1, pos.PosSn, "满足条件,发放盟主开机奖励");
  377. RedisDbconn.Instance.AddList("LeaderRewardQueue", pos.Id.ToString());
  378. //发放奖励MQ
  379. PrizeSetHelper.Do("1", pos.PosSn);
  380. }
  381. }
  382. //立刷格外奖励
  383. public void LiShuaActPrize(PxcModels.WebCMSEntities db, PxcModels.PosMachinesTwo pos, int prize)
  384. {
  385. PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  386. if(merchant != null)
  387. {
  388. PxcModels.Users getUser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new PxcModels.Users();
  389. int BuyTopUserId = 0;
  390. string ParentNav = getUser.ParentNav;
  391. if (!string.IsNullOrEmpty(ParentNav))
  392. {
  393. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  394. if (ParentNavList.Length > 1)
  395. {
  396. BuyTopUserId = int.Parse(ParentNavList[1]);
  397. }
  398. else if (ParentNavList.Length == 1)
  399. {
  400. BuyTopUserId = int.Parse(ParentNavList[0]);
  401. }
  402. }
  403. if(prize == 50)
  404. {
  405. StatService.Instance.doActiveReward(db, merchant, pos, getUser.Id, getUser.ParentNav, BuyTopUserId, prize);
  406. }
  407. if(prize == 40)
  408. {
  409. StatService.Instance.doActiveReward(db, merchant, pos, getUser.Id, getUser.ParentNav, BuyTopUserId, prize, 12);
  410. }
  411. if(prize == 30)
  412. {
  413. StatService.Instance.doActiveReward(db, merchant, pos, getUser.Id, getUser.ParentNav, BuyTopUserId, prize, 133);
  414. }
  415. }
  416. }
  417. private int GetLeShuaHaiKe(int UserId, int BrandId)
  418. {
  419. if(DateTime.Now >= DateTime.Parse("2023-08-01 00:00:00"))
  420. {
  421. return 0;
  422. }
  423. int ActStatus = 1;
  424. try
  425. {
  426. string Date = DateTime.Now.ToString("yyyyMMdd");
  427. int checkNum = 0;
  428. if(Date == "20230720") checkNum = 12;
  429. if(Date == "20230721") checkNum = 11;
  430. if(Date == "20230722") checkNum = 10;
  431. if(Date == "20230723") checkNum = 9;
  432. if(Date == "20230724") checkNum = 8;
  433. if(Date == "20230725") checkNum = 7;
  434. if(Date == "20230726") checkNum = 6;
  435. if(Date == "20230727") checkNum = 5;
  436. if(Date == "20230728") checkNum = 4;
  437. if(Date == "20230729") checkNum = 3;
  438. if(Date == "20230730") checkNum = 2;
  439. if(Date == "20230731") checkNum = 1;
  440. int check = int.Parse(function.CheckInt(RedisDbconn.Instance.Get<string>("lscut:" + UserId + ":" + BrandId + ":" + Date)));
  441. if(check <= checkNum)
  442. {
  443. check += 1;
  444. RedisDbconn.Instance.Set("lscut:" + UserId + ":" + BrandId + ":" + Date, check.ToString());
  445. }
  446. else
  447. {
  448. ActStatus = -1;
  449. RedisDbconn.Instance.Set("lscut:" + UserId + ":" + BrandId + ":" + Date, "0");
  450. }
  451. }
  452. catch(Exception ex)
  453. {
  454. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "乐刷海科计数异常");
  455. }
  456. return ActStatus;
  457. }
  458. }
  459. }