85 lines
2.3 KiB
Go
85 lines
2.3 KiB
Go
package repository
|
|
|
|
import (
|
|
"cron_admin/app/http/entities"
|
|
"cron_admin/app/models"
|
|
"github.com/pkg/errors"
|
|
|
|
"time"
|
|
"xorm.io/xorm"
|
|
)
|
|
|
|
type CommonRepo[P models.PO] struct {
|
|
repo *xorm.Session
|
|
}
|
|
|
|
type ICommonRepo[P models.PO] interface {
|
|
FindAll(list *[]P, opts ...DBOption) error
|
|
FindAndCount(list *[]P, opts ...DBOption) (int64, error)
|
|
Get(db *P, opts ...DBOption) (bool, error)
|
|
Update(db *P, opts ...DBOption) (int64, error)
|
|
Delete(db *P, opts ...DBOption) (int64, error)
|
|
InsertOne(db *P, opts ...DBOption) (int64, error)
|
|
InsertBatch(db *[]P, opts ...DBOption) (int64, error)
|
|
|
|
WithByID(id uint) DBOption
|
|
WithByUserId(userId uint) DBOption
|
|
WithByBrandId(id int) DBOption
|
|
WithByDate(startTime, endTime time.Time) DBOption
|
|
WithByStartDate(startTime time.Time) DBOption
|
|
WithByEndDate(startTime time.Time) DBOption
|
|
WithDesc(orderStr string) DBOption
|
|
WithByStatus(status int) DBOption
|
|
WithIdsIn(ids []uint) DBOption
|
|
WithPage(pageFilter entities.PageRequest) DBOption
|
|
WithByCouponId(couponId uint) DBOption
|
|
WithByProductId(productId uint) DBOption
|
|
}
|
|
|
|
func NewCommonRepo[P models.PO](repo *xorm.Session) ICommonRepo[P] {
|
|
return &CommonRepo[P]{repo: repo}
|
|
}
|
|
|
|
func (this *CommonRepo[P]) FindAll(list *[]P, opts ...DBOption) error {
|
|
return getDb(this.repo, opts...).Find(list)
|
|
}
|
|
|
|
func (this *CommonRepo[P]) FindAndCount(list *[]P, opts ...DBOption) (int64, error) {
|
|
return getDb(this.repo, opts...).FindAndCount(list)
|
|
}
|
|
|
|
func (this *CommonRepo[P]) Get(db *P, opts ...DBOption) (bool, error) {
|
|
return getDb(this.repo, opts...).Get(db)
|
|
}
|
|
|
|
func (this *CommonRepo[P]) Update(db *P, opts ...DBOption) (int64, error) {
|
|
if len(opts) == 0 {
|
|
return 0, errors.New("不允许不带条件的更新")
|
|
}
|
|
return getDb(this.repo, opts...).Update(db)
|
|
}
|
|
|
|
// 不允许不带条件的删除
|
|
func (this *CommonRepo[P]) Delete(db *P, opts ...DBOption) (int64, error) {
|
|
if len(opts) == 0 {
|
|
return 0, errors.New("不允许不带条件的删除")
|
|
}
|
|
return getDb(this.repo, opts...).Delete(db)
|
|
}
|
|
|
|
func (this *CommonRepo[P]) InsertOne(db *P, opts ...DBOption) (int64, error) {
|
|
return getDb(this.repo, opts...).Insert(db)
|
|
}
|
|
|
|
// 批量插入
|
|
func (this *CommonRepo[P]) InsertBatch(db *[]P, opts ...DBOption) (int64, error) {
|
|
return getDb(this.repo, opts...).Insert(db)
|
|
}
|
|
|
|
func getDb(repo *xorm.Session, opts ...DBOption) *xorm.Session {
|
|
for _, opt := range opts {
|
|
repo = opt(repo)
|
|
}
|
|
return repo
|
|
}
|