143 lines
3.2 KiB
Go
143 lines
3.2 KiB
Go
|
|
package service
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"database/sql"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/google/uuid"
|
||
|
|
"github.com/muyuqingfeng/iloom/shared/pkg/database"
|
||
|
|
"github.com/muyuqingfeng/iloom/textile-service/internal/model"
|
||
|
|
"github.com/muyuqingfeng/iloom/textile-service/internal/repository"
|
||
|
|
)
|
||
|
|
|
||
|
|
type CreateYarnStockRequest struct {
|
||
|
|
Name string `json:"name"`
|
||
|
|
Spec string `json:"spec"`
|
||
|
|
Quantity float64 `json:"quantity"`
|
||
|
|
Unit string `json:"unit"`
|
||
|
|
MinStock float64 `json:"min_stock"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type UpdateYarnStockRequest struct {
|
||
|
|
Name string `json:"name"`
|
||
|
|
Spec string `json:"spec"`
|
||
|
|
Unit string `json:"unit"`
|
||
|
|
MinStock float64 `json:"min_stock"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type YarnRecordRequest struct {
|
||
|
|
RecordType string `json:"record_type"`
|
||
|
|
Quantity float64 `json:"quantity"`
|
||
|
|
Unit string `json:"unit"`
|
||
|
|
BatchNo string `json:"batch_no"`
|
||
|
|
PlanID string `json:"plan_id"`
|
||
|
|
Notes string `json:"notes"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type YarnService struct {
|
||
|
|
repo *repository.YarnRepo
|
||
|
|
db *sql.DB
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewYarnService(repo *repository.YarnRepo, db *sql.DB) *YarnService {
|
||
|
|
return &YarnService{repo: repo, db: db}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *YarnService) List(ctx context.Context, companyID string) ([]model.YarnStock, error) {
|
||
|
|
return s.repo.List(ctx, companyID)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *YarnService) Create(ctx context.Context, companyID string, req CreateYarnStockRequest) (*model.YarnStock, error) {
|
||
|
|
var spec *string
|
||
|
|
if req.Spec != "" {
|
||
|
|
spec = &req.Spec
|
||
|
|
}
|
||
|
|
|
||
|
|
unit := req.Unit
|
||
|
|
if unit == "" {
|
||
|
|
unit = "kg"
|
||
|
|
}
|
||
|
|
|
||
|
|
stock := &model.YarnStock{
|
||
|
|
ID: uuid.New().String(),
|
||
|
|
CompanyID: companyID,
|
||
|
|
Name: req.Name,
|
||
|
|
Spec: spec,
|
||
|
|
Quantity: req.Quantity,
|
||
|
|
Unit: unit,
|
||
|
|
MinStock: req.MinStock,
|
||
|
|
UpdatedAt: time.Now(),
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := s.repo.Create(ctx, stock); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return stock, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *YarnService) Update(ctx context.Context, id, companyID string, req UpdateYarnStockRequest) error {
|
||
|
|
var spec *string
|
||
|
|
if req.Spec != "" {
|
||
|
|
spec = &req.Spec
|
||
|
|
}
|
||
|
|
|
||
|
|
stock := &model.YarnStock{
|
||
|
|
ID: id,
|
||
|
|
CompanyID: companyID,
|
||
|
|
Name: req.Name,
|
||
|
|
Spec: spec,
|
||
|
|
Unit: req.Unit,
|
||
|
|
MinStock: req.MinStock,
|
||
|
|
UpdatedAt: time.Now(),
|
||
|
|
}
|
||
|
|
return s.repo.Update(ctx, stock)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *YarnService) AddRecord(ctx context.Context, yarnStockID, companyID, userID string, req YarnRecordRequest) error {
|
||
|
|
return database.WithTx(ctx, s.db, func(tx *sql.Tx) error {
|
||
|
|
var batchNo, planID, notes, operatorID *string
|
||
|
|
if req.BatchNo != "" {
|
||
|
|
batchNo = &req.BatchNo
|
||
|
|
}
|
||
|
|
if req.PlanID != "" {
|
||
|
|
planID = &req.PlanID
|
||
|
|
}
|
||
|
|
if req.Notes != "" {
|
||
|
|
notes = &req.Notes
|
||
|
|
}
|
||
|
|
if userID != "" {
|
||
|
|
operatorID = &userID
|
||
|
|
}
|
||
|
|
|
||
|
|
unit := req.Unit
|
||
|
|
if unit == "" {
|
||
|
|
unit = "kg"
|
||
|
|
}
|
||
|
|
|
||
|
|
rec := &model.YarnStockRecord{
|
||
|
|
ID: uuid.New().String(),
|
||
|
|
YarnStockID: &yarnStockID,
|
||
|
|
CompanyID: companyID,
|
||
|
|
RecordType: req.RecordType,
|
||
|
|
Quantity: req.Quantity,
|
||
|
|
Unit: unit,
|
||
|
|
BatchNo: batchNo,
|
||
|
|
PlanID: planID,
|
||
|
|
Notes: notes,
|
||
|
|
OperatorID: operatorID,
|
||
|
|
CreatedAt: time.Now(),
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := s.repo.CreateRecord(ctx, tx, rec); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
delta := req.Quantity
|
||
|
|
if req.RecordType == "out" {
|
||
|
|
delta = -delta
|
||
|
|
}
|
||
|
|
return s.repo.UpdateQuantity(ctx, tx, yarnStockID, delta)
|
||
|
|
})
|
||
|
|
}
|