42 lines
893 B
Go
42 lines
893 B
Go
|
|
package service
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"database/sql"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"github.com/muyuqingfeng/iloom/purchaser-service/internal/model"
|
||
|
|
"github.com/muyuqingfeng/iloom/purchaser-service/internal/repository"
|
||
|
|
)
|
||
|
|
|
||
|
|
type APService struct {
|
||
|
|
repo *repository.APRepo
|
||
|
|
db *sql.DB
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewAPService(repo *repository.APRepo, db *sql.DB) *APService {
|
||
|
|
return &APService{repo: repo, db: db}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *APService) List(ctx context.Context, companyID string) ([]model.AccountsPayable, error) {
|
||
|
|
return s.repo.List(ctx, companyID)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *APService) Pay(ctx context.Context, apID, companyID string, amount float64) error {
|
||
|
|
if amount <= 0 {
|
||
|
|
return fmt.Errorf("amount must be positive")
|
||
|
|
}
|
||
|
|
|
||
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("begin tx: %w", err)
|
||
|
|
}
|
||
|
|
defer tx.Rollback()
|
||
|
|
|
||
|
|
if err := s.repo.Pay(ctx, tx, apID, amount); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
return tx.Commit()
|
||
|
|
}
|