/
Sturon
/
Diplom
Обзор
Документация
Войти
/
Sturon
/
Diplom
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
backend/src/main/java/ru/diplom/vision/service/InspectionProfileService.java
133 строки
6 KB
Sturon
Initial
03 июн 2026, 15:01
03 июн 2026, 15:01
17b44a2
Код
Авторство
О чём код?
package ru.diplom.vision.service; import java.util.List; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.server.ResponseStatusException; import ru.diplom.vision.api.ApiMapper; import ru.diplom.vision.api.dto.ProfileRequest; import ru.diplom.vision.api.dto.ProfileStatsResponse; import ru.diplom.vision.api.dto.ProfileSummaryResponse; import ru.diplom.vision.domain.InspectionProfile; import ru.diplom.vision.domain.InspectionStatus; import ru.diplom.vision.domain.ProfileStatus; import ru.diplom.vision.repository.InspectionItemRepository; import ru.diplom.vision.repository.InspectionProfileRepository; import static org.springframework.http.HttpStatus.BAD_REQUEST; import static org.springframework.http.HttpStatus.NOT_FOUND; @Service @Transactional public class InspectionProfileService { private final InspectionProfileRepository inspectionProfileRepository; private final InspectionItemRepository inspectionItemRepository; private final CameraSourceService cameraSourceService; private final ReferenceImageService referenceImageService; private final StorageService storageService; private final ApiMapper apiMapper; public InspectionProfileService( InspectionProfileRepository inspectionProfileRepository, InspectionItemRepository inspectionItemRepository, CameraSourceService cameraSourceService, ReferenceImageService referenceImageService, StorageService storageService, ApiMapper apiMapper ) { this.inspectionProfileRepository = inspectionProfileRepository; this.inspectionItemRepository = inspectionItemRepository; this.cameraSourceService = cameraSourceService; this.referenceImageService = referenceImageService; this.storageService = storageService; this.apiMapper = apiMapper; } @Transactional(readOnly = true) public List<InspectionProfile> findAll() { return inspectionProfileRepository.findAllByOrderByUpdatedAtDesc(); } @Transactional(readOnly = true) public InspectionProfile getRequired(Long id) { return inspectionProfileRepository.findById(id) .orElseThrow(() -> new ResponseStatusException(NOT_FOUND, "Точка контроля не найдена")); } public InspectionProfile create(ProfileRequest request) { InspectionProfile profile = new InspectionProfile(); apply(profile, request); return inspectionProfileRepository.save(profile); } public InspectionProfile update(Long id, ProfileRequest request) { InspectionProfile profile = getRequired(id); apply(profile, request); return inspectionProfileRepository.save(profile); } public void delete(Long id) { InspectionProfile profile = getRequired(id); var items = inspectionItemRepository.findAllByProfileIdOrderByIdAsc(id); for (var item : items) { storageService.delete(item.getSourceImagePath()); storageService.delete(item.getAnnotatedImagePath()); item.getDefects().forEach(defect -> storageService.delete(defect.getCropImagePath())); } inspectionItemRepository.deleteAll(items); inspectionProfileRepository.delete(profile); } public InspectionProfile startAnalysis(Long id) { InspectionProfile profile = getRequired(id); validateCanAnalyze(profile); profile.setAnalysisEnabled(true); return inspectionProfileRepository.save(profile); } public InspectionProfile stopAnalysis(Long id) { InspectionProfile profile = getRequired(id); profile.setAnalysisEnabled(false); return inspectionProfileRepository.save(profile); } @Transactional(readOnly = true) public ProfileStatsResponse getStats(Long profileId) { long okCount = inspectionItemRepository.countByProfileIdAndStatus(profileId, InspectionStatus.OK); long defectCount = inspectionItemRepository.countByProfileIdAndStatus(profileId, InspectionStatus.DEFECT); long totalCount = okCount + defectCount; return apiMapper.toStats(totalCount, okCount, defectCount); } @Transactional(readOnly = true) public List<ProfileSummaryResponse> getSummaries() { return findAll().stream() .map(profile -> apiMapper.toProfileSummaryResponse(profile, getStats(profile.getId()))) .toList(); } private void apply(InspectionProfile profile, ProfileRequest request) { profile.setName(request.name().trim()); profile.setDescription(blankToNull(request.description())); profile.setStatus(request.status()); profile.setDetectorMode(request.detectorMode()); profile.setCameraSource(cameraSourceService.getRequired(request.cameraId())); profile.setReferenceImage(referenceImageService.getRequired(request.referenceImageId())); } private String blankToNull(String value) { return value == null || value.isBlank() ? null : value.trim(); } private void validateCanAnalyze(InspectionProfile profile) { if (profile.getStatus() != ProfileStatus.ACTIVE) { throw new ResponseStatusException(BAD_REQUEST, "Точка контроля неактивна"); } if (!profile.getCameraSource().isActive()) { throw new ResponseStatusException(BAD_REQUEST, "Выбранная камера неактивна"); } if (referenceImageService.getEffectiveRegionsOfInterest(profile.getReferenceImage()).isEmpty()) { throw new ResponseStatusException(BAD_REQUEST, "Для эталона не задана область интереса"); } } }