fix(query): 支持12位纯数字时间编号(如202609040184)商品编号的精准索引路由

This commit is contained in:
yml2213
2026-09-05 11:25:14 +08:00
parent a9125b1939
commit cd719fc355
9 changed files with 91 additions and 29 deletions
+15 -2
View File
@@ -104,8 +104,8 @@ func (r *Repository) applyAdminListFilters(db *gorm.DB, query AdminListQuery) *g
if strings.HasPrefix(upper, "RO") || strings.HasPrefix(upper, "ORD") {
// 订单号:命中 rental_orders(order_no) 唯一索引
db = db.Where("EXISTS (SELECT 1 FROM rental_orders AS o WHERE o.listing_id = l.id AND o.order_no = ?)", keyword)
} else if strings.HasPrefix(upper, "L") {
// 商品编号:命中 rental_listings(listing_no) 唯一索引
} else if strings.HasPrefix(upper, "L") || (len(keyword) >= 12 && isAllDigits(keyword)) {
// 商品编号(如 L前缀或时间计算生成的 12位纯数字如 202609040184):命中 rental_listings(listing_no) 唯一索引
db = db.Where("l.listing_no = ?", keyword)
} else if id, err := strconv.ParseUint(keyword, 10, 64); err == nil && id > 0 && keyword[0] != '0' && id < 100000000 {
db = db.Where(
@@ -277,3 +277,16 @@ func (r *Repository) nextListingNo(tx *gorm.DB, now time.Time) (string, error) {
}
return fmt.Sprintf("%s%04d", bizDate, seq), nil
}
func isAllDigits(s string) bool {
if s == "" {
return false
}
for i := 0; i < len(s); i++ {
if s[i] < '0' || s[i] > '9' {
return false
}
}
return true
}