Files
it/backend/HardwarePerformance.API/Controllers/ComparisonController.cs

313 lines
10 KiB
C#
Raw Permalink Normal View History

2025-11-03 17:03:57 +08:00
using Microsoft.AspNetCore.Mvc;
using HardwarePerformance.API.Models;
namespace HardwarePerformance.API.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class ComparisonController : ControllerBase
{
private static readonly List<Product> _products = new()
{
new Product
{
Id = 1,
Name = "Apple A17 Pro",
Model = "A17 Pro",
Manufacturer = "Apple",
CategoryId = 1,
CurrentRank = 1,
ReleaseDate = new DateTime(2023, 9, 12),
Price = null
},
new Product
{
Id = 2,
Name = "Snapdragon 8 Gen 3",
Model = "SM8650-AB",
Manufacturer = "Qualcomm",
CategoryId = 1,
CurrentRank = 2,
ReleaseDate = new DateTime(2023, 10, 24),
Price = null
},
new Product
{
Id = 3,
Name = "Intel Core i9-13900K",
Model = "Core i9-13900K",
Manufacturer = "Intel",
CategoryId = 3,
CurrentRank = 1,
ReleaseDate = new DateTime(2022, 10, 20),
Price = 589.99m
},
new Product
{
Id = 4,
Name = "AMD Ryzen 9 7950X",
Model = "Ryzen 9 7950X",
Manufacturer = "AMD",
CategoryId = 3,
CurrentRank = 2,
ReleaseDate = new DateTime(2022, 9, 27),
Price = 699.99m
},
new Product
{
Id = 5,
Name = "NVIDIA GeForce RTX 4090",
Model = "RTX 4090",
Manufacturer = "NVIDIA",
CategoryId = 4,
CurrentRank = 1,
ReleaseDate = new DateTime(2022, 10, 12),
Price = 1599.99m
},
new Product
{
Id = 6,
Name = "AMD Radeon RX 7900 XTX",
Model = "RX 7900 XTX",
Manufacturer = "AMD",
CategoryId = 4,
CurrentRank = 2,
ReleaseDate = new DateTime(2022, 12, 3),
Price = 999.99m
}
};
[HttpPost]
public ActionResult<ApiResponse<object>> CompareProducts([FromBody] ComparisonRequest request)
{
// 验证请求
if (request.ProductIds == null || request.ProductIds.Count < 2 || request.ProductIds.Count > 4)
{
return BadRequest(new ApiResponse<object>
{
Success = false,
Message = "产品ID数量必须在2到4之间"
});
}
// 获取产品
var products = _products.Where(p => request.ProductIds.Contains(p.Id)).ToList();
if (products.Count != request.ProductIds.Count)
{
return NotFound(new ApiResponse<object>
{
Success = false,
Message = "一个或多个产品ID无效"
});
}
// 检查是否所有产品属于同一类别
var categories = products.Select(p => p.CategoryId).Distinct().ToList();
if (categories.Count > 1)
{
return BadRequest(new ApiResponse<object>
{
Success = false,
Message = "所有产品必须属于同一类别"
});
}
// 创建对比数据
var comparisonData = new
{
Products = products.Select(p => new
{
p.Id,
p.Name,
p.Model,
p.Manufacturer,
p.CategoryId,
p.CurrentRank,
p.ReleaseDate,
p.Price,
PerformanceScore = 100 - p.CurrentRank // 使用排名计算模拟性能分数
}).ToList(),
Comparison = GenerateComparisonMatrix(products)
};
return Ok(new ApiResponse<object>
{
Data = comparisonData
});
}
private object GenerateComparisonMatrix(List<Product> products)
{
var matrix = new List<Dictionary<string, object>>();
// 添加基本信息行
matrix.Add(new Dictionary<string, object>
{
["指标"] = "产品名称",
["类型"] = "基本信息"
});
foreach (var product in products)
{
matrix[0][$"{product.Name}"] = product.Name;
}
// 添加制造商行
matrix.Add(new Dictionary<string, object>
{
["指标"] = "制造商",
["类型"] = "基本信息"
});
foreach (var product in products)
{
matrix[1][$"{product.Name}"] = product.Manufacturer;
}
// 添加型号行
matrix.Add(new Dictionary<string, object>
{
["指标"] = "型号",
["类型"] = "基本信息"
});
foreach (var product in products)
{
matrix[2][$"{product.Name}"] = product.Model;
}
// 添加排名行
matrix.Add(new Dictionary<string, object>
{
["指标"] = "当前排名",
["类型"] = "性能指标"
});
foreach (var product in products)
{
matrix[3][$"{product.Name}"] = product.CurrentRank;
}
// 添加性能分数行(模拟)
matrix.Add(new Dictionary<string, object>
{
["指标"] = "性能分数",
["类型"] = "性能指标"
});
foreach (var product in products)
{
matrix[4][$"{product.Name}"] = 100 - product.CurrentRank;
}
// 添加发布日期行
matrix.Add(new Dictionary<string, object>
{
["指标"] = "发布日期",
["类型"] = "基本信息"
});
foreach (var product in products)
{
matrix[5][$"{product.Name}"] = product.ReleaseDate.ToString("yyyy-MM-dd");
}
// 添加价格行
matrix.Add(new Dictionary<string, object>
{
["指标"] = "价格",
["类型"] = "基本信息"
});
foreach (var product in products)
{
matrix[6][$"{product.Name}"] = product.Price?.ToString("C") ?? "N/A";
}
// 标记最优和最差值
MarkBestAndWorstValues(matrix, products);
return matrix;
}
private void MarkBestAndWorstValues(List<Dictionary<string, object>> matrix, List<Product> products)
{
// 对于排名,越小越好
var rankRow = matrix.FirstOrDefault(m => m["指标"].ToString() == "当前排名");
if (rankRow != null)
{
var ranks = products.Select(p => p.CurrentRank).ToList();
var minRank = ranks.Min();
var maxRank = ranks.Max();
foreach (var product in products)
{
var rank = product.CurrentRank;
if (rank == minRank)
{
rankRow[$"{product.Name}_isBest"] = true;
}
else if (rank == maxRank)
{
rankRow[$"{product.Name}_isWorst"] = true;
}
}
}
// 对于性能分数,越大越好
var scoreRow = matrix.FirstOrDefault(m => m["指标"].ToString() == "性能分数");
if (scoreRow != null)
{
var scores = products.Select(p => 100 - p.CurrentRank).ToList();
var maxScore = scores.Max();
var minScore = scores.Min();
foreach (var product in products)
{
var score = 100 - product.CurrentRank;
if (score == maxScore)
{
scoreRow[$"{product.Name}_isBest"] = true;
}
else if (score == minScore)
{
scoreRow[$"{product.Name}_isWorst"] = true;
}
}
}
// 对于价格,越小越好
var priceRow = matrix.FirstOrDefault(m => m["指标"].ToString() == "价格");
if (priceRow != null)
{
var prices = products.Where(p => p.Price.HasValue).Select(p => p.Price!.Value).ToList();
if (prices.Any())
{
var minPrice = prices.Min();
var maxPrice = prices.Max();
foreach (var product in products)
{
if (product.Price.HasValue)
{
if (product.Price == minPrice)
{
priceRow[$"{product.Name}_isBest"] = true;
}
else if (product.Price == maxPrice)
{
priceRow[$"{product.Name}_isWorst"] = true;
}
}
}
}
}
}
}
public class ComparisonRequest
{
public List<int> ProductIds { get; set; } = new();
}
}