82 lines
2.2 KiB
Go
Raw Normal View History

package handler
import (
"log"
"net/http"
"github.com/gin-gonic/gin"
"github.com/muyuqingfeng/iloom/purchaser-service/internal/service"
"github.com/muyuqingfeng/iloom/shared/pkg/response"
)
type WashingPlanHandler struct {
svc *service.WashingPlanService
}
func NewWashingPlanHandler(svc *service.WashingPlanService) *WashingPlanHandler {
return &WashingPlanHandler{svc: svc}
}
type CreateWashingPlanReq struct {
ProductID string `json:"product_id" binding:"required"`
WashingFactoryID string `json:"washing_factory_id"`
PlannedMeters float64 `json:"planned_meters" binding:"required,gt=0"`
WashingPrice float64 `json:"washing_price" binding:"required,gt=0"`
EstimatedShrinkageRate float64 `json:"estimated_shrinkage_rate"`
Notes string `json:"notes"`
}
func (h *WashingPlanHandler) Create(c *gin.Context) {
companyID := c.GetHeader("X-Company-ID")
userID := c.GetHeader("X-User-ID")
if companyID == "" || userID == "" {
response.BadRequest(c, "missing user or company id")
return
}
var req CreateWashingPlanReq
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, err.Error())
return
}
svcReq := service.CreateWashingPlanRequest{
ProductID: req.ProductID,
WashingFactoryID: req.WashingFactoryID,
PlannedMeters: req.PlannedMeters,
WashingPrice: req.WashingPrice,
EstimatedShrinkageRate: req.EstimatedShrinkageRate,
Notes: req.Notes,
}
wp, err := h.svc.Create(c.Request.Context(), companyID, userID, svcReq)
if err != nil {
log.Printf("create washing plan error: %v", err)
response.InternalError(c, "failed to create washing plan")
return
}
c.JSON(http.StatusCreated, response.Response{
Code: 0,
Message: "success",
Data: wp,
})
}
func (h *WashingPlanHandler) List(c *gin.Context) {
companyID := c.GetHeader("X-Company-ID")
if companyID == "" {
response.BadRequest(c, "missing company id")
return
}
plans, err := h.svc.List(c.Request.Context(), companyID)
if err != nil {
log.Printf("list washing plans error: %v", err)
response.InternalError(c, "failed to list washing plans")
return
}
response.OK(c, plans)
}