60 lines
1.6 KiB
Go
Raw Permalink Normal View History

package repository
import (
"context"
"database/sql"
"github.com/muyuqingfeng/iloom/textile-service/internal/model"
)
type InventoryRepo struct {
db *sql.DB
}
func NewInventoryRepo(db *sql.DB) *InventoryRepo {
return &InventoryRepo{db: db}
}
func (r *InventoryRepo) Create(ctx context.Context, rec *model.InventoryRecord) error {
query := `
INSERT INTO ilm_inventory_record
(id, plan_id, warehouse_id, quantity, rolls, price_per_meter, price_note, warehouse_location, operator_id, company_id, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
_, err := r.db.ExecContext(ctx, query,
rec.ID, rec.PlanID, rec.WarehouseID, rec.Quantity, rec.Rolls,
rec.PricePerMeter, rec.PriceNote, rec.WarehouseLocation,
rec.OperatorID, rec.CompanyID, rec.CreatedAt,
)
return err
}
func (r *InventoryRepo) ListByPlan(ctx context.Context, planID string) ([]model.InventoryRecord, error) {
query := `
SELECT id, plan_id, warehouse_id, quantity, rolls, price_per_meter,
price_note, warehouse_location, operator_id, company_id, created_at
FROM ilm_inventory_record
WHERE plan_id = ?
ORDER BY created_at DESC`
rows, err := r.db.QueryContext(ctx, query, planID)
if err != nil {
return nil, err
}
defer rows.Close()
var records []model.InventoryRecord
for rows.Next() {
var r model.InventoryRecord
if err := rows.Scan(
&r.ID, &r.PlanID, &r.WarehouseID, &r.Quantity, &r.Rolls,
&r.PricePerMeter, &r.PriceNote, &r.WarehouseLocation,
&r.OperatorID, &r.CompanyID, &r.CreatedAt,
); err != nil {
return nil, err
}
records = append(records, r)
}
return records, rows.Err()
}