mirror of
https://jihulab.com/DGP-Studio/Snap.Hutao.git
synced 2025-11-19 21:02:53 +08:00
refactor cultivation service
This commit is contained in:
@@ -52,6 +52,8 @@ internal sealed class DailyNoteEntry : ObservableObject, IMappingFrom<DailyNoteE
|
||||
/// </summary>
|
||||
public DailyNote? DailyNote { get; set; }
|
||||
|
||||
// TODO: Add RefreshTime
|
||||
|
||||
/// <summary>
|
||||
/// 树脂提醒阈值
|
||||
/// </summary>
|
||||
|
||||
@@ -217,7 +217,7 @@ internal sealed partial class AvatarInfoDbBulkOperation
|
||||
// This means that there are duplicate items.
|
||||
if (distinctCount < dbInfos.Count)
|
||||
{
|
||||
avatarInfoDbService.DeleteAvatarInfoByUid(uid);
|
||||
avatarInfoDbService.DeleteAvatarInfoRangeByUid(uid);
|
||||
dbInfos = new();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ internal sealed partial class AvatarInfoDbService : IAvatarInfoDbService
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteAvatarInfoByUid(string uid)
|
||||
public void DeleteAvatarInfoRangeByUid(string uid)
|
||||
{
|
||||
using (IServiceScope scope = serviceProvider.CreateScope())
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace Snap.Hutao.Service.AvatarInfo;
|
||||
|
||||
internal interface IAvatarInfoDbService
|
||||
{
|
||||
void DeleteAvatarInfoByUid(string uid);
|
||||
void DeleteAvatarInfoRangeByUid(string uid);
|
||||
|
||||
List<EnkaAvatarInfo> GetAvatarInfoInfoListByUid(string uid);
|
||||
|
||||
|
||||
@@ -93,4 +93,64 @@ internal sealed partial class CultivationDbService : ICultivationDbService
|
||||
appDbContext.CultivateItems.UpdateAndSave(item);
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<CultivateEntry?> GetCultivateEntryByProjectIdAndItemIdAsync(Guid projectId, uint itemId)
|
||||
{
|
||||
using (IServiceScope scope = serviceProvider.CreateScope())
|
||||
{
|
||||
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
return await appDbContext.CultivateEntries
|
||||
.SingleOrDefaultAsync(e => e.ProjectId == projectId && e.Id == itemId)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask InsertCultivateEntryAsync(CultivateEntry entry)
|
||||
{
|
||||
using (IServiceScope scope = serviceProvider.CreateScope())
|
||||
{
|
||||
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
await appDbContext.CultivateEntries.AddAndSaveAsync(entry).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask DeleteCultivateItemRangeByEntryIdAsync(Guid entryId)
|
||||
{
|
||||
using (IServiceScope scope = serviceProvider.CreateScope())
|
||||
{
|
||||
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
await appDbContext.CultivateItems
|
||||
.ExecuteDeleteWhereAsync(i => i.EntryId == entryId)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask InsertCultivateItemRangeAsync(IEnumerable<CultivateItem> toAdd)
|
||||
{
|
||||
using (IServiceScope scope = serviceProvider.CreateScope())
|
||||
{
|
||||
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
await appDbContext.CultivateItems.AddRangeAndSaveAsync(toAdd).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask AddCultivateProjectAsync(CultivateProject project)
|
||||
{
|
||||
using (IServiceScope scope = serviceProvider.CreateScope())
|
||||
{
|
||||
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
await appDbContext.CultivateProjects.AddAndSaveAsync(project).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask DeleteCultivateProjectByIdAsync(Guid projectId)
|
||||
{
|
||||
using (IServiceScope scope = serviceProvider.CreateScope())
|
||||
{
|
||||
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
await appDbContext.CultivateProjects
|
||||
.ExecuteDeleteWhereAsync(p => p.InnerId == projectId)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,44 +50,36 @@ internal sealed partial class CultivationService
|
||||
return ProjectAddResult.InvalidName;
|
||||
}
|
||||
|
||||
if (projects!.SingleOrDefault(a => a.Name == project.Name) != null)
|
||||
ArgumentNullException.ThrowIfNull(projects);
|
||||
|
||||
if (projects.Any(a => a.Name == project.Name))
|
||||
{
|
||||
return ProjectAddResult.AlreadyExists;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
// Sync cache
|
||||
await taskContext.SwitchToMainThreadAsync();
|
||||
projects!.Add(project);
|
||||
projects.Add(project);
|
||||
|
||||
// Sync database
|
||||
await taskContext.SwitchToBackgroundAsync();
|
||||
using (IServiceScope scope = serviceProvider.CreateScope())
|
||||
{
|
||||
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
await appDbContext.CultivateProjects.AddAndSaveAsync(project).ConfigureAwait(false);
|
||||
}
|
||||
await cultivationDbService.AddCultivateProjectAsync(project).ConfigureAwait(false);
|
||||
|
||||
return ProjectAddResult.Added;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task RemoveProjectAsync(CultivateProject project)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(projects);
|
||||
|
||||
// Sync cache
|
||||
// Keep this on main thread.
|
||||
await taskContext.SwitchToMainThreadAsync();
|
||||
projects!.Remove(project);
|
||||
projects.Remove(project);
|
||||
|
||||
// Sync database
|
||||
await taskContext.SwitchToBackgroundAsync();
|
||||
using (IServiceScope scope = serviceProvider.CreateScope())
|
||||
{
|
||||
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
await appDbContext.CultivateProjects
|
||||
.ExecuteDeleteWhereAsync(p => p.InnerId == project.InnerId)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
await cultivationDbService.DeleteCultivateProjectByIdAsync(project.InnerId).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,41 +167,27 @@ internal sealed partial class CultivationService : ICultivationService
|
||||
}
|
||||
|
||||
await taskContext.SwitchToBackgroundAsync();
|
||||
using (IServiceScope scope = serviceProvider.CreateScope())
|
||||
{
|
||||
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
|
||||
try
|
||||
{
|
||||
Current ??= appDbContext.CultivateProjects.SelectedOrDefault();
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
ThrowHelper.UserdataCorrupted(SH.ServiceCultivationProjectCurrentUserdataCourrpted2, ex);
|
||||
}
|
||||
|
||||
if (Current == null)
|
||||
if (Current is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Guid projectId = Current!.InnerId;
|
||||
CultivateEntry? entry = await appDbContext.CultivateEntries
|
||||
.SingleOrDefaultAsync(e => e.ProjectId == projectId && e.Id == itemId)
|
||||
CultivateEntry? entry = await cultivationDbService
|
||||
.GetCultivateEntryByProjectIdAndItemIdAsync(Current.InnerId, itemId)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (entry == null)
|
||||
{
|
||||
entry = CultivateEntry.From(projectId, type, itemId);
|
||||
await appDbContext.CultivateEntries.AddAndSaveAsync(entry).ConfigureAwait(false);
|
||||
entry = CultivateEntry.From(Current.InnerId, type, itemId);
|
||||
await cultivationDbService.InsertCultivateEntryAsync(entry).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
Guid entryId = entry.InnerId;
|
||||
await appDbContext.CultivateItems.ExecuteDeleteWhereAsync(i => i.EntryId == entryId).ConfigureAwait(false);
|
||||
await cultivationDbService.DeleteCultivateItemRangeByEntryIdAsync(entryId).ConfigureAwait(false);
|
||||
|
||||
IEnumerable<CultivateItem> toAdd = items.Select(item => CultivateItem.From(entryId, item));
|
||||
await appDbContext.CultivateItems.AddRangeAndSaveAsync(toAdd).ConfigureAwait(false);
|
||||
}
|
||||
await cultivationDbService.InsertCultivateItemRangeAsync(toAdd).ConfigureAwait(false);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -7,8 +7,14 @@ namespace Snap.Hutao.Service.Cultivation;
|
||||
|
||||
internal interface ICultivationDbService
|
||||
{
|
||||
ValueTask AddCultivateProjectAsync(CultivateProject project);
|
||||
|
||||
ValueTask DeleteCultivateEntryByIdAsync(Guid entryId);
|
||||
|
||||
ValueTask DeleteCultivateItemRangeByEntryIdAsync(Guid entryId);
|
||||
ValueTask DeleteCultivateProjectByIdAsync(Guid projectId);
|
||||
ValueTask<CultivateEntry?> GetCultivateEntryByProjectIdAndItemIdAsync(Guid projectId, uint itemId);
|
||||
|
||||
ValueTask<List<CultivateEntry>> GetCultivateEntryListByProjectIdAsync(Guid projectId);
|
||||
|
||||
ValueTask<List<CultivateItem>> GetCultivateItemListByEntryIdAsync(Guid entryId);
|
||||
@@ -17,6 +23,10 @@ internal interface ICultivationDbService
|
||||
|
||||
ValueTask<List<InventoryItem>> GetInventoryItemListByProjectIdAsync(Guid projectId);
|
||||
|
||||
ValueTask InsertCultivateEntryAsync(CultivateEntry entry);
|
||||
|
||||
ValueTask InsertCultivateItemRangeAsync(IEnumerable<CultivateItem> toAdd);
|
||||
|
||||
void UpdateCultivateItem(CultivateItem item);
|
||||
|
||||
void UpdateInventoryItem(InventoryItem item);
|
||||
|
||||
Reference in New Issue
Block a user