DeviceRepository.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 
  2. using IMCS.CCS.Entitys;
  3. using Microsoft.EntityFrameworkCore;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. namespace IMCS.CCS.Repository
  9. {
  10. public class DeviceRepository: IDeviceRepository
  11. {
  12. private readonly CcsContext _context;
  13. public DeviceRepository(CcsContext context)
  14. {
  15. _context = context;
  16. }
  17. //查询在线设备列表
  18. public List<Device> GetDeviceList()
  19. {
  20. IQueryable<Device> devices = _context.Device;
  21. devices = devices.Where(x => x.State && x.UseState && !x.ProtocolType.Equals(ProtocalTypeEnum.FTP.ToString()));
  22. return devices.ToList();
  23. }
  24. public List<Device> GetDeviceAllList()
  25. {
  26. IQueryable<Device> devices = _context.Device;
  27. devices = devices.Where(x => x.UseState);
  28. return devices.ToList();
  29. }
  30. public List<Device> GetDevices()
  31. {
  32. IQueryable<Device> devices = _context.Device;
  33. return devices.ToList();
  34. }
  35. //查询设备详情
  36. public async Task<Device> GetDeviceById(int id)
  37. {
  38. Device device = await _context.Device.FindAsync(id);
  39. return device;
  40. }
  41. //添加详情
  42. public async Task<bool> CreateDevice(Device device)
  43. {
  44. _context.Device.Add(device);
  45. await _context.SaveChangesAsync();
  46. return true;
  47. }
  48. //更新详情
  49. public async Task<bool> UpdateDevice(Device device)
  50. {
  51. _context.Entry(device).State = EntityState.Modified;
  52. try
  53. {
  54. await _context.SaveChangesAsync();
  55. }
  56. catch (DbUpdateConcurrencyException)
  57. {
  58. throw;
  59. }
  60. return true;
  61. }
  62. }
  63. }