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" ) // ClothesDao 衣物数据访问 type ClothesDao struct { db *gorm.DB } // NewClothesDao 创建衣物 DAO func NewClothesDao() *ClothesDao { return &ClothesDao{db: global.MysqlEngine} } // ClothesListParams 衣物分页列表筛选参数。 type ClothesListParams struct { UserID int64 // 用户 ID,0 表示全部 ImageProcessStatus int8 // AI 处理状态,0 表示全部 CategoryMain string // 大类筛选 Keyword string // 模糊匹配名称 Page int Size int } // ClothesList 分页查询衣物 func (d *ClothesDao) ClothesList(ctx context.Context, p ClothesListParams) ([]mysqlmodel.Clothes, int64, error) { ctx, span := global.Tracer.Start(ctx, "mysqldao.ClothesList") defer span.End() var list []mysqlmodel.Clothes var total int64 q := d.db.WithContext(ctx).Model(&mysqlmodel.Clothes{}) if p.UserID != 0 { q = q.Where("user_id = ?", p.UserID) } if p.ImageProcessStatus != 0 { q = q.Where("image_process_status = ?", p.ImageProcessStatus) } if p.CategoryMain != "" { q = q.Where("category_main = ?", p.CategoryMain) } if p.Keyword != "" { q = q.Where("name LIKE ?", "%"+p.Keyword+"%") } 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 } // ClothesGetByUUID 按衣物 UUID 查询 func (d *ClothesDao) ClothesGetByUUID(ctx context.Context, clothesUUID string) (*mysqlmodel.Clothes, error) { ctx, span := global.Tracer.Start(ctx, "mysqldao.ClothesGetByUUID") defer span.End() var clothes mysqlmodel.Clothes if err := d.db.WithContext(ctx).Where("clothes_uuid = ?", clothesUUID).First(&clothes).Error; err != nil { span.RecordError(err) return nil, err } return &clothes, nil } // ClothesListByIDs 按主键 ID 批量查询衣物(穿搭详情聚合用) func (d *ClothesDao) ClothesListByIDs(ctx context.Context, ids []int64) ([]mysqlmodel.Clothes, error) { ctx, span := global.Tracer.Start(ctx, "mysqldao.ClothesListByIDs") defer span.End() if len(ids) == 0 { return []mysqlmodel.Clothes{}, nil } var list []mysqlmodel.Clothes if err := d.db.WithContext(ctx).Where("id IN ?", ids).Find(&list).Error; err != nil { span.RecordError(err) return nil, err } return list, nil }