77 lines
1.6 KiB
Go
77 lines
1.6 KiB
Go
package repoimpl
|
|
|
|
import (
|
|
"context"
|
|
"gorm.io/gorm"
|
|
"time"
|
|
"voucher/internal/biz/bo"
|
|
"voucher/internal/biz/repo"
|
|
"voucher/internal/data"
|
|
"voucher/internal/data/model"
|
|
)
|
|
|
|
// UseLogRepoImpl .
|
|
type UseLogRepoImpl struct {
|
|
Base[model.UseLog, bo.UseLogBo]
|
|
db *data.Db
|
|
}
|
|
|
|
// NewUseLogRepoImpl .
|
|
func NewUseLogRepoImpl(db *data.Db) repo.UseLogRepo {
|
|
return &UseLogRepoImpl{db: db}
|
|
}
|
|
|
|
func (this *UseLogRepoImpl) DB(ctx context.Context) *gorm.DB {
|
|
return this.db.DB(ctx).WithContext(ctx).Model(model.UseLog{})
|
|
}
|
|
|
|
func (this *UseLogRepoImpl) Create(ctx context.Context, req *bo.UseLogBo) (*bo.UseLogBo, error) {
|
|
now := time.Now()
|
|
|
|
info := &model.UseLog{
|
|
OrderNo: req.OrderNo,
|
|
Type: req.Type.GetValue(),
|
|
Amount: req.Amount,
|
|
UseTime: req.UseTime,
|
|
CreateTime: &now,
|
|
}
|
|
|
|
if err := this.DB(ctx).Create(info).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return this.ToBo(info), nil
|
|
}
|
|
|
|
func (this *UseLogRepoImpl) GetByID(ctx context.Context, id uint64) (*bo.UseLogBo, error) {
|
|
var item model.UseLog
|
|
|
|
tx := this.DB(ctx).Where(model.UseLog{ID: id}).First(&item)
|
|
|
|
if tx.Error != nil {
|
|
return nil, tx.Error
|
|
}
|
|
|
|
if tx.RowsAffected == 0 {
|
|
return nil, gorm.ErrRecordNotFound
|
|
}
|
|
|
|
return this.ToBo(&item), nil
|
|
}
|
|
|
|
func (this *UseLogRepoImpl) GetByUseTimeOrder(ctx context.Context, orderNo string, useTime *time.Time) (*bo.UseLogBo, error) {
|
|
var item model.UseLog
|
|
|
|
tx := this.DB(ctx).Where(model.UseLog{OrderNo: orderNo, UseTime: useTime}).First(&item)
|
|
|
|
if tx.Error != nil {
|
|
return nil, tx.Error
|
|
}
|
|
|
|
if tx.RowsAffected == 0 {
|
|
return nil, gorm.ErrRecordNotFound
|
|
}
|
|
|
|
return this.ToBo(&item), nil
|
|
}
|