121 lines
3.6 KiB
Go
121 lines
3.6 KiB
Go
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"
|
||
)
|
||
|
||
// OutfitDao 穿搭数据访问
|
||
type OutfitDao struct {
|
||
db *gorm.DB
|
||
}
|
||
|
||
// NewOutfitDao 创建穿搭 DAO
|
||
func NewOutfitDao() *OutfitDao {
|
||
return &OutfitDao{db: global.MysqlEngine}
|
||
}
|
||
|
||
// OutfitListParams 穿搭分页列表筛选参数。
|
||
type OutfitListParams struct {
|
||
UserID int64 // 用户 ID,0 表示全部
|
||
IsPublic int8 // 是否公开,0 表示全部
|
||
AuditStatus int8 // 审核状态,0 表示全部
|
||
Status int8 // 穿搭状态,0 表示全部
|
||
Keyword string // 模糊匹配穿搭名称
|
||
Page int
|
||
Size int
|
||
}
|
||
|
||
// OutfitList 分页查询穿搭(管理端全量视角)
|
||
func (d *OutfitDao) OutfitList(ctx context.Context, p OutfitListParams) ([]mysqlmodel.Outfit, int64, error) {
|
||
ctx, span := global.Tracer.Start(ctx, "mysqldao.OutfitList")
|
||
defer span.End()
|
||
|
||
var list []mysqlmodel.Outfit
|
||
var total int64
|
||
q := d.db.WithContext(ctx).Model(&mysqlmodel.Outfit{})
|
||
if p.UserID != 0 {
|
||
q = q.Where("user_id = ?", p.UserID)
|
||
}
|
||
if p.IsPublic != 0 {
|
||
q = q.Where("is_public = ?", p.IsPublic)
|
||
}
|
||
if p.AuditStatus != 0 {
|
||
q = q.Where("audit_status = ?", p.AuditStatus)
|
||
}
|
||
if p.Status != 0 {
|
||
q = q.Where("status = ?", p.Status)
|
||
}
|
||
if p.Keyword != "" {
|
||
q = q.Where("outfit_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
|
||
}
|
||
|
||
// OutfitGetByUUID 按穿搭 UUID 查询
|
||
func (d *OutfitDao) OutfitGetByUUID(ctx context.Context, outfitUUID string) (*mysqlmodel.Outfit, error) {
|
||
ctx, span := global.Tracer.Start(ctx, "mysqldao.OutfitGetByUUID")
|
||
defer span.End()
|
||
|
||
var outfit mysqlmodel.Outfit
|
||
if err := d.db.WithContext(ctx).Where("outfit_uuid = ?", outfitUUID).First(&outfit).Error; err != nil {
|
||
span.RecordError(err)
|
||
return nil, err
|
||
}
|
||
return &outfit, nil
|
||
}
|
||
|
||
// OutfitItemListByOutfitID 查询穿搭衣物明细
|
||
func (d *OutfitDao) OutfitItemListByOutfitID(ctx context.Context, outfitID int64) ([]mysqlmodel.OutfitItem, error) {
|
||
ctx, span := global.Tracer.Start(ctx, "mysqldao.OutfitItemListByOutfitID")
|
||
defer span.End()
|
||
|
||
var items []mysqlmodel.OutfitItem
|
||
if err := d.db.WithContext(ctx).Where("outfit_id = ?", outfitID).Find(&items).Error; err != nil {
|
||
span.RecordError(err)
|
||
return nil, err
|
||
}
|
||
return items, nil
|
||
}
|
||
|
||
// OutfitUpdateByID 按主键更新穿搭字段
|
||
func (d *OutfitDao) OutfitUpdateByID(ctx context.Context, id int64, updates map[string]any) error {
|
||
ctx, span := global.Tracer.Start(ctx, "mysqldao.OutfitUpdateByID")
|
||
defer span.End()
|
||
|
||
if err := d.db.WithContext(ctx).Model(&mysqlmodel.Outfit{}).Where("id = ?", id).Updates(updates).Error; err != nil {
|
||
span.RecordError(err)
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// OutfitAuditWithLog 事务内更新穿搭审核状态并写审核日志。
|
||
func (d *OutfitDao) OutfitAuditWithLog(ctx context.Context, outfitID int64, auditResult int8, rejectReason string, log *mysqlmodel.OutfitAuditLog) error {
|
||
ctx, span := global.Tracer.Start(ctx, "mysqldao.OutfitAuditWithLog")
|
||
defer span.End()
|
||
|
||
return d.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||
if err := tx.Model(&mysqlmodel.Outfit{}).
|
||
Where("id = ?", outfitID).
|
||
UpdateColumn("audit_status", auditResult).Error; err != nil {
|
||
return err
|
||
}
|
||
log.TargetID = outfitID
|
||
log.AuditResult = auditResult
|
||
log.RejectReason = rejectReason
|
||
return tx.Create(log).Error
|
||
})
|
||
}
|