MachineApplyHelper.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using MySystem.Models;
  2. using System;
  3. using System.Linq;
  4. namespace MySystem
  5. {
  6. public class MachineApplyHelper
  7. {
  8. public readonly static MachineApplyHelper Instance = new MachineApplyHelper();
  9. private MachineApplyHelper()
  10. { }
  11. public string Start()
  12. {
  13. bool op = true;
  14. WebCMSEntities db = new WebCMSEntities();
  15. string result = "";
  16. int total = 0;
  17. while (op)
  18. {
  19. MachineApply PopData = new MachineApply();
  20. try
  21. {
  22. //获取apserver待同步的数据,执行入库
  23. PopData = TendisDbconn.Instance.RPop<MachineApply>("Pop:MachineApply");
  24. if (PopData != null)
  25. {
  26. MachineApply checkExist = db.MachineApply.FirstOrDefault(m => m.Id == PopData.Id);
  27. if (checkExist != null)
  28. {
  29. checkExist = PopData;
  30. }
  31. else
  32. {
  33. db.MachineApply.Add(PopData);
  34. }
  35. if (total >= 20)
  36. {
  37. total = 0;
  38. db.SaveChanges();
  39. }
  40. if (string.IsNullOrEmpty(result)) result = "success";
  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. TendisDbconn.Instance.AddList("Pop:MachineApply:Error", msg);
  55. result = "有异常,请查看Pop:MachineApply:Error队列";
  56. }
  57. }
  58. db.Dispose();
  59. return result;
  60. }
  61. }
  62. }