2022-08-16 23:30:23 +08:00
|
|
|
package repo
|
|
|
|
|
2022-10-11 16:27:58 +08:00
|
|
|
import (
|
|
|
|
"context"
|
2023-06-29 18:40:12 +08:00
|
|
|
"fmt"
|
2023-01-09 22:55:10 +08:00
|
|
|
"time"
|
2023-01-06 18:53:25 +08:00
|
|
|
|
2022-11-02 15:19:14 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2022-10-11 16:27:58 +08:00
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
2022-08-16 23:30:23 +08:00
|
|
|
|
|
|
|
type DBOption func(*gorm.DB) *gorm.DB
|
|
|
|
|
|
|
|
type ICommonRepo interface {
|
|
|
|
WithByID(id uint) DBOption
|
|
|
|
WithByName(name string) DBOption
|
2023-01-06 18:53:25 +08:00
|
|
|
WithByType(tp string) DBOption
|
2022-08-16 23:30:23 +08:00
|
|
|
WithOrderBy(orderStr string) DBOption
|
2023-06-29 18:40:12 +08:00
|
|
|
WithOrderRuleBy(orderBy, order string) DBOption
|
2023-03-06 17:18:13 +08:00
|
|
|
WithByGroupID(groupID uint) DBOption
|
2022-08-16 23:30:23 +08:00
|
|
|
WithLikeName(name string) DBOption
|
|
|
|
WithIdsIn(ids []uint) DBOption
|
2023-01-09 22:55:10 +08:00
|
|
|
WithByDate(startTime, endTime time.Time) DBOption
|
|
|
|
WithByStartDate(startTime time.Time) DBOption
|
2023-03-28 18:00:06 +08:00
|
|
|
WithByStatus(status string) DBOption
|
2023-10-11 14:48:29 +08:00
|
|
|
WithByFrom(from string) DBOption
|
2022-08-16 23:30:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type CommonRepo struct{}
|
|
|
|
|
2023-03-28 18:00:06 +08:00
|
|
|
func NewCommonRepo() ICommonRepo {
|
|
|
|
return &CommonRepo{}
|
|
|
|
}
|
|
|
|
|
2022-08-16 23:30:23 +08:00
|
|
|
func (c *CommonRepo) WithByID(id uint) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("id = ?", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CommonRepo) WithByName(name string) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("name = ?", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-09 22:55:10 +08:00
|
|
|
func (c *CommonRepo) WithByDate(startTime, endTime time.Time) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("start_time > ? AND start_time < ?", startTime, endTime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CommonRepo) WithByStartDate(startTime time.Time) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("start_time < ?", startTime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-06 18:53:25 +08:00
|
|
|
func (c *CommonRepo) WithByType(tp string) DBOption {
|
2022-09-19 19:42:06 +08:00
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
2023-01-06 18:53:25 +08:00
|
|
|
return g.Where("type = ?", tp)
|
2022-09-19 19:42:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 17:18:13 +08:00
|
|
|
func (c *CommonRepo) WithByGroupID(groupID uint) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
if groupID == 0 {
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
return g.Where("group_id = ?", groupID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 17:21:27 +08:00
|
|
|
func (c *CommonRepo) WithByStatus(status string) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
if len(status) == 0 {
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
return g.Where("status = ?", status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-11 14:48:29 +08:00
|
|
|
func (c *CommonRepo) WithByFrom(from string) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("`from` = ?", from)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-16 23:30:23 +08:00
|
|
|
func (c *CommonRepo) WithLikeName(name string) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
2023-02-07 18:48:32 +08:00
|
|
|
if len(name) == 0 {
|
|
|
|
return g
|
|
|
|
}
|
2024-01-03 13:26:29 +08:00
|
|
|
return g.Where("name like ?", "%"+name+"%")
|
2022-08-16 23:30:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CommonRepo) WithOrderBy(orderStr string) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Order(orderStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 18:40:12 +08:00
|
|
|
func (c *CommonRepo) WithOrderRuleBy(orderBy, order string) DBOption {
|
|
|
|
switch order {
|
|
|
|
case constant.OrderDesc:
|
|
|
|
order = "desc"
|
|
|
|
case constant.OrderAsc:
|
|
|
|
order = "asc"
|
|
|
|
default:
|
|
|
|
orderBy = "created_at"
|
|
|
|
order = "desc"
|
|
|
|
}
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Order(fmt.Sprintf("%s %s", orderBy, order))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-16 23:30:23 +08:00
|
|
|
func (c *CommonRepo) WithIdsIn(ids []uint) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("id in (?)", ids)
|
|
|
|
}
|
|
|
|
}
|
2022-10-11 16:27:58 +08:00
|
|
|
|
2022-10-12 10:54:09 +08:00
|
|
|
func (c *CommonRepo) WithIdsNotIn(ids []uint) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("id not in (?)", ids)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-11 16:27:58 +08:00
|
|
|
func getTx(ctx context.Context, opts ...DBOption) *gorm.DB {
|
2022-11-03 17:06:48 +08:00
|
|
|
tx, ok := ctx.Value(constant.DB).(*gorm.DB)
|
|
|
|
if ok {
|
|
|
|
for _, opt := range opts {
|
|
|
|
tx = opt(tx)
|
|
|
|
}
|
2022-11-22 22:47:38 +08:00
|
|
|
return tx
|
2022-10-11 16:27:58 +08:00
|
|
|
}
|
2022-11-22 22:47:38 +08:00
|
|
|
return getDb(opts...)
|
2022-10-11 16:27:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func getDb(opts ...DBOption) *gorm.DB {
|
|
|
|
db := global.DB
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
return db
|
|
|
|
}
|