package dao import ( "context" "cms-api/internal/model" ) // Create 创建附件 func (dao *AttachmentDao) Create(ctx context.Context, attachment *model.Attachment) (int64, error) { result, err := dao.Ctx(ctx).Data(attachment).Insert() if err != nil { return 0, err } id, err := result.LastInsertId() if err != nil { return 0, err } return id, nil } // GetById 根据ID获取附件 func (dao *AttachmentDao) GetById(ctx context.Context, id int) (*model.Attachment, error) { var attachment *model.Attachment err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Scan(&attachment) if err != nil { return nil, err } return attachment, nil } // List 获取附件列表 func (dao *AttachmentDao) List(ctx context.Context, req *model.AttachmentListRequest) ([]*model.Attachment, int, error) { var ( attachments []*model.Attachment total int db = dao.Ctx(ctx) ) // 添加查询条件 if req.FileType != "" { db = db.Where(dao.Columns().FileType, req.FileType) } if req.UploadedBy > 0 { db = db.Where(dao.Columns().UploadedBy, req.UploadedBy) } if req.StorageType != "" { db = db.Where(dao.Columns().StorageType, req.StorageType) } if req.Keyword != "" { db = db.Where(dao.Columns().OriginalName+" LIKE ? OR "+dao.Columns().FileName+" LIKE ?", "%"+req.Keyword+"%", "%"+req.Keyword+"%") } if req.StartDate != "" { db = db.Where(dao.Columns().CreatedAt+" >= ?", req.StartDate) } if req.EndDate != "" { db = db.Where(dao.Columns().CreatedAt+" <= ?", req.EndDate) } // 获取总数 count, err := db.Count() if err != nil { return nil, 0, err } total = count // 分页查询 offset := (req.Page - 1) * req.PageSize err = db.Order(dao.Columns().CreatedAt + " DESC"). Limit(req.PageSize). Offset(offset). Scan(&attachments) if err != nil { return nil, 0, err } return attachments, total, nil } // Delete 删除附件 func (dao *AttachmentDao) Delete(ctx context.Context, id int) error { _, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Delete() return err } // IncrementUsageCount 增加使用次数 func (dao *AttachmentDao) IncrementUsageCount(ctx context.Context, id int) error { _, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Increment(dao.Columns().UsageCount, 1) return err } // GetCount 获取附件总数 func (dao *AttachmentDao) GetCount(ctx context.Context) (int, error) { count, err := dao.Ctx(ctx).Count() return count, err } // GetTotalSize 获取附件总大小 func (dao *AttachmentDao) GetTotalSize(ctx context.Context) (int64, error) { var totalSize int64 err := dao.Ctx(ctx).Fields("SUM(" + dao.Columns().FileSize + ") as total_size").Scan(&totalSize) return totalSize, err } // GetCountByType 根据类型获取附件数量 func (dao *AttachmentDao) GetCountByType(ctx context.Context, fileType string) (int, error) { count, err := dao.Ctx(ctx).Where(dao.Columns().FileType, fileType).Count() return count, err } // BatchDelete 批量删除附件 func (dao *AttachmentDao) BatchDelete(ctx context.Context, ids []int) error { _, err := dao.Ctx(ctx).Where(dao.Columns().Id+" IN (?)", ids).Delete() return err } // GetByFileName 根据文件名获取附件 func (dao *AttachmentDao) GetByFileName(ctx context.Context, fileName string) (*model.Attachment, error) { var attachment *model.Attachment err := dao.Ctx(ctx).Where(dao.Columns().FileName, fileName).Scan(&attachment) if err != nil { return nil, err } return attachment, nil } // GetByUploader 根据上传者获取附件列表 func (dao *AttachmentDao) GetByUploader(ctx context.Context, uploaderId int, page, pageSize int) ([]*model.Attachment, int, error) { var ( attachments []*model.Attachment total int db = dao.Ctx(ctx) ) // 添加上传者条件 db = db.Where(dao.Columns().UploadedBy, uploaderId) // 获取总数 count, err := db.Count() if err != nil { return nil, 0, err } total = count // 分页查询 offset := (page - 1) * pageSize err = db.Order(dao.Columns().CreatedAt + " DESC"). Limit(pageSize). Offset(offset). Scan(&attachments) if err != nil { return nil, 0, err } return attachments, total, nil } // GetByIds 根据ID列表获取附件 func (dao *AttachmentDao) GetByIds(ctx context.Context, ids []int) ([]*model.Attachment, error) { var attachments []*model.Attachment err := dao.Ctx(ctx).Where(dao.Columns().Id+" IN (?)", ids).Scan(&attachments) if err != nil { return nil, err } return attachments, nil }