D6 - sell records

This commit is contained in:
Michał Zieliński
2025-07-07 16:54:29 +02:00
parent 45c37d8bd3
commit bf29367b0c
2 changed files with 105 additions and 1 deletions

View File

@@ -22,6 +22,7 @@ public class MorskaD6Processor : MorskaBaseProcessor
private int Year { get; set; } private int Year { get; set; }
private string? CostSource { get; set; } private string? CostSource { get; set; }
private string? SellSource { get; set; } private string? SellSource { get; set; }
private List<Record> Mappings = new List<Record>();
public MorskaD6Processor( public MorskaD6Processor(
AppDbContext db, AppDbContext db,
@@ -87,6 +88,12 @@ public class MorskaD6Processor : MorskaBaseProcessor
throw new InvalidOperationException("SourceLayerCosts record not found"); throw new InvalidOperationException("SourceLayerCosts record not found");
} }
Mappings = processWorker.Records.Where(x => x.Code!.StartsWith("Sell-Code-")).ToList();
if (Mappings.Count == 0)
{
throw new InvalidOperationException("No Sell-Code- records found");
}
_logger.LogDebug( _logger.LogDebug(
"{ProcessorType}: Configuration loaded - Year: {Year}, SourceCost: {CostSource}, SourceSell: {SellSource}", "{ProcessorType}: Configuration loaded - Year: {Year}, SourceCost: {CostSource}, SourceSell: {SellSource}",
ProcessorType, Year, CostSource, SellSource); ProcessorType, Year, CostSource, SellSource);
@@ -222,6 +229,8 @@ public class MorskaD6Processor : MorskaBaseProcessor
ProcessorType); ProcessorType);
} }
// COSTS
var firstDataSource = dataSources.First(); var firstDataSource = dataSources.First();
if (firstDataSource.Records == null || !firstDataSource.Records.Any()) if (firstDataSource.Records == null || !firstDataSource.Records.Any())
@@ -284,6 +293,101 @@ public class MorskaD6Processor : MorskaBaseProcessor
groupedRecord.Type, groupedRecord.Month); groupedRecord.Type, groupedRecord.Month);
} }
// SELLS
var secondDataSource = dataSources.Last();
if (secondDataSource.Records == null || !secondDataSource.Records.Any())
{
_logger.LogWarning("{ProcessorType}: Second data source has no records to process", ProcessorType);
return newRecords;
}
// Create mapping dictionary from Mappings
var mappingDictionary = new Dictionary<string, string>();
foreach (var mapping in Mappings)
{
if (mapping.Code != null && mapping.Desc1 != null)
{
// Extract code from "Sell-Code-XXXX" -> "XXXX"
var sourceCode = mapping.Code.Replace("Sell-Code-", "");
var targetCode = mapping.Desc1;
mappingDictionary[sourceCode] = targetCode;
_logger.LogDebug("{ProcessorType}: Loaded mapping {SourceCode} -> {TargetCode}",
ProcessorType, sourceCode, targetCode);
}
}
// Dictionary to collect results (key: target code with month, value: sum)
var sellResults = new Dictionary<string, double>();
// Process records from secondDataSource
foreach (var record in secondDataSource.Records)
{
if (string.IsNullOrEmpty(record.Code) || record.Code.Length < 6 || !record.Value1.HasValue)
{
continue;
}
// Extract month from last two digits
var monthStr = record.Code.Substring(record.Code.Length - 2);
if (!int.TryParse(monthStr, out var month) || month < 1 || month > 13)
{
_logger.LogDebug("{ProcessorType}: Invalid month in code {Code}", ProcessorType, record.Code);
continue;
}
// Extract base code (without month)
var baseCode = record.Code.Substring(0, record.Code.Length - 2);
// Check if we have mapping for this code
if (mappingDictionary.TryGetValue(baseCode, out var targetCode))
{
// Create target code with month
var targetCodeWithMonth = $"{targetCode}{monthStr}";
// Add/sum value
if (sellResults.ContainsKey(targetCodeWithMonth))
{
sellResults[targetCodeWithMonth] += record.Value1.Value;
_logger.LogDebug("{ProcessorType}: Added to existing record {TargetCode}: {Value} (total: {Total})",
ProcessorType, targetCodeWithMonth, record.Value1.Value, sellResults[targetCodeWithMonth]);
}
else
{
sellResults[targetCodeWithMonth] = record.Value1.Value;
_logger.LogDebug("{ProcessorType}: Created new record {TargetCode}: {Value}",
ProcessorType, targetCodeWithMonth, record.Value1.Value);
}
}
else
{
_logger.LogDebug("{ProcessorType}: No mapping found for code {BaseCode}", ProcessorType, baseCode);
}
}
// Add results to newRecords
foreach (var sellResult in sellResults)
{
var newRecord = new Record
{
Id = Guid.NewGuid(),
Code = sellResult.Key,
CreatedAt = DateTime.UtcNow,
ModifiedAt = DateTime.UtcNow,
Value1 = sellResult.Value
};
newRecords.Add(newRecord);
_logger.LogDebug("{ProcessorType}: Added sell record {Code} with value {Value}",
ProcessorType, newRecord.Code, newRecord.Value1);
}
_logger.LogInformation("{ProcessorType}: Processed {SellRecordCount} sell records from {OriginalSellCount} source records",
ProcessorType, sellResults.Count, secondDataSource.Records.Count);
_logger.LogInformation( _logger.LogInformation(
"{ProcessorType}: Processed {GroupCount} unique grouped records from {OriginalCount} original records", "{ProcessorType}: Processed {GroupCount} unique grouped records from {OriginalCount} original records",
ProcessorType, newRecords.Count, firstDataSource.Records.Count); ProcessorType, newRecords.Count, firstDataSource.Records.Count);

View File

@@ -1,5 +1,5 @@
INSERT INTO [diunabi-morska].[dbo].[Records] INSERT INTO [diunabi-morska].[dbo].[Records]
([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId]) ([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId])
VALUES ((SELECT NEWID()), 'SourceLayerSell', 'L4187-P-2025-R1-T1', VALUES ((SELECT NEWID()), 'Sell-Code-9091', '1208/',
GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0,
'd1e6e8d2-a13b-421f-84cb-a9ccd5b1c6f5'); 'd1e6e8d2-a13b-421f-84cb-a9ccd5b1c6f5');