package models import ( "fmt" "gorm.io/gorm" "product/backend/moo/db" ) type Subject struct { // body Title string `gorm:"type:varchar(255)" json:"title"` Intro string `gorm:"type:varchar(255)" json:"intro"` Logo string `gorm:"type:varchar(255)" json:"logo"` Type string `gorm:"type:enum('common', 'special')" json:"type"` Remark string `gorm:"type:varchar(1023)" json:"remark"` Order int `gorm:"" json:"order"` State bool `gorm:"" json:"state"` // basic models Model // association KnowledgeGroups []KnowledgeGroup `gorm:"foreignKey:SubjectID" json:"knowledgeGroups"` DirectiveGroups []DirectiveGroup `gorm:"foreignKey:SubjectID" json:"directiveGroups"` ProductGroups []ProductGroup `gorm:"foreignKey:SubjectID" json:"productGroups"` LessonTypes []LessonType `gorm:"foreignKey:SubjectID" json:"lessonTypes"` } // methods func SubjectRow(id uint, withKnowledgeGroup, withDirectiveGroup, withProductGroup, withLessonType bool) (Subject, error) { // model row := Subject{} if err := db.DB.First(&row, id).Error; err != nil { return row, err } // with association if withKnowledgeGroup { _ = db.DB.Model(&row).Association("KnowledgeGroups").Find(&row.KnowledgeGroups) } if withDirectiveGroup { _ = db.DB.Model(&row).Association("DirectiveGroups").Find(&row.DirectiveGroups) } if withProductGroup { _ = db.DB.Model(&row).Association("ProductGroups").Find(&row.ProductGroups) } if withLessonType { _ = db.DB.Model(&row).Association("LessonTypes").Find(&row.LessonTypes) } return row, nil } func SubjectRows(pager Pager, withTotal bool, keyword string, sType string, withKnowledgeGroup, withDirectiveGroup, withProductGroup, withLessonType bool) ([]Subject, int64, error) { // query query := db.DB.Model(&Subject{}) // filter // keyword if sType != "" { query.Where("type == ?", sType) } // keyword if keyword != "" { query.Where("title LIKE ?", keyword+"%") } // fetch total var total int64 if withTotal { query.Count(&total) } // order query.Order(fmt.Sprintf("`%s` %s", pager.SortField, pager.SortOrder)) // # offset limit offset := -1 // 不限制 if pager.PageSize > 0 { offset = (pager.PageIndex - 1) * pager.PageSize } query.Offset(offset).Limit(pager.PageSize) // select query.Select("*") // fetch var rows []Subject if result := query.Find(&rows); result.Error != nil { return rows, 0, result.Error } // with association for i, _ := range rows { if withKnowledgeGroup { _ = db.DB.Model(&rows[i]).Association("KnowledgeGroups").Find(&rows[i].KnowledgeGroups) } if withDirectiveGroup { _ = db.DB.Model(&rows[i]).Association("DirectiveGroups").Find(&rows[i].DirectiveGroups) } if withProductGroup { _ = db.DB.Model(&rows[i]).Association("ProductGroups").Find(&rows[i].ProductGroups) } if withLessonType { _ = db.DB.Model(&rows[i]).Association("LessonTypes").Find(&rows[i].LessonTypes) } } return rows, total, nil } func SubjectRowDel(id uint) error { // db tx return db.DB.Transaction(func(tx *gorm.DB) error { // model delete if result := tx.Delete(&Subject{}, id); result.Error != nil { return result.Error } // association delete return nil }) } func SubjectRowUpdate(model *Subject) error { // db tx return db.DB.Transaction(func(tx *gorm.DB) error { // model update if result := tx.Updates(&model); result.Error != nil { return result.Error } // association update return nil }) } // default value const ( subjectDefaultType = "common" ) func SubjectRowInsert(model *Subject) error { // default value if model.Type == "" { model.Type = subjectDefaultType } // db tx return db.DB.Transaction(func(tx *gorm.DB) error { // model create if result := tx.Create(&model); result.Error != nil { return result.Error } // association create return nil }) }