nwe
This commit is contained in:
@@ -3,7 +3,6 @@ package model
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -17,7 +16,7 @@ func UserMgr(db *gorm.DB) *_UserMgr {
|
||||
panic(fmt.Errorf("UserMgr need init by db"))
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
return &_UserMgr{_BaseMgr: &_BaseMgr{DB: db.Table("user"), isRelated: globalIsRelated, ctx: ctx, cancel: cancel, timeout: -1}}
|
||||
return &_UserMgr{_BaseMgr: &_BaseMgr{DB: db.Model(User{}), isRelated: globalIsRelated, ctx: ctx, cancel: cancel, timeout: -1}}
|
||||
}
|
||||
|
||||
// GetTableName get sql table name.获取数据库名字
|
||||
@@ -27,14 +26,14 @@ func (obj *_UserMgr) GetTableName() string {
|
||||
|
||||
// Get 获取
|
||||
func (obj *_UserMgr) Get() (result User, err error) {
|
||||
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&result).Error
|
||||
err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&result).Error
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Gets 获取批量结果
|
||||
func (obj *_UserMgr) Gets() (results []*User, err error) {
|
||||
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&results).Error
|
||||
err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&results).Error
|
||||
|
||||
return
|
||||
}
|
||||
@@ -70,7 +69,7 @@ func (obj *_UserMgr) GetByOption(opts ...Option) (result User, err error) {
|
||||
o.apply(&options)
|
||||
}
|
||||
|
||||
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&result).Error
|
||||
err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&result).Error
|
||||
|
||||
return
|
||||
}
|
||||
@@ -84,7 +83,7 @@ func (obj *_UserMgr) GetByOptions(opts ...Option) (results []*User, err error) {
|
||||
o.apply(&options)
|
||||
}
|
||||
|
||||
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&results).Error
|
||||
err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&results).Error
|
||||
|
||||
return
|
||||
}
|
||||
@@ -93,56 +92,56 @@ func (obj *_UserMgr) GetByOptions(opts ...Option) (results []*User, err error) {
|
||||
|
||||
// GetFromUserID 通过user_id获取内容
|
||||
func (obj *_UserMgr) GetFromUserID(userID int) (result User, err error) {
|
||||
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("user_id = ?", userID).Find(&result).Error
|
||||
err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`user_id` = ?", userID).Find(&result).Error
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetBatchFromUserID 批量唯一主键查找
|
||||
// GetBatchFromUserID 批量查找
|
||||
func (obj *_UserMgr) GetBatchFromUserID(userIDs []int) (results []*User, err error) {
|
||||
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("user_id IN (?)", userIDs).Find(&results).Error
|
||||
err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`user_id` IN (?)", userIDs).Find(&results).Error
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetFromName 通过name获取内容
|
||||
func (obj *_UserMgr) GetFromName(name string) (results []*User, err error) {
|
||||
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("name = ?", name).Find(&results).Error
|
||||
err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`name` = ?", name).Find(&results).Error
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetBatchFromName 批量唯一主键查找
|
||||
// GetBatchFromName 批量查找
|
||||
func (obj *_UserMgr) GetBatchFromName(names []string) (results []*User, err error) {
|
||||
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("name IN (?)", names).Find(&results).Error
|
||||
err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`name` IN (?)", names).Find(&results).Error
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetFromSex 通过sex获取内容
|
||||
func (obj *_UserMgr) GetFromSex(sex int) (results []*User, err error) {
|
||||
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("sex = ?", sex).Find(&results).Error
|
||||
err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`sex` = ?", sex).Find(&results).Error
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetBatchFromSex 批量唯一主键查找
|
||||
// GetBatchFromSex 批量查找
|
||||
func (obj *_UserMgr) GetBatchFromSex(sexs []int) (results []*User, err error) {
|
||||
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("sex IN (?)", sexs).Find(&results).Error
|
||||
err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`sex` IN (?)", sexs).Find(&results).Error
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetFromJob 通过job获取内容
|
||||
func (obj *_UserMgr) GetFromJob(job int) (results []*User, err error) {
|
||||
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("job = ?", job).Find(&results).Error
|
||||
err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`job` = ?", job).Find(&results).Error
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetBatchFromJob 批量唯一主键查找
|
||||
// GetBatchFromJob 批量查找
|
||||
func (obj *_UserMgr) GetBatchFromJob(jobs []int) (results []*User, err error) {
|
||||
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("job IN (?)", jobs).Find(&results).Error
|
||||
err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`job` IN (?)", jobs).Find(&results).Error
|
||||
|
||||
return
|
||||
}
|
||||
@@ -151,7 +150,7 @@ func (obj *_UserMgr) GetBatchFromJob(jobs []int) (results []*User, err error) {
|
||||
|
||||
// FetchByPrimaryKey primay or index 获取唯一内容
|
||||
func (obj *_UserMgr) FetchByPrimaryKey(userID int) (result User, err error) {
|
||||
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("user_id = ?", userID).Find(&result).Error
|
||||
err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`user_id` = ?", userID).Find(&result).Error
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user