ConsumerOpenIdsHelper.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using Library;
  2. using MySystem.Models;
  3. using System;
  4. using System.Linq;
  5. namespace MySystem
  6. {
  7. public class ConsumerOpenIdsHelper
  8. {
  9. public readonly static ConsumerOpenIdsHelper Instance = new ConsumerOpenIdsHelper();
  10. private ConsumerOpenIdsHelper()
  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. ConsumerOpenIds PopData = new ConsumerOpenIds();
  21. try
  22. {
  23. //获取apserver待同步的数据,执行入库
  24. PopData = RedisDbconn.Instance.RPop<ConsumerOpenIds>("Pop:ConsumerOpenIds");
  25. if (PopData != null)
  26. {
  27. ConsumerOpenIds checkExist = db.ConsumerOpenIds.FirstOrDefault(m => m.OpenId == PopData.OpenId);
  28. if (checkExist != null)
  29. {
  30. checkExist = PopData;
  31. }
  32. else
  33. {
  34. db.ConsumerOpenIds.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. LogHelper.Instance.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(msg), "Pop:ConsumerOpenIds:Error");
  60. result = "有异常,请查看Pop:ConsumerOpenIds:Error队列";
  61. }
  62. }
  63. db.Dispose();
  64. return result;
  65. }
  66. }
  67. }