func base template update
This commit is contained in:
@@ -36,30 +36,53 @@ func ExampleMgr(db *gorm.DB) *_ExampleMgr {
|
||||
// GetFromID 通过id获取内容
|
||||
func (obj *_ExampleMgr) GetFromID(id int) (results []*Example, err error) {
|
||||
err = obj.DB.Table("example").Where("id = ?", id).Find(&results).Error
|
||||
if err != nil {
|
||||
|
||||
if err == nil {
|
||||
for i := 0; i < len(results); i++ {
|
||||
var userList []User
|
||||
err = obj.DB.Where("job = ?", results[i].UserID).Find(&userList).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
results[i].UserList = userList
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetByPrimaryKey 唯一主键查找
|
||||
func (obj *_ExampleMgr) GetByPrimaryKey(id int64) (Example, error) {
|
||||
var tmp Example
|
||||
err := obj.DB.Table("example").Where("id = ?", id).Find(&tmp).Error
|
||||
return tmp, err
|
||||
func (obj *_ExampleMgr) GetByPrimaryKey(id int64) (result Example, err error) {
|
||||
err = obj.DB.Table("example").Where("id = ?", id).Find(&result).Error
|
||||
if err == nil {
|
||||
var info []User
|
||||
err = obj.DB.Where("job = ?", result.UserID).Find(&info).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
result.UserList = info
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetByPrimaryKey 批量唯一主键查找
|
||||
func (obj *_ExampleMgr) GetByPrimaryKeys(ids []int64) ([]*Example, error) {
|
||||
var tmp []*Example
|
||||
err := obj.DB.Table("example").Where("id", ids).Find(&tmp).Error
|
||||
return tmp, err
|
||||
func (obj *_ExampleMgr) GetByPrimaryKeys(ids []int64) (results []*Example, err error) {
|
||||
err = obj.DB.Table("example").Where("id IN (?)", ids).Find(&results).Error
|
||||
if err == nil {
|
||||
for i := 0; i < len(results); i++ {
|
||||
var userList []User
|
||||
err = obj.DB.Where("job = ?", results[i].UserID).Find(&userList).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
results[i].UserList = userList
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//////////////////////////option case ////////////////////////////////////////////
|
||||
|
||||
// GetByPrimaryKey 功能选项模式获取
|
||||
func (obj *_ExampleMgr) GetByOption(opts ...Option) (Example, error) {
|
||||
func (obj *_ExampleMgr) GetByOption(opts ...Option) (result Example, err error) {
|
||||
options := options{
|
||||
query: make(map[string]interface{}, len(opts)),
|
||||
}
|
||||
@@ -67,13 +90,20 @@ func (obj *_ExampleMgr) GetByOption(opts ...Option) (Example, error) {
|
||||
o.apply(&options)
|
||||
}
|
||||
|
||||
var tmp Example
|
||||
err := obj.DB.Table("example").Where(options.query).Find(&tmp).Error
|
||||
return tmp, err
|
||||
err = obj.DB.Table("example").Where(options.query).Find(&result).Error
|
||||
if err == nil {
|
||||
var info []User
|
||||
err = obj.DB.Where("job = ?", result.UserID).Find(&info).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
result.UserList = info
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetByPrimaryKey 批量功能选项模式获取
|
||||
func (obj *_ExampleMgr) GetByOptions(opts ...Option) ([]*Example, error) {
|
||||
func (obj *_ExampleMgr) GetByOptions(opts ...Option) (results []*Example, err error) {
|
||||
options := options{
|
||||
query: make(map[string]interface{}, len(opts)),
|
||||
}
|
||||
@@ -81,16 +111,31 @@ func (obj *_ExampleMgr) GetByOptions(opts ...Option) ([]*Example, error) {
|
||||
o.apply(&options)
|
||||
}
|
||||
|
||||
var tmp []*Example
|
||||
err := obj.DB.Table("example").Where(options.query).Find(&tmp).Error
|
||||
return tmp, err
|
||||
err = obj.DB.Table("example").Where(options.query).Find(&results).Error
|
||||
if err == nil {
|
||||
for i := 0; i < len(results); i++ {
|
||||
var userList []User
|
||||
err = obj.DB.Where("job = ?", results[i].UserID).Find(&userList).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
results[i].UserList = userList
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// WithID id获取
|
||||
func (obj *_ExampleMgr) WithID(id string) Option {
|
||||
func (obj *_ExampleMgr) WithID(id int64) Option {
|
||||
return optionFunc(func(o *options) {
|
||||
o.query["id"] = id
|
||||
})
|
||||
}
|
||||
|
||||
func (obj *_ExampleMgr) WithUserID(id int64) Option {
|
||||
return optionFunc(func(o *options) {
|
||||
o.query["user_id"] = id
|
||||
})
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////
|
||||
|
||||
Reference in New Issue
Block a user