notice.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package dao
  2. import "server/global"
  3. type Notice struct {
  4. global.GVA_MODEL
  5. Title string `json:"title" form:"title" gorm:"comment:标题"`
  6. Content string `json:"content" form:"content" gorm:"comment:内容"`
  7. Type string `json:"type" form:"type" gorm:"comment:类型"`
  8. UserId int `json:"userId" form:"userId" gorm:"comment:接收者ID"`
  9. User SysUser `json:"user" form:"user" gorm:"foreignKey:UserId;references:id;"`
  10. IsRead bool `json:"isRead" form:"isRead" gorm:"default:false;comment:是否已读"`
  11. }
  12. func (Notice) TableName() string {
  13. return "notice"
  14. }
  15. // QueryUserUnreadNotice 查询用户未读通知
  16. func QueryUserUnreadNotice(userId int) (notices []Notice, err error) {
  17. err = global.GVA_DB.Where("user_id = ? AND is_read = ?", userId, false).Find(&notices).Error
  18. return
  19. }
  20. func QueryUserAllNotice(userId int) (notices []Notice, err error) {
  21. err = global.GVA_DB.Where("user_id =?", userId).Order("id").Find(&notices).Error
  22. return
  23. }
  24. // QueryAllNotices 查询所有通知
  25. func QueryAllNotices() (notices []Notice, err error) {
  26. err = global.GVA_DB.Find(&notices).Error
  27. return
  28. }
  29. func (n Notice) CreateNotice() error {
  30. return global.GVA_DB.Create(&n).Error
  31. }
  32. func ReadNotice(id int) error {
  33. return global.GVA_DB.Model(&Notice{}).Where("id =?", id).Update("is_read", true).Error
  34. }
  35. func (n Notice) UpdateNotice() error {
  36. return global.GVA_DB.Model(&n).Updates(&n).Error
  37. }
  38. func (n Notice) DeleteNotice() error {
  39. return global.GVA_DB.Unscoped().Delete(&n).Error
  40. }