548 lines
16 KiB
Go
548 lines
16 KiB
Go
package handler
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
|
|
"affiliate_dash/internal/middleware"
|
|
"affiliate_dash/internal/pkg/response"
|
|
"affiliate_dash/internal/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// MerchantHandler 提供商户后台与平台管理员的多租户管理能力。
|
|
type MerchantHandler struct {
|
|
merchantSvc *service.MerchantService
|
|
fulfillmentSvc *service.FulfillmentService
|
|
callbackSvc *service.CallbackService
|
|
deliverySvc *service.DeliveryService
|
|
}
|
|
|
|
func NewMerchantHandler(merchantSvc *service.MerchantService, fulfillmentSvc *service.FulfillmentService, callbackSvc *service.CallbackService, deliverySvc *service.DeliveryService) *MerchantHandler {
|
|
return &MerchantHandler{
|
|
merchantSvc: merchantSvc,
|
|
fulfillmentSvc: fulfillmentSvc,
|
|
callbackSvc: callbackSvc,
|
|
deliverySvc: deliverySvc,
|
|
}
|
|
}
|
|
|
|
func (h *MerchantHandler) Current(c *gin.Context) {
|
|
merchant, err := h.merchantSvc.GetMerchant(middleware.GetMerchantID(c))
|
|
if err != nil {
|
|
response.NotFound(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, gin.H{
|
|
"merchant": merchant,
|
|
"role": middleware.GetMerchantRole(c),
|
|
})
|
|
}
|
|
|
|
func (h *MerchantHandler) ListProducts(c *gin.Context) {
|
|
page, size := pageParams(c)
|
|
list, total, err := h.merchantSvc.ListMerchantProducts(middleware.GetMerchantID(c), page, size, false)
|
|
if err != nil {
|
|
response.ServerError(c, err.Error())
|
|
return
|
|
}
|
|
response.Page(c, list, total, page, size)
|
|
}
|
|
|
|
type merchantProductReq struct {
|
|
ProductCode string `json:"product_code"`
|
|
ProductName string `json:"product_name"`
|
|
Category string `json:"category"`
|
|
Description string `json:"description"`
|
|
Attributes string `json:"attributes"`
|
|
SKU string `json:"sku" binding:"required"`
|
|
DisplayName string `json:"display_name"`
|
|
PriceAmount int64 `json:"price_amount"`
|
|
CostAmount int64 `json:"cost_amount"`
|
|
Currency string `json:"currency"`
|
|
Stock int64 `json:"stock"`
|
|
Status string `json:"status"`
|
|
FulfillmentConfig string `json:"fulfillment_config"`
|
|
}
|
|
|
|
func (h *MerchantHandler) CreateProduct(c *gin.Context) {
|
|
var req merchantProductReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "参数错误:sku 必填")
|
|
return
|
|
}
|
|
product, err := h.merchantSvc.CreateMerchantProduct(middleware.GetMerchantID(c), service.CreateMerchantProductInput{
|
|
ProductCode: req.ProductCode,
|
|
ProductName: req.ProductName,
|
|
Category: req.Category,
|
|
Description: req.Description,
|
|
Attributes: req.Attributes,
|
|
SKU: req.SKU,
|
|
DisplayName: req.DisplayName,
|
|
PriceAmount: req.PriceAmount,
|
|
CostAmount: req.CostAmount,
|
|
Currency: req.Currency,
|
|
Stock: req.Stock,
|
|
Status: req.Status,
|
|
FulfillmentConfig: req.FulfillmentConfig,
|
|
}, middleware.GetUserID(c))
|
|
if err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, product)
|
|
}
|
|
|
|
type merchantProductUpdateReq struct {
|
|
DisplayName *string `json:"display_name"`
|
|
PriceAmount *int64 `json:"price_amount"`
|
|
CostAmount *int64 `json:"cost_amount"`
|
|
Stock *int64 `json:"stock"`
|
|
Status *string `json:"status"`
|
|
FulfillmentConfig *string `json:"fulfillment_config"`
|
|
}
|
|
|
|
func (h *MerchantHandler) UpdateProduct(c *gin.Context) {
|
|
var req merchantProductUpdateReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "参数错误")
|
|
return
|
|
}
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err := h.merchantSvc.UpdateMerchantProduct(middleware.GetMerchantID(c), uint(id), service.UpdateMerchantProductInput{
|
|
DisplayName: req.DisplayName,
|
|
PriceAmount: req.PriceAmount,
|
|
CostAmount: req.CostAmount,
|
|
Stock: req.Stock,
|
|
Status: req.Status,
|
|
FulfillmentConfig: req.FulfillmentConfig,
|
|
}, middleware.GetUserID(c)); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, nil)
|
|
}
|
|
|
|
func (h *MerchantHandler) ListOrders(c *gin.Context) {
|
|
page, size := pageParams(c)
|
|
list, total, err := h.fulfillmentSvc.ListOrders(middleware.GetMerchantID(c), page, size, c.Query("order_status"))
|
|
if err != nil {
|
|
response.ServerError(c, err.Error())
|
|
return
|
|
}
|
|
response.Page(c, list, total, page, size)
|
|
}
|
|
|
|
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,
|
|
})
|
|
}
|
|
|
|
func (h *MerchantHandler) GetDeliveryLink(c *gin.Context) {
|
|
link, err := h.deliverySvc.GetOrCreateDeliveryLink(middleware.GetMerchantID(c), c.Param("order_no"))
|
|
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), 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), c.Param("order_no"))
|
|
if err != nil {
|
|
writeDeliveryError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, link)
|
|
}
|
|
|
|
func (h *MerchantHandler) GetWallet(c *gin.Context) {
|
|
wallet, err := h.fulfillmentSvc.GetWallet(middleware.GetMerchantID(c))
|
|
if err != nil {
|
|
response.ServerError(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, wallet)
|
|
}
|
|
|
|
func (h *MerchantHandler) ListWalletLedger(c *gin.Context) {
|
|
page, size := pageParams(c)
|
|
referenceNo := c.Query("reference_no")
|
|
entryType := c.Query("type")
|
|
list, total, err := h.fulfillmentSvc.ListWalletLedger(middleware.GetMerchantID(c), page, size, referenceNo, entryType)
|
|
if err != nil {
|
|
response.ServerError(c, err.Error())
|
|
return
|
|
}
|
|
response.Page(c, list, total, page, size)
|
|
}
|
|
|
|
type walletAdjustReq struct {
|
|
Amount int64 `json:"amount" binding:"required"`
|
|
IdempotencyKey string `json:"idempotency_key" binding:"required"`
|
|
Note string `json:"note"`
|
|
}
|
|
|
|
func (h *MerchantHandler) AdjustWallet(c *gin.Context) {
|
|
var req walletAdjustReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "参数错误:amount 与 idempotency_key 必填")
|
|
return
|
|
}
|
|
wallet, err := h.fulfillmentSvc.AdjustWallet(service.WalletAdjustInput{
|
|
MerchantID: middleware.GetMerchantID(c),
|
|
ActorUserID: middleware.GetUserID(c),
|
|
Amount: req.Amount,
|
|
IdempotencyKey: req.IdempotencyKey,
|
|
Note: req.Note,
|
|
})
|
|
if err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, wallet)
|
|
}
|
|
|
|
func (h *MerchantHandler) ListAPIClients(c *gin.Context) {
|
|
clients, err := h.merchantSvc.ListAPIClients(middleware.GetMerchantID(c))
|
|
if err != nil {
|
|
response.ServerError(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, clients)
|
|
}
|
|
|
|
type apiClientReq struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Scopes string `json:"scopes" binding:"required"`
|
|
SignatureVersion string `json:"signature_version"`
|
|
ExpiresAt string `json:"expires_at"`
|
|
}
|
|
|
|
func (h *MerchantHandler) CreateAPIClient(c *gin.Context) {
|
|
var req apiClientReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "参数错误:name 与 scopes 必填")
|
|
return
|
|
}
|
|
var expiresAt *time.Time
|
|
if req.ExpiresAt != "" {
|
|
value, err := time.Parse(time.RFC3339, req.ExpiresAt)
|
|
if err != nil {
|
|
response.BadRequest(c, "expires_at 必须是 RFC3339 时间")
|
|
return
|
|
}
|
|
expiresAt = &value
|
|
}
|
|
credential, err := h.merchantSvc.CreateAPIClient(middleware.GetMerchantID(c), service.CreateAPIClientInput{
|
|
Name: req.Name,
|
|
Scopes: req.Scopes,
|
|
SignatureVersion: req.SignatureVersion,
|
|
ExpiresAt: expiresAt,
|
|
}, middleware.GetUserID(c))
|
|
if err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, credential)
|
|
}
|
|
|
|
type statusReq struct {
|
|
Status string `json:"status" binding:"required"`
|
|
}
|
|
|
|
func (h *MerchantHandler) UpdateAPIClientStatus(c *gin.Context) {
|
|
var req statusReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "参数错误")
|
|
return
|
|
}
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err := h.merchantSvc.UpdateAPIClientStatus(middleware.GetMerchantID(c), uint(id), req.Status, middleware.GetUserID(c)); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, nil)
|
|
}
|
|
|
|
// DeleteAPIClient DELETE /api/merchant/api-clients/:id
|
|
func (h *MerchantHandler) DeleteAPIClient(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err := h.merchantSvc.DeleteAPIClient(middleware.GetMerchantID(c), uint(id), middleware.GetUserID(c)); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, nil)
|
|
}
|
|
|
|
func (h *MerchantHandler) ListCallbacks(c *gin.Context) {
|
|
list, err := h.callbackSvc.ListSubscriptions(middleware.GetMerchantID(c))
|
|
if err != nil {
|
|
response.ServerError(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, list)
|
|
}
|
|
|
|
type callbackReq struct {
|
|
Name string `json:"name" binding:"required"`
|
|
URL string `json:"url" binding:"required"`
|
|
Events string `json:"events" binding:"required"`
|
|
}
|
|
|
|
func (h *MerchantHandler) CreateCallback(c *gin.Context) {
|
|
var req callbackReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "参数错误:name、url、events 必填")
|
|
return
|
|
}
|
|
credential, err := h.callbackSvc.CreateSubscription(middleware.GetMerchantID(c), service.CreateCallbackInput{
|
|
Name: req.Name,
|
|
URL: req.URL,
|
|
Events: req.Events,
|
|
}, middleware.GetUserID(c))
|
|
if err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, credential)
|
|
}
|
|
|
|
func (h *MerchantHandler) UpdateCallbackStatus(c *gin.Context) {
|
|
var req statusReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "参数错误")
|
|
return
|
|
}
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err := h.callbackSvc.UpdateSubscriptionStatus(middleware.GetMerchantID(c), uint(id), req.Status, middleware.GetUserID(c)); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, nil)
|
|
}
|
|
|
|
func (h *MerchantHandler) ListMembers(c *gin.Context) {
|
|
members, err := h.merchantSvc.ListMembers(middleware.GetMerchantID(c))
|
|
if err != nil {
|
|
response.ServerError(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, members)
|
|
}
|
|
|
|
type addMemberReq struct {
|
|
UserID uint `json:"user_id" binding:"required"`
|
|
Role string `json:"role" binding:"required"`
|
|
IsDefault bool `json:"is_default"`
|
|
}
|
|
|
|
func (h *MerchantHandler) AddCurrentMerchantMember(c *gin.Context) {
|
|
var req addMemberReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "参数错误:user_id 与 role 必填")
|
|
return
|
|
}
|
|
member, err := h.merchantSvc.AddMember(middleware.GetMerchantID(c), service.AddMemberInput{
|
|
UserID: req.UserID,
|
|
Role: req.Role,
|
|
IsDefault: req.IsDefault,
|
|
}, middleware.GetUserID(c))
|
|
if err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, member)
|
|
}
|
|
|
|
func (h *MerchantHandler) ListPlatformMerchants(c *gin.Context) {
|
|
page, size := pageParams(c)
|
|
list, total, err := h.merchantSvc.ListMerchants(page, size)
|
|
if err != nil {
|
|
response.ServerError(c, err.Error())
|
|
return
|
|
}
|
|
response.Page(c, list, total, page, size)
|
|
}
|
|
|
|
// ListProductCatalog 返回自营商户的全部可售商品,作为平台默认商品目录供分配。
|
|
func (h *MerchantHandler) ListProductCatalog(c *gin.Context) {
|
|
list, err := h.merchantSvc.ListProductCatalog()
|
|
if err != nil {
|
|
response.ServerError(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, list)
|
|
}
|
|
|
|
// ListMerchantProductsByAdmin 供平台管理员查看指定商户的可售商品。
|
|
func (h *MerchantHandler) ListMerchantProductsByAdmin(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
list, err := h.merchantSvc.ListMerchantProductsByAdmin(uint(id))
|
|
if err != nil {
|
|
response.ServerError(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, list)
|
|
}
|
|
|
|
type assignProductsReq struct {
|
|
CatalogIDs []uint `json:"catalog_ids"`
|
|
}
|
|
|
|
// AssignProducts 按商品目录 ID 批量同步商户的可售商品。
|
|
func (h *MerchantHandler) AssignProducts(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
var req assignProductsReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "参数错误")
|
|
return
|
|
}
|
|
count, err := h.merchantSvc.AssignProducts(uint(id), service.AssignProductsInput{
|
|
CatalogIDs: req.CatalogIDs,
|
|
}, middleware.GetUserID(c))
|
|
if err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"assigned": count})
|
|
}
|
|
|
|
type createMerchantReq struct {
|
|
Code string `json:"code" binding:"required"`
|
|
Name string `json:"name" binding:"required"`
|
|
ContactName string `json:"contact_name"`
|
|
ContactInfo string `json:"contact_info"`
|
|
OwnerUserID uint `json:"owner_user_id"`
|
|
OwnerUsername string `json:"owner_username"`
|
|
OwnerPassword string `json:"owner_password"`
|
|
OwnerNickname string `json:"owner_nickname"`
|
|
Features string `json:"features"`
|
|
FeeType string `json:"fee_type"`
|
|
FeeRateBP int64 `json:"fee_rate_bp"`
|
|
FeeFixedAmount int64 `json:"fee_fixed_amount"`
|
|
}
|
|
|
|
func (h *MerchantHandler) CreateMerchant(c *gin.Context) {
|
|
var req createMerchantReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "参数错误:code、name 必填")
|
|
return
|
|
}
|
|
merchant, err := h.merchantSvc.CreateMerchant(service.CreateMerchantInput{
|
|
Code: req.Code,
|
|
Name: req.Name,
|
|
ContactName: req.ContactName,
|
|
ContactInfo: req.ContactInfo,
|
|
OwnerUserID: req.OwnerUserID,
|
|
OwnerUsername: req.OwnerUsername,
|
|
OwnerPassword: req.OwnerPassword,
|
|
OwnerNickname: req.OwnerNickname,
|
|
Features: req.Features,
|
|
FeeType: req.FeeType,
|
|
FeeRateBP: req.FeeRateBP,
|
|
FeeFixedAmount: req.FeeFixedAmount,
|
|
}, middleware.GetUserID(c))
|
|
if err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, merchant)
|
|
}
|
|
|
|
type updateMerchantSettingsReq struct {
|
|
Name *string `json:"name"`
|
|
Status *string `json:"status"`
|
|
ContactName *string `json:"contact_name"`
|
|
ContactInfo *string `json:"contact_info"`
|
|
Features *string `json:"features"`
|
|
FeeType *string `json:"fee_type"`
|
|
FeeRateBP *int64 `json:"fee_rate_bp"`
|
|
FeeFixedAmount *int64 `json:"fee_fixed_amount"`
|
|
}
|
|
|
|
func (h *MerchantHandler) UpdateMerchantSettings(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
var req updateMerchantSettingsReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "参数错误")
|
|
return
|
|
}
|
|
if err := h.merchantSvc.UpdateMerchantSettings(uint(id), service.UpdateMerchantSettingsInput{
|
|
Name: req.Name,
|
|
Status: req.Status,
|
|
ContactName: req.ContactName,
|
|
ContactInfo: req.ContactInfo,
|
|
Features: req.Features,
|
|
FeeType: req.FeeType,
|
|
FeeRateBP: req.FeeRateBP,
|
|
FeeFixedAmount: req.FeeFixedAmount,
|
|
}, middleware.GetUserID(c)); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, nil)
|
|
}
|
|
|
|
func (h *MerchantHandler) AddPlatformMerchantMember(c *gin.Context) {
|
|
var req addMemberReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "参数错误:user_id 与 role 必填")
|
|
return
|
|
}
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
member, err := h.merchantSvc.AddMember(uint(id), service.AddMemberInput{
|
|
UserID: req.UserID,
|
|
Role: req.Role,
|
|
IsDefault: req.IsDefault,
|
|
}, middleware.GetUserID(c))
|
|
if err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, member)
|
|
}
|
|
|
|
func pageParams(c *gin.Context) (int, int) {
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
size, _ := strconv.Atoi(c.DefaultQuery("size", "20"))
|
|
return page, size
|
|
}
|