SycnSpMerchantService.cs 9.9 KB

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