PNPkurs-PIbd-31-Alekseev-I-S/service/projectlogic.cpp
Иван Алексеев f522412382 готово
2025-01-15 22:57:00 +04:00

127 lines
4.3 KiB
C++
Raw Permalink 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.

#include "projectlogic.h"
ProjectLogic::ProjectLogic() {}
QList<Project> ProjectLogic::findDevicesByAllParameters(
const QMap<int, Project> &projects,
int entityId, int currentEntity,
QString searchText,
const FilterParams &filterParams,
const QString &sortOrder
)
{
auto filteredDevices = filterByEntity(projects, entityId, currentEntity);
filteredDevices = searchDevices(filteredDevices, searchText);
filteredDevices = applyFilters(filteredDevices, filterParams);
return sortDevices(filteredDevices, sortOrder);
}
QMap<int, Project> ProjectLogic::filterByEntity(const QMap<int, Project> &projects, int entityId, int currentEntity)
{
QMap<int, Project> result;
if (entityId == -1 || currentEntity == 0)
return projects; // "Все проекты"
for (auto &project : projects) {
switch (currentEntity) {
case 1: // Типы устройств
if (project.deviceModel().idType() == entityId)
result.insert(project.id(), project);
break;
case 2: // Команды
if (project.idTeam() == entityId)
result.insert(project.id(), project);
break;
case 3: // Работники
if (project.stages().idWorker() == entityId)
result.insert(project.id(), project);
break;
case 4: // Этапы
if (project.stages().id() == entityId)
result.insert(project.id(), project);
break;
case 5: // Модели устройств
if (project.deviceModel().id() == entityId)
result.insert(project.id(), project);
break;
default: break;
}
}
return result;
}
QMap<int, Project> ProjectLogic::searchDevices(const QMap<int, Project> &projects, const QString &searchText)
{
if (searchText.isEmpty())
return projects;
QMap<int, Project> result;
for (auto &project : projects) {
if (project.deviceModel().name().contains(searchText, Qt::CaseInsensitive) ||
project.deviceModel().nameType().contains(searchText, Qt::CaseInsensitive)) {
result.insert(project.id(), project);
}
}
return result;
}
QMap<int, Project> ProjectLogic::applyFilters(const QMap<int, Project> &projects, const FilterParams &filterParams)
{
if (!filterParams.getApllyFilters())
return projects;
QMap<int, Project> result;
if (filterParams.getDisregardState())
{
for (auto &project : projects) {
if (project.budget() < filterParams.priceFrom() || project.budget() > filterParams.priceTo())
continue;
result.insert(project.id(), project);
}
}
else
{
for (auto &project : projects) {
if (project.isReady() != filterParams.isReady())
continue;
if (project.budget() < filterParams.priceFrom() || project.budget() > filterParams.priceTo())
continue;
result.insert(project.id(), project);
}
}
return result;
}
QList<Project> ProjectLogic::sortDevices(QMap<int, Project> &projects, const QString &sortOrder)
{
QList<Project> deviceList = projects.values();
if (sortOrder == "Сначала новые") {
std::sort(deviceList.begin(), deviceList.end(), [](const Project &a, const Project &b) {
return a.StartProjectDate() < b.StartProjectDate();
});
}
if (sortOrder == "Сначала старые") {
std::sort(deviceList.begin(), deviceList.end(), [](const Project &a, const Project &b) {
return a.FinishProjectDate() > b.FinishProjectDate();
});
}
if (sortOrder == "Сначала дешевые") {
std::sort(deviceList.begin(), deviceList.end(), [](const Project &a, const Project &b) {
return a.budget() < b.budget();
});
}
if (sortOrder == "Сначала дорогие") {
std::sort(deviceList.begin(), deviceList.end(), [](const Project &a, const Project &b) {
return a.budget() > b.budget();
});
}
if (sortOrder == "Сначала с лайком") {
std::sort(deviceList.begin(), deviceList.end(), [](const Project &a, const Project &b) {
return a.isLiked() > b.isLiked();
});
}
return deviceList;
}