package models import ( "fmt" "gorm.io/gorm" "silkserver/constvar" "silkserver/pkg/mysqlx" ) type ( // Dict 缫丝词典 Dict struct { gorm.Model DictType constvar.DictType `gorm:"index;type:tinyint(3);not null;comment:字典类型"` //字典类型 Number string `gorm:"type:varchar(255);not null;comment:编号" json:"number"` //编号 Name string `gorm:"type:varchar(255);not null;comment:名称" json:"name"` //名称 Remark string `gorm:"type:varchar(255);not null;comment:备注" json:"remark"` //备注 } DictSearch struct { Dict Order string PageNum int PageSize int Orm *gorm.DB Keyword string DictTypes []constvar.DictType } ) func (slf *Dict) TableName() string { return "silk_dict" } func NewDictSearch() *DictSearch { return &DictSearch{Orm: mysqlx.GetDB()} } func (slf *DictSearch) SetOrm(tx *gorm.DB) *DictSearch { slf.Orm = tx return slf } func (slf *DictSearch) SetPage(page, size int) *DictSearch { slf.PageNum, slf.PageSize = page, size return slf } func (slf *DictSearch) SetOrder(order string) *DictSearch { slf.Order = order return slf } func (slf *DictSearch) SetID(id uint) *DictSearch { slf.ID = id return slf } func (slf *DictSearch) SetNumber(number string) *DictSearch { slf.Number = number return slf } func (slf *DictSearch) SetName(name string) *DictSearch { slf.Name = name return slf } func (slf *DictSearch) SetKeyword(keyword string) *DictSearch { slf.Keyword = keyword return slf } func (slf *DictSearch) SetDictType(dt constvar.DictType) *DictSearch { slf.DictType = dt return slf } func (slf *DictSearch) SetDictTypes(dts []constvar.DictType) *DictSearch { slf.DictTypes = dts return slf } func (slf *DictSearch) build() *gorm.DB { var db = slf.Orm.Table(slf.TableName()) if slf.ID != 0 { db = db.Where("id = ?", slf.ID) } if slf.Order != "" { db = db.Order(slf.Order) } if slf.Number != "" { db = db.Where("number = ?", slf.Number) } if slf.DictType > 0 { db = db.Where("dict_type = ?", slf.DictType) } if slf.Name != "" { db = db.Where("name = ?", slf.Name) } if slf.Keyword != "" { kw := "%" + slf.Keyword + "%" db = db.Where("name like ? or number like ?", kw, kw) } if len(slf.DictTypes) > 0 { db = db.Where("dict_type in (?)", slf.DictTypes) } return db } // Create 单条插入 func (slf *DictSearch) Create(record *Dict) 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 } // CreateBatch 批量插入 func (slf *DictSearch) CreateBatch(records []*Dict) error { var db = slf.build() if err := db.Create(&records).Error; err != nil { return fmt.Errorf("create batch err: %v, records: %+v", err, records) } return nil } func (slf *DictSearch) Save(record *Dict) error { var db = slf.build() if err := db.Omit("CreatedAt").Save(record).Error; err != nil { return fmt.Errorf("save err: %v, record: %+v", err, record) } return nil } func (slf *DictSearch) Update(record *Dict) error { var db = slf.build() if err := db.Updates(record).Error; err != nil { return fmt.Errorf("save err: %v, record: %+v", err, record) } return nil } func (slf *DictSearch) UpdateByMap(upMap map[string]interface{}) error { var ( db = slf.build() ) if err := db.Updates(upMap).Error; err != nil { return fmt.Errorf("update by map err: %v, upMap: %+v", err, upMap) } return nil } func (slf *DictSearch) UpdateByQuery(query string, args []interface{}, upMap map[string]interface{}) error { var ( db = slf.Orm.Table(slf.TableName()).Where(query, args...) ) if err := db.Updates(upMap).Error; err != nil { return fmt.Errorf("update by query err: %v, query: %s, args: %+v, upMap: %+v", err, query, args, upMap) } return nil } func (slf *DictSearch) Delete() error { var db = slf.build() if err := db.Unscoped().Delete(&Dict{}).Error; err != nil { return err } return nil } func (slf *DictSearch) First() (*Dict, error) { var ( record = new(Dict) db = slf.build() ) if err := db.First(record).Error; err != nil { return record, err } return record, nil } func (slf *DictSearch) Find() ([]*Dict, int64, error) { var ( records = make([]*Dict, 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 *DictSearch) FindAll() ([]*Dict, error) { var ( records = make([]*Dict, 0) db = slf.build() ) if err := db.Find(&records).Error; err != nil { return records, fmt.Errorf("find records err: %v", err) } return records, nil } // FindByQuery 指定条件查询. func (slf *DictSearch) FindByQuery(query string, args []interface{}) ([]*Dict, int64, error) { var ( records = make([]*Dict, 0) total int64 db = slf.Orm.Table(slf.TableName()).Where(query, args...) ) if err := db.Count(&total).Error; err != nil { return records, total, fmt.Errorf("find by query 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 by query records err: %v, query: %s, args: %+v", err, query, args) } return records, total, nil } // FindByQueryNotTotal 指定条件查询&不查询总条数. func (slf *DictSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*Dict, error) { var ( records = make([]*Dict, 0) db = slf.Orm.Table(slf.TableName()).Where(query, args...) ) 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, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args) } return records, nil } // InitDefaultData 初始化数据 func (slf *DictSearch) InitDefaultData() error { return nil }