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"). Where("r.receiver_user_id = ?", receiverUserID) if status != 0 { q = q.Where("outfit_shares.status = ?", status) } 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 }