优化支付流程,扩展产品价格返回字段
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
q "platform/web/queries"
|
||||
"time"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"gorm.io/gen/field"
|
||||
)
|
||||
|
||||
@@ -20,7 +21,7 @@ func (s *productService) AllProducts() ([]*m.Product, error) {
|
||||
Find()
|
||||
}
|
||||
|
||||
func (s *productService) AllProductSaleInfos() ([]*m.Product, error) {
|
||||
func (s *productService) AllProductSaleInfos() ([]any, error) {
|
||||
products, err := q.Product.
|
||||
Select(
|
||||
q.Product.ID,
|
||||
@@ -43,7 +44,17 @@ func (s *productService) AllProductSaleInfos() ([]*m.Product, error) {
|
||||
pids[i] = p.ID
|
||||
}
|
||||
|
||||
skus, err := q.ProductSku.
|
||||
type SkuInfo struct {
|
||||
ID int32 `json:"id"`
|
||||
ProductID int32 `json:"product_id"`
|
||||
Name string `json:"name"`
|
||||
Code string `json:"code"`
|
||||
Price decimal.Decimal `json:"price"`
|
||||
CountMin decimal.Decimal `json:"count_min"`
|
||||
Discount float64 `json:"discount"`
|
||||
}
|
||||
var skus []*SkuInfo
|
||||
err = q.ProductSku.
|
||||
Select(
|
||||
q.ProductSku.ID,
|
||||
q.ProductSku.ProductID,
|
||||
@@ -51,29 +62,47 @@ func (s *productService) AllProductSaleInfos() ([]*m.Product, error) {
|
||||
q.ProductSku.Code,
|
||||
q.ProductSku.Price,
|
||||
q.ProductSku.CountMin,
|
||||
q.ProductDiscount.Discount,
|
||||
).
|
||||
Where(
|
||||
q.ProductSku.ProductID.In(pids...),
|
||||
q.ProductSku.Status.Eq(int32(m.SkuStatusEnabled)),
|
||||
).
|
||||
LeftJoin(q.ProductDiscount, q.ProductDiscount.ID.EqCol(q.ProductSku.DiscountId)).
|
||||
Order(q.ProductSku.Sort).
|
||||
Find()
|
||||
Scan(&skus)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pmap := make(map[int32]*m.Product, len(products))
|
||||
type ProductInfo struct {
|
||||
m.Product
|
||||
Skus []*SkuInfo `json:"skus,omitempty"`
|
||||
}
|
||||
pmap := make(map[int32]*ProductInfo, len(products))
|
||||
for _, p := range products {
|
||||
pmap[p.ID] = p
|
||||
p.Skus = make([]*m.ProductSku, 0)
|
||||
pmap[p.ID] = &ProductInfo{Product: *p, Skus: make([]*SkuInfo, 0)}
|
||||
}
|
||||
for _, s := range skus {
|
||||
if p, ok := pmap[s.ProductID]; ok {
|
||||
p.Skus = append(p.Skus, s)
|
||||
p.Skus = append(p.Skus, &SkuInfo{
|
||||
ID: s.ID,
|
||||
ProductID: s.ProductID,
|
||||
Name: s.Name,
|
||||
Code: s.Code,
|
||||
Price: s.Price,
|
||||
CountMin: s.CountMin,
|
||||
Discount: s.Discount,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return products, nil
|
||||
plist := make([]any, 0, len(pmap))
|
||||
for _, p := range pmap {
|
||||
plist = append(plist, p)
|
||||
}
|
||||
|
||||
return plist, nil
|
||||
}
|
||||
|
||||
// 新增产品
|
||||
|
||||
Reference in New Issue
Block a user