FieldWorkClient/Services/FileUploadService.cs

154 lines
5.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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<FUpload> GetAllFiles(int pageNumber = 1, int pageSize = 50)
{
using var db = SqlSugarConfig.GetSqlSugarScope();
return db.Queryable<FUpload>()
.OrderByDescending(f => f.CreateTime) // 按创建时间倒序排列
.ToPageList(pageNumber, pageSize);
}
// 查询未完成的上传文件
public List<FUpload> GetIncompleteFiles(int pageNumber = 1, int pageSize = 50)
{
using var db = SqlSugarConfig.GetSqlSugarScope();
return db.Queryable<FUpload>()
.Where(f => f.IsComplete == false)
.OrderByDescending(f => f.CreateTime)
.ToPageList(pageNumber, pageSize);
}
// 查询已完成的上传文件
public List<FUpload> GetCompleteFiles(int pageNumber = 1, int pageSize = 50)
{
using var db = SqlSugarConfig.GetSqlSugarScope();
return db.Queryable<FUpload>()
.Where(f => f.IsComplete == true)
.OrderByDescending(f => f.CompleteTime)
.ToPageList(pageNumber, pageSize);
}
// 获取文件总数
public int GetTotalFileCount()
{
using var db = SqlSugarConfig.GetSqlSugarScope();
return db.Queryable<FUpload>().Count();
}
// 获取未完成文件数
public int GetIncompleteFileCount()
{
using var db = SqlSugarConfig.GetSqlSugarScope();
return db.Queryable<FUpload>().Where(f => f.IsComplete == false).Count();
}
// 获取已完成文件数
public int GetCompleteFileCount()
{
using var db = SqlSugarConfig.GetSqlSugarScope();
return db.Queryable<FUpload>().Where(f => f.IsComplete == true).Count();
}
// 示例:添加文件信息
public bool AddFile(FUpload file)
{
using var db = SqlSugarConfig.GetSqlSugarScope();
return db.Insertable(file).ExecuteCommand() > 0;
}
// 示例:更新文件信息
public bool UpdateFile(FUpload file)
{
using var db = SqlSugarConfig.GetSqlSugarScope();
return db.Updateable(file).ExecuteCommand() > 0;
}
// 示例:更新文件信息(上传完成更新)
public async Task<bool> UpdateFileCompleteAsync(string id, bool flag)
{
using var db = SqlSugarConfig.GetSqlSugarScope();
var result = db.Updateable<FUpload>()
.SetColumns(r => new FUpload { IsComplete = flag, CompleteTime = DateTime.Now })
.Where(r => r.Id == id)
.ExecuteCommand();
//Console.WriteLine($"数据库更新结果: {result}, ID: {id}, Flag: {flag}");
return result > 0;
}
// 同步版本保持兼容性
public bool UpdateFileComplete(string id, bool flag)
{
return UpdateFileCompleteAsync(id, flag).GetAwaiter().GetResult();
}
// 批量更新文件状态
public async Task UpdateMultipleFilesStatusAsync(List<(string id, bool status)> updates)
{
await Task.Run(() =>
{
using var db = SqlSugarConfig.GetSqlSugarScope();
foreach (var (id, status) in updates)
{
var result = db.Updateable<FUpload>()
.SetColumns(r => new FUpload { IsComplete = status, CompleteTime = DateTime.Now })
.Where(r => r.Id == id)
.ExecuteCommand();
Console.WriteLine($"批量更新结果: {result}, ID: {id}, Status: {status}");
}
});
}
// 示例:删除文件信息
public bool DeleteFile(string id)
{
using var db = SqlSugarConfig.GetSqlSugarScope();
return db.Deleteable<FUpload>().Where(f => f.Id == id).ExecuteCommand() > 0;
}
// 示例:删除文件信息
public bool DeleteFiles(List<string> ids)
{
using var db = SqlSugarConfig.GetSqlSugarScope();
var aa = db.Deleteable<FUpload>().Where(f => ids.Contains(f.Id)).ExecuteCommand();
return aa > 0;
}
// 检查数据库是否存在,不存在则创建
public void InitDatabase()
{
using var db = SqlSugarConfig.GetSqlSugarScope();
db.DbMaintenance.CreateDatabase(); // SQLite不需要创建数据库但可以用于检查
// 如果表不存在则创建
if (!db.DbMaintenance.IsAnyTable("f_upload", false))
{
db.CodeFirst.InitTables(typeof(FUpload));
}
}
}
}