From 1485981d90ab8ff05417ee202f8cd587ca400633 Mon Sep 17 00:00:00 2001 From: Sam <315859133@qq.com> Date: Sun, 8 Feb 2026 21:29:14 +0800 Subject: [PATCH] feat: add points rule repository implementations --- .../ApplicationDbContext.cs | 2 + .../PointsRuleConditionRepository.cs | 32 ++++++++ .../Repositories/PointsRuleRepository.cs | 77 +++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 src/Fengling.Member.Infrastructure/Repositories/PointsRuleConditionRepository.cs create mode 100644 src/Fengling.Member.Infrastructure/Repositories/PointsRuleRepository.cs diff --git a/src/Fengling.Member.Infrastructure/ApplicationDbContext.cs b/src/Fengling.Member.Infrastructure/ApplicationDbContext.cs index 2835f5d..bcc8be2 100644 --- a/src/Fengling.Member.Infrastructure/ApplicationDbContext.cs +++ b/src/Fengling.Member.Infrastructure/ApplicationDbContext.cs @@ -12,6 +12,8 @@ public partial class ApplicationDbContext(DbContextOptions public DbSet MemberTags => Set(); public DbSet WechatAuthorizations => Set(); public DbSet PointsAccounts => Set(); + public DbSet PointsRules => Set(); + public DbSet PointsRuleConditions => Set(); protected override void OnModelCreating(ModelBuilder modelBuilder) { diff --git a/src/Fengling.Member.Infrastructure/Repositories/PointsRuleConditionRepository.cs b/src/Fengling.Member.Infrastructure/Repositories/PointsRuleConditionRepository.cs new file mode 100644 index 0000000..3068428 --- /dev/null +++ b/src/Fengling.Member.Infrastructure/Repositories/PointsRuleConditionRepository.cs @@ -0,0 +1,32 @@ +using Fengling.Member.Domain.Aggregates.PointsRuleModel; +using Fengling.Member.Domain.Repositories; + +namespace Fengling.Member.Infrastructure.Repositories; + +public class PointsRuleConditionRepository : IPointsRuleConditionRepository +{ + private readonly ApplicationDbContext _context; + + public PointsRuleConditionRepository(ApplicationDbContext context) + { + _context = context; + } + + public async Task> GetByRuleIdAsync(PointsRuleId ruleId) + { + return await _context.PointsRuleConditions + .Where(x => x.RuleId == ruleId) + .ToListAsync(); + } + + public async Task AddAsync(PointsRuleCondition condition) + { + await _context.PointsRuleConditions.AddAsync(condition); + } + + public async Task DeleteByRuleIdAsync(PointsRuleId ruleId) + { + var conditions = await GetByRuleIdAsync(ruleId); + _context.PointsRuleConditions.RemoveRange(conditions); + } +} diff --git a/src/Fengling.Member.Infrastructure/Repositories/PointsRuleRepository.cs b/src/Fengling.Member.Infrastructure/Repositories/PointsRuleRepository.cs new file mode 100644 index 0000000..9d6424c --- /dev/null +++ b/src/Fengling.Member.Infrastructure/Repositories/PointsRuleRepository.cs @@ -0,0 +1,77 @@ +using Fengling.Member.Domain.Aggregates.PointsRuleModel; +using Fengling.Member.Domain.Aggregates.PointsRuleModel.Enums; +using Fengling.Member.Domain.Repositories; +using Microsoft.EntityFrameworkCore; + +namespace Fengling.Member.Infrastructure.Repositories; + +public class PointsRuleRepository : IPointsRuleRepository +{ + private readonly ApplicationDbContext _context; + + public PointsRuleRepository(ApplicationDbContext context) + { + _context = context; + } + + public async Task GetByIdAsync(PointsRuleId id) + { + return await _context.PointsRules + .Include(x => x.Conditions) + .FirstOrDefaultAsync(x => x.Id == id); + } + + public async Task GetByCodeAsync(string code) + { + return await _context.PointsRules + .Include(x => x.Conditions) + .FirstOrDefaultAsync(x => x.Code == code); + } + + public async Task> GetActiveRulesAsync() + { + var now = DateTime.UtcNow; + return await _context.PointsRules + .Include(x => x.Conditions) + .Where(x => x.IsActive + && x.EffectiveFrom <= now + && (x.EffectiveTo == null || x.EffectiveTo > now)) + .OrderByDescending(x => x.Priority) + .ToListAsync(); + } + + public async Task> GetActiveRulesByDimensionAsync( + DimensionType dimensionType, + string dimensionValue) + { + var now = DateTime.UtcNow; + return await _context.PointsRules + .Include(x => x.Conditions) + .Where(x => x.IsActive + && x.EffectiveFrom <= now + && (x.EffectiveTo == null || x.EffectiveTo > now) + && x.Conditions.Any(c => c.DimensionType == dimensionType)) + .OrderByDescending(x => x.Priority) + .ToListAsync(); + } + + public async Task AddAsync(PointsRule rule) + { + await _context.PointsRules.AddAsync(rule); + } + + public async Task UpdateAsync(PointsRule rule) + { + _context.PointsRules.Update(rule); + await Task.CompletedTask; + } + + public async Task DeleteAsync(PointsRuleId id) + { + var rule = await GetByIdAsync(id); + if (rule != null) + { + _context.PointsRules.Remove(rule); + } + } +}