From 88f9e831d8745339a11d6a7c54968a510f2ec9be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 9 Aug 2024 17:24:47 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A1=E5=88=92=E5=8D=87=E7=BA=A7or=E8=AF=AD?= =?UTF-8?q?=E5=8F=A5=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base.go | 22 ++++++---------------- option.go | 18 ++++-------------- 2 files changed, 10 insertions(+), 30 deletions(-) diff --git a/base.go b/base.go index b9cff80..2bc37b8 100644 --- a/base.go +++ b/base.go @@ -192,11 +192,6 @@ func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm tx = tx.Model(o.Model) } - // 设置where条件 - if nil != o.Where && len(o.Where) > 0 { - tx = tx.Where(o.Where) - } - // 设置 limit offset if o.Limit > 0 { tx = tx.Limit(o.Limit) @@ -205,6 +200,11 @@ func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm } } + // 设置where条件 + if nil != o.Where && len(o.Where) > 0 { + tx = tx.Where(o.Where) + } + // in 语句 if nil != o.In { tx = tx.Where(o.In) @@ -251,17 +251,7 @@ func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm } // or 语句 - if nil != o.OR { - expression := "" - valList := make([]any, 0) - for _, o := range o.OR { - if len(expression) > 0 { - expression = expression + " OR " - } - expression = expression + " " + o.Express - valList = append(valList, o.Value) - } - tx = tx.Where(expression, valList...) + if len(o.OR) > 0 { } return tx } diff --git a/option.go b/option.go index 31a4319..293e6dc 100644 --- a/option.go +++ b/option.go @@ -18,16 +18,6 @@ import ( // Date : 11:46 2022/5/15 type SetOption func(o *Option) -// ORCondition OR 条件 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 20:07 2022/7/23 -type ORCondition struct { - Express string `json:"express"` // 表达式 - Value any `json:"value"` // 绑定值 -} - // Option 扩展选项 // // Author : go_developer@163.com<白茶清欢> @@ -47,7 +37,7 @@ type Option struct { NotLike map[string]string `json:"not_like"` // not like 语句 NotEqual map[string]any `json:"not_equal"` // != 语句 Order []string `json:"order"` // 排序规则 - OR []*ORCondition `json:"or"` // or 语句, or 和其他属性 and 关系, 内部 OR 关系 + OR [][]SetOption `json:"or"` // or 语句, or 和其他属性 and 关系, 内部 OR 关系 } // WithModel ... @@ -356,12 +346,12 @@ func WithBatchNotEqual[T op_type.BaseType](data map[string]T) SetOption { // Author : go_developer@163.com<白茶清欢> // // Date : 20:03 2022/7/23 -func WithOR(orConditionList ...*ORCondition) SetOption { +func WithOR(orConditionList ...SetOption) SetOption { return func(o *Option) { if nil == o.OR { - o.OR = make([]*ORCondition, 0) + o.OR = make([][]SetOption, 0) } - o.OR = append(o.OR, orConditionList...) + o.OR = append(o.OR, orConditionList) } }