using System;
using System.Collections.Generic;
using System.Linq;
using NPP.SmartSchedue.Api.Contracts.Services.Integration.Models;
namespace NPP.SmartSchedue.Api.Contracts.Services.Integration.Output
{
///
/// 人员分配生成结果
/// 智能分配算法第四层:结果生成阶段的核心数据模型
/// 包含成功分配、失败分配、质量评估、性能指标等完整业务信息
///
public class AllocationGenerationResult
{
#region 核心分配结果
///
/// 成功分配的任务与人员映射
/// Key: 工作任务ID, Value: 最佳匹配的人员候选人
///
public Dictionary SuccessfulAllocations { get; set; } = new();
///
/// 分配失败的任务列表
/// 包含失败原因、类型和建议解决方案
///
public List FailedAllocations { get; set; } = new();
#endregion
#region 质量评估指标
///
/// 整体质量评分 (0-100)
/// 综合考虑成功率、平均匹配分数、约束符合度等因素
///
public double OverallQualityScore { get; set; }
///
/// 分配成功率 (0-100)
/// 计算公式:成功分配数量 / 总任务数量 * 100
///
public double SuccessRate => CalculateSuccessRate();
///
/// 平均匹配质量分数 (0-100)
/// 所有成功分配的人员候选人平均评分
///
public double AverageMatchQuality => CalculateAverageMatchQuality();
#endregion
#region 详细分析报告
///
/// 详细分配报告
/// 包含统计信息、人员利用率、风险分析等详细数据
///
public AllocationReport DetailedReport { get; set; } = new();
///
/// 分配策略执行摘要
/// 人性化的分配结果描述,便于业务人员理解
///
public string AllocationSummary { get; set; } = string.Empty;
#endregion
#region 性能监控指标
///
/// 性能监控指标
/// 记录各个处理阶段的执行时间、资源消耗等性能数据
///
public Dictionary PerformanceMetrics { get; set; } = new();
///
/// 算法执行总耗时(毫秒)
///
public long TotalExecutionTimeMs => GetExecutionTime();
///
/// 内存使用峰值(字节)
///
public long PeakMemoryUsageBytes { get; set; }
#endregion
#region 业务洞察分析
///
/// 人员工作负载分布
/// 分析各人员的任务分配数量和工作强度
///
public Dictionary PersonnelWorkloadDistribution => CalculateWorkloadDistribution();
///
/// 技能匹配度分析
/// 评估整体技能匹配情况和资质覆盖度
///
public SkillMatchAnalysis SkillMatchingAnalysis => AnalyzeSkillMatching();
///
/// 约束冲突统计
/// 统计各类约束冲突的分布情况
///
public Dictionary ConstraintConflictStatistics => AnalyzeConstraintConflicts();
#endregion
#region 辅助计算方法
///
/// 计算分配成功率
///
private double CalculateSuccessRate()
{
var totalTasks = SuccessfulAllocations.Count + FailedAllocations.Count;
return totalTasks > 0 ? (double)SuccessfulAllocations.Count / totalTasks * 100 : 0;
}
///
/// 计算平均匹配质量
///
private double CalculateAverageMatchQuality()
{
return SuccessfulAllocations.Any()
? SuccessfulAllocations.Values.Average(c => c.TotalScore)
: 0;
}
///
/// 获取执行时间
///
private long GetExecutionTime()
{
return PerformanceMetrics.TryGetValue("TotalExecutionTime", out var time)
? Convert.ToInt64(time)
: 0;
}
///
/// 计算人员工作负载分布
///
private Dictionary CalculateWorkloadDistribution()
{
return SuccessfulAllocations.Values
.GroupBy(c => c.PersonnelId)
.ToDictionary(
g => g.Key,
g => new PersonnelWorkloadInfo
{
PersonnelId = g.Key,
PersonnelName = g.First().PersonnelName,
AssignedTaskCount = g.Count(),
AverageMatchScore = g.Average(c => c.TotalScore),
TotalWorkload = g.Sum(c => c.EstimatedWorkload ?? 1.0)
});
}
///
/// 分析技能匹配情况
///
private SkillMatchAnalysis AnalyzeSkillMatching()
{
var analysis = new SkillMatchAnalysis();
if (SuccessfulAllocations.Any())
{
analysis.AverageSkillMatchScore = SuccessfulAllocations.Values
.Average(c => c.QualificationScore?.MatchScore ?? 0);
analysis.FullyQualifiedCount = SuccessfulAllocations.Values
.Count(c => (c.QualificationScore?.MatchScore ?? 0) >= 90);
analysis.PartiallyQualifiedCount = SuccessfulAllocations.Values
.Count(c => (c.QualificationScore?.MatchScore ?? 0) >= 70 &&
(c.QualificationScore?.MatchScore ?? 0) < 90);
analysis.MinimallyQualifiedCount = SuccessfulAllocations.Values
.Count(c => (c.QualificationScore?.MatchScore ?? 0) < 70);
}
return analysis;
}
///
/// 分析约束冲突分布
///
private Dictionary AnalyzeConstraintConflicts()
{
var conflicts = new Dictionary();
foreach (var failure in FailedAllocations)
{
var conflictType = failure.FailureType.ToString();
conflicts[conflictType] = conflicts.GetValueOrDefault(conflictType, 0) + 1;
}
return conflicts;
}
#endregion
#region 验证和健康检查
///
/// 验证结果数据完整性
///
public ValidationResult ValidateResultIntegrity()
{
var result = new ValidationResult { IsValid = true, Issues = new List() };
// 检查基础数据完整性
if (SuccessfulAllocations == null)
{
result.IsValid = false;
result.Issues.Add("成功分配数据为空");
}
if (FailedAllocations == null)
{
result.IsValid = false;
result.Issues.Add("失败分配数据为空");
}
// 检查质量评分合理性
if (OverallQualityScore < 0 || OverallQualityScore > 100)
{
result.IsValid = false;
result.Issues.Add($"整体质量评分超出范围: {OverallQualityScore}");
}
// 检查性能指标
if (PerformanceMetrics == null || !PerformanceMetrics.Any())
{
result.Issues.Add("缺少性能监控数据");
}
return result;
}
///
/// 获取结果健康状态
///
public HealthStatus GetHealthStatus()
{
var successRate = SuccessRate;
var avgQuality = AverageMatchQuality;
if (successRate >= 90 && avgQuality >= 80)
return HealthStatus.Excellent;
else if (successRate >= 70 && avgQuality >= 70)
return HealthStatus.Good;
else if (successRate >= 50 && avgQuality >= 60)
return HealthStatus.Fair;
else
return HealthStatus.Poor;
}
#endregion
}
#region 支持类定义
///
/// 人员候选人信息
/// 包含完整的评分体系和匹配详情
///
public class PersonnelCandidate
{
///
/// 人员ID
///
public long PersonnelId { get; set; }
///
/// 人员姓名
///
public string PersonnelName { get; set; } = string.Empty;
///
/// 资质评分详情
///
public QualificationScore QualificationScore { get; set; } = new();
///
/// 约束条件评分
///
public ConstraintScore ConstraintScore { get; set; } = new();
///
/// 优化决策评分
///
public OptimizationScore OptimizationScore { get; set; }
///
/// 综合总分 (0-100)
///
public double TotalScore { get; set; }
///
/// 是否为最优选择
///
public bool IsOptimalChoice { get; set; }
///
/// 候选人排名
///
public int Rank { get; set; }
///
/// 分配理由说明
///
public string AllocationReason { get; set; } = string.Empty;
///
/// 预估工作负载
///
public double? EstimatedWorkload { get; set; }
///
/// 风险评估等级
///
public RiskLevel RiskLevel { get; set; } = RiskLevel.Low;
///
/// 决策评分
///
public DecisionScore DecisionScore { get; set; } = new();
public RecommendationLevel RecommendationLevel { get; set; } = RecommendationLevel.NotRecommended;
}
///
/// 资质评分详情
///
public class QualificationScore
{
///
/// 资质匹配分数 (0-100)
///
public double MatchScore { get; set; }
///
/// 是否资质有效
///
public bool IsQualificationValid { get; set; }
///
/// 匹配的资质列表
///
public List MatchedQualifications { get; set; } = new();
///
/// 缺失的资质列表
///
public List MissingQualifications { get; set; } = new();
///
/// 资质详细信息
///
public string QualificationDetails { get; set; } = string.Empty;
}
///
/// 约束条件评分
/// 用于记录人员候选人在约束评估阶段的各项评分和详细信息
/// 包含时间可用性、工作限制、班次规则等多维度约束检查结果
///
public class ConstraintScore
{
#region 核心评分指标
///
/// 约束符合度分数 (0-100)
/// 综合各项约束检查的加权平均分数
///
public double ComplianceScore { get; set; }
///
/// 时间可用性分数 (0-100)
/// 基于人员在工作时间段的可用性评估
///
public double TimeAvailabilityScore { get; set; }
///
/// 工作限制符合度分数 (0-100)
/// 基于人员工作限制规则的符合程度评估
///
public double WorkLimitComplianceScore { get; set; }
///
/// 班次规则符合度分数 (0-100)
/// 基于班次规则和排班政策的符合程度评估
///
public double ShiftRuleComplianceScore { get; set; }
#endregion
#region 约束检查结果
///
/// 约束违规列表
/// 记录所有违反的约束规则和具体违规内容
///
public List Violations { get; set; }
///
/// 通过约束检查列表
/// 记录所有通过检查的约束规则
///
public List PassedConstraints { get; set; } = new();
///
/// 约束评估详情
/// 包含详细的约束检查过程和结果说明
///
public string ConstraintDetails { get; set; } = string.Empty;
#endregion
#region 扩展评分指标
///
/// 地理位置约束分数 (0-100)
/// 基于人员与工作地点的距离和可达性评估
///
public double LocationConstraintScore { get; set; }
///
/// 设备可用性分数 (0-100)
/// 基于人员所需设备的可用性评估
///
public double EquipmentAvailabilityScore { get; set; }
///
/// 项目授权分数 (0-100)
/// 基于人员对特定项目的授权和访问权限评估
///
public double ProjectAuthorizationScore { get; set; }
///
/// 技能匹配约束分数 (0-100)
/// 基于人员技能与任务要求的匹配程度评估
///
public double SkillMatchConstraintScore { get; set; }
#endregion
#region 风险评估
///
/// 约束风险等级
/// 基于约束符合度评估的风险等级
///
public RiskLevel RiskLevel => ComplianceScore switch
{
>= 90 => RiskLevel.Low,
>= 80 => RiskLevel.Low,
>= 70 => RiskLevel.Medium,
>= 60 => RiskLevel.Medium,
_ => RiskLevel.High
};
///
/// 约束风险描述
/// 详细描述约束风险的具体内容和影响
///
public string RiskDescription { get; set; } = string.Empty;
///
/// 风险缓解建议
/// 针对识别出的约束风险提供的缓解措施建议
///
public List RiskMitigationSuggestions { get; set; } = new();
#endregion
#region 性能指标
///
/// 约束检查执行时间(毫秒)
/// 记录约束评估过程的执行耗时
///
public long ExecutionTimeMs { get; set; }
///
/// 约束检查项目数量
/// 记录本次约束评估检查的约束项目总数
///
public int TotalConstraintItems { get; set; }
///
/// 约束检查通过率
/// 计算公式:通过项目数 / 总项目数 * 100
///
public double PassRate => TotalConstraintItems > 0 ? (double)PassedConstraints.Count / TotalConstraintItems * 100 : 0;
#endregion
#region 构造函数
public ConstraintScore()
{
Violations = new List();
PassedConstraints = new List();
RiskMitigationSuggestions = new List();
}
#endregion
#region 辅助方法
///
/// 检查是否通过所有关键约束
///
/// 是否通过所有关键约束检查
public bool IsFullyCompliant => ComplianceScore >= 80 && Violations.Count == 0;
///
/// 检查是否满足最低约束要求
///
/// 是否满足最低约束要求
public bool MeetsMinimumRequirements => ComplianceScore >= 60;
///
/// 获取约束评估摘要
///
/// 约束评估的简要总结
public string GetConstraintSummary()
{
var summary = $"约束符合度:{ComplianceScore:F1}分,";
summary += $"通过检查:{PassedConstraints.Count}项,";
summary += $"违规项目:{Violations.Count}项,";
summary += $"风险等级:{RiskLevel}";
return summary;
}
///
/// 添加通过约束检查信息
///
/// 通过的约束描述
public void AddPassedConstraint(string constraint)
{
if (!string.IsNullOrEmpty(constraint) && !PassedConstraints.Contains(constraint))
{
PassedConstraints.Add(constraint);
}
}
///
/// 计算综合约束评分
///
/// 各维度权重配置
/// 加权综合评分
public double CalculateWeightedScore(Dictionary weights)
{
if (weights == null || !weights.Any())
{
// 使用默认权重
weights = new Dictionary
{
{ "TimeAvailability", 0.3 },
{ "WorkLimit", 0.25 },
{ "ShiftRule", 0.25 },
{ "Location", 0.1 },
{ "Equipment", 0.1 }
};
}
var weightedScore = 0.0;
var totalWeight = 0.0;
foreach (var kvp in weights)
{
var score = kvp.Key switch
{
"TimeAvailability" => TimeAvailabilityScore,
"WorkLimit" => WorkLimitComplianceScore,
"ShiftRule" => ShiftRuleComplianceScore,
"Location" => LocationConstraintScore,
"Equipment" => EquipmentAvailabilityScore,
_ => 0.0
};
weightedScore += score * kvp.Value;
totalWeight += kvp.Value;
}
return totalWeight > 0 ? weightedScore / totalWeight : 0;
}
#endregion
}
///
/// 优化决策评分
///
public class OptimizationScore
{
///
/// 指定人员匹配分数
///
public double AssignedPersonnelScore { get; set; }
///
/// 项目FL人员优先分数
///
public double ProjectFLScore { get; set; }
///
/// 工作负载平衡分数
///
public double WorkloadBalanceScore { get; set; }
///
/// 班次连续性分数
///
public double ShiftContinuityScore { get; set; }
///
/// 技能匹配优化分数
///
public double SkillOptimizationScore { get; set; }
///
/// 协作历史分数
///
public double CollaborationHistoryScore { get; set; }
///
/// 优化总分 (0-100)
///
public double TotalOptimizationScore { get; set; }
public double SkillMatchScore { get; set; }
public double EfficiencyScore { get; set; }
public double optimizationScore { get; set; }
public string OptimizationDetails { get; set; } = string.Empty;
}
///
/// 分配失败信息
///
public class FailedAllocation
{
///
/// 工作任务ID
///
public long WorkOrderId { get; set; }
///
/// 任务编码
///
public string WorkOrderCode { get; set; } = string.Empty;
///
/// 失败原因描述
///
public string FailureReason { get; set; } = string.Empty;
///
/// 失败类型
///
public AllocationFailureType FailureType { get; set; }
///
/// 建议解决方案
///
public List SuggestedSolutions { get; set; } = new();
///
/// 详细错误信息
///
public List DetailedErrors { get; set; } = new();
///
/// 冲突的人员列表
///
public List ConflictingPersonnels { get; set; } = new();
}
///
/// 分配失败类型枚举
///
public enum AllocationFailureType
{
///
/// 无可用人员
///
NoAvailablePersonnel = 1,
///
/// 资质不匹配
///
QualificationMismatch = 2,
///
/// 时间冲突
///
TimeConflict = 3,
///
/// 工作量超限
///
WorkloadExceeded = 4,
///
/// 班次规则冲突
///
ShiftRuleConflict = 5,
///
/// 系统错误
///
SystemError = 6,
///
/// 业务规则冲突
///
BusinessRuleConflict = 7
}
///
/// 冲突人员信息
///
public class ConflictingPersonnel
{
///
/// 人员ID
///
public long PersonnelId { get; set; }
///
/// 人员姓名
///
public string PersonnelName { get; set; } = string.Empty;
///
/// 冲突类型
///
public string ConflictType { get; set; } = string.Empty;
///
/// 冲突描述
///
public string ConflictDescription { get; set; } = string.Empty;
}
///
/// 详细分配报告
///
public class AllocationReport
{
///
/// 成功分配数量
///
public int SuccessfulAllocationCount { get; set; }
///
/// 失败分配数量
///
public int FailedAllocationCount { get; set; }
///
/// 成功率百分比
///
public double SuccessRate { get; set; }
///
/// 平均质量评分
///
public double AverageQualityScore { get; set; }
///
/// 人员利用率分析
/// Key: 人员ID, Value: 分配任务数量
///
public Dictionary PersonnelUtilizationRate { get; set; } = new();
///
/// 风险分析报告
///
public RiskAnalysis RiskAnalysis { get; set; } = new();
///
/// 优化建议列表
///
public List OptimizationSuggestions { get; set; } = new();
///
/// 报告生成时间
///
public DateTime GeneratedAt { get; set; } = DateTime.Now;
}
///
/// 风险分析
///
public class RiskAnalysis
{
///
/// 整体风险评分 (0-100)
///
public double OverallRiskScore { get; set; }
///
/// 高风险任务数量
///
public int HighRiskTaskCount { get; set; }
///
/// 中风险任务数量
///
public int MediumRiskTaskCount { get; set; }
///
/// 低风险任务数量
///
public int LowRiskTaskCount { get; set; }
///
///
///
public Dictionary RiskDistribution { get; set; } = new();
///
/// 主要风险因素
///
public List PrimaryRiskFactors { get; set; } = new();
}
///
/// 人员工作负载信息
///
public class PersonnelWorkloadInfo
{
///
/// 人员ID
///
public long PersonnelId { get; set; }
///
/// 人员姓名
///
public string PersonnelName { get; set; } = string.Empty;
///
/// 分配任务数量
///
public int AssignedTaskCount { get; set; }
///
/// 平均匹配分数
///
public double AverageMatchScore { get; set; }
///
/// 总工作负载
///
public double TotalWorkload { get; set; }
///
/// 负载状态
///
public WorkloadStatus LoadStatus => DetermineLoadStatus();
///
/// 确定负载状态
///
private WorkloadStatus DetermineLoadStatus()
{
if (TotalWorkload >= 8.0) return WorkloadStatus.Overloaded;
if (TotalWorkload >= 6.0) return WorkloadStatus.HighLoad;
if (TotalWorkload >= 4.0) return WorkloadStatus.NormalLoad;
if (TotalWorkload >= 2.0) return WorkloadStatus.LightLoad;
return WorkloadStatus.Idle;
}
}
///
/// 技能匹配分析
///
public class SkillMatchAnalysis
{
///
/// 平均技能匹配分数
///
public double AverageSkillMatchScore { get; set; }
///
/// 完全胜任人员数量
///
public int FullyQualifiedCount { get; set; }
///
/// 部分胜任人员数量
///
public int PartiallyQualifiedCount { get; set; }
///
/// 勉强胜任人员数量
///
public int MinimallyQualifiedCount { get; set; }
///
/// 技能覆盖度 (0-100)
///
public double SkillCoverageRate => CalculateCoverageRate();
///
/// 计算技能覆盖度
///
private double CalculateCoverageRate()
{
var total = FullyQualifiedCount + PartiallyQualifiedCount + MinimallyQualifiedCount;
return total > 0 ? (double)(FullyQualifiedCount + PartiallyQualifiedCount) / total * 100 : 0;
}
}
///
/// 验证结果
///
public class ValidationResult
{
///
/// 验证是否通过
///
public bool IsValid { get; set; }
///
/// 问题列表
///
public List Issues { get; set; } = new();
public List Violations { get; set; } = new List();
}
///
/// 风险等级枚举
///
public enum RiskLevel
{
///
/// 低风险
///
Low = 1,
///
/// 中风险
///
Medium = 2,
///
/// 高风险
///
High = 3,
///
/// 极高风险
///
Critical = 4
}
///
/// 工作负载状态枚举
///
public enum WorkloadStatus
{
///
/// 空闲
///
Idle = 0,
///
/// 轻负载
///
LightLoad = 1,
///
/// 正常负载
///
NormalLoad = 2,
///
/// 高负载
///
HighLoad = 3,
///
/// 超负载
///
Overloaded = 4
}
///
/// 健康状态枚举
///
public enum HealthStatus
{
///
/// 优秀
///
Excellent = 4,
///
/// 良好
///
Good = 3,
///
/// 一般
///
Fair = 2,
///
/// 差
///
Poor = 1
}
#endregion
}