feat: 初始提交云印享付费打印服务后端
This commit is contained in:
80
cloudprint-backend/internal/repository/price_repository.go
Normal file
80
cloudprint-backend/internal/repository/price_repository.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/cloudprint/cloudprint-backend/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type PriceRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewPriceRepository(db *gorm.DB) *PriceRepository {
|
||||
return &PriceRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *PriceRepository) Create(ctx context.Context, item *model.PriceList) error {
|
||||
return r.db.WithContext(ctx).Create(item).Error
|
||||
}
|
||||
|
||||
func (r *PriceRepository) GetByID(ctx context.Context, id int32) (*model.PriceList, error) {
|
||||
var item model.PriceList
|
||||
err := r.db.WithContext(ctx).First(&item, id).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (r *PriceRepository) GetAllActive(ctx context.Context) ([]*model.PriceList, error) {
|
||||
var items []*model.PriceList
|
||||
err := r.db.WithContext(ctx).Where("is_active = ?", true).Order("sort_order ASC, id ASC").Find(&items).Error
|
||||
return items, err
|
||||
}
|
||||
|
||||
func (r *PriceRepository) Update(ctx context.Context, item *model.PriceList) error {
|
||||
return r.db.WithContext(ctx).Save(item).Error
|
||||
}
|
||||
|
||||
func (r *PriceRepository) Delete(ctx context.Context, id int32) error {
|
||||
return r.db.WithContext(ctx).Delete(&model.PriceList{}, id).Error
|
||||
}
|
||||
|
||||
type PaymentRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewPaymentRepository(db *gorm.DB) *PaymentRepository {
|
||||
return &PaymentRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *PaymentRepository) Create(ctx context.Context, payment *model.Payment) error {
|
||||
return r.db.WithContext(ctx).Create(payment).Error
|
||||
}
|
||||
|
||||
func (r *PaymentRepository) GetByOrderNo(ctx context.Context, orderNo string) (*model.Payment, error) {
|
||||
var payment model.Payment
|
||||
err := r.db.WithContext(ctx).Where("order_no = ?", orderNo).First(&payment).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &payment, nil
|
||||
}
|
||||
|
||||
func (r *PaymentRepository) UpdateStatus(ctx context.Context, orderNo string, status int8, transactionID string) error {
|
||||
updates := map[string]interface{}{
|
||||
"pay_status": status,
|
||||
"transaction_id": transactionID,
|
||||
}
|
||||
return r.db.WithContext(ctx).Model(&model.Payment{}).
|
||||
Where("order_no = ?", orderNo).Updates(updates).Error
|
||||
}
|
||||
Reference in New Issue
Block a user