SycnSpMerchantRecordService.cs 5.6 KB

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