SycnSpMerchantRecordService.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. PosPushDataNewHelper.Bind(Mer);
  38. PxcModels.MachineForMerNo machineForMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Mer.MerNo) ?? new PxcModels.MachineForMerNo();
  39. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == machineForMerNo.SnId) ?? new PxcModels.PosMachinesTwo();
  40. PxcModels.PosMerchantInfo merinfo = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  41. if(merinfo != null)
  42. {
  43. merinfo.MerIdcardNo = Mer.LegalIdCard;
  44. merinfo.MerchantName = Mer.LegalName;
  45. merinfo.MgrNo = Mer.AgentNo; //代理商号
  46. merinfo.MgrName = Mer.Field2; //终端类型
  47. db.SaveChanges();
  48. }
  49. int IsFirst = 1;
  50. if(!string.IsNullOrEmpty(Mer.LegalIdCard))
  51. {
  52. string startNo = Mer.LegalIdCard.Substring(0, 6);
  53. string endNo = Mer.LegalIdCard.Substring(Mer.LegalIdCard.Length - 4, 4).ToUpper();
  54. string Name = Mer.MerName;
  55. Name = Name.Replace("个体户", "");
  56. Name = Name.Replace("个体商户", "");
  57. Name = Name.Replace("企业户", "");
  58. Name = Name.Replace("企业商户", "");
  59. function.WriteLog(DateTime.Now.ToString() + "-----startNo:" + startNo + ",endNo:" + endNo + ",Name:" + Name, "监控机具是否互斥");
  60. PxcModels.PosMerchantInfo check = db.PosMerchantInfo.FirstOrDefault(m => m.MerIdcardNo.StartsWith(startNo) && m.MerIdcardNo.EndsWith(endNo) && m.MerchantName.Contains(Name) && m.Id != pos.BindMerchantId);
  61. if(check != null)
  62. {
  63. function.WriteLog("互斥机具---sn:" + check.KqSnNo + ",merno:" + check.KqMerNo + ",name:" + check.MerchantName, "监控机具是否互斥");
  64. IsFirst = 0;
  65. int RecordId = PublicFunction.FeeSetRecord(db, pos, check, 0.63M);
  66. string info = "{\"RecordId\":\"" + RecordId + "\",\"PosId\":\"" + pos.Id + "\",\"Fee\": \"0.63\",\"Kind\": \"2\",\"OpMan\": \""+"系统"+"\"}";
  67. RedisDbconn.Instance.AddList("SetDepositQueue", info);
  68. }
  69. }
  70. pos.IsFirst = IsFirst;
  71. db.SaveChanges();
  72. MerchantRecord edit = spdb.MerchantRecord.FirstOrDefault(m => m.Id == Mer.Id);
  73. if (edit != null)
  74. {
  75. edit.Status = 2;
  76. spdb.SaveChanges();
  77. }
  78. tran.Commit();
  79. }
  80. catch (Exception ex)
  81. {
  82. tran.Rollback();
  83. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Mer.Id, "同步SP商户记录数据到MAIN异常");
  84. }
  85. tran.Dispose();
  86. }
  87. spdb.SaveChanges();
  88. spdb.Dispose();
  89. db.SaveChanges();
  90. db.Dispose();
  91. }
  92. catch (Exception ex)
  93. {
  94. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP商户记录数据到MAIN异常");
  95. }
  96. Thread.Sleep(1000);
  97. }
  98. }
  99. }
  100. }