SycnSpBindService.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. WebCMSEntities spdb = new WebCMSEntities();
  17. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  18. DateTime start = DateTime.Now.AddDays(-2);
  19. 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();
  20. foreach (var Bind in Binds)
  21. {
  22. PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == Bind.MerSnNo) ?? new PxcModels.MachineForSnNo();
  23. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
  24. if (pos != null)
  25. {
  26. pos.BindingState = 1;
  27. pos.BindingTime = DateTime.Now;
  28. PxcModels.MachineForMerNo merFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.MerNo);
  29. if (merFor == null)
  30. {
  31. merFor = db.MachineForMerNo.Add(new PxcModels.MachineForMerNo()
  32. {
  33. MerNo = Bind.MerNo,
  34. SnId = pos.Id,
  35. }).Entity;
  36. }
  37. db.SaveChanges();
  38. RedisDbconn.Instance.Set("PosMachinesTwo", pos);
  39. }
  40. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  41. if (edit != null)
  42. {
  43. edit.Status = 2;
  44. }
  45. string IdBrand = pos.UserId + "_" + pos.BrandId;
  46. PxcModels.UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  47. if (MachineData == null)
  48. {
  49. MachineData = db.UserMachineData.Add(new PxcModels.UserMachineData()
  50. {
  51. IdBrand = IdBrand,
  52. }).Entity;
  53. db.SaveChanges();
  54. }
  55. MachineData.BindCount += 1;
  56. MachineData.UnBindCount -= 1;
  57. db.SaveChanges();
  58. BothdisDbconn.Instance.SendMq("Pop:UserMachineData", MachineData);
  59. }
  60. spdb.SaveChanges();
  61. spdb.Dispose();
  62. db.SaveChanges();
  63. db.Dispose();
  64. }
  65. }
  66. }