SycnSpMerchantRecordService.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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(-1);
  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").OrderByDescending(m => m.Id).Take(10).ToList();
  32. foreach (var Mer in Mers)
  33. {
  34. var tran = db.Database.BeginTransaction();
  35. try
  36. {
  37. PxcModels.MachineForMerNo machineForMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Mer.MerNo) ?? new PxcModels.MachineForMerNo();
  38. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == machineForMerNo.SnId) ?? new PxcModels.PosMachinesTwo();
  39. PxcModels.PosMerchantInfo merinfo = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  40. if(merinfo != null)
  41. {
  42. merinfo.MerIdcardNo = Mer.LegalIdCard;
  43. merinfo.MerchantName = Mer.LegalName;
  44. }
  45. int IsFirst = 1;
  46. if(!string.IsNullOrEmpty(Mer.LegalIdCard))
  47. {
  48. string startNo = Mer.LegalIdCard.Substring(0, 6);
  49. string endNo = Mer.LegalIdCard.Substring(Mer.LegalIdCard.Length - 4, 4).ToUpper();
  50. string Name = Mer.MerName;
  51. if (Mer.ProductType == "2")
  52. {
  53. if (Name.Contains("-"))
  54. {
  55. Name = Name.Split('-')[1];
  56. }
  57. else if (Name.Contains("_"))
  58. {
  59. Name = Name.Split('_')[1];
  60. }
  61. }
  62. else if (Mer.ProductType == "4" || Mer.ProductType == "8" || Mer.ProductType == "9")
  63. {
  64. Name = Mer.SeoTitle;
  65. }
  66. else if (Mer.ProductType == "10")
  67. {
  68. Name = Name.Replace("*", "");
  69. }
  70. Name = Name.Replace("个体户", "");
  71. Name = Name.Replace("个体商户", "");
  72. Name = Name.Replace("企业户", "");
  73. Name = Name.Replace("企业商户", "");
  74. function.WriteLog(DateTime.Now.ToString() + "-----startNo:" + startNo + ",endNo:" + endNo + ",Name:" + Name, "监控机具是否互斥");
  75. PxcModels.PosMerchantInfo check = db.PosMerchantInfo.FirstOrDefault(m => m.MerIdcardNo.StartsWith(startNo) && m.MerIdcardNo.EndsWith(endNo) && m.MerchantName.Contains(Name) && m.Id != pos.BindMerchantId);
  76. if(check != null)
  77. {
  78. function.WriteLog("互斥机具---sn:" + check.KqSnNo + ",merno:" + check.KqMerNo + ",name:" + check.MerchantName, "监控机具是否互斥");
  79. IsFirst = 0;
  80. }
  81. }
  82. pos.IsFirst = IsFirst;
  83. MerchantRecord edit = spdb.MerchantRecord.FirstOrDefault(m => m.Id == Mer.Id);
  84. if (edit != null)
  85. {
  86. edit.Status = 2;
  87. spdb.SaveChanges();
  88. }
  89. tran.Commit();
  90. }
  91. catch (Exception ex)
  92. {
  93. tran.Rollback();
  94. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Mer.Id, "同步SP商户记录数据到MAIN异常");
  95. }
  96. tran.Dispose();
  97. }
  98. spdb.SaveChanges();
  99. spdb.Dispose();
  100. db.SaveChanges();
  101. db.Dispose();
  102. }
  103. catch (Exception ex)
  104. {
  105. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP商户记录数据到MAIN异常");
  106. }
  107. Thread.Sleep(10000);
  108. }
  109. }
  110. }
  111. }