优化对order语句的处理

This commit is contained in:
白茶清欢 2025-01-03 11:34:29 +08:00
parent a4cbda626d
commit 0005d1e8f7

View File

@ -341,6 +341,17 @@ func WithOR(orConditionList ...define.SetOption) define.SetOption {
// Date : 20:15 2022/10/20 // Date : 20:15 2022/10/20
func WithOrder(orderRuleList ...string) define.SetOption { func WithOrder(orderRuleList ...string) define.SetOption {
return func(o *define.Option) { 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 o.Order = orderRuleList
} }
} }
@ -351,12 +362,7 @@ func WithOrder(orderRuleList ...string) define.SetOption {
// //
// Date : 15:09 2023/4/3 // Date : 15:09 2023/4/3
func WithOrderDesc(field string) define.SetOption { func WithOrderDesc(field string) define.SetOption {
return func(o *define.Option) { return WithOrder(field, "DESC")
if nil == o.Order {
o.Order = make([]string, 0)
}
o.Order = append(o.Order, field+" desc")
}
} }
// WithOrderAsc 升序排序 // WithOrderAsc 升序排序
@ -365,12 +371,7 @@ func WithOrderDesc(field string) define.SetOption {
// //
// Date : 15:09 2023/4/3 // Date : 15:09 2023/4/3
func WithOrderAsc(field string) define.SetOption { func WithOrderAsc(field string) define.SetOption {
return func(o *define.Option) { return WithOrder(field, "ASC")
if nil == o.Order {
o.Order = make([]string, 0)
}
o.Order = append(o.Order, field+" asc")
}
} }
// newOption 生成新的option // newOption 生成新的option