feat(street): 草稿图支持主副分组(并入/并入上一张/拆出)
This commit is contained in:
@ -37,6 +37,18 @@ type ReviewRepository interface {
|
||||
SoftDeleteRunwayDraftImage(ctx context.Context, draftID, imgID uint32) error
|
||||
// SoftDeleteStreetDraftImage 软删单张街拍草稿图片,并同步 image_count。
|
||||
SoftDeleteStreetDraftImage(ctx context.Context, draftID, imgID uint32) error
|
||||
// AttachStreetDraftImages 把 imgIDs 变成 mainImgID 的副图(批量并入)。
|
||||
// mainImgID 必须属于同一草稿、未被软删、且自身是主图(is_detail=0),否则返回 ErrInvalidImageGroup。
|
||||
// 若某个被并入的图自身还带着副图,则它原有的副图一并改挂到 mainImgID ——
|
||||
// 避免出现「副图的副图」破坏不变量(见计划「实现裁决」第 1 条)。
|
||||
// 幂等:已是 mainImgID 副图的行重复并入不报错。
|
||||
AttachStreetDraftImages(ctx context.Context, draftID, mainImgID uint32, imgIDs []uint32) error
|
||||
// AttachPrevStreetDraftImage 把 imgID 并入「同草稿内 sort_order 紧邻的前一行所属组的主图」。
|
||||
// imgID 是首行(没有上一行)时返回 ErrNotFound。
|
||||
AttachPrevStreetDraftImage(ctx context.Context, draftID, imgID uint32) error
|
||||
// DetachStreetDraftImage 把副图恢复为主图(is_detail=0, parent_image_id=0)。
|
||||
// 对主图调用是幂等成功(无副作用)。
|
||||
DetachStreetDraftImage(ctx context.Context, draftID, imgID uint32) error
|
||||
// GetDraftImageKey 取单张草稿图片的S4 key(删除前清理用);不存在返回空串。
|
||||
GetDraftImageKey(ctx context.Context, kind string, draftID, imgID uint32) (string, error)
|
||||
UpdateStreetDraft(ctx context.Context, id uint32, fields map[string]any) error
|
||||
@ -599,3 +611,103 @@ func (r *reviewRepository) GetDraftImageKey(ctx context.Context, kind string, dr
|
||||
return "", ErrNotFound
|
||||
}
|
||||
}
|
||||
|
||||
// ── 街拍草稿图片:主副图分组 ──────────────────────────────────────────────
|
||||
|
||||
// AttachStreetDraftImages 把 imgIDs 变成 mainImgID 的副图(批量并入)。
|
||||
//
|
||||
// 事务内三步:1) 校验主图存在、未软删、本身是主图;2) 先「改挂孙辈」——被并入图原有的副图
|
||||
// 改挂到新主图(避免副图的副图);3) 再把被并入图自身标为副图。第 2 步必须在第 3 步之前。
|
||||
func (r *reviewRepository) AttachStreetDraftImages(ctx context.Context, draftID, mainImgID uint32, imgIDs []uint32) error {
|
||||
ids := make([]uint32, 0, len(imgIDs))
|
||||
for _, id := range imgIDs {
|
||||
if id != 0 && id != mainImgID {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
}
|
||||
if mainImgID == 0 || len(imgIDs) == 0 {
|
||||
// 主图无效,或传入集合本身就是空的(调用方连一张待并入图都没给)。
|
||||
return ErrInvalidImageGroup
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
// 集合里只有主图自身:视为无操作成功(忽略),不报错也不改动主图。
|
||||
return nil
|
||||
}
|
||||
|
||||
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var main model.StreetSnapDraftImage
|
||||
if err := tx.Where("id = ? AND draft_id = ? AND is_deleted = 0 AND is_detail = 0", mainImgID, draftID).
|
||||
First(&main).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrInvalidImageGroup
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
now := uint32(time.Now().Unix())
|
||||
if err := tx.Model(&model.StreetSnapDraftImage{}).
|
||||
Where("draft_id = ? AND is_deleted = 0 AND parent_image_id IN ?", draftID, ids).
|
||||
Updates(map[string]any{"parent_image_id": mainImgID, "updated_at": now}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Model(&model.StreetSnapDraftImage{}).
|
||||
Where("draft_id = ? AND is_deleted = 0 AND id IN ?", draftID, ids).
|
||||
Updates(map[string]any{"is_detail": 1, "parent_image_id": mainImgID, "updated_at": now}).Error
|
||||
})
|
||||
}
|
||||
|
||||
// AttachPrevStreetDraftImage 把 imgID 并入「同草稿内 sort_order 紧邻的前一行所属组的主图」。
|
||||
// 折回组主图是刻意的:连续「并入上一张」应链式归并到同一主图,而不是造出副图的副图。
|
||||
func (r *reviewRepository) AttachPrevStreetDraftImage(ctx context.Context, draftID, imgID uint32) error {
|
||||
var target uint32
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var cur model.StreetSnapDraftImage
|
||||
if err := tx.Where("id = ? AND draft_id = ? AND is_deleted = 0", imgID, draftID).First(&cur).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
var prev model.StreetSnapDraftImage
|
||||
err := tx.Where("draft_id = ? AND is_deleted = 0 AND (sort_order < ? OR (sort_order = ? AND id < ?))",
|
||||
draftID, cur.SortOrder, cur.SortOrder, cur.ID).
|
||||
Order("sort_order DESC, id DESC").First(&prev).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if prev.IsDetail == 1 && prev.ParentImageID != 0 {
|
||||
target = prev.ParentImageID
|
||||
} else {
|
||||
target = prev.ID
|
||||
}
|
||||
if target == imgID {
|
||||
return ErrInvalidImageGroup
|
||||
}
|
||||
|
||||
now := uint32(time.Now().Unix())
|
||||
if err := tx.Model(&model.StreetSnapDraftImage{}).
|
||||
Where("draft_id = ? AND is_deleted = 0 AND parent_image_id = ?", draftID, imgID).
|
||||
Updates(map[string]any{"parent_image_id": target, "updated_at": now}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Model(&model.StreetSnapDraftImage{}).
|
||||
Where("id = ? AND draft_id = ?", imgID, draftID).
|
||||
Updates(map[string]any{"is_detail": 1, "parent_image_id": target, "updated_at": now}).Error
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// DetachStreetDraftImage 把副图恢复为主图。对主图调用为幂等成功。
|
||||
// 拆出的图若自身带着副图,那些副图保持挂在它下面——它重新成为主图,不变量依然成立。
|
||||
func (r *reviewRepository) DetachStreetDraftImage(ctx context.Context, draftID, imgID uint32) error {
|
||||
return r.db.WithContext(ctx).
|
||||
Model(&model.StreetSnapDraftImage{}).
|
||||
Where("id = ? AND draft_id = ? AND is_deleted = 0 AND is_detail = 1", imgID, draftID).
|
||||
Updates(map[string]any{"is_detail": 0, "parent_image_id": 0, "updated_at": uint32(time.Now().Unix())}).
|
||||
Error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user