89 lines
2.2 KiB
Go
Raw Normal View History

package repository
import (
"context"
"database/sql"
"time"
"github.com/google/uuid"
"github.com/muyuqingfeng/iloom/textile-service/internal/model"
)
type StepRepo struct {
db *sql.DB
}
func NewStepRepo(db *sql.DB) *StepRepo {
return &StepRepo{db: db}
}
func (r *StepRepo) GetByPlan(ctx context.Context, planID string) ([]model.ProcessStep, error) {
query := `
SELECT id, plan_id, step_type, status, notes, operator_id, company_id, timestamp, created_at
FROM ilm_process_step
WHERE plan_id = ?
ORDER BY created_at ASC`
rows, err := r.db.QueryContext(ctx, query, planID)
if err != nil {
return nil, err
}
defer rows.Close()
var steps []model.ProcessStep
for rows.Next() {
var s model.ProcessStep
if err := rows.Scan(
&s.ID, &s.PlanID, &s.StepType, &s.Status, &s.Notes,
&s.OperatorID, &s.CompanyID, &s.Timestamp, &s.CreatedAt,
); err != nil {
return nil, err
}
steps = append(steps, s)
}
return steps, rows.Err()
}
func (r *StepRepo) GetByID(ctx context.Context, id string) (*model.ProcessStep, error) {
query := `
SELECT id, plan_id, step_type, status, notes, operator_id, company_id, timestamp, created_at
FROM ilm_process_step
WHERE id = ?`
var s model.ProcessStep
err := r.db.QueryRowContext(ctx, query, id).Scan(
&s.ID, &s.PlanID, &s.StepType, &s.Status, &s.Notes,
&s.OperatorID, &s.CompanyID, &s.Timestamp, &s.CreatedAt,
)
if err != nil {
return nil, err
}
return &s, nil
}
func (r *StepRepo) UpdateStatus(ctx context.Context, id, status, operatorID string, notes *string) error {
now := time.Now()
query := `
UPDATE ilm_process_step
SET status = ?, operator_id = ?, notes = ?, timestamp = ?
WHERE id = ?`
_, err := r.db.ExecContext(ctx, query, status, operatorID, notes, now, id)
return err
}
func (r *StepRepo) CreateSteps(ctx context.Context, tx *sql.Tx, planID, companyID string) error {
query := `
INSERT INTO ilm_process_step (id, plan_id, step_type, status, company_id, created_at)
VALUES (?, ?, ?, ?, ?, ?)`
now := time.Now()
for _, stepType := range model.StepOrder {
id := uuid.New().String()
if _, err := tx.ExecContext(ctx, query, id, planID, stepType, "pending", companyID, now); err != nil {
return err
}
}
return nil
}