package db import ( "fmt" "gorm.io/gorm" ) // KeyPersonBase 重点人员底库 type KeyPersonBase struct { BaseModel Name string `json:"name" gorm:"type:varchar(255);"` //底库名称 Color string `json:"color" gorm:"type:varchar(255);"` //底库颜色 Level string `json:"level" gorm:"type:varchar(255);"` //底库等级 TagName string `json:"tagName" gorm:"type:varchar(255)"` //标签名称 TagType string `json:"tagType" gorm:"type:varchar(255)"` //标签类型 } func (m *KeyPersonBase) TableName() string { return "key_person_base" } type KeyPersonBaseSearch struct { KeyPersonBase Orm *gorm.DB PageNum int PageSize int Order string TagTypes []string } func NewKeyPersonBaseSearch() *KeyPersonBaseSearch { return &KeyPersonBaseSearch{ Orm: GetDB(), PageNum: 1, PageSize: 10, } } func (slf *KeyPersonBaseSearch) SetPage(page, size int) *KeyPersonBaseSearch { slf.PageNum, slf.PageSize = page, size return slf } func (slf *KeyPersonBaseSearch) SetOrder(order string) *KeyPersonBaseSearch { slf.Order = order return slf } func (slf *KeyPersonBaseSearch) SetID(id string) *KeyPersonBaseSearch { slf.ID = id return slf } func (slf *KeyPersonBaseSearch) Set(id string) *KeyPersonBaseSearch { slf.ID = id return slf } func (slf *KeyPersonBaseSearch) SetTagTypes(ids []string) *KeyPersonBaseSearch { slf.TagTypes = ids return slf } func (slf *KeyPersonBaseSearch) build() *gorm.DB { var db = slf.Orm.Table(slf.TableName()) if slf.Order != "" { db = db.Order(slf.Order) } if slf.ID != "" { db = db.Where("id = ?", slf.ID) } if slf.Name != "" { db = db.Where("name = ?", slf.Name) } if len(slf.TagTypes) != 0 { db = db.Where("tag_type in ?", slf.TagTypes) } return db } func (slf *KeyPersonBaseSearch) First() (*KeyPersonBase, error) { var ( record = new(KeyPersonBase) db = slf.build() ) if err := db.First(record).Error; err != nil { return record, err } return record, nil } func (slf *KeyPersonBaseSearch) Find() ([]*KeyPersonBase, int64, error) { var ( records = make([]*KeyPersonBase, 0) total int64 db = slf.build() ) if err := db.Count(&total).Error; err != nil { return records, total, fmt.Errorf("find count err: %v", err) } if slf.PageNum*slf.PageSize > 0 { db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize) } if err := db.Find(&records).Error; err != nil { return records, total, fmt.Errorf("find records err: %v", err) } return records, total, nil } func (slf *KeyPersonBaseSearch) FindAll() ([]*KeyPersonBase, error) { var ( records = make([]*KeyPersonBase, 0) db = slf.build() ) if err := db.Find(&records).Error; err != nil { return records, fmt.Errorf("find records err: %v", err) } return records, nil } func (slf *KeyPersonBaseSearch) Count() int64 { var ( count int64 db = slf.build() ) if err := db.Count(&count).Error; err != nil { return count } return count } func (slf *KeyPersonBaseSearch) Create(record *KeyPersonBase) error { var db = slf.build() if err := db.Create(record).Error; err != nil { return fmt.Errorf("create err: %v, record: %+v", err, record) } return nil } func (slf *KeyPersonBaseSearch) Update(record *KeyPersonBase) error { var db = slf.build() if err := db.Updates(record).Error; err != nil { return fmt.Errorf("update err: %v, record: %+v", err, record) } return nil } func (slf *KeyPersonBaseSearch) Delete() error { var db = slf.build() return db.Delete(&KeyPersonBase{}).Error }