StoreChangeHistoryHelper.cs 2.2 KB

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