SycnSpMerchantRecordService.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. if (Utils.PushTimeCheck())
  28. {
  29. List<string> SaveDataBindIds = Utils.Instance.SaveDataBindIds();
  30. WebCMSEntities spdb = new WebCMSEntities();
  31. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  32. DateTime start = DateTime.Now.AddDays(-30);
  33. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/MerchantRecordId.txt")));
  34. var Mers = spdb.MerchantRecord.Where(m => m.Id >= StartId && m.CreateTime >= start && m.Status == 1 && (m.ProductType == "12" || m.ProductType == "30") && SaveDataBindIds.Contains(m.ProductType)).OrderByDescending(m => m.Id).Take(10).ToList();
  35. foreach (var Mer in Mers)
  36. {
  37. var tran = db.Database.BeginTransaction();
  38. try
  39. {
  40. MerchantRecord edit = spdb.MerchantRecord.FirstOrDefault(m => m.Id == Mer.Id);
  41. if (edit != null)
  42. {
  43. edit.Status = 2;
  44. spdb.SaveChanges();
  45. }
  46. tran.Commit();
  47. }
  48. catch (Exception ex)
  49. {
  50. tran.Rollback();
  51. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Mer.Id, "同步SP商户记录数据到MAIN异常");
  52. }
  53. tran.Dispose();
  54. }
  55. spdb.SaveChanges();
  56. spdb.Dispose();
  57. db.SaveChanges();
  58. db.Dispose();
  59. }
  60. }
  61. catch (Exception ex)
  62. {
  63. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP商户记录数据到MAIN异常");
  64. }
  65. Thread.Sleep(1000);
  66. }
  67. }
  68. }
  69. }