using DownKyi.Core.Storage.Database.Download; using DownKyi.Models; using DownKyi.ViewModels.DownloadManager; using System.Collections.Generic; namespace DownKyi.Services.Download { public class DownloadStorageService { #region 下载中数据 /// /// 添加下载中数据 /// /// public void AddDownloading(DownloadingItem downloadingItem) { AddDownloadBase(downloadingItem.DownloadBase); DownloadingDb downloadingDb = new DownloadingDb(); object obj = downloadingDb.QueryById(downloadingItem.DownloadBase.Uuid); if (obj == null) { downloadingDb.Insert(downloadingItem.DownloadBase.Uuid, downloadingItem.Downloading); } downloadingDb.Close(); } /// /// 删除下载中数据 /// /// public void RemoveDownloading(DownloadingItem downloadingItem) { RemoveDownloadBase(downloadingItem.DownloadBase.Uuid); DownloadingDb downloadingDb = new DownloadingDb(); downloadingDb.Delete(downloadingItem.DownloadBase.Uuid); downloadingDb.Close(); } /// /// 获取所有的下载中数据 /// /// public List GetDownloading() { // 从数据库获取数据 DownloadingDb downloadingDb = new DownloadingDb(); Dictionary dic = downloadingDb.QueryAll(); downloadingDb.Close(); // 遍历 List list = new List(); foreach (KeyValuePair item in dic) { if (item.Value is Downloading downloading) { DownloadingItem downloadingItem = new DownloadingItem { DownloadBase = GetDownloadBase(item.Key), Downloading = downloading }; list.Add(downloadingItem); } } return list; } /// /// 修改下载中数据 /// /// public void UpdateDownloading(DownloadingItem downloadingItem) { UpdateDownloadBase(downloadingItem.DownloadBase); DownloadingDb downloadingDb = new DownloadingDb(); downloadingDb.Update(downloadingItem.DownloadBase.Uuid, downloadingItem.Downloading); downloadingDb.Close(); } #endregion #region 下载完成数据 /// /// 添加下载完成数据 /// /// public void AddDownloaded(DownloadedItem downloadedItem) { AddDownloadBase(downloadedItem.DownloadBase); DownloadedDb downloadedDb = new DownloadedDb(); object obj = downloadedDb.QueryById(downloadedItem.DownloadBase.Uuid); if (obj == null) { downloadedDb.Insert(downloadedItem.DownloadBase.Uuid, downloadedItem.Downloaded); } downloadedDb.Close(); } /// /// 删除下载完成数据 /// /// public void RemoveDownloaded(DownloadedItem downloadedItem) { RemoveDownloadBase(downloadedItem.DownloadBase.Uuid); DownloadedDb downloadedDb = new DownloadedDb(); downloadedDb.Delete(downloadedItem.DownloadBase.Uuid); downloadedDb.Close(); } /// /// 获取所有的下载完成数据 /// /// public List GetDownloaded() { // 从数据库获取数据 DownloadedDb downloadedDb = new DownloadedDb(); Dictionary dic = downloadedDb.QueryAll(); downloadedDb.Close(); // 遍历 List list = new List(); foreach (KeyValuePair item in dic) { if (item.Value is Downloaded downloaded) { DownloadedItem downloadingItem = new DownloadedItem { DownloadBase = GetDownloadBase(item.Key), Downloaded = downloaded }; list.Add(downloadingItem); } } return list; } /// /// 修改下载完成数据 /// /// public void UpdateDownloaded(DownloadedItem downloadedItem) { UpdateDownloadBase(downloadedItem.DownloadBase); DownloadedDb downloadedDb = new DownloadedDb(); downloadedDb.Update(downloadedItem.DownloadBase.Uuid, downloadedItem.Downloaded); downloadedDb.Close(); } #endregion #region DownloadBase /// /// 向数据库添加DownloadBase /// /// private void AddDownloadBase(DownloadBase downloadBase) { DownloadBaseDb downloadBaseDb = new DownloadBaseDb(); object obj = downloadBaseDb.QueryById(downloadBase.Uuid); if (obj == null) { downloadBaseDb.Insert(downloadBase.Uuid, downloadBase); } downloadBaseDb.Close(); } /// /// 从数据库删除DownloadBase /// /// private void RemoveDownloadBase(string uuid) { DownloadBaseDb downloadBaseDb = new DownloadBaseDb(); downloadBaseDb.Delete(uuid); downloadBaseDb.Close(); } /// /// 从数据库获取所有的DownloadBase /// /// private DownloadBase GetDownloadBase(string uuid) { DownloadBaseDb downloadBaseDb = new DownloadBaseDb(); object obj = downloadBaseDb.QueryById(uuid); downloadBaseDb.Close(); return obj is DownloadBase downloadBase ? downloadBase : null; } /// /// 从数据库修改DownloadBase /// /// private void UpdateDownloadBase(DownloadBase downloadBase) { DownloadBaseDb downloadBaseDb = new DownloadBaseDb(); downloadBaseDb.Update(downloadBase.Uuid, downloadBase); downloadBaseDb.Close(); } #endregion } }