tm_order_before.go 4.3 KB
package tmv3

import (
	"context"
	"github.com/alibabacloud-go/linkedmall-20230930/v2/client"
	"github.com/alibabacloud-go/tea/tea"
	"github.com/gogf/gf/encoding/gjson"
	"github.com/gogf/gf/os/gtime"
	"github.com/gogf/gf/util/gconv"
)

type OrderBeforeReq struct {
	DeliveryAddress OrderDeliveryAddress `json:"deliveryAddress"`
	BuyerId         string               `json:"buyerId"`
	ProductList     []OrderItem          `json:"productList"`
}

type OrderBeforeRes struct {
	RequestId string `json:"requestId"`
	OrderList []struct {
		ProductList []struct {
			ProductId     string      `json:"productId"`
			ProductTitle  string      `json:"productTitle"`
			SkuTitle      string      `json:"skuTitle"`
			SkuId         string      `json:"skuId"`
			PurchaserId   string      `json:"purchaserId"`
			PromotionFee  interface{} `json:"promotionFee"`
			Quantity      int         `json:"quantity"`
			ProductUrl    interface{} `json:"productUrl"`
			ProductPicUrl string      `json:"productPicUrl"`
			Price         int         `json:"price"`
			CanSell       bool        `json:"canSell"`
			Message       interface{} `json:"message"`
			Features      interface{} `json:"features"`
		} `json:"productList"`
		DeliveryInfoList []struct {
			Id          string `json:"id"`
			DisplayName string `json:"displayName"`
			PostFee     int    `json:"postFee"`
			ServiceType int    `json:"serviceType"`
		} `json:"deliveryInfoList"`
		InvoiceInfo interface{} `json:"invoiceInfo"`
		ExtInfo     interface{} `json:"extInfo"`
		CanSell     bool        `json:"canSell"`
		Message     interface{} `json:"message"`
	} `json:"orderList"`
	UnsellableOrderList []struct {
		ProductList []struct {
			ProductId     string      `json:"productId"`
			ProductTitle  string      `json:"productTitle"`
			SkuTitle      string      `json:"skuTitle"`
			SkuId         string      `json:"skuId"`
			PurchaserId   string      `json:"purchaserId"`
			PromotionFee  interface{} `json:"promotionFee"`
			Quantity      int         `json:"quantity"`
			ProductUrl    interface{} `json:"productUrl"`
			ProductPicUrl string      `json:"productPicUrl"`
			Price         int         `json:"price"`
			CanSell       bool        `json:"canSell"`
			Message       interface{} `json:"message"`
			Features      interface{} `json:"features"`
		} `json:"productList"`
		DeliveryInfoList interface{} `json:"deliveryInfoList"`
		InvoiceInfo      interface{} `json:"invoiceInfo"`
		ExtInfo          interface{} `json:"extInfo"`
		CanSell          bool        `json:"canSell"`
		Message          interface{} `json:"message"`
	} `json:"unsellableOrderList"`
	AddressList []struct {
		AddressId        int    `json:"addressId"`
		Receiver         string `json:"receiver"`
		ReceiverPhone    string `json:"receiverPhone"`
		AddressDetail    string `json:"addressDetail"`
		DivisionCode     string `json:"divisionCode"`
		TownDivisionCode string `json:"townDivisionCode"`
	} `json:"addressList"`
	ExtInfo interface{} `json:"extInfo"`
	CanSell bool        `json:"canSell"`
	Message interface{} `json:"message"`
}

// Before 采购单渲染
func (s orderTm) Before(ctx context.Context, req OrderBeforeReq) (res *OrderBeforeRes, err error) {
	Start := gtime.TimestampMilli()
	ctx = context.WithValue(ctx, "URI", "RenderPurchaseOrder")
	defer func() {
		Log(ctx, req, res, err, Start)
	}()
	Request := &client.RenderPurchaseOrderRequest{}
	var Body = new(client.PurchaseOrderRenderQuery)
	Body.SetBuyerId(req.BuyerId)

	var DeliveryAddress = new(client.AddressInfo)
	DeliveryAddress.SetDivisionCode(req.DeliveryAddress.DivisionCode)
	DeliveryAddress.SetAddressDetail(req.DeliveryAddress.AddressDetail)
	DeliveryAddress.SetReceiverPhone(req.DeliveryAddress.ReceiverPhone)
	DeliveryAddress.SetReceiver(req.DeliveryAddress.Receiver)
	DeliveryAddress.SetTownDivisionCode(req.DeliveryAddress.TownDivisionCode)
	Body.SetDeliveryAddress(DeliveryAddress)

	var list []*client.OrderRenderProductDTO
	for _, item := range req.ProductList {
		list = append(list, &client.OrderRenderProductDTO{
			ProductId:   tea.String(item.ProductId),
			PurchaserId: tea.String(PurchaserId),
			Quantity:    tea.Int32(gconv.Int32(item.Quantity)),
			SkuId:       tea.String(item.SkuId),
		})
	}
	Body.SetProductList(list)
	Request.SetBody(Body)

	r, err := server.RenderPurchaseOrder(Request)
	if err != nil {
		return
	}

	err = gjson.New(r.Body).Scan(&res)
	return
}