feat: 支持自动下架商品

This commit is contained in:
chenchun
2024-11-14 18:56:10 +08:00
parent 8e805a4cf8
commit 02d45bb6a7
3 changed files with 85 additions and 15 deletions

View File

@@ -0,0 +1,32 @@
using Microsoft.Extensions.Logging;
using Quartz;
using Volo.Abp.BackgroundWorkers.Quartz;
using Yi.Framework.DigitalCollectibles.Domain.Managers;
namespace Yi.Framework.DigitalCollectibles.Application.Jobs;
/// <summary>
/// 自动下架商品
/// </summary>
public class AutoPassInGoodsJob: QuartzBackgroundWorkerBase
{
private readonly MarketManager _marketManager;
private readonly ILogger<AutoPassInGoodsJob> _logger;
public AutoPassInGoodsJob(MarketManager marketManager, ILogger<AutoPassInGoodsJob> logger)
{
_marketManager = marketManager;
_logger = logger;
JobDetail = JobBuilder.Create<AutoPassInGoodsJob>().WithIdentity(nameof(AutoPassInGoodsJob))
.Build();
//每小时,第10分钟执行一次
Trigger = TriggerBuilder.Create().WithIdentity(nameof(AutoPassInGoodsJob))
.WithCronSchedule("0 10 * * * ?")
.Build();
}
public override async Task Execute(IJobExecutionContext context)
{
await _marketManager.AutoPassInGoodsAsync();
}
}

View File

@@ -28,7 +28,10 @@ namespace Yi.Abp.Domain.Shared.Settings
new SettingDefinition("MarketTaxRate", "0.02"),
//矿池刷新内容
new SettingDefinition("PoolData", "60,26,10,3,1")
new SettingDefinition("PoolData", "60,26,10,3,1"),
//自动下架多久天前的商品
new SettingDefinition("AutoPassInGoodsDay","3")
);
}
}

View File

@@ -19,9 +19,11 @@ public class MarketManager : DomainService
private readonly ISqlSugarRepository<MarketGoodsAggregateRoot> _marketGoodsRepository;
private readonly ILocalEventBus _localEventBus;
public readonly ISettingProvider _settingProvider;
private readonly ISettingProvider _settingProvider;
public MarketManager(ISqlSugarRepository<CollectiblesUserStoreAggregateRoot> collectiblesUserStoreRepository,
ISqlSugarRepository<MarketGoodsAggregateRoot> marketGoodsRepository, ILocalEventBus localEventBus, ISettingProvider settingProvider)
ISqlSugarRepository<MarketGoodsAggregateRoot> marketGoodsRepository, ILocalEventBus localEventBus,
ISettingProvider settingProvider)
{
_collectiblesUserStoreRepository = collectiblesUserStoreRepository;
_marketGoodsRepository = marketGoodsRepository;
@@ -29,6 +31,35 @@ public class MarketManager : DomainService
_settingProvider = settingProvider;
}
/// <summary>
/// 商品自动流拍,自动下架
/// </summary>
public async Task AutoPassInGoodsAsync()
{
var autoPassInGoodsDay = (await _settingProvider.GetOrNullAsync("AutoPassInGoodsDay")).To<int>();
//下架3天前的商品
var endTine = DateTime.Now.AddDays(-autoPassInGoodsDay);
//获取所有需要下架商品
var marketGoods = await _marketGoodsRepository._DbQueryable.Where(x => x.CreationTime >= endTine).ToListAsync();
//单独处理每一个下架的商品
foreach (var goods in marketGoods)
{
//用户库存
var userStore = await _collectiblesUserStoreRepository._DbQueryable
.Where(x => x.CollectiblesId == goods.CollectiblesId)
.Where(x => x.IsAtMarketing == true).FirstAsync();
if (userStore is not null)
{
userStore.IsAtMarketing = false;
await _collectiblesUserStoreRepository.UpdateAsync(userStore);
//删除商品
await _marketGoodsRepository.DeleteByIdAsync(goods.Id);
}
}
}
/// <summary>
/// 上架货物
/// </summary>
@@ -39,13 +70,13 @@ public class MarketManager : DomainService
/// <returns></returns>
public async Task ShelvedGoodsAsync(Guid userId, Guid collectiblesId, int number, decimal money)
{
if (number<=0)
if (number <= 0)
{
throw new UserFriendlyException("上架藏品数量需大于0");
}
var collectiblesList = await _collectiblesUserStoreRepository._DbQueryable
.Where(x=>x.UserId==userId)
.Where(x => x.UserId == userId)
.Where(x => x.IsAtMarketing == false)
.Where(x => x.CollectiblesId == collectiblesId).ToListAsync();
if (collectiblesList.Count < number)
@@ -80,10 +111,11 @@ public class MarketManager : DomainService
/// <returns></returns>
public async Task PurchaseGoodsAsync(Guid userId, Guid marketGoodsId, int number)
{
if (number<=0)
if (number <= 0)
{
throw new UserFriendlyException("交易藏品数量需大于0");
}
//1-市场扣减或者关闭该商品
//2-出售者新增钱,购买者扣钱
//3-出售者删除对应库存,购买者新增对应库存
@@ -103,17 +135,19 @@ public class MarketManager : DomainService
{
throw new UserFriendlyException($"交易失败,当前交易市场库存不足");
}
//2-出售者新增钱,购买者扣钱
//发布一个其他领域的事件-购买者扣钱
await _localEventBus.PublishAsync(new MoneyChangeEventArgs() { UserId = userId, Number = -(number*marketGoods.UnitPrice) },false);
await _localEventBus.PublishAsync(
new MoneyChangeEventArgs() { UserId = userId, Number = -(number * marketGoods.UnitPrice) }, false);
//发布一个其他领域的事件-出售者加钱,同时扣税
var marketTaxRate = decimal.Parse(await _settingProvider.GetOrNullAsync("MarketTaxRate"));
//价格*扣减税
var realTotalPrice = (number*marketGoods.UnitPrice) * (1 - marketTaxRate);
var realTotalPrice = (number * marketGoods.UnitPrice) * (1 - marketTaxRate);
//出售者实际加钱
await _localEventBus.PublishAsync(new MoneyChangeEventArgs() { UserId = marketGoods.SellUserId, Number = realTotalPrice },false);
await _localEventBus.PublishAsync(
new MoneyChangeEventArgs() { UserId = marketGoods.SellUserId, Number = realTotalPrice }, false);
//3-出售者删除对应库存,购买者新增对应库存(只需更改用户者即可)
var collectiblesList = await _collectiblesUserStoreRepository._DbQueryable.Where(x => x.IsAtMarketing == true)
.Where(x => x.UserId == marketGoods.SellUserId)
@@ -129,8 +163,9 @@ public class MarketManager : DomainService
{
userStore.PurchaseMarket(userId);
}
await _collectiblesUserStoreRepository.UpdateRangeAsync(updateStore);
//发布一个成功交易事件
await _localEventBus.PublishAsync(new SuccessMarketEto
{
@@ -139,7 +174,7 @@ public class MarketManager : DomainService
CollectiblesId = marketGoods.CollectiblesId,
SellNumber = marketGoods.SellNumber,
UnitPrice = marketGoods.UnitPrice,
RealTotalPrice =realTotalPrice
},false);
RealTotalPrice = realTotalPrice
}, false);
}
}