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"
"github.com/codinl/go-logger"
"github.com/jinzhu/gorm"
"sort"
"strings"
"time"
)
@ -2161,6 +2162,14 @@ func NewGetGameCardList(sortType, page, pageSize int, gameTypeIds []uint64, stor
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
if startIndex > len(sortedGameCards) {
@ -2174,10 +2183,7 @@ func NewGetGameCardList(sortType, page, pageSize int, gameTypeIds []uint64, stor
}
// 分页后的数据
pagedGameCards := sortedGameCards[startIndex:endIndex]
cards = pagedGameCards
GameCardListSetStockState(cards, storeId, effectiveStoreList)
cards = sortedGameCards[startIndex:endIndex]
} else {
err = qs.OrderDescByID().All(&cards)
if err != nil && err != RecordNotFound {
@ -2186,25 +2192,24 @@ func NewGetGameCardList(sortType, page, pageSize int, gameTypeIds []uint64, stor
}
list := make([]GameCard, 0, len(cards))
cardMap := make(map[uint32]GameCard, 0)
for i, _ := range cards {
cardMap[cards[i].ID] = cards[i]
// 如果游戏列表有记录但游戏库存列表无记录则也添加到gameIds库存记录为3无库存
_, ok := gameIdMap[cards[i].ID]
for _, card := range cards {
// 默认库存状态:如果 gameIdMap 有记录就取值,否则为 3无库存
stockState, ok := gameIdMap[card.ID]
if !ok {
gameIds = append(gameIds, uint32(cards[i].ID))
gameIdMap[cards[i].ID] = 3
}
}
for i, _ := range gameIds {
v, ok1 := cardMap[gameIds[i]]
v2, _ := gameIdMap[gameIds[i]]
v.StockState = v2
if ok1 {
list = append(list, v)
stockState = 3
gameIdMap[card.ID] = stockState
}
// 设置库存状态
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)
totalPage = uint32(count/pageSize + 1)
if count%pageSize == 0 {