优化代码

This commit is contained in:
xing 2022-11-04 17:58:31 +08:00
parent c759dfcdf6
commit c70bcf5895
5 changed files with 14 additions and 9 deletions

View File

@ -62,9 +62,9 @@ func GetCommentByIds(ctx context.Context, ids []uint64) ([]models.WpComments, er
func getCommentByIds(args ...any) (map[uint64]models.WpComments, error) { func getCommentByIds(args ...any) (map[uint64]models.WpComments, error) {
ids := args[0].([]uint64) ids := args[0].([]uint64)
m := make(map[uint64]models.WpComments) m := make(map[uint64]models.WpComments)
r, err := models.Find[models.WpComments](models.SqlBuilder{ r, err := models.SimpleFind[models.WpComments](models.SqlBuilder{
{"comment_ID", "in", ""}, {"comment_approved", "1"}, {"comment_ID", "in", ""}, {"comment_approved", "1"},
}, "*", "", nil, nil, nil, 0, helper.SliceMap(ids, helper.ToAny[uint64])) }, "*", helper.SliceMap(ids, helper.ToAny[uint64]))
if err != nil { if err != nil {
return m, err return m, err
} }

View File

@ -109,7 +109,7 @@ func searchPostIds(args ...any) (ids PostIds, err error) {
} }
func getMaxPostId(...any) ([]uint64, error) { func getMaxPostId(...any) ([]uint64, error) {
r, err := models.Find[models.WpPosts](models.SqlBuilder{{"post_type", "post"}, {"post_status", "publish"}}, "max(ID) ID", "", nil, nil, nil, 0) r, err := models.SimpleFind[models.WpPosts](models.SqlBuilder{{"post_type", "post"}, {"post_status", "publish"}}, "max(ID) ID")
var id uint64 var id uint64
if len(r) > 0 { if len(r) > 0 {
id = r[0].Id id = r[0].Id

View File

@ -9,7 +9,7 @@ import (
func getUsers(...any) (m map[uint64]models.WpUsers, err error) { func getUsers(...any) (m map[uint64]models.WpUsers, err error) {
m = make(map[uint64]models.WpUsers) m = make(map[uint64]models.WpUsers)
r, err := models.Find[models.WpUsers](nil, "*", "", nil, nil, nil, 0) r, err := models.SimpleFind[models.WpUsers](nil, "*")
for _, user := range r { for _, user := range r {
m[user.Id] = user m[user.Id] = user
} }

View File

@ -22,14 +22,14 @@ func InitOptions() error {
} }
func InitTerms() (err error) { func InitTerms() (err error) {
terms, err := Find[WpTerms](nil, "*", "", nil, nil, nil, 0) terms, err := SimpleFind[WpTerms](nil, "*")
if err != nil { if err != nil {
return err return err
} }
for _, wpTerms := range terms { for _, wpTerms := range terms {
Terms[wpTerms.TermId] = wpTerms Terms[wpTerms.TermId] = wpTerms
} }
termTax, err := Find[WpTermTaxonomy](nil, "*", "", nil, nil, nil, 0) termTax, err := SimpleFind[WpTermTaxonomy](nil, "*")
if err != nil { if err != nil {
return err return err
} }

View File

@ -297,10 +297,15 @@ func LastOne[T Model](where ParseWhere, fields string, in ...[]any) (T, error) {
func SimpleFind[T Model](where ParseWhere, fields string, in ...[]any) ([]T, error) { func SimpleFind[T Model](where ParseWhere, fields string, in ...[]any) ([]T, error) {
var r []T var r []T
var rr T var rr T
w, args, err := where.ParseWhere(&in) var err error
args := make([]any, 0, 0)
var w string
if where != nil {
w, args, err = where.ParseWhere(&in)
if err != nil { if err != nil {
return r, err return r, err
} }
}
tp := "select %s from %s %s" tp := "select %s from %s %s"
sql := fmt.Sprintf(tp, fields, rr.Table(), w) sql := fmt.Sprintf(tp, fields, rr.Table(), w)
err = db.Db.Select(&r, sql, args...) err = db.Db.Select(&r, sql, args...)