157 lines
5.1 KiB
Go
157 lines
5.1 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
|
|
}
|
|
|
|
// 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
|
|
}
|