实现换货资产快照与新旧独立搜索
This commit is contained in:
@@ -1,25 +1,38 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||||
exchangeService "github.com/break/junhong_cmp_fiber/internal/service/exchange"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/logger"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/response"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// ExchangeLister 定义换货列表读取用例。
|
||||
type ExchangeLister interface {
|
||||
List(ctx context.Context, req *dto.ExchangeListRequest) (*dto.ExchangeListResponse, error)
|
||||
}
|
||||
|
||||
// ExchangeHandler 处理后台换货管理接口。
|
||||
type ExchangeHandler struct {
|
||||
service *exchangeService.Service
|
||||
listQuery ExchangeLister
|
||||
validator *validator.Validate
|
||||
}
|
||||
|
||||
func NewExchangeHandler(service *exchangeService.Service, validator *validator.Validate) *ExchangeHandler {
|
||||
return &ExchangeHandler{service: service, validator: validator}
|
||||
// NewExchangeHandler 创建后台换货管理 Handler。
|
||||
func NewExchangeHandler(service *exchangeService.Service, listQuery ExchangeLister, validator *validator.Validate) *ExchangeHandler {
|
||||
return &ExchangeHandler{service: service, listQuery: listQuery, validator: validator}
|
||||
}
|
||||
|
||||
// Create 创建换货单。
|
||||
// POST /api/admin/exchanges
|
||||
func (h *ExchangeHandler) Create(c *fiber.Ctx) error {
|
||||
var req dto.CreateExchangeRequest
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
@@ -36,22 +49,37 @@ func (h *ExchangeHandler) Create(c *fiber.Ctx) error {
|
||||
return response.Success(c, data)
|
||||
}
|
||||
|
||||
// List 查询换货单列表。
|
||||
// GET /api/admin/exchanges
|
||||
func (h *ExchangeHandler) List(c *fiber.Ctx) error {
|
||||
var req dto.ExchangeListRequest
|
||||
if err := c.QueryParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
h.logListValidationFailure(c, err)
|
||||
return errors.New(errors.CodeInvalidParam)
|
||||
}
|
||||
if err := h.validator.Struct(&req); err != nil {
|
||||
h.logListValidationFailure(c, err)
|
||||
return errors.New(errors.CodeInvalidParam)
|
||||
}
|
||||
|
||||
data, err := h.service.List(c.UserContext(), &req)
|
||||
data, err := h.listQuery.List(c.UserContext(), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return response.Success(c, data)
|
||||
}
|
||||
|
||||
func (h *ExchangeHandler) logListValidationFailure(c *fiber.Ctx, err error) {
|
||||
logger.GetAppLogger().Warn("换货列表参数验证失败",
|
||||
zap.String("method", c.Method()),
|
||||
zap.String("path", c.Path()),
|
||||
zap.String("query", c.Context().QueryArgs().String()),
|
||||
zap.Error(err),
|
||||
)
|
||||
}
|
||||
|
||||
// Get 查询换货单详情。
|
||||
// GET /api/admin/exchanges/:id
|
||||
func (h *ExchangeHandler) Get(c *fiber.Ctx) error {
|
||||
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
@@ -65,6 +93,8 @@ func (h *ExchangeHandler) Get(c *fiber.Ctx) error {
|
||||
return response.Success(c, data)
|
||||
}
|
||||
|
||||
// Ship 执行物流换货发货。
|
||||
// POST /api/admin/exchanges/:id/ship
|
||||
func (h *ExchangeHandler) Ship(c *fiber.Ctx) error {
|
||||
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
@@ -86,6 +116,8 @@ func (h *ExchangeHandler) Ship(c *fiber.Ctx) error {
|
||||
return response.Success(c, data)
|
||||
}
|
||||
|
||||
// Complete 确认换货完成。
|
||||
// POST /api/admin/exchanges/:id/complete
|
||||
func (h *ExchangeHandler) Complete(c *fiber.Ctx) error {
|
||||
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
@@ -98,6 +130,8 @@ func (h *ExchangeHandler) Complete(c *fiber.Ctx) error {
|
||||
return response.Success(c, nil)
|
||||
}
|
||||
|
||||
// Cancel 取消换货单。
|
||||
// POST /api/admin/exchanges/:id/cancel
|
||||
func (h *ExchangeHandler) Cancel(c *fiber.Ctx) error {
|
||||
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
@@ -118,6 +152,8 @@ func (h *ExchangeHandler) Cancel(c *fiber.Ctx) error {
|
||||
return response.Success(c, nil)
|
||||
}
|
||||
|
||||
// Renew 将已换出的旧资产转为新资产。
|
||||
// POST /api/admin/exchanges/:id/renew
|
||||
func (h *ExchangeHandler) Renew(c *fiber.Ctx) error {
|
||||
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
|
||||
Reference in New Issue
Block a user