track/src/SJZY.Track.Application/Helpers/ForewarningFactorHelper.cs
2024-07-24 08:54:56 +08:00

58 lines
2.2 KiB
C#

using JetBrains.Annotations;
using SJZY.Track.Domain.Aggregates.ForewarningAggregate;
namespace SJZY.Track.Application.Helpers;
public class ForewarningFactorHelper
{
public static RuleCollectEntity[] GetFactorItem([NotNull]RuleFactorEntity ruleFactors)
{
var total = Factorial(ruleFactors);
var ruleCollect = AllocateFactor(total,ruleFactors.RuleId);
Assignment(ruleCollect,total,ruleFactors.CargoTypes,(item,value)=>item.CargoType=value);
Assignment(ruleCollect,total,ruleFactors.Months,(item,value)=>item.Month=value);
Assignment(ruleCollect,total,ruleFactors.Areas,(item,value)=>item.Area=value);
Assignment(ruleCollect,total,ruleFactors.IsTransit,(item,value)=>item.IsTransit=value);
Assignment(ruleCollect,total,ruleFactors.DeparturePorts,(item,value)=>item.DeparturePort=value);
Assignment(ruleCollect,total,ruleFactors.ProductCodes,(item,value)=>item.ProductCode=value);
return ruleCollect.ToArray();
}
private static void Assignment<T>(Span<RuleCollectEntity> ruleCollect,int total,IReadOnlyList<T> values,Action<RuleCollectEntity,T> action)
{
if (values.Count == 0)
{
return;
}
var segment = total / values.Count;
for(int index=0;index<values.Count;index++)
{
var start = index * segment;
var assignmentSlice = ruleCollect.Slice(start, segment);
foreach (var item in assignmentSlice)
{
action(item,values[index]);
}
}
}
private static Span<RuleCollectEntity> AllocateFactor(int total,int ruleId)
{
Span<RuleCollectEntity> factors = new RuleCollectEntity[total];
for (int index = 0; index < total; index++)
{
factors[index] = new RuleCollectEntity()
{
RuleId = ruleId
};
}
return factors;
}
internal static int Factorial(RuleFactorEntity ruleFactors)
{
return ruleFactors.CargoTypes.Count*ruleFactors.Months.Count*ruleFactors.Areas.Count*ruleFactors.IsTransit.Count*ruleFactors.DeparturePorts.Count*ruleFactors.ProductCodes.Count;
}
}