service.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. }
  49. // QueryPayments 查询收费记录列表
  50. func (s *PaymentService) QueryPayments(q repository.PaymentListQuery) ([]repository.PaymentListResult, int64, error) {
  51. return s.repo.List(q)
  52. }
  53. // GetTodaySummary 获取当日收费汇总
  54. func (s *PaymentService) GetTodaySummary() (repository.TodaySummary, error) {
  55. return s.repo.GetTodaySummary()
  56. }