暂存一下,防止丢失
This commit is contained in:
@@ -1,13 +1,17 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"go.uber.org/zap"
|
||||
|
||||
shopapp "github.com/break/junhong_cmp_fiber/internal/application/shop"
|
||||
walletapp "github.com/break/junhong_cmp_fiber/internal/application/wallet"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||||
shopquery "github.com/break/junhong_cmp_fiber/internal/query/shop"
|
||||
shopService "github.com/break/junhong_cmp_fiber/internal/service/shop"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
@@ -17,8 +21,32 @@ import (
|
||||
|
||||
// ShopHandler 店铺管理处理器。
|
||||
type ShopHandler struct {
|
||||
service *shopService.Service
|
||||
validator *validator.Validate
|
||||
service *shopService.Service
|
||||
createService *shopapp.CreateService
|
||||
updateService *shopapp.UpdateService
|
||||
ownerQuery *shopquery.BusinessOwnerQuery
|
||||
changeCreditService *walletapp.ChangeCreditService
|
||||
validator *validator.Validate
|
||||
}
|
||||
|
||||
// SetChangeCreditService 注入既有店铺实际信用额度调整用例。
|
||||
func (h *ShopHandler) SetChangeCreditService(service *walletapp.ChangeCreditService) {
|
||||
h.changeCreditService = service
|
||||
}
|
||||
|
||||
// SetCreateService 注入店铺创建 Application 事务脚本。
|
||||
func (h *ShopHandler) SetCreateService(service *shopapp.CreateService) {
|
||||
h.createService = service
|
||||
}
|
||||
|
||||
// SetUpdateService 注入店铺更新 Application 事务脚本。
|
||||
func (h *ShopHandler) SetUpdateService(service *shopapp.UpdateService) {
|
||||
h.updateService = service
|
||||
}
|
||||
|
||||
// SetBusinessOwnerQuery 注入店铺业务员归属 Query。
|
||||
func (h *ShopHandler) SetBusinessOwnerQuery(query *shopquery.BusinessOwnerQuery) {
|
||||
h.ownerQuery = query
|
||||
}
|
||||
|
||||
// NewShopHandler 创建店铺管理处理器。
|
||||
@@ -49,7 +77,10 @@ func (h *ShopHandler) List(c *fiber.Ctx) error {
|
||||
|
||||
normalizeShopListPagination(&req)
|
||||
|
||||
shops, total, err := h.service.ListShopResponses(c.UserContext(), &req)
|
||||
if h.ownerQuery == nil {
|
||||
return errors.New(errors.CodeInternalError, "店铺业务员查询服务未配置")
|
||||
}
|
||||
shops, total, err := h.ownerQuery.List(c.UserContext(), req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -66,7 +97,83 @@ func (h *ShopHandler) 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.Bool("page_present", c.Context().QueryArgs().Has("page")),
|
||||
zap.Bool("page_size_present", c.Context().QueryArgs().Has("page_size")),
|
||||
zap.Error(err),
|
||||
)
|
||||
}
|
||||
|
||||
// Detail 查询店铺详情。
|
||||
// GET /api/admin/shops/:id
|
||||
func (h *ShopHandler) Detail(c *fiber.Ctx) error {
|
||||
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
||||
if err != nil || id == 0 || id > math.MaxInt64 {
|
||||
return errors.New(errors.CodeInvalidParam)
|
||||
}
|
||||
if h.ownerQuery == nil {
|
||||
return errors.New(errors.CodeInternalError, "店铺业务员查询服务未配置")
|
||||
}
|
||||
result, err := h.ownerQuery.Detail(c.UserContext(), uint(id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return response.Success(c, result)
|
||||
}
|
||||
|
||||
// UpdateCreditLimit 调整既有店铺代理主钱包实际信用额度。
|
||||
// PUT /api/admin/shops/:id/credit-limit
|
||||
func (h *ShopHandler) UpdateCreditLimit(c *fiber.Ctx) error {
|
||||
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
||||
if err != nil || id == 0 || id > math.MaxInt64 {
|
||||
return errors.New(errors.CodeInvalidParam)
|
||||
}
|
||||
var request dto.UpdateShopCreditLimitRequest
|
||||
if err := c.BodyParser(&request); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam)
|
||||
}
|
||||
if h.validator == nil || h.validator.Struct(&request) != nil {
|
||||
return errors.New(errors.CodeInvalidParam)
|
||||
}
|
||||
if h.changeCreditService == nil {
|
||||
return errors.New(errors.CodeInternalError, "店铺信用额度服务未配置")
|
||||
}
|
||||
result, err := h.changeCreditService.Execute(c.UserContext(), uint(id), *request.CreditEnabled, *request.CreditLimit, *request.Version)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return response.Success(c, result)
|
||||
}
|
||||
|
||||
// BusinessOwnerCandidates 查询当前可人工绑定的平台业务员候选。
|
||||
// GET /api/admin/shops/business-owner-candidates
|
||||
func (h *ShopHandler) BusinessOwnerCandidates(c *fiber.Ctx) error {
|
||||
var request dto.ShopBusinessOwnerCandidateRequest
|
||||
if err := c.QueryParser(&request); err != nil {
|
||||
h.logBusinessOwnerCandidateValidationFailure(c, err)
|
||||
return errors.New(errors.CodeInvalidParam)
|
||||
}
|
||||
if h.validator == nil {
|
||||
return errors.New(errors.CodeInternalError, "业务员候选校验器未配置")
|
||||
}
|
||||
if err := h.validator.Struct(request); err != nil {
|
||||
h.logBusinessOwnerCandidateValidationFailure(c, err)
|
||||
return errors.New(errors.CodeInvalidParam)
|
||||
}
|
||||
if h.ownerQuery == nil {
|
||||
return errors.New(errors.CodeInternalError, "店铺业务员查询服务未配置")
|
||||
}
|
||||
items, total, page, pageSize, err := h.ownerQuery.Candidates(c.UserContext(), request)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return response.SuccessWithPagination(c, items, total, page, pageSize)
|
||||
}
|
||||
|
||||
func (h *ShopHandler) logBusinessOwnerCandidateValidationFailure(c *fiber.Ctx, err error) {
|
||||
logger.GetAppLogger().Warn("店铺业务员候选参数验证失败",
|
||||
zap.String("method", c.Method()),
|
||||
zap.String("path", c.Path()),
|
||||
zap.Bool("keyword_present", c.Context().QueryArgs().Has("keyword")),
|
||||
zap.Error(err),
|
||||
)
|
||||
}
|
||||
@@ -87,8 +194,20 @@ func (h *ShopHandler) Create(c *fiber.Ctx) error {
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
if h.validator == nil {
|
||||
return errors.New(errors.CodeInternalError, "店铺创建校验器未配置")
|
||||
}
|
||||
if err := h.validator.Struct(&req); err != nil {
|
||||
logger.GetAppLogger().Warn("店铺创建参数验证失败",
|
||||
zap.String("method", c.Method()), zap.String("path", c.Path()), zap.Error(err))
|
||||
return errors.New(errors.CodeInvalidParam)
|
||||
}
|
||||
|
||||
shop, err := h.service.Create(c.UserContext(), &req)
|
||||
createService := h.createService
|
||||
if createService == nil {
|
||||
return errors.New(errors.CodeInternalError, "店铺创建服务未配置")
|
||||
}
|
||||
shop, err := createService.Create(c.UserContext(), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -108,8 +227,19 @@ func (h *ShopHandler) Update(c *fiber.Ctx) error {
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
if h.validator == nil {
|
||||
return errors.New(errors.CodeInternalError, "店铺更新校验器未配置")
|
||||
}
|
||||
if err := h.validator.Struct(&req); err != nil {
|
||||
logger.GetAppLogger().Warn("店铺更新参数验证失败",
|
||||
zap.String("method", c.Method()), zap.String("path", c.Path()), zap.Error(err))
|
||||
return errors.New(errors.CodeInvalidParam)
|
||||
}
|
||||
|
||||
shop, err := h.service.Update(c.UserContext(), uint(id), &req)
|
||||
if h.updateService == nil {
|
||||
return errors.New(errors.CodeInternalError, "店铺更新服务未配置")
|
||||
}
|
||||
shop, err := h.updateService.Update(c.UserContext(), uint(id), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user