#include "projectlogic.h" ProjectLogic::ProjectLogic() {} QList ProjectLogic::findDevicesByAllParameters( const QMap &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 ProjectLogic::filterByEntity(const QMap &projects, int entityId, int currentEntity) { QMap 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 ProjectLogic::searchDevices(const QMap &projects, const QString &searchText) { if (searchText.isEmpty()) return projects; QMap 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 ProjectLogic::applyFilters(const QMap &projects, const FilterParams &filterParams) { if (!filterParams.getApllyFilters()) return projects; QMap 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 ProjectLogic::sortDevices(QMap &projects, const QString &sortOrder) { QList 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; }