This commit is contained in:
xxj
2021-06-13 21:55:36 +08:00
parent 96794856b6
commit e3e175313e
9 changed files with 233 additions and 58 deletions

View File

@@ -3,7 +3,6 @@ package model
import (
"context"
"fmt"
"gorm.io/gorm"
)
@@ -17,7 +16,7 @@ func AccountMgr(db *gorm.DB) *_AccountMgr {
panic(fmt.Errorf("AccountMgr need init by db"))
}
ctx, cancel := context.WithCancel(context.Background())
return &_AccountMgr{_BaseMgr: &_BaseMgr{DB: db.Table("account"), isRelated: globalIsRelated, ctx: ctx, cancel: cancel, timeout: -1}}
return &_AccountMgr{_BaseMgr: &_BaseMgr{DB: db.Model(Account{}), isRelated: globalIsRelated, ctx: ctx, cancel: cancel, timeout: -1}}
}
// GetTableName get sql table name.获取数据库名字
@@ -27,7 +26,7 @@ func (obj *_AccountMgr) GetTableName() string {
// Get 获取
func (obj *_AccountMgr) Get() (result Account, 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
if err == nil && obj.isRelated {
if err = obj.New().Table("user").Where("user_id = ?", result.UserID).Find(&result.User).Error; err != nil { //
if err != gorm.ErrRecordNotFound { // 非 没找到
@@ -41,7 +40,7 @@ func (obj *_AccountMgr) Get() (result Account, err error) {
// Gets 获取批量结果
func (obj *_AccountMgr) Gets() (results []*Account, 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
if err == nil && obj.isRelated {
for i := 0; i < len(results); i++ {
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
@@ -90,7 +89,7 @@ func (obj *_AccountMgr) GetByOption(opts ...Option) (result Account, 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
if err == nil && obj.isRelated {
if err = obj.New().Table("user").Where("user_id = ?", result.UserID).Find(&result.User).Error; err != nil { //
if err != gorm.ErrRecordNotFound { // 非 没找到
@@ -111,7 +110,7 @@ func (obj *_AccountMgr) GetByOptions(opts ...Option) (results []*Account, err er
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
if err == nil && obj.isRelated {
for i := 0; i < len(results); i++ {
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
@@ -128,7 +127,7 @@ func (obj *_AccountMgr) GetByOptions(opts ...Option) (results []*Account, err er
// GetFromID 通过id获取内容
func (obj *_AccountMgr) GetFromID(id int) (result Account, err error) {
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("id = ?", id).Find(&result).Error
err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`id` = ?", id).Find(&result).Error
if err == nil && obj.isRelated {
if err = obj.New().Table("user").Where("user_id = ?", result.UserID).Find(&result.User).Error; err != nil { //
if err != gorm.ErrRecordNotFound { // 非 没找到
@@ -140,9 +139,9 @@ func (obj *_AccountMgr) GetFromID(id int) (result Account, err error) {
return
}
// GetBatchFromID 批量唯一主键查找
// GetBatchFromID 批量查找
func (obj *_AccountMgr) GetBatchFromID(ids []int) (results []*Account, err error) {
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("id IN (?)", ids).Find(&results).Error
err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`id` IN (?)", ids).Find(&results).Error
if err == nil && obj.isRelated {
for i := 0; i < len(results); i++ {
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
@@ -157,7 +156,7 @@ func (obj *_AccountMgr) GetBatchFromID(ids []int) (results []*Account, err error
// GetFromAccountID 通过account_id获取内容
func (obj *_AccountMgr) GetFromAccountID(accountID int) (results []*Account, err error) {
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("account_id = ?", accountID).Find(&results).Error
err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`account_id` = ?", accountID).Find(&results).Error
if err == nil && obj.isRelated {
for i := 0; i < len(results); i++ {
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
@@ -170,9 +169,9 @@ func (obj *_AccountMgr) GetFromAccountID(accountID int) (results []*Account, err
return
}
// GetBatchFromAccountID 批量唯一主键查找
// GetBatchFromAccountID 批量查找
func (obj *_AccountMgr) GetBatchFromAccountID(accountIDs []int) (results []*Account, err error) {
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("account_id IN (?)", accountIDs).Find(&results).Error
err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`account_id` IN (?)", accountIDs).Find(&results).Error
if err == nil && obj.isRelated {
for i := 0; i < len(results); i++ {
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
@@ -187,7 +186,7 @@ func (obj *_AccountMgr) GetBatchFromAccountID(accountIDs []int) (results []*Acco
// GetFromUserID 通过user_id获取内容
func (obj *_AccountMgr) GetFromUserID(userID int) (results []*Account, err error) {
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("user_id = ?", userID).Find(&results).Error
err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`user_id` = ?", userID).Find(&results).Error
if err == nil && obj.isRelated {
for i := 0; i < len(results); i++ {
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
@@ -200,9 +199,9 @@ func (obj *_AccountMgr) GetFromUserID(userID int) (results []*Account, err error
return
}
// GetBatchFromUserID 批量唯一主键查找
// GetBatchFromUserID 批量查找
func (obj *_AccountMgr) GetBatchFromUserID(userIDs []int) (results []*Account, 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
if err == nil && obj.isRelated {
for i := 0; i < len(results); i++ {
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
@@ -217,7 +216,7 @@ func (obj *_AccountMgr) GetBatchFromUserID(userIDs []int) (results []*Account, e
// GetFromType 通过type获取内容
func (obj *_AccountMgr) GetFromType(_type int) (results []*Account, err error) {
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("type = ?", _type).Find(&results).Error
err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`type` = ?", _type).Find(&results).Error
if err == nil && obj.isRelated {
for i := 0; i < len(results); i++ {
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
@@ -230,9 +229,9 @@ func (obj *_AccountMgr) GetFromType(_type int) (results []*Account, err error) {
return
}
// GetBatchFromType 批量唯一主键查找
// GetBatchFromType 批量查找
func (obj *_AccountMgr) GetBatchFromType(_types []int) (results []*Account, err error) {
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("type IN (?)", _types).Find(&results).Error
err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`type` IN (?)", _types).Find(&results).Error
if err == nil && obj.isRelated {
for i := 0; i < len(results); i++ {
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
@@ -247,7 +246,7 @@ func (obj *_AccountMgr) GetBatchFromType(_types []int) (results []*Account, err
// GetFromName 通过name获取内容
func (obj *_AccountMgr) GetFromName(name string) (results []*Account, 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
if err == nil && obj.isRelated {
for i := 0; i < len(results); i++ {
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
@@ -260,9 +259,9 @@ func (obj *_AccountMgr) GetFromName(name string) (results []*Account, err error)
return
}
// GetBatchFromName 批量唯一主键查找
// GetBatchFromName 批量查找
func (obj *_AccountMgr) GetBatchFromName(names []string) (results []*Account, 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
if err == nil && obj.isRelated {
for i := 0; i < len(results); i++ {
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
@@ -279,7 +278,7 @@ func (obj *_AccountMgr) GetBatchFromName(names []string) (results []*Account, er
// FetchByPrimaryKey primay or index 获取唯一内容
func (obj *_AccountMgr) FetchByPrimaryKey(id int) (result Account, err error) {
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("id = ?", id).Find(&result).Error
err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`id` = ?", id).Find(&result).Error
if err == nil && obj.isRelated {
if err = obj.New().Table("user").Where("user_id = ?", result.UserID).Find(&result.User).Error; err != nil { //
if err != gorm.ErrRecordNotFound { // 非 没找到
@@ -293,7 +292,7 @@ func (obj *_AccountMgr) FetchByPrimaryKey(id int) (result Account, err error) {
// FetchUniqueIndexByAccount primay or index 获取唯一内容
func (obj *_AccountMgr) FetchUniqueIndexByAccount(accountID int, userID int) (result Account, err error) {
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("account_id = ? AND user_id = ?", accountID, userID).Find(&result).Error
err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`account_id` = ? AND `user_id` = ?", accountID, userID).Find(&result).Error
if err == nil && obj.isRelated {
if err = obj.New().Table("user").Where("user_id = ?", result.UserID).Find(&result.User).Error; err != nil { //
if err != gorm.ErrRecordNotFound { // 非 没找到
@@ -307,7 +306,7 @@ func (obj *_AccountMgr) FetchUniqueIndexByAccount(accountID int, userID int) (re
// FetchIndexByTp 获取多个内容
func (obj *_AccountMgr) FetchIndexByTp(userID int, _type int) (results []*Account, err error) {
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("user_id = ? AND type = ?", userID, _type).Find(&results).Error
err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`user_id` = ? AND `type` = ?", userID, _type).Find(&results).Error
if err == nil && obj.isRelated {
for i := 0; i < len(results); i++ {
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //