Files
it/MinimalAPI/Repositories/ProductRepository.cs
2025-11-03 17:03:57 +08:00

265 lines
9.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.EntityFrameworkCore;
using HardwarePerformance.Data;
using HardwarePerformance.Models;
namespace HardwarePerformance.Repositories
{
public class ProductRepository : Repository<Product>, IProductRepository
{
public ProductRepository(AppDbContext context) : base(context)
{
}
public async Task<IEnumerable<Product>> GetByCategoryAsync(int categoryId)
{
return await _dbSet
.Where(p => p.CategoryId == categoryId)
.OrderBy(p => p.CurrentRank)
.ToListAsync();
}
public async Task<IEnumerable<Product>> GetByCategoryAsync(int categoryId, int page, int pageSize, string sortBy = "CurrentRank", string sortOrder = "asc")
{
var query = _dbSet.Where(p => p.CategoryId == categoryId);
// 排序
query = sortBy.ToLower() switch
{
"name" => sortOrder.ToLower() == "desc" ? query.OrderByDescending(p => p.Name) : query.OrderBy(p => p.Name),
"manufacturer" => sortOrder.ToLower() == "desc" ? query.OrderByDescending(p => p.Manufacturer) : query.OrderBy(p => p.Manufacturer),
"releasedate" => sortOrder.ToLower() == "desc" ? query.OrderByDescending(p => p.ReleaseDate) : query.OrderBy(p => p.ReleaseDate),
"price" => sortOrder.ToLower() == "desc" ? query.OrderByDescending(p => p.Price) : query.OrderBy(p => p.Price),
_ => sortOrder.ToLower() == "desc" ? query.OrderByDescending(p => p.CurrentRank) : query.OrderBy(p => p.CurrentRank)
};
return await query
.Skip((page - 1) * pageSize)
.Take(pageSize)
.ToListAsync();
}
public async Task<IEnumerable<Product>> SearchAsync(string query)
{
return await _dbSet
.Where(p =>
p.Name.Contains(query) ||
p.Model.Contains(query) ||
p.Manufacturer.Contains(query))
.ToListAsync();
}
public async Task<IEnumerable<Product>> SearchAsync(string query, int? categoryId = null, string? manufacturer = null, int? minScore = null, int? maxScore = null, int page = 1, int pageSize = 20)
{
var queryable = _dbSet.AsQueryable();
if (categoryId.HasValue)
{
queryable = queryable.Where(p => p.CategoryId == categoryId.Value);
}
if (!string.IsNullOrEmpty(manufacturer))
{
queryable = queryable.Where(p => p.Manufacturer == manufacturer);
}
if (minScore.HasValue)
{
// 使用排名计算性能分数100 - 排名)
queryable = queryable.Where(p => (100 - p.CurrentRank) >= minScore.Value);
}
if (maxScore.HasValue)
{
// 使用排名计算性能分数100 - 排名)
queryable = queryable.Where(p => (100 - p.CurrentRank) <= maxScore.Value);
}
if (!string.IsNullOrEmpty(query))
{
queryable = queryable.Where(p =>
p.Name.Contains(query) ||
p.Model.Contains(query) ||
p.Manufacturer.Contains(query));
}
return await queryable
.OrderBy(p => p.CurrentRank)
.Skip((page - 1) * pageSize)
.Take(pageSize)
.ToListAsync();
}
public async Task<IEnumerable<Product>> FilterAsync(int? categoryId, string? manufacturer, int? minScore, int? maxScore, int? year)
{
var query = _dbSet.AsQueryable();
if (categoryId.HasValue)
{
query = query.Where(p => p.CategoryId == categoryId.Value);
}
if (!string.IsNullOrEmpty(manufacturer))
{
query = query.Where(p => p.Manufacturer == manufacturer);
}
if (minScore.HasValue)
{
// 使用排名计算性能分数100 - 排名)
query = query.Where(p => (100 - p.CurrentRank) >= minScore.Value);
}
if (maxScore.HasValue)
{
// 使用排名计算性能分数100 - 排名)
query = query.Where(p => (100 - p.CurrentRank) <= maxScore.Value);
}
if (year.HasValue)
{
query = query.Where(p => p.ReleaseDate.Year == year.Value);
}
return await query.ToListAsync();
}
public async Task<IEnumerable<Product>> FilterAsync(int? categoryId, string? manufacturer, int? minScore, int? maxScore, int? year, int page, int pageSize, string sortBy = "CurrentRank", string sortOrder = "asc")
{
var query = _dbSet.AsQueryable();
if (categoryId.HasValue)
{
query = query.Where(p => p.CategoryId == categoryId.Value);
}
if (!string.IsNullOrEmpty(manufacturer))
{
query = query.Where(p => p.Manufacturer == manufacturer);
}
if (minScore.HasValue)
{
// 使用排名计算性能分数100 - 排名)
query = query.Where(p => (100 - p.CurrentRank) >= minScore.Value);
}
if (maxScore.HasValue)
{
// 使用排名计算性能分数100 - 排名)
query = query.Where(p => (100 - p.CurrentRank) <= maxScore.Value);
}
if (year.HasValue)
{
query = query.Where(p => p.ReleaseDate.Year == year.Value);
}
// 排序
query = sortBy.ToLower() switch
{
"name" => sortOrder.ToLower() == "desc" ? query.OrderByDescending(p => p.Name) : query.OrderBy(p => p.Name),
"manufacturer" => sortOrder.ToLower() == "desc" ? query.OrderByDescending(p => p.Manufacturer) : query.OrderBy(p => p.Manufacturer),
"releasedate" => sortOrder.ToLower() == "desc" ? query.OrderByDescending(p => p.ReleaseDate) : query.OrderBy(p => p.ReleaseDate),
"price" => sortOrder.ToLower() == "desc" ? query.OrderByDescending(p => p.Price) : query.OrderBy(p => p.Price),
_ => sortOrder.ToLower() == "desc" ? query.OrderByDescending(p => p.CurrentRank) : query.OrderBy(p => p.CurrentRank)
};
return await query
.Skip((page - 1) * pageSize)
.Take(pageSize)
.ToListAsync();
}
public async Task<int> CountAsync(int? categoryId = null)
{
var query = _dbSet.AsQueryable();
if (categoryId.HasValue)
{
query = query.Where(p => p.CategoryId == categoryId.Value);
}
return await query.CountAsync();
}
public async Task<int> CountSearchResultsAsync(string query, int? categoryId = null, string? manufacturer = null, int? minScore = null, int? maxScore = null)
{
var queryable = _dbSet.AsQueryable();
if (categoryId.HasValue)
{
queryable = queryable.Where(p => p.CategoryId == categoryId.Value);
}
if (!string.IsNullOrEmpty(manufacturer))
{
queryable = queryable.Where(p => p.Manufacturer == manufacturer);
}
if (minScore.HasValue)
{
// 使用排名计算性能分数100 - 排名)
queryable = queryable.Where(p => (100 - p.CurrentRank) >= minScore.Value);
}
if (maxScore.HasValue)
{
// 使用排名计算性能分数100 - 排名)
queryable = queryable.Where(p => (100 - p.CurrentRank) <= maxScore.Value);
}
if (!string.IsNullOrEmpty(query))
{
queryable = queryable.Where(p =>
p.Name.Contains(query) ||
p.Model.Contains(query) ||
p.Manufacturer.Contains(query));
}
return await queryable.CountAsync();
}
public async Task<int> CountFilterResultsAsync(int? categoryId, string? manufacturer, int? minScore, int? maxScore, int? year)
{
var query = _dbSet.AsQueryable();
if (categoryId.HasValue)
{
query = query.Where(p => p.CategoryId == categoryId.Value);
}
if (!string.IsNullOrEmpty(manufacturer))
{
query = query.Where(p => p.Manufacturer == manufacturer);
}
if (minScore.HasValue)
{
// 使用排名计算性能分数100 - 排名)
query = query.Where(p => (100 - p.CurrentRank) >= minScore.Value);
}
if (maxScore.HasValue)
{
// 使用排名计算性能分数100 - 排名)
query = query.Where(p => (100 - p.CurrentRank) <= maxScore.Value);
}
if (year.HasValue)
{
query = query.Where(p => p.ReleaseDate.Year == year.Value);
}
return await query.CountAsync();
}
public async Task<Category?> GetCategoryByProductIdAsync(int productId)
{
var product = await _dbSet
.Include(p => p.Category)
.FirstOrDefaultAsync(p => p.Id == productId);
return product?.Category;
}
}
}