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 { //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; public FileUploadService() { //_db = SqlSugarConfig.GetSqlSugarScope(); } // 示例:查询所有文件信息 public List GetAllFiles() { return _db.Queryable().ToList(); } // 示例:添加文件信息 public bool AddFile(FUpload file) { return _db.Insertable(file).ExecuteCommand() > 0; } // 示例:更新文件信息 public bool UpdateFile(FUpload file) { return _db.Updateable(file).ExecuteCommand() > 0; } // 示例:更新文件信息(上传完成更新) public bool UpdateFileComplete(string id,bool flag) { lock (_dbLock) { return _db.Updateable() .SetColumns(r => new FUpload { IsComplete = flag, CompleteTime = DateTime.Now }) .Where(r => r.Id == id) .ExecuteCommand() > 0; } } // 示例:删除文件信息 public bool DeleteFile(string id) { return _db.Deleteable().Where(f => f.Id == id).ExecuteCommand() > 0; } // 示例:删除文件信息 public bool DeleteFiles(List ids) { var aa = _db.Deleteable().Where(f => ids.Contains(f.Id)).ExecuteCommand(); return aa> 0; } // 检查数据库是否存在,不存在则创建 public void InitDatabase() { _db.DbMaintenance.CreateDatabase(); // SQLite不需要创建数据库,但可以用于检查 // 如果表不存在则创建 if (!_db.DbMaintenance.IsAnyTable("f_upload", false)) { _db.CodeFirst.InitTables(typeof(FUpload)); } } } }