126 lines
3.0 KiB
Go
126 lines
3.0 KiB
Go
package dataTemp
|
|
|
|
import (
|
|
"ai_scheduler/internal/pkg/mapstructure"
|
|
"ai_scheduler/utils"
|
|
"context"
|
|
"database/sql"
|
|
|
|
"github.com/go-kratos/kratos/v2/log"
|
|
"gorm.io/gorm"
|
|
|
|
"xorm.io/builder"
|
|
)
|
|
|
|
type PrimaryKey struct {
|
|
Id int `json:"id"`
|
|
}
|
|
|
|
type GormDb struct {
|
|
Client *gorm.DB
|
|
}
|
|
type contextTxKey struct{}
|
|
|
|
func (d *Db) DB(ctx context.Context) *gorm.DB {
|
|
tx, ok := ctx.Value(contextTxKey{}).(*gorm.DB)
|
|
if ok {
|
|
return tx
|
|
}
|
|
return d.Db.Client
|
|
}
|
|
|
|
func (t *Db) ExecTx(ctx context.Context, f func(ctx context.Context) error) error {
|
|
return t.Db.Client.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
ctx = context.WithValue(ctx, contextTxKey{}, tx)
|
|
return f(ctx)
|
|
})
|
|
}
|
|
|
|
type Db struct {
|
|
Db *GormDb
|
|
Log *log.Helper
|
|
}
|
|
|
|
type DataTemp struct {
|
|
Db *gorm.DB
|
|
Model interface{}
|
|
Do interface{}
|
|
}
|
|
|
|
func NewDataTemp(db *utils.Db, model interface{}) *DataTemp {
|
|
return &DataTemp{Db: db.Client, Model: model}
|
|
}
|
|
|
|
func (k DataTemp) GetById(id int) (data map[string]interface{}, err error) {
|
|
err = k.Db.Model(k.Model).Where("id = ?", id).Find(&data).Error
|
|
if data == nil {
|
|
err = sql.ErrNoRows
|
|
}
|
|
return
|
|
}
|
|
|
|
func (k DataTemp) Add(data interface{}) (id int, err error) {
|
|
var primary *PrimaryKey
|
|
add := k.Db.Model(k.Model).Create(data)
|
|
_ = mapstructure.Decode(data, &primary)
|
|
return primary.Id, add.Error
|
|
}
|
|
|
|
func (k DataTemp) GetList(cond *builder.Cond, pageBoIn *ReqPageBo) (list []map[string]interface{}, pageBoOut *RespPageBo, err error) {
|
|
var (
|
|
query, _ = builder.ToBoundSQL(*cond)
|
|
model = k.Db.Model(k.Model).Where(query)
|
|
total int64
|
|
)
|
|
model.Count(&total)
|
|
pageBoOut = pageBoOut.SetDataByReq(total, pageBoIn)
|
|
model.Limit(pageBoIn.GetSize()).Offset(pageBoIn.GetOffset()).Order("updated_at desc").Find(&list)
|
|
return
|
|
}
|
|
|
|
func (k DataTemp) GetRange(cond *builder.Cond) (list []map[string]interface{}, err error) {
|
|
var (
|
|
query, _ = builder.ToBoundSQL(*cond)
|
|
model = k.Db.Model(k.Model).Where(query)
|
|
)
|
|
err = model.Find(&list).Error
|
|
return list, err
|
|
}
|
|
|
|
func (k DataTemp) GetOneBySearch(cond *builder.Cond) (data map[string]interface{}, err error) {
|
|
query, _ := builder.ToBoundSQL(*cond)
|
|
err = k.Db.Model(k.Model).Where(query).Limit(1).Find(&data).Error
|
|
if data == nil {
|
|
err = sql.ErrNoRows
|
|
}
|
|
return
|
|
}
|
|
|
|
func (k DataTemp) GetOneBySearchToStrut(cond *builder.Cond, result interface{}) error {
|
|
query, _ := builder.ToBoundSQL(*cond)
|
|
err := k.Db.Model(k.Model).Where(query).Limit(1).Find(&result).Error
|
|
|
|
return err
|
|
}
|
|
|
|
func (k DataTemp) GetListToStruct(cond *builder.Cond, pageBoIn *ReqPageBo, result []interface{}) (pageBoOut *RespPageBo, err error) {
|
|
var (
|
|
query, _ = builder.ToBoundSQL(*cond)
|
|
model = k.Db.Model(k.Model).Where(query)
|
|
total int64
|
|
)
|
|
model.Count(&total)
|
|
pageBoOut = pageBoOut.SetDataByReq(total, pageBoIn)
|
|
model.Limit(pageBoIn.GetSize()).Offset(pageBoIn.GetOffset()).Order("updated_at desc").Find(&result)
|
|
return
|
|
}
|
|
|
|
func (k DataTemp) UpdateByCond(cond *builder.Cond, data interface{}) (err error) {
|
|
var (
|
|
query, _ = builder.ToBoundSQL(*cond)
|
|
model = k.Db.Model(k.Model)
|
|
)
|
|
err = model.Where(query).Updates(data).Error
|
|
return
|
|
}
|