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-07-18 10:51:58 +08:00
|
|
|
|
//private static readonly SqlSugarScope _db = SqlSugarConfig.GetSqlSugarScope();
|
|
|
|
|
|
//private static readonly SqlSugarClient _db = SqlSugarConfig.GetSqlSugarScope();
|
|
|
|
|
|
//private static readonly object _dbLock = new object();
|
|
|
|
|
|
private readonly SqlSugarClient _db;
|
2025-04-02 09:30:13 +08:00
|
|
|
|
|
|
|
|
|
|
public FileUploadService()
|
|
|
|
|
|
{
|
2025-07-18 10:51:58 +08:00
|
|
|
|
_db = SqlSugarConfig.GetSqlSugarScope();
|
2025-04-02 09:30:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 示例:查询所有文件信息
|
|
|
|
|
|
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
|
|
|
|
// 示例:更新文件信息(上传完成更新)
|
2025-07-14 17:32:18 +08:00
|
|
|
|
public bool UpdateFileComplete(string id,bool flag)
|
2025-04-07 11:30:57 +08:00
|
|
|
|
{
|
2025-07-18 10:51:58 +08:00
|
|
|
|
|
2025-07-15 14:39:21 +08:00
|
|
|
|
return _db.Updateable<FUpload>()
|
|
|
|
|
|
.SetColumns(r => new FUpload { IsComplete = flag, CompleteTime = DateTime.Now })
|
|
|
|
|
|
.Where(r => r.Id == id)
|
2025-04-07 11:30:57 +08:00
|
|
|
|
.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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-04 17:20:25 +08:00
|
|
|
|
// 示例:删除文件信息
|
|
|
|
|
|
public bool DeleteFiles(List<string> ids)
|
|
|
|
|
|
{
|
|
|
|
|
|
var aa = _db.Deleteable<FUpload>().Where(f => ids.Contains(f.Id)).ExecuteCommand();
|
|
|
|
|
|
return aa> 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-02 09:30:13 +08:00
|
|
|
|
// 检查数据库是否存在,不存在则创建
|
|
|
|
|
|
public void InitDatabase()
|
|
|
|
|
|
{
|
|
|
|
|
|
_db.DbMaintenance.CreateDatabase(); // SQLite不需要创建数据库,但可以用于检查
|
|
|
|
|
|
|
|
|
|
|
|
// 如果表不存在则创建
|
|
|
|
|
|
if (!_db.DbMaintenance.IsAnyTable("f_upload", false))
|
|
|
|
|
|
{
|
|
|
|
|
|
_db.CodeFirst.InitTables(typeof(FUpload));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|