first commit
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user