StoreChangeHistoryHelper.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using MySystem.Models;
  2. using System;
  3. using System.Linq;
  4. namespace MySystem
  5. {
  6. public class StoreChangeHistoryHelper
  7. {
  8. public readonly static StoreChangeHistoryHelper Instance = new StoreChangeHistoryHelper();
  9. private StoreChangeHistoryHelper()
  10. { }
  11. public string Start()
  12. {
  13. bool op = true;
  14. WebCMSEntities db = new WebCMSEntities();
  15. string result = "success";
  16. int total = 0;
  17. while (op)
  18. {
  19. StoreChangeHistory PopData = new StoreChangeHistory();
  20. try
  21. {
  22. //获取apserver待同步的数据,执行入库
  23. PopData = TendisDbconn.Instance.RPop<StoreChangeHistory>("Pop:StoreChangeHistory");
  24. if (PopData != null)
  25. {
  26. // StoreChangeHistory checkExist = db.StoreChangeHistory.FirstOrDefault(m => m.Id == PopData.Id);
  27. // if (checkExist != null)
  28. // {
  29. // checkExist = PopData;
  30. // }
  31. // else
  32. // {
  33. db.StoreChangeHistory.Add(PopData);
  34. // }
  35. if (total >= 20)
  36. {
  37. total = 0;
  38. db.SaveChanges();
  39. }
  40. }
  41. else
  42. {
  43. db.SaveChanges();
  44. op = false;
  45. }
  46. }
  47. catch (Exception ex)
  48. {
  49. ErrorMsg msg = new ErrorMsg();
  50. msg.Obj = PopData;
  51. msg.Time = DateTime.Now;
  52. msg.ErrorContent = ex.ToString();
  53. TendisDbconn.Instance.AddList("Pop:StoreChangeHistory:Error", msg);
  54. result = "有异常,请查看Pop:StoreChangeHistory:Error队列";
  55. }
  56. }
  57. db.Dispose();
  58. return result;
  59. }
  60. }
  61. }