暂存一下,防止丢失

This commit is contained in:
2026-07-24 16:07:18 +08:00
parent 5d6e23f1a5
commit a18ed8bc8d
180 changed files with 13597 additions and 1986 deletions

View File

@@ -0,0 +1,146 @@
package admin
import (
"math"
"strconv"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
notificationapp "github.com/break/junhong_cmp_fiber/internal/application/notification"
"github.com/break/junhong_cmp_fiber/internal/model/dto"
notificationquery "github.com/break/junhong_cmp_fiber/internal/query/notification"
"github.com/break/junhong_cmp_fiber/pkg/errors"
"github.com/break/junhong_cmp_fiber/pkg/logger"
"github.com/break/junhong_cmp_fiber/pkg/middleware"
"github.com/break/junhong_cmp_fiber/pkg/response"
"go.uber.org/zap"
)
// NotificationHandler 提供当前后台账号的站内通知接口。
type NotificationHandler struct {
query *notificationquery.Query
readService *notificationapp.ReadService
validate *validator.Validate
}
// NewNotificationHandler 创建后台站内通知 Handler。
func NewNotificationHandler(query *notificationquery.Query, readService *notificationapp.ReadService, validate *validator.Validate) *NotificationHandler {
return &NotificationHandler{query: query, readService: readService, validate: validate}
}
// UnreadCount 查询当前后台账号的通知未读数。
// GET /api/admin/notifications/unread-count
func (h *NotificationHandler) UnreadCount(c *fiber.Ctx) error {
recipientID := middleware.GetUserIDFromContext(c.UserContext())
result, err := h.query.UnreadCount(c.UserContext(), recipientID)
if err != nil {
return err
}
return response.Success(c, result)
}
// UnreadSummary 查询当前后台账号的固定分类未读汇总。
// GET /api/admin/notifications/unread-summary
func (h *NotificationHandler) UnreadSummary(c *fiber.Ctx) error {
recipientID := middleware.GetUserIDFromContext(c.UserContext())
result, err := h.query.UnreadSummary(c.UserContext(), recipientID)
if err != nil {
return err
}
return response.Success(c, result)
}
// List 查询当前后台账号的未过期通知列表。
// GET /api/admin/notifications
func (h *NotificationHandler) List(c *fiber.Ctx) error {
var request dto.NotificationListRequest
if err := c.QueryParser(&request); err != nil {
logNotificationListValidationFailure(c, request, err)
return errors.New(errors.CodeInvalidParam)
}
if h.validate != nil {
if err := h.validate.Struct(request); err != nil {
logNotificationListValidationFailure(c, request, err)
return errors.New(errors.CodeInvalidParam)
}
}
recipientID := middleware.GetUserIDFromContext(c.UserContext())
result, err := h.query.List(c.UserContext(), recipientID, request)
if err != nil {
return err
}
return response.SuccessWithPagination(c, result.Items, result.Total, result.Page, result.Size)
}
func logNotificationListValidationFailure(c *fiber.Ctx, request dto.NotificationListRequest, err error) {
logger.GetAppLogger().Warn("站内通知列表参数验证失败",
zap.String("method", c.Method()),
zap.String("path", c.Path()),
zap.Int("page", request.Page),
zap.Int("page_size", request.PageSize),
zap.Error(err),
)
}
// MarkRead 将当前后台账号的一条通知幂等标记为已读。
// PUT /api/admin/notifications/:id/read
func (h *NotificationHandler) MarkRead(c *fiber.Ctx) error {
notificationID, err := strconv.ParseUint(c.Params("id"), 10, 64)
if err != nil || notificationID == 0 || notificationID > math.MaxInt64 {
return errors.New(errors.CodeInvalidParam)
}
recipientID := middleware.GetUserIDFromContext(c.UserContext())
if err := h.readService.MarkRead(c.UserContext(), recipientID, uint(notificationID)); err != nil {
return err
}
return response.Success(c, dto.NotificationReadResponse{Success: true})
}
// Target 解析当前后台账号通知的受控结构化目标。
// GET /api/admin/notifications/:id/target
func (h *NotificationHandler) Target(c *fiber.Ctx) error {
notificationID, err := strconv.ParseUint(c.Params("id"), 10, 64)
if err != nil || notificationID == 0 || notificationID > math.MaxInt64 {
return errors.New(errors.CodeInvalidParam)
}
recipientID := middleware.GetUserIDFromContext(c.UserContext())
result, err := h.query.Target(c.UserContext(), recipientID, uint(notificationID))
if err != nil {
return err
}
return response.Success(c, result)
}
// MarkAllRead 将当前后台账号全部或指定类别通知幂等标记为已读。
// PUT /api/admin/notifications/read-all
func (h *NotificationHandler) MarkAllRead(c *fiber.Ctx) error {
var request dto.NotificationReadAllRequest
if len(c.Body()) > 0 {
if err := c.BodyParser(&request); err != nil {
logNotificationReadAllValidationFailure(c, request, err)
return errors.New(errors.CodeInvalidParam)
}
}
if h.validate != nil {
if err := h.validate.Struct(request); err != nil {
logNotificationReadAllValidationFailure(c, request, err)
return errors.New(errors.CodeInvalidParam)
}
}
recipientID := middleware.GetUserIDFromContext(c.UserContext())
result, err := h.readService.MarkAllRead(c.UserContext(), recipientID, request)
if err != nil {
return err
}
return response.Success(c, result)
}
func logNotificationReadAllValidationFailure(c *fiber.Ctx, request dto.NotificationReadAllRequest, err error) {
logger.GetAppLogger().Warn("站内通知批量已读参数验证失败",
zap.String("method", c.Method()),
zap.String("path", c.Path()),
zap.Bool("category_present", request.Category != ""),
zap.Error(err),
)
}

