All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 6m19s
- 订单管理:增加 payment_method 字段支持,合并代购订单逻辑 - 套餐系列分配:增加强充配置字段(enable_force_recharge、force_recharge_amount、force_recharge_trigger_type) - 数据库迁移:添加 force_recharge_trigger_type 字段 - 测试:更新订单服务测试用例 - OpenSpec:归档 fix-force-recharge-missing-interfaces 变更
141 lines
3.8 KiB
Go
141 lines
3.8 KiB
Go
package admin
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/break/junhong_cmp_fiber/internal/model"
|
|
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
|
orderService "github.com/break/junhong_cmp_fiber/internal/service/order"
|
|
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
|
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
|
"github.com/break/junhong_cmp_fiber/pkg/middleware"
|
|
"github.com/break/junhong_cmp_fiber/pkg/response"
|
|
)
|
|
|
|
type OrderHandler struct {
|
|
service *orderService.Service
|
|
}
|
|
|
|
func NewOrderHandler(service *orderService.Service) *OrderHandler {
|
|
return &OrderHandler{service: service}
|
|
}
|
|
|
|
func (h *OrderHandler) Create(c *fiber.Ctx) error {
|
|
var req dto.CreateOrderRequest
|
|
if err := c.BodyParser(&req); err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
|
}
|
|
|
|
ctx := c.UserContext()
|
|
userType := middleware.GetUserTypeFromContext(ctx)
|
|
shopID := middleware.GetShopIDFromContext(ctx)
|
|
|
|
if req.PaymentMethod == model.PaymentMethodOffline {
|
|
if userType != constants.UserTypeSuperAdmin && userType != constants.UserTypePlatform {
|
|
return errors.New(errors.CodeForbidden, "只有平台可以使用线下支付")
|
|
}
|
|
} else if req.PaymentMethod == model.PaymentMethodWallet {
|
|
if userType != constants.UserTypeAgent && userType != constants.UserTypePlatform && userType != constants.UserTypeSuperAdmin {
|
|
return errors.New(errors.CodeForbidden, "无权创建订单")
|
|
}
|
|
}
|
|
|
|
buyerType := ""
|
|
buyerID := uint(0)
|
|
if userType == constants.UserTypeAgent {
|
|
buyerType = model.BuyerTypeAgent
|
|
buyerID = shopID
|
|
}
|
|
|
|
order, err := h.service.Create(ctx, &req, buyerType, buyerID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return response.Success(c, order)
|
|
}
|
|
|
|
func (h *OrderHandler) Get(c *fiber.Ctx) error {
|
|
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
|
if err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "无效的订单ID")
|
|
}
|
|
|
|
order, err := h.service.Get(c.UserContext(), uint(id))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return response.Success(c, order)
|
|
}
|
|
|
|
func (h *OrderHandler) List(c *fiber.Ctx) error {
|
|
var req dto.OrderListRequest
|
|
if err := c.QueryParser(&req); err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
|
}
|
|
|
|
ctx := c.UserContext()
|
|
userType := middleware.GetUserTypeFromContext(ctx)
|
|
shopID := middleware.GetShopIDFromContext(ctx)
|
|
|
|
var buyerType string
|
|
var buyerID uint
|
|
|
|
if userType == constants.UserTypeAgent {
|
|
buyerType = model.BuyerTypeAgent
|
|
buyerID = shopID
|
|
} else {
|
|
buyerType = ""
|
|
buyerID = 0
|
|
}
|
|
|
|
orders, err := h.service.List(ctx, &req, buyerType, buyerID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return response.Success(c, orders)
|
|
}
|
|
|
|
func (h *OrderHandler) Cancel(c *fiber.Ctx) error {
|
|
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
|
if err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "无效的订单ID")
|
|
}
|
|
|
|
ctx := c.UserContext()
|
|
userType := middleware.GetUserTypeFromContext(ctx)
|
|
shopID := middleware.GetShopIDFromContext(ctx)
|
|
|
|
if userType != constants.UserTypeAgent {
|
|
return errors.New(errors.CodeForbidden, "只有代理账号可以取消订单")
|
|
}
|
|
|
|
if err := h.service.Cancel(ctx, uint(id), model.BuyerTypeAgent, shopID); err != nil {
|
|
return err
|
|
}
|
|
|
|
return response.Success(c, nil)
|
|
}
|
|
|
|
// PurchaseCheck 代购订单预检接口
|
|
// 路由: POST /api/admin/orders/purchase-check
|
|
// 参数: order_type(订单类型), resource_id(资源ID), package_ids(套餐ID列表)
|
|
// 响应: 套餐总价、强充要求、实际支付金额等预检结果
|
|
func (h *OrderHandler) PurchaseCheck(c *fiber.Ctx) error {
|
|
var req dto.PurchaseCheckRequest
|
|
if err := c.BodyParser(&req); err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
|
}
|
|
|
|
result, err := h.service.GetPurchaseCheck(c.UserContext(), &req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return response.Success(c, result)
|
|
}
|