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