86 lines
1.8 KiB
Go
86 lines
1.8 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"xy_sh/internal/data/impl"
|
|
"xy_sh/pkg"
|
|
|
|
"xy_sh/internal/biz"
|
|
"xy_sh/internal/entities"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
type Order struct {
|
|
biz *biz.OrderStore
|
|
logImpl *impl.LogImpl
|
|
}
|
|
|
|
func NewOrder(biz *biz.OrderStore, logImpl *impl.LogImpl) *Order {
|
|
return &Order{
|
|
biz: biz,
|
|
logImpl: logImpl,
|
|
}
|
|
}
|
|
|
|
func (o *Order) CreateOrderHandler(c *fiber.Ctx) error {
|
|
|
|
decryptedData := c.Locals("decryptedData").([]byte)
|
|
var req entities.OrderRequest
|
|
if err := json.Unmarshal(decryptedData, &req); err != nil {
|
|
return fmt.Errorf("业务参数解析失败: %w", err)
|
|
}
|
|
|
|
if req.ActCode == "" || req.GoodsCode == "" || req.ActOrderNum == "" {
|
|
return fmt.Errorf("缺少必填参数: actCode, goodsCode, actOrderNum")
|
|
|
|
}
|
|
|
|
result, err := o.biz.CreateOrder(c.UserContext(), &req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return pkg.HandleResponse(c, result)
|
|
}
|
|
|
|
func (o *Order) QueryOrderHandler(c *fiber.Ctx) error {
|
|
decryptedData := c.Locals("decryptedData").(string)
|
|
if len(decryptedData) == 0 {
|
|
return nil
|
|
}
|
|
|
|
var req entities.QueryOrderRequest
|
|
if err := json.Unmarshal([]byte(decryptedData), &req); err != nil {
|
|
return fmt.Errorf("业务参数解析失败: " + err.Error())
|
|
|
|
}
|
|
|
|
if req.ActCode == "" && req.OrderNo == "" {
|
|
return fmt.Errorf("缺少必填参数: actCode, orderNo")
|
|
|
|
}
|
|
|
|
result, err := o.biz.QueryOrder(&req)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusOK).JSON(entities.CommonResponse{
|
|
Code: entities.CodeFailed,
|
|
Msg: err.Error(),
|
|
Data: nil,
|
|
})
|
|
}
|
|
return pkg.HandleResponse(c, result)
|
|
}
|
|
|
|
func (o *Order) CallbackHandler(c *fiber.Ctx) error {
|
|
|
|
var req entities.CallBack
|
|
|
|
if err := o.biz.HandleCallback(&req); err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).SendString("处理失败")
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).SendString("ok")
|
|
}
|