SycnSpMerchantService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. PosPushDataNewHelper.Bind(Mer, rnd);
  86. }
  87. }
  88. else if (!Utils.Instance.IsHaoDa(int.Parse(Mer.ProductType)))
  89. {
  90. PosPushDataNewHelper.Bind(Mer, rnd);
  91. }
  92. }
  93. Merchants edit = spdb.Merchants.FirstOrDefault(m => m.Id == Mer.Id);
  94. if (edit != null)
  95. {
  96. edit.Status = 2;
  97. spdb.SaveChanges();
  98. }
  99. tran.Commit();
  100. }
  101. catch (Exception ex)
  102. {
  103. tran.Rollback();
  104. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Mer.Id, "同步SP商户数据到MAIN异常");
  105. }
  106. tran.Dispose();
  107. }
  108. spdb.SaveChanges();
  109. spdb.Dispose();
  110. db.SaveChanges();
  111. db.Dispose();
  112. }
  113. catch (Exception ex)
  114. {
  115. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP商户数据到MAIN异常");
  116. }
  117. Thread.Sleep(1000);
  118. }
  119. }
  120. public string GetRandom(WebCMSEntities spdb, string sn, string merno)
  121. {
  122. 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();
  123. if(list.Count > 1)
  124. {
  125. return function.get_Random(5);
  126. }
  127. return "";
  128. }
  129. public void WifiSendPrize(PxcModels.WebCMSEntities db, PxcModels.PosMachinesTwo pos, PxcModels.PosMerchantInfo add)
  130. {
  131. PxcModels.WifiTradeRecord trade = db.WifiTradeRecord.FirstOrDefault(m => m.SnNo == pos.PosSn && m.Status == 0);
  132. if (trade != null)
  133. {
  134. trade.Status = 1;
  135. db.SaveChanges();
  136. decimal Prize = trade.TradeAmount * 0.15M;
  137. StatService.Instance.doActiveReward(db, add, pos, pos.BuyUserId, "", 1, Prize);
  138. if (pos.CardType < 100)
  139. {
  140. RedisDbconn.Instance.AddList("OpenRewardQueue", pos.Id.ToString());
  141. }
  142. if (pos.LeaderUserId > 0 && db.Leaders.Any(m => m.Id == pos.LeaderUserId && m.ExpiredDate > DateTime.Now) && pos.CardType < 100)
  143. {
  144. RedisDbconn.Instance.AddList("LeaderPrizeQueue", pos.Id);
  145. }
  146. if (pos.CardType < 100)
  147. {
  148. RedisDbconn.Instance.AddList("OperatePrizeQueue", pos.Id);
  149. }
  150. }
  151. }
  152. public void StartSim()
  153. {
  154. Thread th = new Thread(StartSimDo);
  155. th.IsBackground = true;
  156. th.Start();
  157. }
  158. public void StartSimDo()
  159. {
  160. while (true)
  161. {
  162. try
  163. {
  164. List<string> SaveDataBindIds = Utils.Instance.SaveDataBindIds();
  165. WebCMSEntities spdb = new WebCMSEntities();
  166. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  167. DateTime start = DateTime.Now.AddDays(-365);
  168. DateTime end = DateTime.Now.AddMinutes(-2);
  169. int StartId = 400000;
  170. 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();
  171. foreach (var Mer in Mers)
  172. {
  173. var tran = db.Database.BeginTransaction();
  174. try
  175. {
  176. if (Mer.Field2 != "解绑" && Mer.Field2 != "UNBIND")
  177. {
  178. if (!Utils.Instance.IsHaoDa(int.Parse(Mer.ProductType)))
  179. {
  180. PosPushDataNewHelper.Bind(Mer);
  181. }
  182. }
  183. Merchants edit = spdb.Merchants.FirstOrDefault(m => m.Id == Mer.Id);
  184. if (edit != null)
  185. {
  186. edit.Status = 2;
  187. spdb.SaveChanges();
  188. }
  189. tran.Commit();
  190. }
  191. catch (Exception ex)
  192. {
  193. tran.Rollback();
  194. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Mer.Id, "同步SP广电卡商户数据到MAIN异常");
  195. }
  196. tran.Dispose();
  197. }
  198. spdb.SaveChanges();
  199. spdb.Dispose();
  200. db.SaveChanges();
  201. db.Dispose();
  202. }
  203. catch (Exception ex)
  204. {
  205. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP广电卡商户数据到MAIN异常");
  206. }
  207. Thread.Sleep(1000);
  208. }
  209. }
  210. }
  211. }