78 lines
2.0 KiB
Go
78 lines
2.0 KiB
Go
|
|
package service
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/google/uuid"
|
||
|
|
"github.com/muyuqingfeng/iloom/textile-service/internal/model"
|
||
|
|
"github.com/muyuqingfeng/iloom/textile-service/internal/repository"
|
||
|
|
)
|
||
|
|
|
||
|
|
type InboundRequest struct {
|
||
|
|
WarehouseID string `json:"warehouse_id"`
|
||
|
|
Quantity float64 `json:"quantity"`
|
||
|
|
Rolls int `json:"rolls"`
|
||
|
|
PricePerMeter float64 `json:"price_per_meter"`
|
||
|
|
PriceNote string `json:"price_note"`
|
||
|
|
WarehouseLocation string `json:"warehouse_location"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type InventoryService struct {
|
||
|
|
repo *repository.InventoryRepo
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewInventoryService(repo *repository.InventoryRepo) *InventoryService {
|
||
|
|
return &InventoryService{repo: repo}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *InventoryService) Inbound(ctx context.Context, planID, companyID, userID string, req InboundRequest) (*model.InventoryRecord, error) {
|
||
|
|
var rolls *int
|
||
|
|
if req.Rolls > 0 {
|
||
|
|
rolls = &req.Rolls
|
||
|
|
}
|
||
|
|
var pricePerMeter *float64
|
||
|
|
if req.PricePerMeter > 0 {
|
||
|
|
pricePerMeter = &req.PricePerMeter
|
||
|
|
}
|
||
|
|
var priceNote *string
|
||
|
|
if req.PriceNote != "" {
|
||
|
|
priceNote = &req.PriceNote
|
||
|
|
}
|
||
|
|
var warehouseLocation *string
|
||
|
|
if req.WarehouseLocation != "" {
|
||
|
|
warehouseLocation = &req.WarehouseLocation
|
||
|
|
}
|
||
|
|
var operatorID *string
|
||
|
|
if userID != "" {
|
||
|
|
operatorID = &userID
|
||
|
|
}
|
||
|
|
var companyIDPtr *string
|
||
|
|
if companyID != "" {
|
||
|
|
companyIDPtr = &companyID
|
||
|
|
}
|
||
|
|
|
||
|
|
rec := &model.InventoryRecord{
|
||
|
|
ID: uuid.New().String(),
|
||
|
|
PlanID: planID,
|
||
|
|
WarehouseID: req.WarehouseID,
|
||
|
|
Quantity: req.Quantity,
|
||
|
|
Rolls: rolls,
|
||
|
|
PricePerMeter: pricePerMeter,
|
||
|
|
PriceNote: priceNote,
|
||
|
|
WarehouseLocation: warehouseLocation,
|
||
|
|
OperatorID: operatorID,
|
||
|
|
CompanyID: companyIDPtr,
|
||
|
|
CreatedAt: time.Now(),
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := s.repo.Create(ctx, rec); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return rec, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *InventoryService) ListByPlan(ctx context.Context, planID string) ([]model.InventoryRecord, error) {
|
||
|
|
return s.repo.ListByPlan(ctx, planID)
|
||
|
|
}
|