DeviceRepository.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. //查询设备详情
  31. public async Task<Device> GetDeviceById(int id)
  32. {
  33. Device device = await _context.Device.FindAsync(id);
  34. return device;
  35. }
  36. //添加详情
  37. public async Task<bool> CreateDevice(Device device)
  38. {
  39. _context.Device.Add(device);
  40. await _context.SaveChangesAsync();
  41. return true;
  42. }
  43. //更新详情
  44. public async Task<bool> UpdateDevice(Device device)
  45. {
  46. _context.Entry(device).State = EntityState.Modified;
  47. try
  48. {
  49. await _context.SaveChangesAsync();
  50. }
  51. catch (DbUpdateConcurrencyException)
  52. {
  53. throw;
  54. }
  55. return true;
  56. }
  57. }
  58. }