SycnSpTradeZkbService.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 SycnSpTradeZkbService
  11. {
  12. public readonly static SycnSpTradeZkbService Instance = new SycnSpTradeZkbService();
  13. private SycnSpTradeZkbService()
  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. if (Utils.PushTimeCheck())
  28. {
  29. List<string> BrandIds = new List<string>();
  30. BrandIds.Add("17");
  31. List<string> SaveDataBindIds = Utils.Instance.SaveDataBindIds();
  32. WebCMSEntities spdb = new WebCMSEntities();
  33. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  34. DateTime start = DateTime.Now.AddDays(-10);
  35. DateTime end = DateTime.Now.AddMinutes(-1);
  36. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/TradeRecordId.txt")));
  37. IQueryable<TradeRecord> trades = spdb.TradeRecord.Where(m => m.Id >= StartId && BrandIds.Contains(m.ProductType) && SaveDataBindIds.Contains(m.ProductType) && m.CreateDate >= start && m.Status == 1).OrderBy(m => m.CreateDate).Take(20);
  38. foreach (TradeRecord trade in trades.ToList())
  39. {
  40. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == trade.TradeSnNo);
  41. if (pos != null)
  42. {
  43. PxcModels.KqProducts brand = db.KqProducts.FirstOrDefault(m => m.Id == pos.BrandId) ?? new PxcModels.KqProducts();
  44. if (pos.BindMerchantId > 0)
  45. {
  46. PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == pos.UserId) ?? new PxcModels.Users();
  47. int TopUserId = 0;
  48. if (!string.IsNullOrEmpty(user.ParentNav))
  49. {
  50. TopUserId = int.Parse(user.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  51. }
  52. bool check = db.SpOrderNos.Any(m => m.OrderNo == trade.TradeSerialNo);
  53. if (!check)
  54. {
  55. decimal TradeAmount = trade.TradeAmount / 100;
  56. int BankCardType = 1;
  57. int QrPayFlag = 0;
  58. int VipFlag = 0;
  59. int PayType = 0;
  60. int BrandId = pos.BrandId;
  61. int ActStatus = pos.ActivationState;
  62. if (TradeAmount > 0)
  63. {
  64. PxcModels.TradeRecord add = db.TradeRecord.Add(new PxcModels.TradeRecord()
  65. {
  66. ParentNav = user.ParentNav,
  67. CreateDate = trade.CreateDate,
  68. UpdateDate = trade.UpdateDate,
  69. RecordNo = trade.TradeSerialNo, //单号
  70. UserId = pos.UserId, //创客
  71. MerchantId = pos.BindMerchantId, //商户
  72. MerNo = trade.MerNo, //渠道商户编号
  73. MerHelpFlag = 0,
  74. HelpMonthCount = 0, //扶持第几个月
  75. MerBuddyType = user.MerchantType, //商户创客类型
  76. SnNo = trade.TradeSnNo, //渠道SN号
  77. TradeSerialNo = trade.ChannelSerial, //交易流水号
  78. TradeAmount = TradeAmount, //交易金额
  79. BankCardType = BankCardType, //银行卡类型
  80. QrPayFlag = QrPayFlag, //云闪付标识
  81. VipFlag = VipFlag, //会员标记
  82. PayType = PayType, //支付方式
  83. BrandId = BrandId, //品牌
  84. Remark = trade.Remark, //备注
  85. TopUserId = TopUserId, //顶级创客
  86. MerUserId = pos.UserId, //商户直属创客M
  87. ActStatus = ActStatus,
  88. CreateMan = trade.AgentNo,
  89. UpdateMan = trade.BankCardType,
  90. SeoKeyword = trade.SerEntryMode,
  91. SeoDescription = trade.TradeType,
  92. TradeDate = pos.BindingTime,
  93. }).Entity;
  94. db.SaveChanges();
  95. }
  96. db.SpOrderNos.Add(new PxcModels.SpOrderNos()
  97. {
  98. OrderNo = trade.TradeSerialNo
  99. });
  100. db.SaveChanges();
  101. }
  102. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  103. if (edit != null)
  104. {
  105. edit.Status = 2;
  106. }
  107. spdb.SaveChanges();
  108. }
  109. else
  110. {
  111. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  112. if (edit != null)
  113. {
  114. edit.Status = 0;
  115. }
  116. spdb.SaveChanges();
  117. }
  118. }
  119. else
  120. {
  121. TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  122. if (edit != null)
  123. {
  124. edit.Status = 0;
  125. }
  126. spdb.SaveChanges();
  127. }
  128. }
  129. spdb.Dispose();
  130. db.Dispose();
  131. }
  132. }
  133. catch (Exception ex)
  134. {
  135. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP吱客宝交易数据到MAIN异常");
  136. }
  137. Thread.Sleep(500);
  138. }
  139. }
  140. }
  141. }