MachineApplyHelper.cs 2.1 KB

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