2025-04-02 09:30:13 +08:00
|
|
|
|
using Hopetry.Models;
|
|
|
|
|
|
using Hopetry.Provider;
|
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Hopetry.Services
|
|
|
|
|
|
{
|
|
|
|
|
|
public class FileUploadService
|
|
|
|
|
|
{
|
2025-04-07 11:30:57 +08:00
|
|
|
|
private readonly SqlSugarClient _db;
|
2025-04-02 09:30:13 +08:00
|
|
|
|
|
|
|
|
|
|
public FileUploadService()
|
|
|
|
|
|
{
|
|
|
|
|
|
_db = SqlSugarConfig.GetSqlSugarScope();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 示例:查询所有文件信息
|
|
|
|
|
|
public List<FUpload> GetAllFiles()
|
|
|
|
|
|
{
|
|
|
|
|
|
return _db.Queryable<FUpload>().ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 示例:添加文件信息
|
|
|
|
|
|
public bool AddFile(FUpload file)
|
|
|
|
|
|
{
|
|
|
|
|
|
return _db.Insertable(file).ExecuteCommand() > 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 示例:更新文件信息
|
|
|
|
|
|
public bool UpdateFile(FUpload file)
|
|
|
|
|
|
{
|
|
|
|
|
|
return _db.Updateable(file).ExecuteCommand() > 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-07 11:30:57 +08:00
|
|
|
|
// 示例:更新文件信息(上传完成更新)
|
|
|
|
|
|
public bool UpdateFileComplete(string id)
|
|
|
|
|
|
{
|
|
|
|
|
|
return _db.Updateable<FUpload>()
|
|
|
|
|
|
.SetColumns(r=> new FUpload { IsComplete = true,CompleteTime=DateTime.Now })
|
|
|
|
|
|
.Where(r=>r.Id==id)
|
|
|
|
|
|
.ExecuteCommand() > 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-02 09:30:13 +08:00
|
|
|
|
// 示例:删除文件信息
|
2025-04-07 11:30:57 +08:00
|
|
|
|
public bool DeleteFile(string id)
|
2025-04-02 09:30:13 +08:00
|
|
|
|
{
|
|
|
|
|
|
return _db.Deleteable<FUpload>().Where(f => f.Id == id).ExecuteCommand() > 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查数据库是否存在,不存在则创建
|
|
|
|
|
|
public void InitDatabase()
|
|
|
|
|
|
{
|
|
|
|
|
|
_db.DbMaintenance.CreateDatabase(); // SQLite不需要创建数据库,但可以用于检查
|
|
|
|
|
|
|
|
|
|
|
|
// 如果表不存在则创建
|
|
|
|
|
|
if (!_db.DbMaintenance.IsAnyTable("f_upload", false))
|
|
|
|
|
|
{
|
|
|
|
|
|
_db.CodeFirst.InitTables(typeof(FUpload));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|