ConsumersHelper.cs 2.2 KB

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