SycnSpBindService.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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.MerNo, 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. PxcModels.MachineForMerNo merFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.MerNo);
  32. if (merFor == null)
  33. {
  34. merFor = db.MachineForMerNo.Add(new PxcModels.MachineForMerNo()
  35. {
  36. MerNo = Bind.MerNo,
  37. SnId = pos.Id,
  38. }).Entity;
  39. }
  40. db.SaveChanges();
  41. RedisDbconn.Instance.Set("PosMachinesTwo", pos);
  42. }
  43. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  44. if (edit != null)
  45. {
  46. edit.Status = 2;
  47. }
  48. string IdBrand = pos.UserId + "_" + pos.BrandId;
  49. PxcModels.UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  50. if (MachineData == null)
  51. {
  52. MachineData = db.UserMachineData.Add(new PxcModels.UserMachineData()
  53. {
  54. IdBrand = IdBrand,
  55. }).Entity;
  56. db.SaveChanges();
  57. }
  58. MachineData.BindCount += 1;
  59. MachineData.UnBindCount -= 1;
  60. db.SaveChanges();
  61. BothdisDbconn.Instance.SendMq("Pop:UserMachineData", MachineData);
  62. function.WriteLog("BindId:" + Bind.Id, "SycnSpServer监控");
  63. }
  64. spdb.SaveChanges();
  65. spdb.Dispose();
  66. db.SaveChanges();
  67. db.Dispose();
  68. function.WriteLog(":ok:", "SycnSpServer监控");
  69. }
  70. }
  71. }