SycnSpTradeService.cs 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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. WebCMSEntities spdb = new WebCMSEntities();
  28. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  29. DateTime start = DateTime.Now.AddDays(-10);
  30. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/TradeRecordId.txt")));
  31. IQueryable<TradeRecord> trades = spdb.TradeRecord.Where(m => m.Id >= StartId && m.CreateDate >= start && m.Status == 1).OrderBy(m => m.CreateDate).Take(20);
  32. foreach (TradeRecord trade in trades.ToList())
  33. {
  34. bool op = true;
  35. if (trade.SerEntryMode == "N" && trade.ProductType == "1") //金控押金获取
  36. {
  37. if (trade.TradeAmount == 9900 || trade.TradeAmount == 19900 || trade.TradeAmount == 29900)
  38. {
  39. op = false;
  40. PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == trade.TradeSnNo) ?? new PxcModels.MachineForSnNo();
  41. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
  42. if (pos != null)
  43. {
  44. pos.SeoKeyword = trade.TradeAmount.ToString("f2");
  45. // pos.IsPurchase = 0;
  46. db.SaveChanges();
  47. }
  48. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  49. if (edit != null)
  50. {
  51. edit.Status = 2;
  52. }
  53. spdb.SaveChanges();
  54. }
  55. }
  56. else if ((trade.ProductType == "4" || trade.ProductType == "8" || trade.ProductType == "9") && trade.Field2 == "5200" && trade.Field3 == "300") //乐刷海科费率0.52的不入库
  57. {
  58. op = false;
  59. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  60. if (edit != null)
  61. {
  62. edit.Status = 2;
  63. }
  64. spdb.SaveChanges();
  65. }
  66. else if (trade.SerEntryMode == "1" && trade.ProductType == "10") //联动押金获取
  67. {
  68. if (trade.TradeAmount == 99 || trade.TradeAmount == 199 || trade.TradeAmount == 299)
  69. {
  70. op = false;
  71. PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == trade.TradeSnNo) ?? new PxcModels.MachineForSnNo();
  72. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
  73. if (pos != null)
  74. {
  75. pos.SeoKeyword = trade.TradeAmount.ToString("f2");
  76. db.SaveChanges();
  77. }
  78. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  79. if (edit != null)
  80. {
  81. edit.Status = 2;
  82. }
  83. spdb.SaveChanges();
  84. }
  85. }
  86. else if (trade.SerEntryMode == "40" && trade.ProductType == "12") //盒易付押金获取
  87. {
  88. if (trade.TradeAmount == 99 || trade.TradeAmount == 199 || trade.TradeAmount == 299)
  89. {
  90. op = false;
  91. PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == trade.TradeSnNo) ?? new PxcModels.MachineForSnNo();
  92. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
  93. if (pos != null)
  94. {
  95. pos.SeoKeyword = trade.TradeAmount.ToString("f2");
  96. PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId) ?? new PxcModels.PosMerchantInfo();
  97. db.MerchantDepositRecord.Add(new PxcModels.MerchantDepositRecord()
  98. {
  99. CreateDate = DateTime.Now,
  100. SnNo = pos.PosSn,
  101. MerNo = function.CheckNull(merchant.KqMerNo),
  102. DepositAmount = trade.TradeAmount,
  103. UserId = pos.BuyUserId,
  104. AgentNo = function.CheckNull(merchant.MgrNo),
  105. OrderId = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8),
  106. });
  107. db.SaveChanges();
  108. }
  109. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  110. if (edit != null)
  111. {
  112. edit.Status = 2;
  113. }
  114. spdb.SaveChanges();
  115. }
  116. }
  117. else if (trade.TradeType == "40" && trade.ProductType == "15") //联客宝押金获取
  118. {
  119. if (trade.TradeAmount == 99 || trade.TradeAmount == 199 || trade.TradeAmount == 299)
  120. {
  121. op = false;
  122. PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == trade.TradeSnNo) ?? new PxcModels.MachineForSnNo();
  123. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
  124. if (pos != null)
  125. {
  126. pos.SeoKeyword = trade.TradeAmount.ToString("f2");
  127. PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId) ?? new PxcModels.PosMerchantInfo();
  128. db.MerchantDepositRecord.Add(new PxcModels.MerchantDepositRecord()
  129. {
  130. CreateDate = DateTime.Now,
  131. SnNo = pos.PosSn,
  132. MerNo = merchant.KqMerNo,
  133. DepositAmount = trade.TradeAmount,
  134. UserId = pos.BuyUserId,
  135. AgentNo = function.CheckNull(merchant.MgrNo),
  136. OrderId = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8),
  137. });
  138. db.SaveChanges();
  139. }
  140. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  141. if (edit != null)
  142. {
  143. edit.Status = 2;
  144. }
  145. spdb.SaveChanges();
  146. }
  147. }
  148. if (trade.DiscountRateFlag == "True")
  149. {
  150. op = false;
  151. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  152. if (edit != null)
  153. {
  154. edit.Status = 2;
  155. }
  156. spdb.SaveChanges();
  157. }
  158. if(trade.ProductType == "7" && trade.TradeStatus != "00")
  159. {
  160. op = false;
  161. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  162. if (edit != null)
  163. {
  164. edit.Status = 2;
  165. }
  166. spdb.SaveChanges();
  167. }
  168. if (op)
  169. {
  170. PxcModels.MachineForMerNo posFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == trade.MerNo) ?? new PxcModels.MachineForMerNo();
  171. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
  172. if (pos != null)
  173. {
  174. PxcModels.KqProducts brand = db.KqProducts.FirstOrDefault(m => m.Id == pos.BrandId) ?? new PxcModels.KqProducts();
  175. if (pos.BindMerchantId > 0)
  176. {
  177. PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == pos.UserId) ?? new PxcModels.Users();
  178. int TopUserId = 0;
  179. if (!string.IsNullOrEmpty(user.ParentNav))
  180. {
  181. TopUserId = int.Parse(user.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  182. }
  183. bool check = db.SpOrderNos.Any(m => m.OrderNo == trade.TradeSerialNo);
  184. if (!check)
  185. {
  186. decimal TradeAmount = trade.TradeAmount;
  187. int BankCardType = -1;
  188. int QrPayFlag = 0;
  189. int VipFlag = 0;
  190. int PayType = 0;
  191. if (pos.BrandId == 1 || pos.BrandId == 3)
  192. {
  193. TradeAmount = TradeAmount / 100;
  194. BankCardType = int.Parse(trade.BankCardType);
  195. if (trade.TradeType == "02") QrPayFlag = 1;
  196. else if (trade.TradeType == "NQ") PayType = 1;
  197. if (trade.SettleMethod == "E") VipFlag = 1;
  198. }
  199. else if (pos.BrandId == 2)
  200. {
  201. if (trade.TradeType == "CREDIT_BY_CARD")
  202. {
  203. BankCardType = 1;
  204. }
  205. else
  206. {
  207. BankCardType = 0;
  208. }
  209. if (trade.TradeType == "CLOUD_PAY") QrPayFlag = 1;
  210. }
  211. else if (pos.BrandId == 4 || pos.BrandId == 5)
  212. {
  213. TradeAmount = TradeAmount / 100;
  214. if (trade.BankCardType == "100")
  215. {
  216. BankCardType = 1;
  217. }
  218. else if (trade.BankCardType == "200")
  219. {
  220. BankCardType = 0;
  221. }
  222. if (trade.TradeType == "200") QrPayFlag = 1;
  223. if (trade.Field1 == "1") VipFlag = 1;
  224. }
  225. else if (pos.BrandId == 6)
  226. {
  227. TradeAmount = TradeAmount / 100;
  228. if (trade.BankCardType == "02" || trade.BankCardType == "12")
  229. {
  230. BankCardType = 1;
  231. }
  232. else
  233. {
  234. BankCardType = 0;
  235. }
  236. }
  237. else if (pos.BrandId == 7)
  238. {
  239. TradeAmount = TradeAmount / 100;
  240. if (trade.BankCardType == "OA" || trade.BankCardType == "CC" || trade.BankCardType == "SCC")
  241. {
  242. BankCardType = 1;
  243. }
  244. else
  245. {
  246. BankCardType = 0;
  247. }
  248. }
  249. else if (pos.BrandId == 8 || pos.BrandId == 9)
  250. {
  251. TradeAmount = TradeAmount / 100;
  252. if (trade.BankCardType == "100")
  253. {
  254. BankCardType = 1;
  255. }
  256. else if (trade.BankCardType == "200")
  257. {
  258. BankCardType = 0;
  259. }
  260. if (trade.TradeType == "200") QrPayFlag = 1;
  261. if (trade.Field1 == "1") VipFlag = 1;
  262. }
  263. else if (pos.BrandId == 10 || pos.BrandId == 11)
  264. {
  265. if (trade.BankCardType == "01")
  266. {
  267. BankCardType = 1;
  268. }
  269. else if (trade.BankCardType == "00")
  270. {
  271. BankCardType = 0;
  272. }
  273. if (trade.TradeType == "F010") QrPayFlag = 1;
  274. PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  275. if(merchant != null)
  276. {
  277. bool flag = false;
  278. if(function.CheckNull(merchant.MerchantName).Contains("*")) merchant.MerchantName = trade.MerName;
  279. if(string.IsNullOrEmpty(merchant.MerIdcardNo) && !string.IsNullOrEmpty(trade.Field2))
  280. {
  281. merchant.MerIdcardNo = trade.Field2.ToUpper(); //商户身份证号
  282. flag = true;
  283. }
  284. if(!string.IsNullOrEmpty(trade.Field1))
  285. {
  286. merchant.MerchantMobile = trade.Field1; //商户手机号
  287. }
  288. if(flag)
  289. {
  290. string startNo = merchant.MerIdcardNo.Substring(0, 6);
  291. string endNo = merchant.MerIdcardNo.Substring(merchant.MerIdcardNo.Length - 4, 4).ToUpper();
  292. string Name = merchant.MerchantName;
  293. Name = Name.Replace("*", "");
  294. function.WriteLog(DateTime.Now.ToString() + "-----startNo:" + startNo + ",endNo:" + endNo + ",Name:" + Name, "监控机具是否互斥");
  295. PxcModels.PosMerchantInfo chk = db.PosMerchantInfo.FirstOrDefault(m => m.MerIdcardNo.StartsWith(startNo) && m.MerIdcardNo.EndsWith(endNo) && m.MerchantName.Contains(Name));
  296. if(chk != null)
  297. {
  298. function.WriteLog("互斥机具---sn:" + chk.KqSnNo + ",merno:" + chk.KqMerNo + ",name:" + chk.MerchantName, "监控机具是否互斥");
  299. pos.IsFirst = 0;
  300. }
  301. }
  302. db.SaveChanges();
  303. }
  304. }
  305. else if (pos.BrandId == 12 || pos.BrandId == 13) //盒易付
  306. {
  307. if (trade.BankCardType == "C")
  308. {
  309. BankCardType = 1;
  310. }
  311. else if (trade.BankCardType == "D")
  312. {
  313. BankCardType = 0;
  314. }
  315. if (trade.Field3 == "1") QrPayFlag = 1;
  316. }
  317. else if (pos.BrandId == 15 || pos.BrandId == 16) //联客宝
  318. {
  319. TradeAmount = TradeAmount / 100;
  320. // 支付类型
  321. // 01-内卡借记卡
  322. // 02-内卡贷记卡
  323. // 03-银联二维码
  324. // 11-外卡借记卡
  325. // 12-外卡贷记卡
  326. // 30-微信
  327. // 31-支付宝
  328. // M3-扫码(银联二维码、微信、支付宝)
  329. if (trade.BankCardType == "02")
  330. {
  331. BankCardType = 1;
  332. }
  333. else
  334. {
  335. BankCardType = 0;
  336. }
  337. // if (trade.SerEntryMode == "1") QrPayFlag = 1;
  338. }
  339. int BrandId = pos.BrandId;
  340. // int BrandId = int.Parse(trade.ProductType);
  341. // if (BrandId == 1 && trade.MerNo.StartsWith("M900"))
  342. // {
  343. // BrandId = 3;
  344. // }
  345. // if (BrandId == 4 && trade.DeviceType == "200")
  346. // {
  347. // BrandId = 5;
  348. // }
  349. decimal SourceTradeAmount = TradeAmount;
  350. if (pos.Sort == 0 && pos.BrandId == 7)
  351. {
  352. decimal deposit = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  353. if (deposit > 0)
  354. {
  355. TradeAmount -= deposit;
  356. pos.Sort = 1;
  357. }
  358. }
  359. decimal CheckMoney = brand.ActTradeAmount > 0 ? brand.ActTradeAmount : 1000;
  360. int CheckDays = 30;
  361. //联动
  362. if(BankCardType == 1 && trade.TradeType == "M015" && (pos.BrandId == 10 || pos.BrandId == 11))
  363. {
  364. pos.CreditTrade += SourceTradeAmount;
  365. }
  366. //盒易付
  367. else if(BankCardType == 1 && trade.TradeType == "0" && trade.SerEntryMode != "40" && (pos.BrandId == 12 || pos.BrandId == 13))
  368. {
  369. pos.CreditTrade += SourceTradeAmount;
  370. }
  371. //联客宝
  372. else if(BankCardType == 1 && trade.TradeType != "40" && (pos.BrandId == 15 || pos.BrandId == 16))
  373. {
  374. pos.CreditTrade += SourceTradeAmount;
  375. }
  376. //金控
  377. else if((BankCardType == 1 || BankCardType == 2) && (pos.BrandId == 1 || pos.BrandId == 3))
  378. {
  379. pos.CreditTrade += SourceTradeAmount;
  380. }
  381. //开店宝
  382. else if (BankCardType == 1 && pos.BrandId == 2)
  383. {
  384. pos.CreditTrade += SourceTradeAmount;
  385. }
  386. //乐刷or海科
  387. else if (BankCardType == 1 && (trade.TradeType == "100" || trade.TradeType == "200" || trade.TradeType == "300" || trade.TradeType == "400" || trade.TradeType == "500" || trade.TradeType == "600") && (pos.BrandId == 4 || pos.BrandId == 5 || pos.BrandId == 8 || pos.BrandId == 9))
  388. {
  389. pos.CreditTrade += SourceTradeAmount;
  390. }
  391. //立刷
  392. else if(BankCardType == 1 && trade.Field2 == "0104" && pos.BrandId == 6)
  393. {
  394. pos.CreditTrade += SourceTradeAmount;
  395. }
  396. //盛付通
  397. else if(BankCardType == 1 && pos.BrandId == 7)
  398. {
  399. pos.CreditTrade += SourceTradeAmount;
  400. }
  401. //广电卡
  402. else if(pos.BrandId == 14)
  403. {
  404. pos.CreditTrade += SourceTradeAmount;
  405. }
  406. else if (BankCardType == 0)
  407. {
  408. pos.DebitCardTrade += TradeAmount;
  409. }
  410. //判断激活条件并激活
  411. ActPos(db, pos, CheckMoney, CheckDays);
  412. if (TradeAmount > 0)
  413. {
  414. ulong MerHelpFlag = 0;
  415. decimal DepositAmt = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  416. if (pos.DownFeeFlag == 1)
  417. {
  418. if(pos.BrandId == 12 || pos.BrandId == 13)
  419. {
  420. MerHelpFlag = 3;
  421. }
  422. else
  423. {
  424. if (pos.DownFee == 0.6M)
  425. {
  426. MerHelpFlag = 3;
  427. }
  428. else
  429. {
  430. MerHelpFlag = 2;
  431. }
  432. }
  433. }
  434. else if ((DepositAmt > 0 && pos.IsFirst == 1) || pos.BrandId == 14)
  435. {
  436. string DayString = RedisDbconn.Instance.Get<string>("pobjrule:" + pos.BrandId + ":HelpPolicy:Days");
  437. int Days = int.Parse(DayString); //天数
  438. if (pos.BindingTime != null)
  439. {
  440. if (pos.BindingTime.Value.AddDays(Days) > DateTime.Now)
  441. {
  442. MerHelpFlag = 1;
  443. }
  444. }
  445. }
  446. if((pos.BrandId == 12 || pos.BrandId == 13) && pos.UpFeeFlag == 1 && pos.DownFeeFlag == 0)
  447. {
  448. MerHelpFlag = 2;
  449. }
  450. if((pos.BrandId == 10 || pos.BrandId == 11) && trade.TradeType == "T015" && MerHelpFlag == 1)
  451. {
  452. MerHelpFlag = 0;
  453. }
  454. int CapFlag = 0;
  455. if(BankCardType == 0 && TradeAmount >= 1666.67M)
  456. {
  457. CapFlag = 1;
  458. }
  459. string HelpProfitFlag = "";
  460. bool checkHelpProfit = db.HelpProfitMerchantForUser.Any(m => m.MerchantId == pos.BindMerchantId);
  461. if(checkHelpProfit)
  462. {
  463. HelpProfitFlag = "HelpProfit";
  464. }
  465. int ActStatus = pos.ActivationState;
  466. // if(BrandId == 4 || BrandId == 5 || BrandId == 8 || BrandId == 9)
  467. // {
  468. // ActStatus = GetLeShuaHaiKe(pos.UserId, BrandId);
  469. // }
  470. int Version = 0;
  471. if(MerHelpFlag == 2) Version = 1;
  472. if(MerHelpFlag == 3) Version = 2;
  473. //盒易付和来客吧电签,稳定期自动转到稳定期A
  474. if((pos.BrandId == 12 || pos.BrandId == 13 || pos.BrandId == 15) && Version == 0)
  475. {
  476. Version = 1;
  477. }
  478. decimal SettleFee = trade.SettleFee * 100;
  479. PxcModels.TradeRecord add = db.TradeRecord.Add(new PxcModels.TradeRecord()
  480. {
  481. ParentNav = user.ParentNav,
  482. CreateDate = trade.CreateDate,
  483. UpdateDate = trade.UpdateDate,
  484. RecordNo = trade.TradeSerialNo, //单号
  485. UserId = pos.UserId, //创客
  486. MerchantId = pos.BindMerchantId, //商户
  487. MerNo = trade.MerNo, //渠道商户编号
  488. MerHelpFlag = MerHelpFlag >= 2 ? 1 : MerHelpFlag, //1扶持周,0稳定期,2盈利期
  489. HelpMonthCount = 0, //扶持第几个月
  490. MerBuddyType = user.MerchantType, //商户创客类型
  491. SnNo = trade.TradeSnNo, //渠道SN号
  492. TradeSerialNo = trade.ChannelSerial, //交易流水号
  493. TradeAmount = TradeAmount, //交易金额
  494. BankCardType = BankCardType, //银行卡类型
  495. QrPayFlag = QrPayFlag, //云闪付标识
  496. CapFlag = CapFlag, //借记卡是否封顶
  497. VipFlag = VipFlag, //会员标记
  498. PayType = PayType, //支付方式
  499. BrandId = BrandId, //品牌
  500. Remark = trade.Remark, //备注
  501. TopUserId = TopUserId, //顶级创客
  502. MerUserId = pos.UserId, //商户直属创客
  503. SeoTitle = HelpProfitFlag,
  504. Version = Version,
  505. ActStatus = ActStatus,
  506. CreateMan = trade.AgentNo,
  507. UpdateMan = trade.BankCardType,
  508. SeoKeyword = trade.SerEntryMode,
  509. SeoDescription = trade.TradeType,
  510. Sort = (int)SettleFee,
  511. }).Entity;
  512. db.SaveChanges();
  513. string TradeMonth = trade.CreateDate == null ? DateTime.Now.ToString("yyyyMM") : trade.CreateDate.Value.ToString("yyyyMM");
  514. PublicFunction.SplitTradeRecord(add, TradeMonth);
  515. //统计商户交易额
  516. string TradeDate = trade.CreateDate == null ? DateTime.Now.ToString("yyyyMMdd") : trade.CreateDate.Value.ToString("yyyyMMdd");
  517. RedisDbconn.Instance.AddList("StatMerTradeAmountQueue", "{\"TradeDate\":\"" + TradeDate + "\",\"TradeAmount\":\"" + TradeAmount + "\",\"MerchantId\":\"" + pos.BindMerchantId + "\",\"BrandId\":\"" + BrandId + "\"}");
  518. }
  519. // 立刷60天内刷满10000,再返40
  520. // if (pos.BrandId == 6 && decimal.Parse(function.CheckNum(pos.SeoKeyword)) > 0&& pos.CreditTrade >= 10000 && pos.ActivationState == 1 && pos.BindingTime > DateTime.Now.AddDays(-60))
  521. // {
  522. // LiShuaActPrize(db, pos, 40);
  523. // }
  524. //9月1日-9月30日立刷机具刷5000激活时额外奖励30元立刷活动奖
  525. // if(pos.BrandId == 6 && pos.CreditTrade >= 5000M && pos.ActivationTime >= DateTime.Parse("2023-08-31 00:00:00") && pos.ActivationTime < DateTime.Parse("2023-11-01 00:00:00"))
  526. // {
  527. // LiShuaActPrize(db, pos, 30);
  528. // }
  529. db.SpOrderNos.Add(new PxcModels.SpOrderNos()
  530. {
  531. OrderNo = trade.TradeSerialNo
  532. });
  533. db.SaveChanges();
  534. // 推荐王逻辑(交易)
  535. // if(TradeAmount > 0)
  536. // {
  537. // string Send = "{\"Kind\":\"4\",\"Data\":{\"PosId\":\"" + pos.Id + "\",\"TradeMonth\":\"" + trade.CreateDate.Value.ToString("yyyyMM") + "\",\"TradeAmount\":\"" + TradeAmount + "\"}}";
  538. // RedisDbconn.Instance.AddList("RecommendActStatQueue", Send);
  539. // }
  540. }
  541. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  542. if (edit != null)
  543. {
  544. edit.Status = 2;
  545. }
  546. spdb.SaveChanges();
  547. }
  548. else
  549. {
  550. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  551. if (edit != null)
  552. {
  553. edit.Status = 0;
  554. }
  555. spdb.SaveChanges();
  556. }
  557. }
  558. else
  559. {
  560. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  561. if (edit != null)
  562. {
  563. edit.Status = 0;
  564. }
  565. spdb.SaveChanges();
  566. }
  567. }
  568. }
  569. spdb.Dispose();
  570. db.Dispose();
  571. }
  572. catch (Exception ex)
  573. {
  574. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP交易数据到MAIN异常");
  575. }
  576. Thread.Sleep(500);
  577. }
  578. }
  579. public void StartTradeStatus()
  580. {
  581. Thread th = new Thread(StartTradeStatusDo);
  582. th.IsBackground = true;
  583. th.Start();
  584. }
  585. public void StartTradeStatusDo()
  586. {
  587. while (true)
  588. {
  589. try
  590. {
  591. WebCMSEntities spdb = new WebCMSEntities();
  592. DateTime start = DateTime.Now.AddDays(-10);
  593. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/TradeRecordId.txt")));
  594. IQueryable<TradeRecord> trades = spdb.TradeRecord.Where(m => m.Id >= StartId && m.CreateDate >= start && m.Status == 0).OrderBy(m => m.CreateDate).Take(20);
  595. foreach (TradeRecord trade in trades.ToList())
  596. {
  597. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  598. if (edit != null)
  599. {
  600. edit.Status = 1;
  601. spdb.SaveChanges();
  602. }
  603. }
  604. spdb.Dispose();
  605. }
  606. catch (Exception ex)
  607. {
  608. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "恢复原始交易没有统计上的数据异常");
  609. }
  610. Thread.Sleep(600000);
  611. }
  612. }
  613. //机具激活逻辑
  614. public void ActPos(PxcModels.WebCMSEntities db, PxcModels.PosMachinesTwo pos, decimal CheckMoney, int CheckDays, bool PrizeFlag = true, string ActivationTime = "")
  615. {
  616. DateTime now = DateTime.Now;
  617. if(!string.IsNullOrEmpty(ActivationTime))
  618. {
  619. now = DateTime.Parse(ActivationTime);
  620. }
  621. DateTime TransferTime = pos.TransferTime == null ? DateTime.Now : pos.TransferTime.Value;
  622. int minute = 30;
  623. if(pos.BrandId == 14)
  624. {
  625. minute = 0;
  626. }
  627. if (pos.CreditTrade >= CheckMoney && pos.BuyUserId > 0 && pos.ActivationState == 0 && TransferTime.AddMinutes(-minute) < pos.BindingTime)
  628. {
  629. pos.ActivationState = 1;
  630. pos.ActivationTime = now;
  631. function.WriteLog("机具" + pos.PosSn, "开机奖励在激活中监控");
  632. function.WriteLog("PrizeFlag:" + PrizeFlag, "开机奖励在激活中监控");
  633. PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  634. if (merchant != null)
  635. {
  636. function.WriteLog("查询到商户", "开机奖励在激活中监控");
  637. merchant.ActiveStatus = 1;
  638. merchant.MerStandardDate = now;
  639. db.SaveChanges();
  640. function.WriteLog("商户" + merchant.KqMerNo, "开机奖励在激活中监控");
  641. //发放开机奖励
  642. function.WriteLog("首台" + pos.IsFirst, "开机奖励在激活中监控");
  643. if(pos.IsFirst == 1 && pos.BindingTime > now.AddDays(-CheckDays) && PrizeFlag)
  644. {
  645. function.WriteLog("开机奖发放", "开机奖励在激活中监控");
  646. RedisDbconn.Instance.AddList("OpenRewardQueue", pos.Id.ToString());
  647. }
  648. }
  649. else if(pos.BrandId == 14 && pos.IsFirst == 1 && pos.BindingTime > now.AddDays(-CheckDays) && PrizeFlag)
  650. {
  651. function.WriteLog("开机奖发放", "开机奖励在激活中监控");
  652. RedisDbconn.Instance.AddList("OpenRewardQueue", pos.Id.ToString());
  653. }
  654. //发放大盟主奖励
  655. function.WriteLog("大盟主---PrizeFlag:" + PrizeFlag + ";LeaderUserId:" + pos.LeaderUserId + ";pos.IsFirst:" + pos.IsFirst + ";pos.BindingTime:" + pos.BindingTime + ";now.AddDays(-CheckDays):" + now.AddDays(-CheckDays) + ";", "开机奖励在激活中监控");
  656. if (pos.LeaderUserId > 0 && pos.IsFirst == 1 && db.Leaders.Any(m => m.Id == pos.LeaderUserId && m.ExpiredDate > now) && pos.BindingTime > now.AddDays(-CheckDays) && PrizeFlag)
  657. {
  658. function.WriteLog("大盟主奖发放", "开机奖励在激活中监控");
  659. RedisDbconn.Instance.AddList("LeaderPrizeQueue", pos.Id);
  660. }
  661. //发放运营中心奖励
  662. if (pos.IsFirst == 1 && pos.BindingTime > now.AddDays(-CheckDays) && PrizeFlag)
  663. {
  664. function.WriteLog("运营中心奖发放", "开机奖励在激活中监控");
  665. RedisDbconn.Instance.AddList("OperatePrizeQueue", pos.Id);
  666. }
  667. AlipayPayBack2Service.Instance.ActReserveBack(pos.OpId, pos.OpReserve1, pos.OpReserve2, pos.OpReserve3);
  668. //统计激活数
  669. RedisDbconn.Instance.AddList("StatActQueue", "{\"TradeDate\":\"" + now.ToString("yyyyMMdd") + "\",\"UserId\":\"" + pos.BuyUserId + "\",\"BrandId\":\"" + pos.BrandId + "\"}");
  670. }
  671. }
  672. //立刷格外奖励
  673. public void LiShuaActPrize(PxcModels.WebCMSEntities db, PxcModels.PosMachinesTwo pos, int prize)
  674. {
  675. PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  676. if(merchant != null)
  677. {
  678. PxcModels.Users getUser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new PxcModels.Users();
  679. int BuyTopUserId = 0;
  680. string ParentNav = getUser.ParentNav;
  681. if (!string.IsNullOrEmpty(ParentNav))
  682. {
  683. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  684. if (ParentNavList.Length > 1)
  685. {
  686. BuyTopUserId = int.Parse(ParentNavList[1]);
  687. }
  688. else if (ParentNavList.Length == 1)
  689. {
  690. BuyTopUserId = int.Parse(ParentNavList[0]);
  691. }
  692. }
  693. if(prize == 50)
  694. {
  695. StatService.Instance.doActiveReward(db, merchant, pos, getUser.Id, getUser.ParentNav, BuyTopUserId, prize);
  696. }
  697. if(prize == 40)
  698. {
  699. StatService.Instance.doActiveReward(db, merchant, pos, getUser.Id, getUser.ParentNav, BuyTopUserId, prize, 12);
  700. }
  701. if(prize == 30)
  702. {
  703. StatService.Instance.doActiveReward(db, merchant, pos, getUser.Id, getUser.ParentNav, BuyTopUserId, prize, 133);
  704. }
  705. }
  706. }
  707. private int GetLeShuaHaiKe(int UserId, int BrandId)
  708. {
  709. if(DateTime.Now >= DateTime.Parse("2023-08-01 00:00:00"))
  710. {
  711. return 0;
  712. }
  713. int ActStatus = 1;
  714. try
  715. {
  716. string Date = DateTime.Now.ToString("yyyyMMdd");
  717. int checkNum = 0;
  718. if(Date == "20230720") checkNum = 12;
  719. if(Date == "20230721") checkNum = 11;
  720. if(Date == "20230722") checkNum = 10;
  721. if(Date == "20230723") checkNum = 9;
  722. if(Date == "20230724") checkNum = 8;
  723. if(Date == "20230725") checkNum = 7;
  724. if(Date == "20230726") checkNum = 6;
  725. if(Date == "20230727") checkNum = 5;
  726. if(Date == "20230728") checkNum = 4;
  727. if(Date == "20230729") checkNum = 3;
  728. if(Date == "20230730") checkNum = 2;
  729. if(Date == "20230731") checkNum = 1;
  730. int check = int.Parse(function.CheckInt(RedisDbconn.Instance.Get<string>("lscut:" + UserId + ":" + BrandId + ":" + Date)));
  731. if(check <= checkNum)
  732. {
  733. check += 1;
  734. RedisDbconn.Instance.Set("lscut:" + UserId + ":" + BrandId + ":" + Date, check.ToString());
  735. }
  736. else
  737. {
  738. ActStatus = -1;
  739. RedisDbconn.Instance.Set("lscut:" + UserId + ":" + BrandId + ":" + Date, "0");
  740. }
  741. }
  742. catch(Exception ex)
  743. {
  744. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "乐刷海科计数异常");
  745. }
  746. return ActStatus;
  747. }
  748. }
  749. }