SycnSpTradeService.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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);
  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.SerEntryMode == "1" && trade.ProductType == "10") //联动押金获取
  57. {
  58. if (trade.TradeAmount == 99 || trade.TradeAmount == 199 || trade.TradeAmount == 299)
  59. {
  60. op = false;
  61. PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == trade.TradeSnNo) ?? new PxcModels.MachineForSnNo();
  62. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
  63. if (pos != null)
  64. {
  65. pos.SeoKeyword = trade.TradeAmount.ToString("f2");
  66. db.SaveChanges();
  67. }
  68. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  69. if (edit != null)
  70. {
  71. edit.Status = 2;
  72. }
  73. spdb.SaveChanges();
  74. }
  75. }
  76. if (trade.DiscountRateFlag == "True")
  77. {
  78. op = false;
  79. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  80. if (edit != null)
  81. {
  82. edit.Status = 2;
  83. }
  84. spdb.SaveChanges();
  85. }
  86. if(trade.ProductType == "7" && trade.TradeStatus != "00")
  87. {
  88. op = false;
  89. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  90. if (edit != null)
  91. {
  92. edit.Status = 2;
  93. }
  94. spdb.SaveChanges();
  95. }
  96. if (op)
  97. {
  98. PxcModels.MachineForMerNo posFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == trade.MerNo) ?? new PxcModels.MachineForMerNo();
  99. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
  100. if (pos != null)
  101. {
  102. if (pos.BindMerchantId > 0)
  103. {
  104. PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == pos.UserId) ?? new PxcModels.Users();
  105. int TopUserId = 0;
  106. if (!string.IsNullOrEmpty(user.ParentNav))
  107. {
  108. TopUserId = int.Parse(user.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  109. }
  110. bool check = db.SpOrderNos.Any(m => m.OrderNo == trade.TradeSerialNo);
  111. if (!check)
  112. {
  113. decimal TradeAmount = trade.TradeAmount;
  114. int BankCardType = -1;
  115. int QrPayFlag = 0;
  116. int VipFlag = 0;
  117. int PayType = 0;
  118. if (pos.BrandId == 1 || pos.BrandId == 3)
  119. {
  120. TradeAmount = TradeAmount / 100;
  121. BankCardType = int.Parse(trade.BankCardType);
  122. if (trade.TradeType == "02") QrPayFlag = 1;
  123. else if (trade.TradeType == "NQ") PayType = 1;
  124. if (trade.SettleMethod == "E") VipFlag = 1;
  125. }
  126. else if (pos.BrandId == 2)
  127. {
  128. if (trade.TradeType == "DEBIT_BY_CARD")
  129. {
  130. BankCardType = 0;
  131. }
  132. else
  133. {
  134. BankCardType = 1;
  135. }
  136. if (trade.TradeType == "CLOUD_PAY") QrPayFlag = 1;
  137. }
  138. else if (pos.BrandId == 4 || pos.BrandId == 5)
  139. {
  140. TradeAmount = TradeAmount / 100;
  141. if (trade.BankCardType == "100")
  142. {
  143. BankCardType = 1;
  144. }
  145. else if (trade.BankCardType == "200")
  146. {
  147. BankCardType = 0;
  148. }
  149. if (trade.TradeType == "200") QrPayFlag = 1;
  150. if (trade.Field1 == "1") VipFlag = 1;
  151. }
  152. else if (pos.BrandId == 6)
  153. {
  154. TradeAmount = TradeAmount / 100;
  155. if (trade.BankCardType == "01" || trade.BankCardType == "11")
  156. {
  157. BankCardType = 0;
  158. }
  159. else
  160. {
  161. BankCardType = 1;
  162. }
  163. }
  164. else if (pos.BrandId == 7)
  165. {
  166. TradeAmount = TradeAmount / 100;
  167. if (trade.BankCardType == "OA" || trade.BankCardType == "CC" || trade.BankCardType == "SCC")
  168. {
  169. BankCardType = 1;
  170. }
  171. else
  172. {
  173. BankCardType = 0;
  174. }
  175. }
  176. else if (pos.BrandId == 8 || pos.BrandId == 9)
  177. {
  178. TradeAmount = TradeAmount / 100;
  179. if (trade.BankCardType == "100")
  180. {
  181. BankCardType = 1;
  182. }
  183. else if (trade.BankCardType == "200")
  184. {
  185. BankCardType = 0;
  186. }
  187. if (trade.TradeType == "200") QrPayFlag = 1;
  188. if (trade.Field1 == "1") VipFlag = 1;
  189. }
  190. else if (pos.BrandId == 10 || pos.BrandId == 11)
  191. {
  192. if (trade.BankCardType == "01")
  193. {
  194. BankCardType = 1;
  195. }
  196. else if (trade.BankCardType == "00")
  197. {
  198. BankCardType = 0;
  199. }
  200. if (trade.TradeType == "F010") QrPayFlag = 1;
  201. PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  202. if(merchant != null)
  203. {
  204. if(string.IsNullOrEmpty(merchant.MerIdcardNo) && !string.IsNullOrEmpty(trade.Field2))
  205. {
  206. merchant.MerIdcardNo = trade.Field2.ToUpper(); //商户身份证号
  207. }
  208. if(!string.IsNullOrEmpty(trade.Field1))
  209. {
  210. merchant.MerchantMobile = trade.Field1; //商户手机号
  211. }
  212. if(!string.IsNullOrEmpty(merchant.MerIdcardNo))
  213. {
  214. string startNo = merchant.MerIdcardNo.Substring(0, 6);
  215. string endNo = merchant.MerIdcardNo.Substring(merchant.MerIdcardNo.Length - 4, 4).ToUpper();
  216. string Name = merchant.MerchantName;
  217. Name = Name.Replace("*", "");
  218. function.WriteLog(DateTime.Now.ToString() + "-----startNo:" + startNo + ",endNo:" + endNo + ",Name:" + Name, "监控机具是否互斥");
  219. bool chk = db.PosMerchantInfo.Any(m => m.MerIdcardNo.StartsWith(startNo) && m.MerIdcardNo.EndsWith(endNo) && m.MerchantName.Contains(Name));
  220. if(chk)
  221. {
  222. pos.IsFirst = 0;
  223. }
  224. }
  225. db.SaveChanges();
  226. }
  227. }
  228. int BrandId = pos.BrandId;
  229. // int BrandId = int.Parse(trade.ProductType);
  230. // if (BrandId == 1 && trade.MerNo.StartsWith("M900"))
  231. // {
  232. // BrandId = 3;
  233. // }
  234. // if (BrandId == 4 && trade.DeviceType == "200")
  235. // {
  236. // BrandId = 5;
  237. // }
  238. decimal SourceTradeAmount = TradeAmount;
  239. if (pos.Sort == 0 && pos.BrandId == 7)
  240. {
  241. decimal deposit = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  242. if (deposit > 0)
  243. {
  244. TradeAmount -= deposit;
  245. pos.Sort = 1;
  246. }
  247. }
  248. decimal CheckMoney = 1000;
  249. int CheckDays = 30;
  250. if (pos.BrandId == 6)
  251. {
  252. CheckMoney = 5000;
  253. }
  254. //联动
  255. if(BankCardType == 1 && trade.TradeType == "M015" && (pos.BrandId == 10 || pos.BrandId == 11))
  256. {
  257. pos.CreditTrade += SourceTradeAmount;
  258. }
  259. //金控
  260. else if((BankCardType == 1 || BankCardType == 2) && (pos.BrandId == 1 || pos.BrandId == 3))
  261. {
  262. pos.CreditTrade += SourceTradeAmount;
  263. }
  264. //开店宝
  265. else if (BankCardType == 1 && pos.BrandId == 2)
  266. {
  267. pos.CreditTrade += SourceTradeAmount;
  268. }
  269. //乐刷or海科
  270. else if (BankCardType == 1 && (trade.TradeType == "100" || trade.TradeType == "200") && (pos.BrandId == 4 || pos.BrandId == 5 || pos.BrandId == 8 || pos.BrandId == 9))
  271. {
  272. pos.CreditTrade += SourceTradeAmount;
  273. }
  274. //立刷
  275. else if(BankCardType == 1 && trade.Field2 == "0104" && pos.BrandId == 6)
  276. {
  277. pos.CreditTrade += SourceTradeAmount;
  278. }
  279. //盛付通
  280. else if(BankCardType == 1 && trade.TradeType == "PUR" && pos.BrandId == 7)
  281. {
  282. pos.CreditTrade += SourceTradeAmount;
  283. }
  284. else if (BankCardType == 0)
  285. {
  286. pos.DebitCardTrade += TradeAmount;
  287. }
  288. //判断激活条件并激活
  289. if (pos.CreditTrade >= CheckMoney && pos.ActivationState == 0 && pos.TransferTime < pos.BindingTime && pos.BindingTime > DateTime.Now.AddDays(-CheckDays))
  290. {
  291. pos.ActivationState = 1;
  292. pos.ActivationTime = DateTime.Now;
  293. PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  294. if (merchant != null)
  295. {
  296. merchant.ActiveStatus = 1;
  297. merchant.MerStandardDate = DateTime.Now;
  298. db.SaveChanges();
  299. // 推荐王逻辑(激活)
  300. string SendData = "{\"Kind\":\"3\",\"Data\":{\"UserId\":\"" + pos.BuyUserId + "\",\"PosId\":\"" + pos.Id + "\",\"TradeMonth\":\"" + DateTime.Now.ToString("yyyyMM") + "\"}}";
  301. RedisDbconn.Instance.AddList("RecommendActStatQueue", SendData);
  302. //发放开机奖励
  303. if(pos.IsFirst == 1)
  304. {
  305. RedisDbconn.Instance.AddList("OpenRewardQueue", pos.Id.ToString());
  306. }
  307. }
  308. //发放大盟主奖励
  309. if(pos.LeaderUserId > 0)
  310. {
  311. RedisDbconn.Instance.AddList("LeaderPrizeQueue", pos.Id);
  312. }
  313. //发放运营中心奖励
  314. RedisDbconn.Instance.AddList("OperatePrizeQueue", pos.Id);
  315. AlipayPayBack2Service.Instance.ActReserveBack(pos.OpId, pos.OpReserve1, pos.OpReserve2, pos.OpReserve3);
  316. }
  317. if (TradeAmount > 0)
  318. {
  319. ulong MerHelpFlag = 0;
  320. decimal DepositAmt = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  321. if (pos.DownFeeFlag == 1)
  322. {
  323. MerHelpFlag = 2;
  324. }
  325. else if (DepositAmt > 0 && pos.IsFirst == 1)
  326. {
  327. string DayString = RedisDbconn.Instance.Get<string>("pobjrule:" + pos.BrandId + ":HelpPolicy:Days");
  328. int Days = int.Parse(DayString); //天数
  329. if (pos.BindingTime != null)
  330. {
  331. if (pos.BindingTime.Value.AddDays(Days) > DateTime.Now)
  332. {
  333. MerHelpFlag = 1;
  334. }
  335. }
  336. }
  337. if((pos.BrandId == 10 || pos.BrandId == 11) && trade.TradeType == "T015" && MerHelpFlag == 1)
  338. {
  339. MerHelpFlag = 0;
  340. }
  341. int CapFlag = 0;
  342. if(BankCardType == 0 && TradeAmount >= 1666.67M)
  343. {
  344. CapFlag = 1;
  345. }
  346. string HelpProfitFlag = "";
  347. bool checkHelpProfit = db.HelpProfitMerchantForUser.Any(m => m.MerchantId == pos.BindMerchantId);
  348. if(checkHelpProfit)
  349. {
  350. HelpProfitFlag = "HelpProfit";
  351. }
  352. db.TradeRecord.Add(new PxcModels.TradeRecord()
  353. {
  354. ParentNav = user.ParentNav,
  355. CreateDate = trade.CreateDate,
  356. UpdateDate = trade.UpdateDate,
  357. RecordNo = trade.TradeSerialNo, //单号
  358. UserId = pos.UserId, //创客
  359. MerchantId = pos.BindMerchantId, //商户
  360. MerNo = trade.MerNo, //渠道商户编号
  361. MerHelpFlag = MerHelpFlag, //1扶持周,0稳定期,2盈利期
  362. HelpMonthCount = 0, //扶持第几个月
  363. MerBuddyType = user.MerchantType, //商户创客类型
  364. SnNo = trade.TradeSnNo, //渠道SN号
  365. TradeSerialNo = trade.ChannelSerial, //交易流水号
  366. TradeAmount = TradeAmount, //交易金额
  367. BankCardType = BankCardType, //银行卡类型
  368. QrPayFlag = QrPayFlag, //云闪付标识
  369. CapFlag = CapFlag, //借记卡是否封顶
  370. VipFlag = VipFlag, //会员标记
  371. PayType = PayType, //支付方式
  372. BrandId = BrandId, //品牌
  373. Remark = trade.Remark, //备注
  374. TopUserId = TopUserId, //顶级创客
  375. MerUserId = pos.UserId, //商户直属创客
  376. SeoTitle = HelpProfitFlag,
  377. Version = MerHelpFlag == 2 ? 1 : 0,
  378. ActStatus = pos.ActivationState,
  379. });
  380. }
  381. // 立刷60天内刷满10000,再返40
  382. if (pos.BrandId == 6 && decimal.Parse(function.CheckNum(pos.SeoKeyword)) > 0 && pos.ActivationState == 1 && pos.CreditTrade >= 10000 && pos.BindingTime > DateTime.Now.AddDays(-60))
  383. {
  384. // PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId && m.Sort == 1);
  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. StatService.Instance.doActiveReward(db, merchant, pos, getUser.Id, getUser.ParentNav, BuyTopUserId, 40, 12);
  404. }
  405. }
  406. db.SpOrderNos.Add(new PxcModels.SpOrderNos()
  407. {
  408. OrderNo = trade.TradeSerialNo
  409. });
  410. db.SaveChanges();
  411. // 推荐王逻辑(交易)
  412. if(TradeAmount > 0)
  413. {
  414. string Send = "{\"Kind\":\"4\",\"Data\":{\"PosId\":\"" + pos.Id + "\",\"TradeMonth\":\"" + trade.CreateDate.Value.ToString("yyyyMM") + "\",\"TradeAmount\":\"" + TradeAmount + "\"}}";
  415. RedisDbconn.Instance.AddList("RecommendActStatQueue", Send);
  416. }
  417. }
  418. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  419. if (edit != null)
  420. {
  421. edit.Status = 2;
  422. }
  423. spdb.SaveChanges();
  424. }
  425. }
  426. }
  427. }
  428. spdb.Dispose();
  429. db.Dispose();
  430. }
  431. catch (Exception ex)
  432. {
  433. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP交易数据到MAIN异常");
  434. }
  435. Thread.Sleep(1000);
  436. }
  437. }
  438. }
  439. }