diff --git a/base.go b/base.go index 3c6248e..9ee6c5e 100644 --- a/base.go +++ b/base.go @@ -7,16 +7,6 @@ package mysql import "gorm.io/gorm" -// Option 扩展选项 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 8:05 下午 2021/8/8 -type Option struct { - Limit int `json:"limit"` // 限制数量 - Offset int `json:"offset"` // 偏移量 -} - type BaseDao struct { } diff --git a/option.go b/option.go new file mode 100644 index 0000000..ff682c6 --- /dev/null +++ b/option.go @@ -0,0 +1,240 @@ +// Package mysql ... +// +// Description : mysql ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2022-05-15 11:43 +package mysql + +// SetOption 设置选项 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 11:46 2022/5/15 +type SetOption func(o *Option) + +// Option 扩展选项 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 8:05 下午 2021/8/8 +type Option struct { + Limit int `json:"limit"` // 限制数量 + Offset int `json:"offset"` // 偏移量 + In map[string]interface{} `json:"in"` // in语句 + NotIn map[string]interface{} `json:"not_in"` // not in语句 + Where map[string]interface{} `json:"where"` // where 条件 + Start map[string]interface{} `json:"start"` // >= 条件 + End map[string]interface{} `json:"end"` // < 条件 + Like map[string]string `json:"like"` // like 语句 + NotEqual map[string]interface{} `json:"not_equal"` // != 语句 +} + +// WithWhere 设置where条件 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:17 2022/5/15 +func WithWhere(where map[string]interface{}) SetOption { + return func(o *Option) { + if nil == o.Where { + o.Where = make(map[string]interface{}) + } + for field, value := range where { + o.Where[field] = value + } + } +} + +// WithLimit 设置limit条件 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:00 2022/5/15 +func WithLimit(limit int, offset int) SetOption { + return func(o *Option) { + o.Limit = limit + o.Offset = offset + } +} + +// WithIn 设置in条件 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:23 2022/5/15 +func WithIn(field string, value interface{}) SetOption { + return func(o *Option) { + if nil == o.In { + o.In = make(map[string]interface{}) + } + o.In[field] = value + } +} + +// WithBatchIn ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:24 2022/5/15 +func WithBatchIn(batchIn map[string]interface{}) SetOption { + return func(o *Option) { + if nil == o.In { + o.In = make(map[string]interface{}) + } + for field, value := range batchIn { + o.In[field] = value + } + } +} + +// WithNotIn 设置 notin 条件 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:18 2022/5/15 +func WithNotIn(field string, value interface{}) SetOption { + return func(o *Option) { + if nil == o.NotIn { + o.NotIn = make(map[string]interface{}) + } + o.NotIn[field] = value + } +} + +// WithBatchNotIn 批量设置 NOT IN +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:23 2022/5/15 +func WithBatchNotIn(data map[string]interface{}) SetOption { + return func(o *Option) { + if nil == o.NotIn { + o.NotIn = make(map[string]interface{}) + } + for field, value := range data { + o.NotIn[field] = value + } + } +} + +// WithStart >= 条件 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:01 2022/5/15 +func WithStart(field string, value interface{}) SetOption { + return func(o *Option) { + if nil == o.Start { + o.Start = make(map[string]interface{}) + } + o.Start[field] = value + } +} + +// WithBatchStart 批量设置起始条件 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:03 2022/5/15 +func WithBatchStart(data map[string]interface{}) SetOption { + return func(o *Option) { + if nil == o.Start { + o.Start = make(map[string]interface{}) + } + for field, value := range data { + o.Start[field] = value + } + } +} + +// WithEnd 设置 < 条件 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:07 2022/5/15 +func WithEnd(field string, value interface{}) SetOption { + return func(o *Option) { + if nil == o.End { + o.End = make(map[string]interface{}) + } + o.End[field] = value + } +} + +// WithBatchEnd 批量设置 < 条件 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:10 2022/5/15 +func WithBatchEnd(data map[string]interface{}) SetOption { + return func(o *Option) { + if nil == o.End { + o.End = make(map[string]interface{}) + } + for field, value := range data { + o.End[field] = value + } + } +} + +// WithLike 设置 like 查询条件 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:16 2022/5/15 +func WithLike(field string, value string) SetOption { + return func(o *Option) { + if nil == o.Like { + o.Like = make(map[string]string) + } + o.Like[field] = value + } +} + +// WithBatchLike 批量设置like条件 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:19 2022/5/15 +func WithBatchLike(data map[string]string) SetOption { + return func(o *Option) { + if nil == o.Like { + o.Like = make(map[string]string) + } + for field, value := range data { + o.Like[field] = value + } + } +} + +// WithNotEqual 设置不等于语句 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:31 2022/5/15 +func WithNotEqual(field string, value interface{}) SetOption { + return func(o *Option) { + if nil == o.NotEqual { + o.NotEqual = make(map[string]interface{}) + } + o.NotEqual[field] = value + } +} + +// WithBatchNotEqual 批量设置不等于条件 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:33 2022/5/15 +func WithBatchNotEqual(data map[string]interface{}) SetOption { + return func(o *Option) { + if nil == o.NotEqual { + o.NotEqual = make(map[string]interface{}) + } + for field, value := range data { + o.NotEqual[field] = value + } + } +}