1、租赁订单排序优化,重新调整了排序规则,优先展示有库存的卡带,不在本店和抢光了排在最后

This commit is contained in:
chenlin 2025-09-02 17:38:59 +08:00
parent 8c52102d44
commit 65dc77df88

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"github.com/codinl/go-logger" "github.com/codinl/go-logger"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"sort"
"strings" "strings"
"time" "time"
) )
@ -2161,6 +2162,14 @@ func NewGetGameCardList(sortType, page, pageSize int, gameTypeIds []uint64, stor
sortedGameCards[i] = gameCardMap[gameId] sortedGameCards[i] = gameCardMap[gameId]
} }
// ✅ 在分页之前就赋库存状态
GameCardListSetStockState(cards, storeId, effectiveStoreList)
// ✅ 库存状态赋值后,做二次排序 (1 → 2 → 3)
sort.SliceStable(sortedGameCards, func(i, j int) bool {
return sortedGameCards[i].StockState < sortedGameCards[j].StockState
})
// 计算分页的起始和结束索引 // 计算分页的起始和结束索引
startIndex := page * pageSize startIndex := page * pageSize
if startIndex > len(sortedGameCards) { if startIndex > len(sortedGameCards) {
@ -2174,10 +2183,7 @@ func NewGetGameCardList(sortType, page, pageSize int, gameTypeIds []uint64, stor
} }
// 分页后的数据 // 分页后的数据
pagedGameCards := sortedGameCards[startIndex:endIndex] cards = sortedGameCards[startIndex:endIndex]
cards = pagedGameCards
GameCardListSetStockState(cards, storeId, effectiveStoreList)
} else { } else {
err = qs.OrderDescByID().All(&cards) err = qs.OrderDescByID().All(&cards)
if err != nil && err != RecordNotFound { if err != nil && err != RecordNotFound {
@ -2186,25 +2192,24 @@ func NewGetGameCardList(sortType, page, pageSize int, gameTypeIds []uint64, stor
} }
list := make([]GameCard, 0, len(cards)) list := make([]GameCard, 0, len(cards))
cardMap := make(map[uint32]GameCard, 0) for _, card := range cards {
for i, _ := range cards { // 默认库存状态:如果 gameIdMap 有记录就取值,否则为 3无库存
cardMap[cards[i].ID] = cards[i] stockState, ok := gameIdMap[card.ID]
// 如果游戏列表有记录但游戏库存列表无记录则也添加到gameIds库存记录为3无库存
_, ok := gameIdMap[cards[i].ID]
if !ok { if !ok {
gameIds = append(gameIds, uint32(cards[i].ID)) stockState = 3
gameIdMap[cards[i].ID] = 3 gameIdMap[card.ID] = stockState
}
}
for i, _ := range gameIds {
v, ok1 := cardMap[gameIds[i]]
v2, _ := gameIdMap[gameIds[i]]
v.StockState = v2
if ok1 {
list = append(list, v)
} }
// 设置库存状态
card.StockState = stockState
list = append(list, card)
} }
// ✅ 在分页之前先排序 (1 → 2 → 3)
sort.SliceStable(list, func(i, j int) bool {
return list[i].StockState < list[j].StockState
})
count = len(list) count = len(list)
totalPage = uint32(count/pageSize + 1) totalPage = uint32(count/pageSize + 1)
if count%pageSize == 0 { if count%pageSize == 0 {