service.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package service
  2. import (
  3. "fmt"
  4. "time"
  5. "wails-app/internal/dao"
  6. "wails-app/internal/global"
  7. "wails-app/internal/modules/payment/repository"
  8. )
  9. // Payment method constants — add new methods here for POS/Wechat/Alipay
  10. const (
  11. PaymentCash = "cash"
  12. PaymentFree = "free"
  13. PaymentPOS = "pos"
  14. PaymentWechat = "wechat"
  15. PaymentAlipay = "alipay"
  16. )
  17. type PaymentService struct {
  18. repo *repository.PaymentRepository
  19. }
  20. func NewPaymentService() *PaymentService {
  21. return &PaymentService{repo: &repository.PaymentRepository{}}
  22. }
  23. // ProcessPayment creates a payment_record and updates vehicle_record payment status
  24. func (s *PaymentService) ProcessPayment(recordID uint, paymentMethod string, amount float64, paidAmount float64, operatorID uint) (*dao.PaymentRecord, error) {
  25. changeAmount := 0.0
  26. if paymentMethod == PaymentCash && paidAmount > amount {
  27. changeAmount = paidAmount - amount
  28. }
  29. now := time.Now()
  30. pr := &dao.PaymentRecord{
  31. RecordID: recordID,
  32. PaymentMethod: paymentMethod,
  33. Amount: amount,
  34. PaidAmount: paidAmount,
  35. ChangeAmount: changeAmount,
  36. OperatorID: operatorID,
  37. PaidAt: now,
  38. }
  39. if err := s.repo.Create(pr); err != nil {
  40. return nil, fmt.Errorf("创建支付记录失败: %w", err)
  41. }
  42. // Update vehicle_record payment status
  43. global.GVA_DB.Model(&dao.VehicleRecord{}).Where("id = ?", recordID).Updates(map[string]interface{}{
  44. "payment_status": "paid",
  45. "payment_method": paymentMethod,
  46. })
  47. return pr, nil
  48. }