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>
108 lines
2.9 KiB
Go
108 lines
2.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
|
|
"github.com/muyuqingfeng/iloom/textile-service/internal/model"
|
|
)
|
|
|
|
type YarnRepo struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
func NewYarnRepo(db *sql.DB) *YarnRepo {
|
|
return &YarnRepo{db: db}
|
|
}
|
|
|
|
func (r *YarnRepo) List(ctx context.Context, companyID string) ([]model.YarnStock, error) {
|
|
query := `
|
|
SELECT id, company_id, name, spec, quantity, unit, min_stock, warehouse_id, updated_at
|
|
FROM ilm_yarn_stock
|
|
WHERE company_id = ?
|
|
ORDER BY updated_at DESC`
|
|
|
|
rows, err := r.db.QueryContext(ctx, query, companyID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var stocks []model.YarnStock
|
|
for rows.Next() {
|
|
var s model.YarnStock
|
|
if err := rows.Scan(
|
|
&s.ID, &s.CompanyID, &s.Name, &s.Spec, &s.Quantity,
|
|
&s.Unit, &s.MinStock, &s.WarehouseID, &s.UpdatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
stocks = append(stocks, s)
|
|
}
|
|
return stocks, rows.Err()
|
|
}
|
|
|
|
func (r *YarnRepo) Create(ctx context.Context, y *model.YarnStock) error {
|
|
query := `
|
|
INSERT INTO ilm_yarn_stock (id, company_id, name, spec, quantity, unit, min_stock, warehouse_id, updated_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
|
|
|
_, err := r.db.ExecContext(ctx, query,
|
|
y.ID, y.CompanyID, y.Name, y.Spec, y.Quantity,
|
|
y.Unit, y.MinStock, y.WarehouseID, y.UpdatedAt,
|
|
)
|
|
return err
|
|
}
|
|
|
|
func (r *YarnRepo) Update(ctx context.Context, y *model.YarnStock) error {
|
|
query := `
|
|
UPDATE ilm_yarn_stock
|
|
SET name = ?, spec = ?, unit = ?, min_stock = ?, warehouse_id = ?, updated_at = ?
|
|
WHERE id = ? AND company_id = ?`
|
|
|
|
_, err := r.db.ExecContext(ctx, query,
|
|
y.Name, y.Spec, y.Unit, y.MinStock, y.WarehouseID, y.UpdatedAt,
|
|
y.ID, y.CompanyID,
|
|
)
|
|
return err
|
|
}
|
|
|
|
func (r *YarnRepo) UpdateQuantity(ctx context.Context, tx *sql.Tx, id string, delta float64) error {
|
|
query := `
|
|
UPDATE ilm_yarn_stock
|
|
SET quantity = quantity + ?, updated_at = NOW()
|
|
WHERE id = ?`
|
|
|
|
_, err := tx.ExecContext(ctx, query, delta, id)
|
|
return err
|
|
}
|
|
|
|
func (r *YarnRepo) CreateRecord(ctx context.Context, tx *sql.Tx, rec *model.YarnStockRecord) error {
|
|
query := `
|
|
INSERT INTO ilm_yarn_stock_record
|
|
(id, yarn_stock_id, company_id, record_type, quantity, unit, batch_no, plan_id, notes, operator_id, created_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
|
|
|
_, err := tx.ExecContext(ctx, query,
|
|
rec.ID, rec.YarnStockID, rec.CompanyID, rec.RecordType, rec.Quantity,
|
|
rec.Unit, rec.BatchNo, rec.PlanID, rec.Notes, rec.OperatorID, rec.CreatedAt,
|
|
)
|
|
return err
|
|
}
|
|
|
|
func (r *YarnRepo) Count(ctx context.Context, companyID string) (int, error) {
|
|
var count int
|
|
err := r.db.QueryRowContext(ctx,
|
|
`SELECT COUNT(*) FROM ilm_yarn_stock WHERE company_id = ?`, companyID,
|
|
).Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
func (r *YarnRepo) CountLowStock(ctx context.Context, companyID string) (int, error) {
|
|
var count int
|
|
err := r.db.QueryRowContext(ctx,
|
|
`SELECT COUNT(*) FROM ilm_yarn_stock WHERE company_id = ? AND quantity <= min_stock`, companyID,
|
|
).Scan(&count)
|
|
return count, err
|
|
}
|