mirror of
https://github.com/Sea-Haven-Industries/proposal-system.git
synced 2026-10-01 14:03:13 +00:00
40 lines
1.1 KiB
C#
40 lines
1.1 KiB
C#
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
using ProposalSystem.Application.Interfaces;
|
||
|
|
using ProposalSystem.Infrastructure.Data;
|
||
|
|
|
||
|
|
namespace ProposalSystem.Infrastructure.Services;
|
||
|
|
|
||
|
|
public class ProposalNumberGenerator : IProposalNumberGenerator
|
||
|
|
{
|
||
|
|
private readonly ProposalDbContext _db;
|
||
|
|
|
||
|
|
public ProposalNumberGenerator(ProposalDbContext db)
|
||
|
|
{
|
||
|
|
_db = db;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<string> GenerateAsync(CancellationToken ct = default)
|
||
|
|
{
|
||
|
|
var year = DateTime.UtcNow.Year;
|
||
|
|
var prefix = $"SHI-{year}-";
|
||
|
|
|
||
|
|
var lastNumber = await _db.Proposals
|
||
|
|
.Where(p => p.ProposalNumber.StartsWith(prefix))
|
||
|
|
.OrderByDescending(p => p.ProposalNumber)
|
||
|
|
.Select(p => p.ProposalNumber)
|
||
|
|
.FirstOrDefaultAsync(ct);
|
||
|
|
|
||
|
|
int nextSequence = 1;
|
||
|
|
if (lastNumber != null)
|
||
|
|
{
|
||
|
|
var sequencePart = lastNumber[prefix.Length..];
|
||
|
|
if (int.TryParse(sequencePart, out var current))
|
||
|
|
{
|
||
|
|
nextSequence = current + 1;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return $"{prefix}{nextSequence:D4}";
|
||
|
|
}
|
||
|
|
}
|