ProtocolProxy.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using DBHelper;
  2. using Microsoft.EntityFrameworkCore;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Linq.Dynamic.Core;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using WCS.Core;
  11. using WCS.Entity;
  12. namespace WCS.Service
  13. {
  14. public class ProtocolProxy : ProtocolProxyBase
  15. {
  16. MethodInfo SetMethod;
  17. public ProtocolProxy(string id, WCS_DATABLOCK db, ushort start, WCS_DEVICEPROTOCOL protocol) : base(id, db, start, protocol)
  18. {
  19. SetMethod = typeof(DbContext).GetMethod("Set", new Type[] { }).MakeGenericMethod(this.ProtocolDataType);
  20. }
  21. protected override WCS_PROTOCOLDATA GetLastData()
  22. {
  23. return DB.Do(db =>
  24. {
  25. dynamic q= SetMethod.Invoke(db.Default, null);
  26. try
  27. {
  28. q = DynamicQueryableExtensions.Where(q, "ISLAST==@0 && DEVICE.CODE==@1", true, PROTOCOL.DEVICE.CODE);
  29. var res = Enumerable.FirstOrDefault(q);
  30. return res;
  31. }
  32. catch (Exception ex)
  33. {
  34. throw;
  35. }
  36. });
  37. }
  38. protected override WCS_PROTOCOLDATA SaveNewData(WCS_PROTOCOLDATA last, WCS_PROTOCOLDATA newobj,string user)
  39. {
  40. return DB.Do(db =>
  41. {
  42. if (last != null)
  43. {
  44. db.Default.Attach(last);
  45. last.ISLAST = false;
  46. }
  47. db.Default.Attach(PROTOCOL.DEVICE);
  48. newobj.DEVICE = PROTOCOL.DEVICE;
  49. newobj.ISLAST = true;
  50. newobj.UPDATETIME = DateTime.Now;
  51. newobj.UPDATEUSER = user;
  52. newobj.FRAME = LogicHandler.Frame;
  53. db.Default.Add(newobj);
  54. db.Default.SaveChanges();
  55. return newobj;
  56. });
  57. }
  58. }
  59. }