151 lines
3.8 KiB
Go
151 lines
3.8 KiB
Go
package dao
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
"dd_fiber_api/internal/document"
|
|
"dd_fiber_api/pkg/database"
|
|
|
|
"github.com/didi/gendry/builder"
|
|
)
|
|
|
|
// FileDAO 文档文件数据访问
|
|
type FileDAO struct {
|
|
client *database.MySQLClient
|
|
}
|
|
|
|
// NewFileDAO 创建 FileDAO
|
|
func NewFileDAO(client *database.MySQLClient) *FileDAO {
|
|
return &FileDAO{client: client}
|
|
}
|
|
|
|
// Create 创建文档记录
|
|
func (d *FileDAO) Create(f *document.DocFile) error {
|
|
if d.client == nil {
|
|
return fmt.Errorf("mysql client is nil")
|
|
}
|
|
table := "doc_files"
|
|
data := []map[string]any{
|
|
{
|
|
"id": f.ID,
|
|
"folder_id": f.FolderID,
|
|
"name": f.Name,
|
|
"file_name": f.FileName,
|
|
"file_url": f.FileURL,
|
|
"file_size": f.FileSize,
|
|
"mime_type": f.MimeType,
|
|
},
|
|
}
|
|
cond, vals, err := builder.BuildInsert(table, data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = d.client.DB.Exec(cond, vals...)
|
|
return err
|
|
}
|
|
|
|
// GetByID 根据 ID 获取
|
|
func (d *FileDAO) GetByID(id string) (*document.DocFile, error) {
|
|
if d.client == nil {
|
|
return nil, fmt.Errorf("mysql client is nil")
|
|
}
|
|
table := "doc_files"
|
|
where := map[string]any{"id": id}
|
|
cond, vals, err := builder.BuildSelect(table, where, []string{"id", "folder_id", "name", "file_name", "file_url", "file_size", "mime_type", "created_at", "updated_at"})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var f document.DocFile
|
|
var createdAt, updatedAt sql.NullString
|
|
err = d.client.DB.QueryRow(cond, vals...).Scan(&f.ID, &f.FolderID, &f.Name, &f.FileName, &f.FileURL, &f.FileSize, &f.MimeType, &createdAt, &updatedAt)
|
|
if err == sql.ErrNoRows {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if createdAt.Valid {
|
|
f.CreatedAt = createdAt.String
|
|
}
|
|
if updatedAt.Valid {
|
|
f.UpdatedAt = updatedAt.String
|
|
}
|
|
return &f, nil
|
|
}
|
|
|
|
// Update 更新(仅名称等可改)
|
|
func (d *FileDAO) Update(f *document.DocFile) error {
|
|
if d.client == nil {
|
|
return fmt.Errorf("mysql client is nil")
|
|
}
|
|
table := "doc_files"
|
|
where := map[string]any{"id": f.ID}
|
|
data := map[string]any{"name": f.Name}
|
|
cond, vals, err := builder.BuildUpdate(table, where, data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = d.client.DB.Exec(cond, vals...)
|
|
return err
|
|
}
|
|
|
|
// Delete 删除
|
|
func (d *FileDAO) Delete(id string) error {
|
|
if d.client == nil {
|
|
return fmt.Errorf("mysql client is nil")
|
|
}
|
|
table := "doc_files"
|
|
where := map[string]any{"id": id}
|
|
cond, vals, err := builder.BuildDelete(table, where)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = d.client.DB.Exec(cond, vals...)
|
|
return err
|
|
}
|
|
|
|
// ListByFolderID 按文件夹 ID 列出文件
|
|
func (d *FileDAO) ListByFolderID(folderID string) ([]*document.DocFile, error) {
|
|
if d.client == nil {
|
|
return nil, fmt.Errorf("mysql client is nil")
|
|
}
|
|
table := "doc_files"
|
|
where := map[string]any{"folder_id": folderID}
|
|
cond, vals, err := builder.BuildSelect(table, where, []string{"id", "folder_id", "name", "file_name", "file_url", "file_size", "mime_type", "created_at", "updated_at"})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
cond += " ORDER BY created_at DESC"
|
|
rows, err := d.client.DB.Query(cond, vals...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var list []*document.DocFile
|
|
for rows.Next() {
|
|
var f document.DocFile
|
|
var createdAt, updatedAt sql.NullString
|
|
if err := rows.Scan(&f.ID, &f.FolderID, &f.Name, &f.FileName, &f.FileURL, &f.FileSize, &f.MimeType, &createdAt, &updatedAt); err != nil {
|
|
continue
|
|
}
|
|
if createdAt.Valid {
|
|
f.CreatedAt = createdAt.String
|
|
}
|
|
if updatedAt.Valid {
|
|
f.UpdatedAt = updatedAt.String
|
|
}
|
|
list = append(list, &f)
|
|
}
|
|
return list, rows.Err()
|
|
}
|
|
|
|
// DeleteByFolderID 删除该文件夹下所有文件记录
|
|
func (d *FileDAO) DeleteByFolderID(folderID string) error {
|
|
if d.client == nil {
|
|
return fmt.Errorf("mysql client is nil")
|
|
}
|
|
_, err := d.client.DB.Exec("DELETE FROM doc_files WHERE folder_id = ?", folderID)
|
|
return err
|
|
}
|