修复逻辑问题
This commit is contained in:
@@ -21,24 +21,58 @@ func (s *productService) AllProducts() ([]*m.Product, error) {
|
||||
}
|
||||
|
||||
func (s *productService) AllProductSaleInfos() ([]*m.Product, error) {
|
||||
return q.Product.
|
||||
Joins(q.Product.Skus).
|
||||
products, err := q.Product.
|
||||
Select(
|
||||
q.Product.ID,
|
||||
q.Product.Code,
|
||||
q.Product.Name,
|
||||
q.Product.Description,
|
||||
q.Product.Sort,
|
||||
).
|
||||
Where(
|
||||
q.Product.Status.Eq(int(m.ProductStatusEnabled)),
|
||||
).
|
||||
Order(q.Product.Sort).
|
||||
Find()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pids := make([]int32, len(products))
|
||||
for i, p := range products {
|
||||
pids[i] = p.ID
|
||||
}
|
||||
|
||||
skus, err := q.ProductSku.
|
||||
Select(
|
||||
q.ProductSku.ID,
|
||||
q.ProductSku.Code,
|
||||
q.ProductSku.ProductID,
|
||||
q.ProductSku.Name,
|
||||
q.ProductSku.Code,
|
||||
q.ProductSku.Price,
|
||||
).
|
||||
Where(
|
||||
q.Product.Status.Eq(int(m.ProxyStatusOnline)),
|
||||
q.ProductSku.Status.Eq(int32(m.ProductStatusEnabled)),
|
||||
q.ProductSku.ProductID.In(pids...),
|
||||
q.ProductSku.Status.Eq(int32(m.SkuStatusEnabled)),
|
||||
).
|
||||
Order(q.ProductSku.Sort).
|
||||
Find()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pmap := make(map[int32]*m.Product, len(products))
|
||||
for _, p := range products {
|
||||
pmap[p.ID] = p
|
||||
p.Skus = make([]*m.ProductSku, 0)
|
||||
}
|
||||
for _, s := range skus {
|
||||
if p, ok := pmap[s.ProductID]; ok {
|
||||
p.Skus = append(p.Skus, s)
|
||||
}
|
||||
}
|
||||
|
||||
return products, nil
|
||||
}
|
||||
|
||||
// 新增产品
|
||||
|
||||
Reference in New Issue
Block a user