refactor(handler): 按业务域拆分 merchant.go(847 → 最大 182 行)
纯文件级移动,无行为变更: - merchant.go 结构体、构造函数与跨域共用辅助(mapKeys/pageParams) - merchant_product.go 商品目录与分配 - merchant_order.go 订单、人工/测试下单与发货链接 - merchant_wallet.go 钱包、积分流水与管理员调账 - merchant_api_client.go API 客户端与回调订阅 - merchant_member.go 成员与角色 - merchant_platform.go 平台侧租户管理 校验:与 HEAD 的声明行 diff 为空,MerchantHandler 方法数 41 → 41。
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"affiliate_dash/internal/middleware"
|
||||
"affiliate_dash/internal/pkg/response"
|
||||
"affiliate_dash/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func (h *MerchantHandler) ListOrders(c *gin.Context) {
|
||||
page, size := pageParams(c)
|
||||
orderSource := strings.TrimSpace(c.Query("order_source"))
|
||||
list, total, err := h.fulfillmentSvc.ListOrders(middleware.GetMerchantID(c), page, size, c.Query("order_status"), orderSource, c.Query("keyword"))
|
||||
if err != nil {
|
||||
if err.Error() == "无效的订单来源" || err.Error() == "无效的订单状态" {
|
||||
response.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
response.ServerError(c, err.Error())
|
||||
return
|
||||
}
|
||||
response.Page(c, list, total, page, size)
|
||||
}
|
||||
|
||||
type merchantManualOrderReq struct {
|
||||
ClientOrderNo string `json:"client_order_no" binding:"required,max=96"`
|
||||
SKU string `json:"sku" binding:"required,max=96"`
|
||||
Quantity int64 `json:"quantity"`
|
||||
BuyerReference string `json:"buyer_reference" binding:"max=128"`
|
||||
GameAccount string `json:"game_account" binding:"max=128"`
|
||||
Note string `json:"note" binding:"max=512"`
|
||||
}
|
||||
|
||||
// CreateManualOrder 在商户后台创建真实订单;扣款、库存和回调与开放 API 下单保持一致。
|
||||
func (h *MerchantHandler) CreateManualOrder(c *gin.Context) {
|
||||
var req merchantManualOrderReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "参数错误:商户单号与商品必填")
|
||||
return
|
||||
}
|
||||
data := map[string]string{}
|
||||
if gameAccount := strings.TrimSpace(req.GameAccount); gameAccount != "" {
|
||||
data["game_account"] = gameAccount
|
||||
}
|
||||
if note := strings.TrimSpace(req.Note); note != "" {
|
||||
data["manual_note"] = note
|
||||
}
|
||||
result, err := h.fulfillmentSvc.CreateManualOrder(service.CreateManualOrderInput{
|
||||
MerchantID: middleware.GetMerchantID(c),
|
||||
ActorUserID: middleware.GetUserID(c),
|
||||
ClientOrderNo: req.ClientOrderNo,
|
||||
SKU: req.SKU,
|
||||
Quantity: req.Quantity,
|
||||
BuyerReference: req.BuyerReference,
|
||||
RequestData: data,
|
||||
})
|
||||
if err != nil {
|
||||
response.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
canShip, cannotShipReason := service.CanFulfill(result.Order)
|
||||
response.OK(c, gin.H{
|
||||
"order": result.Order,
|
||||
"idempotent": result.Idempotent,
|
||||
"can_ship": canShip,
|
||||
"cannot_ship_reason": cannotShipReason,
|
||||
})
|
||||
}
|
||||
|
||||
type merchantTestOrderReq struct {
|
||||
SKU string `json:"sku" binding:"required"`
|
||||
BuyerReference string `json:"buyer_reference"`
|
||||
Note string `json:"note"`
|
||||
OrderStatus string `json:"order_status"`
|
||||
}
|
||||
|
||||
func (h *MerchantHandler) CreateTestOrder(c *gin.Context) {
|
||||
var req merchantTestOrderReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "参数错误:sku 必填")
|
||||
return
|
||||
}
|
||||
order, err := h.fulfillmentSvc.CreateTestOrder(service.CreateTestOrderInput{
|
||||
MerchantID: middleware.GetMerchantID(c),
|
||||
ActorUserID: middleware.GetUserID(c),
|
||||
SKU: req.SKU,
|
||||
BuyerReference: req.BuyerReference,
|
||||
Note: req.Note,
|
||||
OrderStatus: req.OrderStatus,
|
||||
})
|
||||
if err != nil {
|
||||
response.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
canShip, cannotShipReason := service.CanFulfill(order)
|
||||
response.OK(c, gin.H{
|
||||
"order": order,
|
||||
"can_ship": canShip,
|
||||
"cannot_ship_reason": cannotShipReason,
|
||||
})
|
||||
}
|
||||
|
||||
type merchantCancelOrderReq struct {
|
||||
Reason string `json:"reason" binding:"required,max=512"`
|
||||
}
|
||||
|
||||
// CancelOrder 商户后台取消订单:全额退还积分并回补库存,取消后订单不可再发货。
|
||||
func (h *MerchantHandler) CancelOrder(c *gin.Context) {
|
||||
var req merchantCancelOrderReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "参数错误:取消原因必填且最长 512 字")
|
||||
return
|
||||
}
|
||||
order, err := h.fulfillmentSvc.CancelOrderByUser(middleware.GetMerchantID(c), middleware.GetUserID(c), c.Param("order_no"), req.Reason)
|
||||
if err != nil {
|
||||
response.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
response.OK(c, gin.H{
|
||||
"order": order,
|
||||
"refund_amount": order.Amount,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *MerchantHandler) GetDeliveryLink(c *gin.Context) {
|
||||
link, err := h.deliverySvc.GetOrCreateDeliveryLink(middleware.GetMerchantID(c), c.Param("order_no"), requestBaseURL(c))
|
||||
if err != nil {
|
||||
writeDeliveryError(c, err)
|
||||
return
|
||||
}
|
||||
response.OK(c, link)
|
||||
}
|
||||
|
||||
func (h *MerchantHandler) RevokeDeliveryLink(c *gin.Context) {
|
||||
if err := h.deliverySvc.RevokeDeliveryLink(middleware.GetMerchantID(c), middleware.GetUserID(c), c.Param("order_no")); err != nil {
|
||||
writeDeliveryError(c, err)
|
||||
return
|
||||
}
|
||||
response.OK(c, nil)
|
||||
}
|
||||
|
||||
func (h *MerchantHandler) RestoreDeliveryLink(c *gin.Context) {
|
||||
link, err := h.deliverySvc.RestoreDeliveryLink(middleware.GetMerchantID(c), middleware.GetUserID(c), c.Param("order_no"), requestBaseURL(c))
|
||||
if err != nil {
|
||||
writeDeliveryError(c, err)
|
||||
return
|
||||
}
|
||||
response.OK(c, link)
|
||||
}
|
||||
Reference in New Issue
Block a user