note.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package note
  2. import (
  3. "errors"
  4. "strings"
  5. "gorm.io/gorm"
  6. )
  7. type QorNote struct {
  8. gorm.Model
  9. UserID uint `gorm:"index"`
  10. Creator string
  11. ResourceType string `gorm:"index"`
  12. ResourceID string `gorm:"index"`
  13. Content string `sql:"size:5000"`
  14. }
  15. func (this *QorNote) BeforeCreate(tx *gorm.DB) (err error) {
  16. if strings.TrimSpace(this.Content) == "" {
  17. err = errors.New("Note cannot be empty")
  18. }
  19. return
  20. }
  21. type UserNote struct {
  22. gorm.Model
  23. UserID uint `gorm:"index"`
  24. ResourceType string `gorm:"index"`
  25. ResourceID string `gorm:"index"`
  26. Number int64
  27. }
  28. func GetUnreadNotesCount(db *gorm.DB, userID uint, resourceType, resourceID string) int64 {
  29. var total int64
  30. db.Model(&QorNote{}).Where("resource_type = ? AND resource_id = ?", resourceType, resourceID).Count(&total)
  31. if total == 0 {
  32. return 0
  33. }
  34. userNote := UserNote{}
  35. db.Where("user_id = ? AND resource_type = ? AND resource_id = ?", userID, resourceType, resourceID).First(&userNote)
  36. return total - userNote.Number
  37. }