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.

2 年之前
2 年之前
2 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. // ListOffLineByUserId 查询用户所有的离线设备
  43. func (*deviceDao) ListOffLineByUserId(userId int64) ([]Device, error) {
  44. var devices []Device
  45. err := db.DB.Find(&devices, "user_id = ? and status = ?", userId, DeviceOnLine).Error
  46. if err != nil {
  47. return nil, gerrors.WrapError(err)
  48. }
  49. return devices, nil
  50. }
  51. // ListOnlineByConnAddr 查询用户所有的在线设备
  52. func (*deviceDao) ListOnlineByConnAddr(connAddr string) ([]Device, error) {
  53. var devices []Device
  54. err := db.DB.Find(&devices, "conn_addr = ? and status = ?", connAddr, DeviceOnLine).Error
  55. if err != nil {
  56. return nil, gerrors.WrapError(err)
  57. }
  58. return devices, nil
  59. }
  60. // UpdateStatus 更新在线状态
  61. func (*deviceDao) UpdateStatus(deviceId int64, connAddr string, status int) (int64, error) {
  62. db := db.DB.Model(&model.Device{}).Where("id = ? and conn_addr = ?", deviceId, connAddr).
  63. Update("status", status)
  64. if db.Error != nil {
  65. return 0, gerrors.WrapError(db.Error)
  66. }
  67. return db.RowsAffected, nil
  68. }