package repo import ( "admin/apps/game/domain/entity" "admin/apps/game/model" "admin/internal/errcode" "gorm.io/gorm" ) var NoticeRepoInstance INoticeRepo type INoticeRepo interface { List(projectId int) ([]*entity.CommonResource, error) DisableAll(projectId int) error EnableAll(projectId int) error DisableSome(projectId int, ids []int) error EnableSome(projectId int, ids []int) error } func NewNoticeRepo(db *gorm.DB) INoticeRepo { return ¬iceRepoImpl{db: db} } type noticeRepoImpl struct { db *gorm.DB } func (impl *noticeRepoImpl) List(projectId int) ([]*entity.CommonResource, error) { list := make([]*model.Notice, 0) err := impl.db.Where("project_id = ? and status = ?", projectId, true).Find(&list).Error if err != nil { return nil, errcode.New(errcode.DBError, "list server error:%v", err) } list1 := make([]*entity.CommonResource, 0, len(list)) for _, v := range list { list1 = append(list1, (&entity.CommonResource{}).FromPo(v)) } return list1, nil } func (impl *noticeRepoImpl) DisableAll(projectId int) error { err := impl.db.Model(&model.Notice{}).Where("project_id = ?", projectId).Update("status", false).Error if err != nil { return errcode.New(errcode.DBError, "disable all error:%v", err) } return nil } func (impl *noticeRepoImpl) EnableAll(projectId int) error { err := impl.db.Model(&model.Notice{}).Where("project_id = ?", projectId).Update("status", true).Error if err != nil { return errcode.New(errcode.DBError, "enable all error:%v", err) } return nil } func (impl *noticeRepoImpl) DisableSome(projectId int, ids []int) error { err := impl.db.Model(&model.Notice{}).Where("project_id = ? and id in ?", projectId, ids).Update("status", false).Error if err != nil { return errcode.New(errcode.DBError, "disable all error:%v", err) } return nil } func (impl *noticeRepoImpl) EnableSome(projectId int, ids []int) error { err := impl.db.Model(&model.Notice{}).Where("project_id = ? and id in ?", projectId, ids).Update("status", true).Error if err != nil { return errcode.New(errcode.DBError, "disable all error:%v", err) } return nil }