You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

device_repo.go 1.5 KiB

1 month ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package device
  2. type deviceRepo struct{}
  3. var DeviceRepo = new(deviceRepo)
  4. // Get 获取设备
  5. func (*deviceRepo) Get(deviceId int64) (*Device, error) {
  6. device, err := DeviceDao.Get(deviceId)
  7. if err != nil {
  8. return nil, err
  9. }
  10. return device, nil
  11. }
  12. // Save 保存设备信息
  13. func (*deviceRepo) Save(device *Device) error {
  14. err := DeviceDao.Save(device)
  15. if err != nil {
  16. return err
  17. }
  18. if device.UserId != 0 {
  19. err = UserDeviceCache.Del(device.UserId)
  20. if err != nil {
  21. return err
  22. }
  23. }
  24. return nil
  25. }
  26. // ListOnlineByUserId 获取用户的所有在线设备
  27. func (*deviceRepo) ListOnlineByUserId(userId int64) ([]Device, error) {
  28. devices, err := UserDeviceCache.Get(userId)
  29. if err != nil {
  30. return nil, err
  31. }
  32. if devices != nil {
  33. return devices, nil
  34. }
  35. devices, err = DeviceDao.ListOnlineByUserId(userId)
  36. if err != nil {
  37. return nil, err
  38. }
  39. err = UserDeviceCache.Set(userId, devices)
  40. if err != nil {
  41. return nil, err
  42. }
  43. return devices, nil
  44. }
  45. // ListOnlineByConnAddr 查询用户所有的在线设备
  46. func (*deviceRepo) ListOnlineByConnAddr(connAddr string) ([]Device, error) {
  47. return DeviceDao.ListOnlineByConnAddr(connAddr)
  48. }
  49. // UpdateStatusOffline 更新设备为离线状态
  50. func (*deviceRepo) UpdateStatusOffline(device Device) error {
  51. affected, err := DeviceDao.UpdateStatus(device.Id, device.ConnAddr, DeviceOffLine)
  52. if err != nil {
  53. return err
  54. }
  55. if affected == 1 && device.UserId != 0 {
  56. err = UserDeviceCache.Del(device.UserId)
  57. if err != nil {
  58. return err
  59. }
  60. }
  61. return nil
  62. }