Files
2026-09-15 16:02:45 +08:00

149 lines
5.1 KiB
Go

package mysqldao
import (
"context"
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/global"
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/internal/mysqlmodel"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
// OutfitShareDao 穿搭分享数据访问
type OutfitShareDao struct {
db *gorm.DB
}
// NewOutfitShareDao 创建穿搭分享 DAO
func NewOutfitShareDao() *OutfitShareDao {
return &OutfitShareDao{db: global.MysqlEngine}
}
// OutfitShareCreate 创建穿搭分享
func (d *OutfitShareDao) OutfitShareCreate(ctx context.Context, share *mysqlmodel.OutfitShare) error {
ctx, span := global.Tracer.Start(ctx, "mysqldao.OutfitShareCreate")
defer span.End()
if err := d.db.WithContext(ctx).Create(share).Error; err != nil {
span.RecordError(err)
return err
}
return nil
}
// OutfitShareGetByUUID 按 UUID 查询穿搭分享
func (d *OutfitShareDao) OutfitShareGetByUUID(ctx context.Context, shareUUID string) (*mysqlmodel.OutfitShare, error) {
ctx, span := global.Tracer.Start(ctx, "mysqldao.OutfitShareGetByUUID")
defer span.End()
var share mysqlmodel.OutfitShare
if err := d.db.WithContext(ctx).Where("share_uuid = ?", shareUUID).First(&share).Error; err != nil {
span.RecordError(err)
return nil, err
}
return &share, nil
}
// OutfitShareUpdateStatus 更新穿搭分享状态(取消)
func (d *OutfitShareDao) OutfitShareUpdateStatus(ctx context.Context, shareUUID string, status int8) error {
ctx, span := global.Tracer.Start(ctx, "mysqldao.OutfitShareUpdateStatus")
defer span.End()
if err := d.db.WithContext(ctx).Model(&mysqlmodel.OutfitShare{}).
Where("share_uuid = ?", shareUUID).
UpdateColumn("status", status).Error; err != nil {
span.RecordError(err)
return err
}
return nil
}
// OutfitShareListByUserID 分页查询我的穿搭分享(status 为 0 不筛选)
func (d *OutfitShareDao) OutfitShareListByUserID(ctx context.Context, userID int64, status int8, page, pageSize int) ([]mysqlmodel.OutfitShare, int64, error) {
ctx, span := global.Tracer.Start(ctx, "mysqldao.OutfitShareListByUserID")
defer span.End()
var list []mysqlmodel.OutfitShare
var total int64
q := d.db.WithContext(ctx).Model(&mysqlmodel.OutfitShare{}).Where("user_id = ?", userID)
if status != 0 {
q = q.Where("status = ?", status)
}
if err := q.Count(&total).Error; err != nil {
span.RecordError(err)
return nil, 0, err
}
if err := q.Offset((page - 1) * pageSize).Limit(pageSize).Order("id DESC").Find(&list).Error; err != nil {
span.RecordError(err)
return nil, 0, err
}
return list, total, nil
}
// OutfitShareReceiverDao 穿搭分享接收人数据访问
type OutfitShareReceiverDao struct {
db *gorm.DB
}
// NewOutfitShareReceiverDao 创建穿搭分享接收人 DAO
func NewOutfitShareReceiverDao() *OutfitShareReceiverDao {
return &OutfitShareReceiverDao{db: global.MysqlEngine}
}
// OutfitShareReceiverUpsert 幂等记录接收关系:唯一索引冲突时不做任何事,避免重复记录。
func (d *OutfitShareReceiverDao) OutfitShareReceiverUpsert(ctx context.Context, shareUUID string, receiverUserID int64) error {
ctx, span := global.Tracer.Start(ctx, "mysqldao.OutfitShareReceiverUpsert")
defer span.End()
receiver := &mysqlmodel.OutfitShareReceiver{
ShareUUID: shareUUID,
ReceiverUserID: receiverUserID,
}
if err := d.db.WithContext(ctx).Clauses(clause.OnConflict{DoNothing: true}).Create(receiver).Error; err != nil {
span.RecordError(err)
return err
}
return nil
}
// OutfitShareReceivedList 联表分页查询某接收人收到的穿搭分享(status 为 0 不筛选)。
func (d *OutfitShareReceiverDao) OutfitShareReceivedList(ctx context.Context, receiverUserID int64, status int8, page, pageSize int) ([]mysqlmodel.OutfitShare, int64, error) {
ctx, span := global.Tracer.Start(ctx, "mysqldao.OutfitShareReceivedList")
defer span.End()
var list []mysqlmodel.OutfitShare
var total int64
q := d.db.WithContext(ctx).
Model(&mysqlmodel.OutfitShare{}).
Joins("JOIN outfit_share_receivers r ON r.share_uuid = outfit_shares.share_uuid").
Joins("JOIN outfits o ON o.id = outfit_shares.outfit_id").
Where("r.receiver_user_id = ?", receiverUserID).
// 可见性:仅排除「已删除」与「已封禁」的穿搭。
// 分享与公开状态解耦(仅自己可见的穿搭同样可分享),故不再按 is_public / audit_status 过滤;
// 穿搭删除或封禁后不再下发内容。
Where("o.status <> ? AND o.deleted_at = 0", 2)
if status != 0 {
// 显式筛选分享状态:1=正常 2=已取消。
// 修复:此前这里无条件叠加 `outfit_shares.status = 1`,与下面的参数条件形成
// `status = 1 AND status = 2`,导致传 status=2 恒返回空列表。
q = q.Where("outfit_shares.status = ?", status)
} else {
// 默认只返回正常分享:已取消的分享不应再出现在接收列表(保留「取消分享后不再下发」语义)。
q = q.Where("outfit_shares.status = ?", 1)
}
if err := q.Count(&total).Error; err != nil {
span.RecordError(err)
return nil, 0, err
}
if err := q.Select("outfit_shares.*").
Order("r.id DESC").
Offset((page - 1) * pageSize).
Limit(pageSize).
Find(&list).Error; err != nil {
span.RecordError(err)
return nil, 0, err
}
return list, total, nil
}