SycnSpMerchantService.cs 11 KB

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