SycnSpMerchantRecordService.cs 6.9 KB

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