View File

@@ -11,14 +11,21 @@ import (
"github.com/break/junhong_cmp_fiber/pkg/logger"
"github.com/break/junhong_cmp_fiber/pkg/response"
roleApp "github.com/break/junhong_cmp_fiber/internal/application/role"
"github.com/break/junhong_cmp_fiber/internal/model/dto"
roleService "github.com/break/junhong_cmp_fiber/internal/service/role"
)
// RoleHandler 角色 Handler
type RoleHandler struct {
service *roleService.Service
validator *validator.Validate
service *roleService.Service
defaultCreditService *roleApp.DefaultCreditService
validator *validator.Validate
}
// SetDefaultCreditService 设置角色默认信用模板应用服务。
func (h *RoleHandler) SetDefaultCreditService(service *roleApp.DefaultCreditService) {
h.defaultCreditService = service
}
// NewRoleHandler 创建角色 Handler
@@ -254,3 +261,36 @@ func (h *RoleHandler) UpdateStatus(c *fiber.Ctx) error {
return response.Success(c, nil)
}
// UpdateDefaultCredit 更新客户角色的新建代理默认信用模板。
// PUT /api/admin/roles/:id/default-credit
func (h *RoleHandler) UpdateDefaultCredit(c *fiber.Ctx) error {
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
if err != nil || id == 0 {
return errors.New(errors.CodeInvalidParam, "无效的角色 ID")
}
var req dto.UpdateRoleDefaultCreditRequest
if err := c.BodyParser(&req); err != nil {
return errors.New(errors.CodeInvalidParam)
}
if err := h.validator.Struct(&req); err != nil {
logger.GetAppLogger().Warn("角色默认信用参数验证失败", zap.Error(err))
return errors.New(errors.CodeInvalidParam)
}
if h.defaultCreditService == nil {
return errors.New(errors.CodeInternalError, "角色默认信用服务未配置")
}
role, err := h.defaultCreditService.Update(c.UserContext(), uint(id), *req.CreditEnabled, *req.CreditLimit)
if err != nil {
return err
}
return response.Success(c, dto.RoleDefaultCreditResponse{
RoleID: role.ID,
CreditEnabled: role.DefaultCreditEnabled,
CreditLimit: role.DefaultCreditLimit,
Scope: "new_shops_only",
AffectsExistingWallets: false,
})
}

View File

@@ -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
}

View File

@@ -6,6 +6,7 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/break/junhong_cmp_fiber/internal/model/dto"
shopQuery "github.com/break/junhong_cmp_fiber/internal/query/shop"
shopCommissionService "github.com/break/junhong_cmp_fiber/internal/service/shop_commission"
"github.com/break/junhong_cmp_fiber/pkg/errors"
"github.com/break/junhong_cmp_fiber/pkg/response"
@@ -13,7 +14,8 @@ import (
// ShopCommissionHandler 代理商资金管理 Handler
type ShopCommissionHandler struct {
service *shopCommissionService.Service
service *shopCommissionService.Service
fundSummaryQuery *shopQuery.FundSummaryQuery
}
// NewShopCommissionHandler 创建代理商资金管理 Handler
@@ -21,6 +23,11 @@ func NewShopCommissionHandler(service *shopCommissionService.Service) *ShopCommi
return &ShopCommissionHandler{service: service}
}
// SetFundSummaryQuery 注入代理商资金概况 Query。
func (h *ShopCommissionHandler) SetFundSummaryQuery(query *shopQuery.FundSummaryQuery) {
h.fundSummaryQuery = query
}
// ListFundSummary 代理商资金概况列表
// GET /api/admin/shops/fund-summary
func (h *ShopCommissionHandler) ListFundSummary(c *fiber.Ctx) error {
@@ -29,7 +36,10 @@ func (h *ShopCommissionHandler) ListFundSummary(c *fiber.Ctx) error {
return errors.New(errors.CodeInvalidParam)
}
result, err := h.service.ListShopFundSummary(c.UserContext(), &req)
if h.fundSummaryQuery == nil {
return errors.New(errors.CodeInternalError, "代理商资金概况查询能力未配置")
}
result, err := h.fundSummaryQuery.List(c.UserContext(), req)
if err != nil {
return err
}