notice.go 1.5 KB

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