feat(shipping): 发货列表返回收货地址信息

为用户发货分组接口补充 address 字段,在按批次聚合发货记录时一并收集 address_id,批量查询 user_addresses 并回填到 shipment 返回结果。
这样小程序发货列表卡片可以直接展示本次申请发货所选的联系人、手机号和详细地址,避免前端额外发起地址查询或依赖本地拼装。
This commit is contained in:
Zuncle 2026-04-17 20:42:46 +08:00
parent 5dc9f034c8
commit 1cf57657ca

View File

@ -2,6 +2,7 @@ package user
import (
"context"
"sort"
"strconv"
"time"
)
@ -14,18 +15,30 @@ type ProductInfo struct {
Price int64 `json:"price"`
}
type ShipmentAddressInfo struct {
ID int64 `json:"id"`
Name string `json:"name"`
Phone string `json:"phone"`
Province string `json:"province"`
City string `json:"city"`
District string `json:"district"`
Detail string `json:"detail"`
IsDefault bool `json:"is_default"`
}
type ShipmentGroup struct {
ExpressCode string `json:"express_code"`
ExpressNo string `json:"express_no"`
BatchNo string `json:"batch_no"`
Status int32 `json:"status"`
Count int64 `json:"count"`
ShippedAt *time.Time `json:"shipped_at,omitempty"`
ReceivedAt *time.Time `json:"received_at,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"` // 发货申请创建时间用于前端判断48小时撤销限制
InventoryIDs []int64 `json:"inventory_ids"`
ProductIDs []int64 `json:"product_ids"`
Products []ProductInfo `json:"products"`
ExpressCode string `json:"express_code"`
ExpressNo string `json:"express_no"`
BatchNo string `json:"batch_no"`
Status int32 `json:"status"`
Count int64 `json:"count"`
ShippedAt *time.Time `json:"shipped_at,omitempty"`
ReceivedAt *time.Time `json:"received_at,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"` // 发货申请创建时间用于前端判断48小时撤销限制
InventoryIDs []int64 `json:"inventory_ids"`
ProductIDs []int64 `json:"product_ids"`
Products []ProductInfo `json:"products"`
Address *ShipmentAddressInfo `json:"address,omitempty"`
}
func (s *service) ListUserShipmentGroups(ctx context.Context, userID int64, page, pageSize int) (items []*ShipmentGroup, total int64, err error) {
@ -55,6 +68,7 @@ func (s *service) ListUserShipmentGroups(ctx context.Context, userID int64, page
shippedAt *time.Time
receivedAt *time.Time
createdAt *time.Time // 最早的创建时间
addressID int64
inv []int64
pid []int64
}
@ -99,11 +113,47 @@ func (s *service) ListUserShipmentGroups(ctx context.Context, userID int64, page
a.createdAt = &t
}
}
if a.addressID == 0 && r.AddressID > 0 {
a.addressID = r.AddressID
}
a.inv = append(a.inv, r.InventoryID)
if r.ProductID > 0 {
a.pid = append(a.pid, r.ProductID)
}
}
addressIDs := make([]int64, 0, len(m))
addressIDSet := make(map[int64]struct{}, len(m))
for _, a := range m {
if a.addressID > 0 {
if _, ok := addressIDSet[a.addressID]; !ok {
addressIDSet[a.addressID] = struct{}{}
addressIDs = append(addressIDs, a.addressID)
}
}
}
sort.Slice(addressIDs, func(i, j int) bool { return addressIDs[i] < addressIDs[j] })
addressMap := make(map[int64]*ShipmentAddressInfo, len(addressIDs))
if len(addressIDs) > 0 {
addresses, addrErr := s.readDB.UserAddresses.WithContext(ctx).ReadDB().Where(s.readDB.UserAddresses.ID.In(addressIDs...)).Find()
if addrErr != nil {
return nil, 0, addrErr
}
for _, addr := range addresses {
addressMap[addr.ID] = &ShipmentAddressInfo{
ID: addr.ID,
Name: addr.Name,
Phone: addr.Mobile,
Province: addr.Province,
City: addr.City,
District: addr.District,
Detail: addr.Address,
IsDefault: addr.IsDefault == 1,
}
}
}
items = make([]*ShipmentGroup, 0, len(m))
for _, k := range order {
a := m[k]
@ -139,6 +189,7 @@ func (s *service) ListUserShipmentGroups(ctx context.Context, userID int64, page
InventoryIDs: a.inv,
ProductIDs: a.pid,
Products: products,
Address: addressMap[a.addressID],
})
}
return items, total, nil