实现多商户履约平台基础
This commit is contained in:
@@ -0,0 +1,383 @@
|
||||
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
|
||||
}
|
||||
|
||||
func NewMerchantHandler(merchantSvc *service.MerchantService, fulfillmentSvc *service.FulfillmentService, callbackSvc *service.CallbackService) *MerchantHandler {
|
||||
return &MerchantHandler{
|
||||
merchantSvc: merchantSvc,
|
||||
fulfillmentSvc: fulfillmentSvc,
|
||||
callbackSvc: callbackSvc,
|
||||
}
|
||||
}
|
||||
|
||||
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("fulfillment_status"))
|
||||
if err != nil {
|
||||
response.ServerError(c, err.Error())
|
||||
return
|
||||
}
|
||||
response.Page(c, list, total, page, size)
|
||||
}
|
||||
|
||||
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)
|
||||
list, total, err := h.fulfillmentSvc.ListWalletLedger(middleware.GetMerchantID(c), page, size)
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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" binding:"required"`
|
||||
}
|
||||
|
||||
func (h *MerchantHandler) CreateMerchant(c *gin.Context) {
|
||||
var req createMerchantReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "参数错误:code、name、owner_user_id 必填")
|
||||
return
|
||||
}
|
||||
merchant, err := h.merchantSvc.CreateMerchant(service.CreateMerchantInput{
|
||||
Code: req.Code,
|
||||
Name: req.Name,
|
||||
ContactName: req.ContactName,
|
||||
ContactInfo: req.ContactInfo,
|
||||
OwnerUserID: req.OwnerUserID,
|
||||
}, middleware.GetUserID(c))
|
||||
if err != nil {
|
||||
response.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
response.OK(c, merchant)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user