diff --git a/internal/service/user/shipping_groups.go b/internal/service/user/shipping_groups.go index d2c875f..fc5dad0 100755 --- a/internal/service/user/shipping_groups.go +++ b/internal/service/user/shipping_groups.go @@ -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