golang-im聊天
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_dao.go 1.7 KiB

2 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package device
  2. import (
  3. "gim/internal/business/domain/user/model"
  4. "gim/pkg/db"
  5. "gim/pkg/gerrors"
  6. "time"
  7. "github.com/jinzhu/gorm"
  8. )
  9. type deviceDao struct{}
  10. var DeviceDao = new(deviceDao)
  11. // Save 插入一条设备信息
  12. func (*deviceDao) Save(device *Device) error {
  13. device.CreateTime = time.Now()
  14. device.UpdateTime = time.Now()
  15. err := db.DB.Save(&device).Error
  16. if err != nil {
  17. return gerrors.WrapError(err)
  18. }
  19. return nil
  20. }
  21. // Get 获取设备
  22. func (*deviceDao) Get(deviceId int64) (*Device, error) {
  23. var device = Device{Id: deviceId}
  24. err := db.DB.First(&device).Error
  25. if err != nil && err != gorm.ErrRecordNotFound {
  26. return nil, gerrors.WrapError(err)
  27. }
  28. if err == gorm.ErrRecordNotFound {
  29. return nil, nil
  30. }
  31. return &device, nil
  32. }
  33. // ListOnlineByUserId 查询用户所有的在线设备
  34. func (*deviceDao) ListOnlineByUserId(userId int64) ([]Device, error) {
  35. var devices []Device
  36. err := db.DB.Find(&devices, "user_id = ? and status = ?", userId, DeviceOnLine).Error
  37. if err != nil {
  38. return nil, gerrors.WrapError(err)
  39. }
  40. return devices, nil
  41. }
  42. // ListOnlineByConnAddr 查询用户所有的在线设备
  43. func (*deviceDao) ListOnlineByConnAddr(connAddr string) ([]Device, error) {
  44. var devices []Device
  45. err := db.DB.Find(&devices, "conn_addr = ? and status = ?", connAddr, DeviceOnLine).Error
  46. if err != nil {
  47. return nil, gerrors.WrapError(err)
  48. }
  49. return devices, nil
  50. }
  51. // UpdateStatus 更新在线状态
  52. func (*deviceDao) UpdateStatus(deviceId int64, connAddr string, status int) (int64, error) {
  53. db := db.DB.Model(&model.Device{}).Where("id = ? and conn_addr = ?", deviceId, connAddr).
  54. Update("status", status)
  55. if db.Error != nil {
  56. return 0, gerrors.WrapError(db.Error)
  57. }
  58. return db.RowsAffected, nil
  59. }