SycnSpBindService.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MySystem.SpModels;
  5. using Library;
  6. using LitJson;
  7. namespace MySystem
  8. {
  9. public class SycnSpBindService
  10. {
  11. public readonly static SycnSpBindService Instance = new SycnSpBindService();
  12. private SycnSpBindService()
  13. { }
  14. public void Start()
  15. {
  16. function.WriteLog("1", "SycnSpServer监控");
  17. WebCMSEntities spdb = new WebCMSEntities();
  18. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  19. DateTime start = DateTime.Now.AddDays(-2);
  20. function.WriteLog("2", "SycnSpServer监控");
  21. var Binds = spdb.BindRecord.Select(m => new { m.Id, m.CreateTime, m.Status, m.MerSnNo }).Where(m => m.CreateTime >= start && m.Status == 1).OrderByDescending(m => m.Id).ToList();
  22. function.WriteLog("3:count:" + Binds.Count, "SycnSpServer监控");
  23. foreach (var Bind in Binds)
  24. {
  25. PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == Bind.MerSnNo) ?? new PxcModels.MachineForSnNo();
  26. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
  27. if (pos != null)
  28. {
  29. pos.BindingState = 1;
  30. pos.BindingTime = DateTime.Now;
  31. BothdisDbconn.Instance.SendMq("Pop:PosMachinesTwo", pos);
  32. }
  33. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  34. if (edit != null)
  35. {
  36. edit.Status = 2;
  37. }
  38. string IdBrand = pos.UserId + "_" + pos.BrandId;
  39. PxcModels.UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  40. if (MachineData == null)
  41. {
  42. MachineData = db.UserMachineData.Add(new PxcModels.UserMachineData()
  43. {
  44. IdBrand = IdBrand,
  45. }).Entity;
  46. db.SaveChanges();
  47. }
  48. MachineData.BindCount += 1;
  49. MachineData.UnBindCount -= 1;
  50. db.SaveChanges();
  51. BothdisDbconn.Instance.SendMq("Pop:UserMachineData", MachineData);
  52. function.WriteLog("BindId:" + Bind.Id, "SycnSpServer监控");
  53. }
  54. spdb.SaveChanges();
  55. spdb.Dispose();
  56. db.SaveChanges();
  57. db.Dispose();
  58. function.WriteLog(":ok:", "SycnSpServer监控");
  59. }
  60. }
  61. }