iloom/washing-service/internal/service/finished_product_service.go
Chever John 9c39c8cbd7
init: iloom WMS monorepo with K8s deployment manifests
Go workspace (go.work) with 5 microservices + shared library:
- gateway (8080), auth-service (8081), purchaser-service (8082)
- textile-service (8083), washing-service (8084)
- shared: proto definitions, common packages

Infrastructure: docker-compose for local dev, K8s manifests for
K3s cluster deployment (mysql/redis/etcd + traefik ingress).

Frontend (iloom-flatten) added as git submodule.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-14 11:33:31 +08:00

74 lines
2.0 KiB
Go

package service
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/google/uuid"
"github.com/muyuqingfeng/iloom/shared/pkg/database"
"github.com/muyuqingfeng/iloom/washing-service/internal/model"
"github.com/muyuqingfeng/iloom/washing-service/internal/repository"
)
type FinishedProductService struct {
repo *repository.FinishedProductRepo
db *sql.DB
}
func NewFinishedProductService(repo *repository.FinishedProductRepo, db *sql.DB) *FinishedProductService {
return &FinishedProductService{repo: repo, db: db}
}
func (s *FinishedProductService) List(ctx context.Context, companyID string) ([]model.FinishedProduct, error) {
return s.repo.List(ctx, companyID)
}
func (s *FinishedProductService) Create(ctx context.Context, companyID, userID string, fp *model.FinishedProduct) error {
now := time.Now()
fp.ID = uuid.New().String()
fp.CompanyID = companyID
fp.CreatedBy = &userID
fp.CreatedAt = now
fp.UpdatedAt = now
if fp.Status == "" {
fp.Status = "in_stock"
}
fp.StockMeters = fp.WashedMeters
fp.StockRolls = 0
if fp.Rolls != nil {
fp.StockRolls = *fp.Rolls
}
return database.WithTx(ctx, s.db, func(tx *sql.Tx) error {
return s.repo.Create(ctx, tx, fp)
})
}
func (s *FinishedProductService) Outbound(ctx context.Context, fpID, companyID, userID string, rolls int, meters float64, notes string) error {
return database.WithTx(ctx, s.db, func(tx *sql.Tx) error {
if err := s.repo.UpdateStock(ctx, tx, fpID, -meters, -rolls); err != nil {
return fmt.Errorf("update stock: %w", err)
}
var notePtr *string
if notes != "" {
notePtr = &notes
}
rec := &model.FinishedProductInventoryRecord{
ID: uuid.New().String(),
FinishedProductID: fpID,
CompanyID: companyID,
RecordType: "outbound",
Rolls: rolls,
Meters: meters,
Notes: notePtr,
OperatorID: &userID,
CreatedAt: time.Now(),
}
return s.repo.CreateInventoryRecord(ctx, tx, rec)
})
}