From 0005d1e8f7efbb4bbfa4f85fe498997a83d14908 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, 3 Jan 2025 11:34:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AF=B9order=E8=AF=AD?= =?UTF-8?q?=E5=8F=A5=E7=9A=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- option.go | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/option.go b/option.go index 3da670a..b3d59a3 100644 --- a/option.go +++ b/option.go @@ -341,6 +341,17 @@ func WithOR(orConditionList ...define.SetOption) define.SetOption { // Date : 20:15 2022/10/20 func WithOrder(orderRuleList ...string) define.SetOption { return func(o *define.Option) { + if len(orderRuleList) != 2 { + return + } + if len(orderRuleList[0]) == 0 { + // 未指定排序字段 + return + } + if len(orderRuleList[1]) == 0 { + // 未指定排序规则, 默认倒序 + orderRuleList[1] = "DESC" + } o.Order = orderRuleList } } @@ -351,12 +362,7 @@ func WithOrder(orderRuleList ...string) define.SetOption { // // Date : 15:09 2023/4/3 func WithOrderDesc(field string) define.SetOption { - return func(o *define.Option) { - if nil == o.Order { - o.Order = make([]string, 0) - } - o.Order = append(o.Order, field+" desc") - } + return WithOrder(field, "DESC") } // WithOrderAsc 升序排序 @@ -365,12 +371,7 @@ func WithOrderDesc(field string) define.SetOption { // // Date : 15:09 2023/4/3 func WithOrderAsc(field string) define.SetOption { - return func(o *define.Option) { - if nil == o.Order { - o.Order = make([]string, 0) - } - o.Order = append(o.Order, field+" asc") - } + return WithOrder(field, "ASC") } // newOption 生成新的option