77 lines
2.3 KiB
Go
77 lines
2.3 KiB
Go
|
|
package service
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/google/uuid"
|
||
|
|
"github.com/muyuqingfeng/iloom/purchaser-service/internal/model"
|
||
|
|
"github.com/muyuqingfeng/iloom/purchaser-service/internal/repository"
|
||
|
|
)
|
||
|
|
|
||
|
|
type WashingPlanService struct {
|
||
|
|
repo *repository.WashingPlanRepo
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewWashingPlanService(repo *repository.WashingPlanRepo) *WashingPlanService {
|
||
|
|
return &WashingPlanService{repo: repo}
|
||
|
|
}
|
||
|
|
|
||
|
|
type CreateWashingPlanRequest struct {
|
||
|
|
ProductID string `json:"product_id" binding:"required"`
|
||
|
|
WashingFactoryID string `json:"washing_factory_id"`
|
||
|
|
PlannedMeters float64 `json:"planned_meters" binding:"required,gt=0"`
|
||
|
|
WashingPrice float64 `json:"washing_price" binding:"required,gt=0"`
|
||
|
|
EstimatedShrinkageRate float64 `json:"estimated_shrinkage_rate"`
|
||
|
|
Notes string `json:"notes"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *WashingPlanService) Create(ctx context.Context, companyID, userID string, req CreateWashingPlanRequest) (*model.WashingPlan, error) {
|
||
|
|
planCode, err := s.repo.GeneratePlanCode(ctx)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
now := time.Now()
|
||
|
|
|
||
|
|
estimatedWashedMeters := req.PlannedMeters * (1 - req.EstimatedShrinkageRate/100)
|
||
|
|
estimatedTotalCost := req.PlannedMeters * req.WashingPrice
|
||
|
|
|
||
|
|
var washingFactoryPtr *string
|
||
|
|
if req.WashingFactoryID != "" {
|
||
|
|
washingFactoryPtr = &req.WashingFactoryID
|
||
|
|
}
|
||
|
|
var notesPtr *string
|
||
|
|
if req.Notes != "" {
|
||
|
|
notesPtr = &req.Notes
|
||
|
|
}
|
||
|
|
|
||
|
|
wp := &model.WashingPlan{
|
||
|
|
ID: uuid.New().String(),
|
||
|
|
PlanCode: planCode,
|
||
|
|
CompanyID: companyID,
|
||
|
|
ProductID: req.ProductID,
|
||
|
|
WashingFactoryID: washingFactoryPtr,
|
||
|
|
PlannedMeters: req.PlannedMeters,
|
||
|
|
WashingPrice: req.WashingPrice,
|
||
|
|
EstimatedShrinkageRate: req.EstimatedShrinkageRate,
|
||
|
|
EstimatedWashedMeters: &estimatedWashedMeters,
|
||
|
|
EstimatedTotalCost: &estimatedTotalCost,
|
||
|
|
Status: "pending",
|
||
|
|
Notes: notesPtr,
|
||
|
|
CreatedBy: &userID,
|
||
|
|
CreatedAt: now,
|
||
|
|
UpdatedAt: now,
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := s.repo.Create(ctx, wp); err != nil {
|
||
|
|
return nil, fmt.Errorf("create washing plan: %w", err)
|
||
|
|
}
|
||
|
|
return wp, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *WashingPlanService) List(ctx context.Context, companyID string) ([]model.WashingPlan, error) {
|
||
|
|
return s.repo.List(ctx, companyID)
|
||
|
|
}
|