199 lines
7.0 KiB
Go
199 lines
7.0 KiB
Go
package mysqldao
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/global"
|
|
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/internal/mysqlmodel"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// ClothesDao 衣物数据访问
|
|
type ClothesDao struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewClothesDao 创建衣物 DAO
|
|
func NewClothesDao() *ClothesDao {
|
|
return &ClothesDao{db: global.MysqlEngine}
|
|
}
|
|
|
|
// ClothesCreate 创建衣物
|
|
func (d *ClothesDao) ClothesCreate(ctx context.Context, clothes *mysqlmodel.Clothes) error {
|
|
ctx, span := global.Tracer.Start(ctx, "mysqldao.ClothesCreate")
|
|
defer span.End()
|
|
|
|
if err := d.db.WithContext(ctx).Create(clothes).Error; err != nil {
|
|
span.RecordError(err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ClothesDeleteByUUID 按 UUID 软删除衣物
|
|
func (d *ClothesDao) ClothesDeleteByUUID(ctx context.Context, clothesUUID string) error {
|
|
ctx, span := global.Tracer.Start(ctx, "mysqldao.ClothesDeleteByUUID")
|
|
defer span.End()
|
|
|
|
if err := d.db.WithContext(ctx).Where("clothes_uuid = ?", clothesUUID).Delete(&mysqlmodel.Clothes{}).Error; err != nil {
|
|
span.RecordError(err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ClothesUpdateByUUID 按 UUID 更新衣物字段
|
|
func (d *ClothesDao) ClothesUpdateByUUID(ctx context.Context, clothesUUID string, updates map[string]any) error {
|
|
ctx, span := global.Tracer.Start(ctx, "mysqldao.ClothesUpdateByUUID")
|
|
defer span.End()
|
|
|
|
if err := d.db.WithContext(ctx).Model(&mysqlmodel.Clothes{}).Where("clothes_uuid = ?", clothesUUID).Updates(updates).Error; err != nil {
|
|
span.RecordError(err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ClothesUpdateByUUIDIfStatusIn 条件更新衣物:仅当当前图片处理状态在 statuses 内时才更新,返回是否更新成功。
|
|
// 用于「处理成功回填」这类必须与状态守卫绑定的写操作:恢复任务可能已把该记录置为失败并回退额度,
|
|
// 无条件写回会把状态改回成功,造成「额度已退但衣物成功」的不一致。
|
|
func (d *ClothesDao) ClothesUpdateByUUIDIfStatusIn(ctx context.Context, clothesUUID string, statuses []int8, updates map[string]any) (bool, error) {
|
|
ctx, span := global.Tracer.Start(ctx, "mysqldao.ClothesUpdateByUUIDIfStatusIn")
|
|
defer span.End()
|
|
|
|
res := d.db.WithContext(ctx).Model(&mysqlmodel.Clothes{}).
|
|
Where("clothes_uuid = ? AND image_process_status IN ?", clothesUUID, statuses).
|
|
Updates(updates)
|
|
if res.Error != nil {
|
|
span.RecordError(res.Error)
|
|
return false, res.Error
|
|
}
|
|
return res.RowsAffected > 0, nil
|
|
}
|
|
|
|
// ClothesReferencedCount 统计衣物被「穿搭明细 / 衣柜分享明细」引用的数量(删除前校验,>0 表示不可删除)。
|
|
// 删除被引用的衣物会留下悬挂引用,公开穿搭与分享会变成空壳(明细里查不到衣物,前端静默跳过)。
|
|
func (d *ClothesDao) ClothesReferencedCount(ctx context.Context, clothesUUID string) (int64, error) {
|
|
ctx, span := global.Tracer.Start(ctx, "mysqldao.ClothesReferencedCount")
|
|
defer span.End()
|
|
|
|
subQuery := "clothes_id IN (SELECT id FROM clothes WHERE clothes_uuid = ? AND deleted_at = 0)"
|
|
|
|
var total int64
|
|
if err := d.db.WithContext(ctx).Model(&mysqlmodel.OutfitItem{}).
|
|
Where(subQuery, clothesUUID).Count(&total).Error; err != nil {
|
|
span.RecordError(err)
|
|
return 0, err
|
|
}
|
|
if total > 0 {
|
|
return total, nil
|
|
}
|
|
if err := d.db.WithContext(ctx).Model(&mysqlmodel.ClosetShareItem{}).
|
|
Where(subQuery, clothesUUID).Count(&total).Error; err != nil {
|
|
span.RecordError(err)
|
|
return 0, err
|
|
}
|
|
return 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
|
|
}
|
|
|
|
// ClothesGetByID 按主键 ID 查询衣物
|
|
func (d *ClothesDao) ClothesGetByID(ctx context.Context, id int64) (*mysqlmodel.Clothes, error) {
|
|
ctx, span := global.Tracer.Start(ctx, "mysqldao.ClothesGetByID")
|
|
defer span.End()
|
|
|
|
var clothes mysqlmodel.Clothes
|
|
if err := d.db.WithContext(ctx).First(&clothes, id).Error; err != nil {
|
|
span.RecordError(err)
|
|
return nil, err
|
|
}
|
|
return &clothes, nil
|
|
}
|
|
|
|
// ClothesGetByUUIDs 按 UUID 批量查询衣物
|
|
func (d *ClothesDao) ClothesGetByUUIDs(ctx context.Context, uuids []string) ([]mysqlmodel.Clothes, error) {
|
|
ctx, span := global.Tracer.Start(ctx, "mysqldao.ClothesGetByUUIDs")
|
|
defer span.End()
|
|
|
|
var list []mysqlmodel.Clothes
|
|
if len(uuids) == 0 {
|
|
return list, nil
|
|
}
|
|
if err := d.db.WithContext(ctx).Where("clothes_uuid IN ?", uuids).Find(&list).Error; err != nil {
|
|
span.RecordError(err)
|
|
return nil, err
|
|
}
|
|
return list, nil
|
|
}
|
|
|
|
// ClothesGetByIDs 按主键 ID 批量查询衣物
|
|
func (d *ClothesDao) ClothesGetByIDs(ctx context.Context, ids []int64) ([]mysqlmodel.Clothes, error) {
|
|
ctx, span := global.Tracer.Start(ctx, "mysqldao.ClothesGetByIDs")
|
|
defer span.End()
|
|
|
|
var list []mysqlmodel.Clothes
|
|
if len(ids) == 0 {
|
|
return list, nil
|
|
}
|
|
if err := d.db.WithContext(ctx).Where("id IN ?", ids).Find(&list).Error; err != nil {
|
|
span.RecordError(err)
|
|
return nil, err
|
|
}
|
|
return list, nil
|
|
}
|
|
|
|
// ClothesListByUserID 分页查询用户衣物(categoryMain/categorySub 为空不筛选)
|
|
func (d *ClothesDao) ClothesListByUserID(ctx context.Context, userID int64, categoryMain, categorySub string, page, pageSize int) ([]mysqlmodel.Clothes, int64, error) {
|
|
ctx, span := global.Tracer.Start(ctx, "mysqldao.ClothesListByUserID")
|
|
defer span.End()
|
|
|
|
var list []mysqlmodel.Clothes
|
|
var total int64
|
|
q := d.db.WithContext(ctx).Model(&mysqlmodel.Clothes{}).Where("user_id = ?", userID)
|
|
if categoryMain != "" {
|
|
q = q.Where("category_main = ?", categoryMain)
|
|
}
|
|
if categorySub != "" {
|
|
q = q.Where("category_sub = ?", categorySub)
|
|
}
|
|
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
|
|
}
|
|
|
|
// ClothesListStaleProcessing 查询超时未完成的图片任务。
|
|
func (d *ClothesDao) ClothesListStaleProcessing(ctx context.Context, before int64, limit int) ([]mysqlmodel.Clothes, error) {
|
|
var list []mysqlmodel.Clothes
|
|
err := d.db.WithContext(ctx).
|
|
Where("image_process_status IN ? AND updated_at < ?", []int8{1, 2, 3}, before).
|
|
Order("id ASC").Limit(limit).Find(&list).Error
|
|
return list, err
|
|
}
|
|
|
|
// ClothesClaimStaleProcessing 原子认领超时任务,避免多实例重复恢复。
|
|
func (d *ClothesDao) ClothesClaimStaleProcessing(ctx context.Context, clothesUUID string, before int64) (bool, error) {
|
|
res := d.db.WithContext(ctx).Model(&mysqlmodel.Clothes{}).
|
|
Where("clothes_uuid = ? AND image_process_status IN ? AND updated_at < ?", clothesUUID, []int8{1, 2, 3}, before).
|
|
Updates(map[string]any{"image_process_status": 3, "updated_at": time.Now().Unix()})
|
|
return res.RowsAffected > 0, res.Error
|
|
}
|