You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
2.2 KiB

2 months ago
package models
import (
"fmt"
"gorm.io/gorm"
"product/backend/moo/db"
)
type LabelValue struct {
// body
Val string `gorm:"type:varchar(255)" json:"val"`
LabelID uint `gorm:"" json:"labelID"`
Order int `gorm:"" json:"order"`
State bool `gorm:"" json:"state"`
// basic models
Model
// association
}
// methods
func LabelValueRow(id uint) (LabelValue, error) {
// model
row := LabelValue{}
if err := db.DB.First(&row, id).Error; err != nil {
return row, err
}
// with association
return row, nil
}
func LabelValueRows(pager Pager, withTotal bool, keyword string, labelID uint) ([]LabelValue, int64, error) {
// query
query := db.DB.Model(&LabelValue{})
// filter
// keyID
if labelID != 0 {
query.Where("labelID = ?", labelID)
}
// 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 []LabelValue
if result := query.Find(&rows); result.Error != nil {
return rows, 0, result.Error
}
// with association
return rows, total, nil
}
func LabelValueRowDel(id uint) error {
// db tx
return db.DB.Transaction(func(tx *gorm.DB) error {
// model delete
if result := tx.Delete(&LabelValue{}, id); result.Error != nil {
return result.Error
}
// association delete
return nil
})
}
func LabelValueRowUpdate(model *LabelValue) 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 ()
func LabelValueRowInsert(model *LabelValue) error {
// default value
// 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
})
}