75 lines
2.1 KiB
C#
75 lines
2.1 KiB
C#
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 readonly SqlSugarClient _db;
|
||
|
||
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;
|
||
}
|
||
|
||
// 示例:更新文件信息(上传完成更新)
|
||
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;
|
||
}
|
||
|
||
// 示例:删除文件信息
|
||
public bool DeleteFile(string id)
|
||
{
|
||
return _db.Deleteable<FUpload>().Where(f => f.Id == id).ExecuteCommand() > 0;
|
||
}
|
||
|
||
// 示例:删除文件信息
|
||
public bool DeleteFiles(List<string> ids)
|
||
{
|
||
var aa = _db.Deleteable<FUpload>().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));
|
||
}
|
||
}
|
||
}
|
||
}
|