Files
zt-closet-manage-backend/internal/mysqldao/closet.go
T
2026-09-11 01:47:20 +08:00

80 lines
2.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package mysqldao
import (
"context"
"git.lzcwy.cn/zhengchiliu/zt-closet-manage-backend/global"
"git.lzcwy.cn/zhengchiliu/zt-closet-manage-backend/internal/mysqlmodel"
"gorm.io/gorm"
)
// ClosetShareDao 衣柜分享数据访问
type ClosetShareDao struct {
db *gorm.DB
}
// NewClosetShareDao 创建衣柜分享 DAO
func NewClosetShareDao() *ClosetShareDao {
return &ClosetShareDao{db: global.MysqlEngine}
}
// ClosetShareListParams 分享分页列表筛选参数。
type ClosetShareListParams struct {
UserID int64 // 用户 ID,0 表示全部
Status int8 // 分享状态,0 表示全部
Page int
Size int
}
// ClosetShareList 分页查询衣柜分享
func (d *ClosetShareDao) ClosetShareList(ctx context.Context, p ClosetShareListParams) ([]mysqlmodel.ClosetShare, int64, error) {
ctx, span := global.Tracer.Start(ctx, "mysqldao.ClosetShareList")
defer span.End()
var list []mysqlmodel.ClosetShare
var total int64
q := d.db.WithContext(ctx).Model(&mysqlmodel.ClosetShare{})
if p.UserID != 0 {
q = q.Where("user_id = ?", p.UserID)
}
if p.Status != 0 {
q = q.Where("status = ?", p.Status)
}
if err := q.Count(&total).Error; err != nil {
span.RecordError(err)
return nil, 0, err
}
if err := q.Offset((p.Page - 1) * p.Size).Limit(p.Size).Order("id DESC").Find(&list).Error; err != nil {
span.RecordError(err)
return nil, 0, err
}
return list, total, nil
}
// ClosetShareGetByUUID 按分享 UUID 查询
func (d *ClosetShareDao) ClosetShareGetByUUID(ctx context.Context, shareUUID string) (*mysqlmodel.ClosetShare, error) {
ctx, span := global.Tracer.Start(ctx, "mysqldao.ClosetShareGetByUUID")
defer span.End()
var share mysqlmodel.ClosetShare
if err := d.db.WithContext(ctx).Where("share_uuid = ?", shareUUID).First(&share).Error; err != nil {
span.RecordError(err)
return nil, err
}
return &share, nil
}
// ClosetShareUpdateStatus 更新分享状态(封禁/解封)
func (d *ClosetShareDao) ClosetShareUpdateStatus(ctx context.Context, shareUUID string, status int8) error {
ctx, span := global.Tracer.Start(ctx, "mysqldao.ClosetShareUpdateStatus")
defer span.End()
if err := d.db.WithContext(ctx).Model(&mysqlmodel.ClosetShare{}).
Where("share_uuid = ?", shareUUID).
UpdateColumn("status", status).Error; err != nil {
span.RecordError(err)
return err
}
return nil
}