SycnSpMerchantRecordService.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 SycnSpMerchantRecordService
  11. {
  12. public readonly static SycnSpMerchantRecordService Instance = new SycnSpMerchantRecordService();
  13. private SycnSpMerchantRecordService()
  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(-30);
  30. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/MerchantRecordId.txt")));
  31. var Mers = spdb.MerchantRecord.Where(m => m.Id >= StartId && m.CreateTime >= start && m.Status == 1 && (m.ProductType == "12" || m.ProductType == "30")).OrderByDescending(m => m.Id).Take(10).ToList();
  32. foreach (var Mer in Mers)
  33. {
  34. var tran = db.Database.BeginTransaction();
  35. try
  36. {
  37. if(Mer.ProductType == "12") Mer.MerSnNo = RedisDbconn.Instance.Get<string>("sppos:" + Mer.MerNo);
  38. if(Mer.ProductType == "30")
  39. {
  40. Mer.MerSnNo = RedisDbconn.Instance.Get<string>("sppos:" + Mer.Field3);
  41. if(!db.LklMerNo.Any(m => m.ExtMerNo == Mer.Field3))
  42. {
  43. db.LklMerNo.Add(new PxcModels.LklMerNo()
  44. {
  45. ExtMerNo = Mer.Field3,
  46. MerNo = Mer.MerNo,
  47. });
  48. db.SaveChanges();
  49. }
  50. }
  51. PosPushDataNewHelper.Bind2(Mer);
  52. int PosId = 0;
  53. PxcModels.MachineForMerNo machineForMerNo = new PxcModels.MachineForMerNo();
  54. if(Mer.ProductType == "12")
  55. {
  56. machineForMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Mer.MerNo) ?? new PxcModels.MachineForMerNo();
  57. PosId = machineForMerNo.SnId;
  58. }
  59. else
  60. {
  61. machineForMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Mer.Field3);
  62. if (machineForMerNo != null)
  63. {
  64. PosId = machineForMerNo.SnId;
  65. db.MachineForMerNo.Remove(machineForMerNo);
  66. db.MachineForMerNo.Add(new PxcModels.MachineForMerNo()
  67. {
  68. MerNo = Mer.MerNo,
  69. SnId = PosId,
  70. });
  71. }
  72. else
  73. {
  74. machineForMerNo = new PxcModels.MachineForMerNo();
  75. }
  76. db.SaveChanges();
  77. }
  78. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == machineForMerNo.SnId) ?? new PxcModels.PosMachinesTwo();
  79. PxcModels.PosMerchantInfo merinfo = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  80. if(merinfo != null)
  81. {
  82. merinfo.MerIdcardNo = Mer.LegalIdCard;
  83. merinfo.MerchantName = Mer.LegalName;
  84. if(Mer.ProductType == "30")
  85. {
  86. merinfo.KqMerNo = Mer.MerNo;
  87. merinfo.MerchantNo = Mer.MerNo.Length > 16 ? Mer.MerNo.Substring(0, 16) : Mer.MerNo;
  88. }
  89. merinfo.MgrNo = Mer.AgentNo; //代理商号
  90. merinfo.MgrName = Mer.Field2; //终端类型
  91. db.SaveChanges();
  92. }
  93. int IsFirst = 1;
  94. if(!string.IsNullOrEmpty(Mer.LegalIdCard))
  95. {
  96. string startNo = Mer.LegalIdCard.Substring(0, 6);
  97. string endNo = Mer.LegalIdCard.Substring(Mer.LegalIdCard.Length - 4, 4).ToUpper();
  98. string Name = Mer.MerName;
  99. Name = Name.Replace("个体户", "");
  100. Name = Name.Replace("个体商户", "");
  101. Name = Name.Replace("企业户", "");
  102. Name = Name.Replace("企业商户", "");
  103. function.WriteLog(DateTime.Now.ToString() + "-----startNo:" + startNo + ",endNo:" + endNo + ",Name:" + Name, "监控机具是否互斥");
  104. PxcModels.PosMerchantInfo check = db.PosMerchantInfo.FirstOrDefault(m => m.MerIdcardNo.StartsWith(startNo) && m.MerIdcardNo.EndsWith(endNo) && m.MerchantName.Contains(Name) && m.Id != pos.BindMerchantId);
  105. if(check != null)
  106. {
  107. function.WriteLog("互斥机具---sn:" + check.KqSnNo + ",merno:" + check.KqMerNo + ",name:" + check.MerchantName, "监控机具是否互斥");
  108. IsFirst = 0;
  109. int RecordId = PublicFunction.FeeSetRecord(db, pos, check, 0.63M);
  110. string info = "{\"RecordId\":\"" + RecordId + "\",\"PosId\":\"" + pos.Id + "\",\"Fee\": \"0.63\",\"Kind\": \"2\",\"OpMan\": \""+"系统"+"\"}";
  111. RedisDbconn.Instance.AddList("SetDepositQueue", info);
  112. }
  113. }
  114. pos.IsFirst = IsFirst;
  115. db.SaveChanges();
  116. MerchantRecord edit = spdb.MerchantRecord.FirstOrDefault(m => m.Id == Mer.Id);
  117. if (edit != null)
  118. {
  119. edit.Status = 2;
  120. spdb.SaveChanges();
  121. }
  122. tran.Commit();
  123. }
  124. catch (Exception ex)
  125. {
  126. tran.Rollback();
  127. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Mer.Id, "同步SP商户记录数据到MAIN异常");
  128. }
  129. tran.Dispose();
  130. }
  131. spdb.SaveChanges();
  132. spdb.Dispose();
  133. db.SaveChanges();
  134. db.Dispose();
  135. }
  136. catch (Exception ex)
  137. {
  138. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP商户记录数据到MAIN异常");
  139. }
  140. Thread.Sleep(1000);
  141. }
  142. }
  143. }
  144. }