SycnSpMerchantService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 SycnSpMerchantService
  11. {
  12. public readonly static SycnSpMerchantService Instance = new SycnSpMerchantService();
  13. private SycnSpMerchantService()
  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> SaveDataBindIds = Utils.Instance.SaveDataBindIds();
  28. WebCMSEntities spdb = new WebCMSEntities();
  29. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  30. DateTime start = DateTime.Now.AddDays(-100);
  31. DateTime end = DateTime.Now.AddMinutes(-2);
  32. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/MerchantsId.txt")));
  33. var Mers = spdb.Merchants.Where(m => m.Id >= StartId && m.CreateTime >= start && m.CreateTime <= end && m.Status == 1 && m.ProductType != "14" && SaveDataBindIds.Contains(m.ProductType)).OrderByDescending(m => m.Id).Take(20).ToList();
  34. // var Mers = spdb.Merchants.Where(m => m.Id == 765841).ToList();
  35. foreach (var Mer in Mers)
  36. {
  37. var tran = db.Database.BeginTransaction();
  38. try
  39. {
  40. if (Mer.Field2 != "解绑" && Mer.Field2 != "UNBIND" && Mer.Field2 != "CHANGE_BIND" && Mer.Field2 != "TRUE")
  41. {
  42. string rnd = "";
  43. if (!Utils.Instance.IsWifi(int.Parse(Mer.ProductType)) && !Utils.Instance.IsHaoDa(int.Parse(Mer.ProductType)) && Mer.ProductType != "14")
  44. {
  45. rnd = GetRandom(spdb, Mer.SnNo, Mer.MerNo);
  46. }
  47. if (Utils.Instance.IsWifi(int.Parse(Mer.ProductType)))
  48. {
  49. Mer.MerNo = Mer.SnNo;
  50. PosPushDataNewHelper.Bind(Mer);
  51. if (function.CheckNull(Mer.AgentName).StartsWith("YCJG"))
  52. {
  53. PosPushDataNewHelper.Deposit(new ActivateRecord()
  54. {
  55. SeoTitle = "0.00",
  56. ProductType = Mer.ProductType,
  57. SnNo = Mer.SnNo,
  58. MerNo = Mer.SnNo,
  59. Id = Mer.Id,
  60. });
  61. }
  62. }
  63. else if (Mer.ProductType == "12" || Mer.ProductType == "30" || Mer.ProductType == "34")
  64. {
  65. MerchantRecord merInfo = new MerchantRecord();
  66. if (Mer.ProductType == "12" || Mer.ProductType == "34") merInfo = spdb.MerchantRecord.FirstOrDefault(m => m.MerNo == Mer.MerNo);
  67. if (Mer.ProductType == "30")
  68. {
  69. merInfo = spdb.MerchantRecord.FirstOrDefault(m => m.Field3 == Mer.MerNo);
  70. if (merInfo == null) merInfo = spdb.MerchantRecord.FirstOrDefault(m => m.MerNo == Mer.MerNo);
  71. }
  72. if (merInfo != null)
  73. {
  74. Mer.MerNo = merInfo.MerNo;
  75. Mer.MerIdcardNo = merInfo.LegalIdCard;
  76. Mer.MerName = merInfo.MerName;
  77. if (Mer.ProductType == "30")
  78. {
  79. Mer.MerMobile = merInfo.Field2;
  80. }
  81. if (Mer.ProductType == "34")
  82. {
  83. Mer.MerMobile = merInfo.MerNewSnNo;
  84. }
  85. if (Mer.ProductType == "30" && Mer.SeoTitle == "PAPER_CODE") Mer.ProductType = "51";
  86. PosPushDataNewHelper.Bind(Mer, rnd);
  87. }
  88. }
  89. else if (!Utils.Instance.IsHaoDa(int.Parse(Mer.ProductType)))
  90. {
  91. PosPushDataNewHelper.Bind(Mer, rnd);
  92. }
  93. }
  94. Merchants edit = spdb.Merchants.FirstOrDefault(m => m.Id == Mer.Id);
  95. if (edit != null)
  96. {
  97. edit.Status = 2;
  98. spdb.SaveChanges();
  99. }
  100. tran.Commit();
  101. }
  102. catch (Exception ex)
  103. {
  104. tran.Rollback();
  105. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Mer.Id, "同步SP商户数据到MAIN异常");
  106. }
  107. tran.Dispose();
  108. }
  109. spdb.SaveChanges();
  110. spdb.Dispose();
  111. db.SaveChanges();
  112. db.Dispose();
  113. }
  114. catch (Exception ex)
  115. {
  116. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP商户数据到MAIN异常");
  117. }
  118. Thread.Sleep(1000);
  119. }
  120. }
  121. public string GetRandom(WebCMSEntities spdb, string sn, string merno)
  122. {
  123. var list = spdb.BindRecord.Select(m => new { m.MerSnNo, m.MerNo, m.CreateTime }).Where(m => m.MerSnNo == sn && m.MerNo == merno).ToList().Select(m => m.CreateTime).Distinct().ToList();
  124. if(list.Count > 1)
  125. {
  126. return function.get_Random(5);
  127. }
  128. return "";
  129. }
  130. public void WifiSendPrize(PxcModels.WebCMSEntities db, PxcModels.PosMachinesTwo pos, PxcModels.PosMerchantInfo add)
  131. {
  132. PxcModels.WifiTradeRecord trade = db.WifiTradeRecord.FirstOrDefault(m => m.SnNo == pos.PosSn && m.Status == 0);
  133. if (trade != null)
  134. {
  135. trade.Status = 1;
  136. db.SaveChanges();
  137. decimal Prize = trade.TradeAmount * 0.15M;
  138. StatService.Instance.doActiveReward(db, add, pos, pos.BuyUserId, "", 1, Prize);
  139. if (pos.CardType < 100)
  140. {
  141. RedisDbconn.Instance.AddList("OpenRewardQueue", pos.Id.ToString());
  142. }
  143. if (pos.LeaderUserId > 0 && db.Leaders.Any(m => m.Id == pos.LeaderUserId && m.ExpiredDate > DateTime.Now) && pos.CardType < 100)
  144. {
  145. RedisDbconn.Instance.AddList("LeaderPrizeQueue", pos.Id);
  146. }
  147. if (pos.CardType < 100)
  148. {
  149. RedisDbconn.Instance.AddList("OperatePrizeQueue", pos.Id);
  150. }
  151. }
  152. }
  153. public void StartSim()
  154. {
  155. Thread th = new Thread(StartSimDo);
  156. th.IsBackground = true;
  157. th.Start();
  158. }
  159. public void StartSimDo()
  160. {
  161. while (true)
  162. {
  163. try
  164. {
  165. List<string> SaveDataBindIds = Utils.Instance.SaveDataBindIds();
  166. WebCMSEntities spdb = new WebCMSEntities();
  167. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  168. DateTime start = DateTime.Now.AddDays(-365);
  169. DateTime end = DateTime.Now.AddMinutes(-2);
  170. int StartId = 400000;
  171. var Mers = spdb.Merchants.Where(m => m.Id >= StartId && m.CreateTime >= start && m.CreateTime <= end && m.Status == 1 && m.ProductType == "14" && SaveDataBindIds.Contains(m.ProductType)).OrderByDescending(m => m.Id).Take(20).ToList();
  172. foreach (var Mer in Mers)
  173. {
  174. var tran = db.Database.BeginTransaction();
  175. try
  176. {
  177. if (Mer.Field2 != "解绑" && Mer.Field2 != "UNBIND")
  178. {
  179. if (!Utils.Instance.IsHaoDa(int.Parse(Mer.ProductType)))
  180. {
  181. PosPushDataNewHelper.Bind(Mer);
  182. }
  183. }
  184. Merchants edit = spdb.Merchants.FirstOrDefault(m => m.Id == Mer.Id);
  185. if (edit != null)
  186. {
  187. edit.Status = 2;
  188. spdb.SaveChanges();
  189. }
  190. tran.Commit();
  191. }
  192. catch (Exception ex)
  193. {
  194. tran.Rollback();
  195. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Mer.Id, "同步SP广电卡商户数据到MAIN异常");
  196. }
  197. tran.Dispose();
  198. }
  199. spdb.SaveChanges();
  200. spdb.Dispose();
  201. db.SaveChanges();
  202. db.Dispose();
  203. }
  204. catch (Exception ex)
  205. {
  206. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP广电卡商户数据到MAIN异常");
  207. }
  208. Thread.Sleep(1000);
  209. }
  210. }
  211. }
  212. }