SycnSpMerchantRecordService.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. merinfo.MgrNo = Mer.AgentNo; //代理商号
  45. merinfo.MgrName = Mer.Field2; //终端类型
  46. db.SaveChanges();
  47. }
  48. int IsFirst = 1;
  49. if(!string.IsNullOrEmpty(Mer.LegalIdCard))
  50. {
  51. string startNo = Mer.LegalIdCard.Substring(0, 6);
  52. string endNo = Mer.LegalIdCard.Substring(Mer.LegalIdCard.Length - 4, 4).ToUpper();
  53. string Name = Mer.MerName;
  54. if (Mer.ProductType == "2")
  55. {
  56. if (Name.Contains("-"))
  57. {
  58. Name = Name.Split('-')[1];
  59. }
  60. else if (Name.Contains("_"))
  61. {
  62. Name = Name.Split('_')[1];
  63. }
  64. }
  65. else if (Mer.ProductType == "4" || Mer.ProductType == "8" || Mer.ProductType == "9")
  66. {
  67. Name = Mer.SeoTitle;
  68. }
  69. else if (Mer.ProductType == "10")
  70. {
  71. Name = Name.Replace("*", "");
  72. }
  73. Name = Name.Replace("个体户", "");
  74. Name = Name.Replace("个体商户", "");
  75. Name = Name.Replace("企业户", "");
  76. Name = Name.Replace("企业商户", "");
  77. function.WriteLog(DateTime.Now.ToString() + "-----startNo:" + startNo + ",endNo:" + endNo + ",Name:" + Name, "监控机具是否互斥");
  78. PxcModels.PosMerchantInfo check = db.PosMerchantInfo.FirstOrDefault(m => m.MerIdcardNo.StartsWith(startNo) && m.MerIdcardNo.EndsWith(endNo) && m.MerchantName.Contains(Name) && m.Id != pos.BindMerchantId);
  79. if(check != null)
  80. {
  81. function.WriteLog("互斥机具---sn:" + check.KqSnNo + ",merno:" + check.KqMerNo + ",name:" + check.MerchantName, "监控机具是否互斥");
  82. IsFirst = 0;
  83. int RecordId = PublicFunction.FeeSetRecord(db, pos, check, 0.63M);
  84. string info = "{\"RecordId\":\"" + RecordId + "\",\"PosId\":\"" + pos.Id + "\",\"Fee\": \"0.63\",\"Kind\": \"2\",\"OpMan\": \""+"系统"+"\"}";
  85. RedisDbconn.Instance.AddList("SetDepositQueue", info);
  86. }
  87. }
  88. pos.IsFirst = IsFirst;
  89. db.SaveChanges();
  90. // if(pos.PrizeParams == "0")
  91. // {
  92. // int RecordId = PublicFunction.FeeSetRecord(db, pos, merinfo, 0.63M);
  93. // string info = "{\"RecordId\":\"" + RecordId + "\",\"PosId\":\"" + pos.Id + "\",\"Fee\": \"0.63\",\"Kind\": \"2\",\"OpMan\": \""+"系统"+"\"}";
  94. // RedisDbconn.Instance.AddList("SetDepositQueue", info);
  95. // }
  96. MerchantRecord edit = spdb.MerchantRecord.FirstOrDefault(m => m.Id == Mer.Id);
  97. if (edit != null)
  98. {
  99. edit.Status = 2;
  100. spdb.SaveChanges();
  101. }
  102. tran.Commit();
  103. }
  104. catch (Exception ex)
  105. {
  106. tran.Rollback();
  107. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Mer.Id, "同步SP商户记录数据到MAIN异常");
  108. }
  109. tran.Dispose();
  110. }
  111. spdb.SaveChanges();
  112. spdb.Dispose();
  113. db.SaveChanges();
  114. db.Dispose();
  115. }
  116. catch (Exception ex)
  117. {
  118. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP商户记录数据到MAIN异常");
  119. }
  120. Thread.Sleep(10000);
  121. }
  122. }
  123. }
  124. }