feat(distribution): 分站发布群只使用自己的二维码池
主站单独维护分站码池,池空时不再借用主站或其他分站,并去掉已停用的固定二维码入口。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -96,7 +96,7 @@ func EnsureListingConversation(tx *gorm.DB, listing model.RentalListing, preferr
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 取二维码:分站优先用站点固定码(可复用);否则消耗对应码池。
|
||||
// 4. 取二维码:主站只用主站池,分站只用自己的池。池空则稍后补发,不跨池借用。
|
||||
qrcode, qrcodeImageURL, err := resolveListingQrCode(tx, siteID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -338,39 +338,15 @@ func resolveListingDistributionSiteID(tx *gorm.DB, listing model.RentalListing)
|
||||
}
|
||||
|
||||
func resolveListingQrCode(tx *gorm.DB, siteID uint64) (*model.ChatQrCode, string, error) {
|
||||
var scope *uint64
|
||||
if siteID > 0 {
|
||||
var site model.DistributionSite
|
||||
err := tx.Select("id", "listing_qrcode_url").First(&site, siteID).Error
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, "", err
|
||||
}
|
||||
if err == nil {
|
||||
if url := strings.TrimSpace(site.ListingQrcodeURL); url != "" {
|
||||
return nil, url, nil
|
||||
}
|
||||
}
|
||||
qrcode, err := fetchUnusedQrCodeForSite(tx, &siteID)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
if qrcode != nil {
|
||||
return qrcode, qrcode.ImageURL, nil
|
||||
}
|
||||
// 分站没有固定码、也没有自己的码池时,沿用主站二维码池。
|
||||
mainQR, err := fetchUnusedQrCode(tx)
|
||||
if err != nil || mainQR == nil {
|
||||
return nil, "", err
|
||||
}
|
||||
return mainQR, mainQR.ImageURL, nil
|
||||
scope = &siteID
|
||||
}
|
||||
qrcode, err := fetchUnusedQrCodeForSite(tx, nil)
|
||||
if err != nil {
|
||||
qrcode, err := fetchUnusedQrCodeForSite(tx, scope)
|
||||
if err != nil || qrcode == nil {
|
||||
return nil, "", err
|
||||
}
|
||||
if qrcode != nil {
|
||||
return qrcode, qrcode.ImageURL, nil
|
||||
}
|
||||
return nil, "", nil
|
||||
return qrcode, qrcode.ImageURL, nil
|
||||
}
|
||||
|
||||
func fetchUnusedQrCodeForSite(tx *gorm.DB, siteID *uint64) (*model.ChatQrCode, error) {
|
||||
@@ -399,6 +375,18 @@ func fetchUnusedQrCodeForSite(tx *gorm.DB, siteID *uint64) (*model.ChatQrCode, e
|
||||
return &qrcode, nil
|
||||
}
|
||||
|
||||
func fetchUnusedQrCodeForConversation(tx *gorm.DB, conversationID uint64) (*model.ChatQrCode, error) {
|
||||
var conversation model.ChatConversation
|
||||
if err := tx.Select("id", "distribution_site_id").First(&conversation, conversationID).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var siteID *uint64
|
||||
if conversation.DistributionSiteID != nil && *conversation.DistributionSiteID > 0 {
|
||||
siteID = conversation.DistributionSiteID
|
||||
}
|
||||
return fetchUnusedQrCodeForSite(tx, siteID)
|
||||
}
|
||||
|
||||
func markQrCodeAsUsed(tx *gorm.DB, qrcodeID uint64, conversationID uint64) error {
|
||||
now := time.Now()
|
||||
return tx.Model(&model.ChatQrCode{}).
|
||||
@@ -446,12 +434,22 @@ func checkQrCodeStockAndAlert(tx *gorm.DB, conversation *model.ChatConversation)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// 统计未使用的二维码数量
|
||||
var count int64
|
||||
if err := tx.Model(&model.ChatQrCode{}).
|
||||
// 只统计当前会话所属池子,分站库存不影响主站预警。
|
||||
countQuery := tx.Model(&model.ChatQrCode{}).
|
||||
Where("status = ?", QrCodeStatusUnused).
|
||||
Where("expires_at IS NULL OR expires_at > ?", time.Now()).
|
||||
Count(&count).Error; err != nil {
|
||||
Where("expires_at IS NULL OR expires_at > ?", time.Now())
|
||||
poolLabel := ""
|
||||
if conversation != nil && conversation.DistributionSiteID != nil && *conversation.DistributionSiteID > 0 {
|
||||
countQuery = countQuery.Where("distribution_site_id = ?", *conversation.DistributionSiteID)
|
||||
var site model.DistributionSite
|
||||
if err := tx.Select("site_name").First(&site, *conversation.DistributionSiteID).Error; err == nil {
|
||||
poolLabel = strings.TrimSpace(site.SiteName)
|
||||
}
|
||||
} else {
|
||||
countQuery = countQuery.Where("distribution_site_id IS NULL")
|
||||
}
|
||||
var count int64
|
||||
if err := countQuery.Count(&count).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -471,6 +469,9 @@ func checkQrCodeStockAndAlert(tx *gorm.DB, conversation *model.ChatConversation)
|
||||
}
|
||||
|
||||
alertContent := strings.ReplaceAll(messageTemplate, "{{.Count}}", fmt.Sprintf("%d", count))
|
||||
if poolLabel != "" {
|
||||
alertContent = "分站「" + poolLabel + "」" + alertContent
|
||||
}
|
||||
|
||||
entries := make([]map[string]interface{}, 0, len(csAdmins))
|
||||
now := time.Now()
|
||||
|
||||
Reference in New Issue
Block a user