fix(listing): 修复编号序列冲突并按角色隔离价格字段

This commit is contained in:
yml2213
2026-10-31 13:55:13 +08:00
parent 3ea02661ea
commit 8bd6d7fe8f
3 changed files with 50 additions and 3 deletions
+13 -3
View File
@@ -258,11 +258,21 @@ func (r *Repository) findForReviewUpdate(tx *gorm.DB, listingID uint64) (*model.
func (r *Repository) nextListingNo(tx *gorm.DB, now time.Time) (string, error) {
bizDate := now.Format("20060102")
if tx.Dialector.Name() == "mysql" {
// 以当天已有编号的最大值为兜底,避免种子数据或外部写入后序列落后导致编号冲突。
var maxSeq int
if err := tx.Raw(
`SELECT COALESCE(MAX(CAST(SUBSTRING(listing_no, 9) AS UNSIGNED)), 0)
FROM rental_listings WHERE listing_no LIKE ?`,
bizDate+"%",
).Scan(&maxSeq).Error; err != nil {
return "", err
}
nextSeq := maxSeq + 1
if err := tx.Exec(`
INSERT INTO listing_no_sequences (biz_date, next_seq)
VALUES (?, LAST_INSERT_ID(1))
ON DUPLICATE KEY UPDATE next_seq = LAST_INSERT_ID(next_seq + 1)
`, bizDate).Error; err != nil {
VALUES (?, LAST_INSERT_ID(?))
ON DUPLICATE KEY UPDATE next_seq = LAST_INSERT_ID(GREATEST(next_seq + 1, ?))
`, bizDate, nextSeq, nextSeq).Error; err != nil {
return "", err
}
var seq int