Przeglądaj źródła

feat: 创建payment模块——支付服务+仓储层

lq 1 miesiąc temu
rodzic
commit
00eec25fad

+ 12 - 0
internal/modules/payment/repository/repo.go

@@ -0,0 +1,12 @@
+package repository
+
+import (
+	"wails-app/internal/dao"
+	"wails-app/internal/global"
+)
+
+type PaymentRepository struct{}
+
+func (r *PaymentRepository) Create(record *dao.PaymentRecord) error {
+	return global.GVA_DB.Create(record).Error
+}

+ 57 - 0
internal/modules/payment/service/service.go

@@ -0,0 +1,57 @@
+package service
+
+import (
+	"fmt"
+	"time"
+	"wails-app/internal/dao"
+	"wails-app/internal/global"
+	"wails-app/internal/modules/payment/repository"
+)
+
+// Payment method constants — add new methods here for POS/Wechat/Alipay
+const (
+	PaymentCash   = "cash"
+	PaymentFree   = "free"
+	PaymentPOS    = "pos"
+	PaymentWechat = "wechat"
+	PaymentAlipay = "alipay"
+)
+
+type PaymentService struct {
+	repo *repository.PaymentRepository
+}
+
+func NewPaymentService() *PaymentService {
+	return &PaymentService{repo: &repository.PaymentRepository{}}
+}
+
+// ProcessPayment creates a payment_record and updates vehicle_record payment status
+func (s *PaymentService) ProcessPayment(recordID uint, paymentMethod string, amount float64, paidAmount float64, operatorID uint) (*dao.PaymentRecord, error) {
+	changeAmount := 0.0
+	if paymentMethod == PaymentCash && paidAmount > amount {
+		changeAmount = paidAmount - amount
+	}
+
+	now := time.Now()
+	pr := &dao.PaymentRecord{
+		RecordID:      recordID,
+		PaymentMethod: paymentMethod,
+		Amount:        amount,
+		PaidAmount:    paidAmount,
+		ChangeAmount:  changeAmount,
+		OperatorID:    operatorID,
+		PaidAt:        now,
+	}
+
+	if err := s.repo.Create(pr); err != nil {
+		return nil, fmt.Errorf("创建支付记录失败: %w", err)
+	}
+
+	// Update vehicle_record payment status
+	global.GVA_DB.Model(&dao.VehicleRecord{}).Where("id = ?", recordID).Updates(map[string]interface{}{
+		"payment_status": "paid",
+		"payment_method": paymentMethod,
+	})
+
+	return pr, nil
+}