Fix auto pricelist resolution and latest-price selection; update Bible

This commit is contained in:
Mikhail Chusavitin
2026-02-20 19:15:24 +03:00
parent 7f8491d197
commit 3c46cd7bf0
11 changed files with 419 additions and 28 deletions

View File

@@ -46,8 +46,30 @@ func (h *PricelistHandler) List(c *gin.Context) {
}
localPLs = filtered
}
if activeOnly {
// Local cache stores only active snapshots for normal operations.
type pricelistWithCount struct {
pricelist localdb.LocalPricelist
itemCount int64
usageCount int
}
withCounts := make([]pricelistWithCount, 0, len(localPLs))
for _, lpl := range localPLs {
itemCount := h.localDB.CountLocalPricelistItems(lpl.ID)
if activeOnly && itemCount == 0 {
continue
}
usageCount := 0
if lpl.IsUsed {
usageCount = 1
}
withCounts = append(withCounts, pricelistWithCount{
pricelist: lpl,
itemCount: itemCount,
usageCount: usageCount,
})
}
localPLs = localPLs[:0]
for _, row := range withCounts {
localPLs = append(localPLs, row.pricelist)
}
sort.SliceStable(localPLs, func(i, j int) bool { return localPLs[i].CreatedAt.After(localPLs[j].CreatedAt) })
total := len(localPLs)
@@ -62,10 +84,14 @@ func (h *PricelistHandler) List(c *gin.Context) {
pageSlice := localPLs[start:end]
summaries := make([]map[string]interface{}, 0, len(pageSlice))
for _, lpl := range pageSlice {
itemCount := h.localDB.CountLocalPricelistItems(lpl.ID)
itemCount := int64(0)
usageCount := 0
if lpl.IsUsed {
usageCount = 1
for _, row := range withCounts {
if row.pricelist.ID == lpl.ID {
itemCount = row.itemCount
usageCount = row.usageCount
break
}
}
summaries = append(summaries, map[string]interface{}{
"id": lpl.ServerID,