package com.gkhy.fourierSpecialGasMonitor.service.impl; import com.gkhy.fourierSpecialGasMonitor.commons.domain.Result; import com.gkhy.fourierSpecialGasMonitor.commons.domain.SearchResult; import com.gkhy.fourierSpecialGasMonitor.commons.enums.ResultCode; import com.gkhy.fourierSpecialGasMonitor.commons.exception.BusinessException; import com.gkhy.fourierSpecialGasMonitor.commons.model.PageQuery; import com.gkhy.fourierSpecialGasMonitor.entity.*; import com.gkhy.fourierSpecialGasMonitor.entity.query.GasAtmospherePageQuery; import com.gkhy.fourierSpecialGasMonitor.entity.query.GasFluxPageQuery; import com.gkhy.fourierSpecialGasMonitor.entity.query.GasPageQuery; import com.gkhy.fourierSpecialGasMonitor.entity.req.GasAtmosphereLineChartReqDTO; import com.gkhy.fourierSpecialGasMonitor.entity.req.GasFluxLineChartReqDTO; import com.gkhy.fourierSpecialGasMonitor.entity.req.GasLineChartReqDTO; import com.gkhy.fourierSpecialGasMonitor.entity.resp.*; import com.gkhy.fourierSpecialGasMonitor.service.*; import io.micrometer.core.instrument.util.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import java.lang.reflect.Field; import java.time.LocalDateTime; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * @author Mr.huang * @decription * @date 2023/8/10 9:16 */ @Service public class MonitorDataServiceImpl implements MonitorDataService { private LocalDateTime zeroTime = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0); private final Logger logger = LoggerFactory.getLogger(this.getClass()); private LocalDateTime nowTime = LocalDateTime.now(); @Autowired private GasCategoryService gasCategoryService; @Autowired private GasFluxService gasFluxService; @Autowired private RegionService regionService; @Autowired private GasConcentrationService gasConcentrationService; @Override public Result gasLineChart(GasLineChartReqDTO reqDto) { Result success = Result.success(); if (reqDto == null || reqDto.getGasName() == null) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"参数不能为空"); LocalDateTime startTime = reqDto.getStartTime(); LocalDateTime endTime = reqDto.getEndTime(); if (startTime == null || endTime == null){ startTime = zeroTime; endTime = nowTime; } List gasConcentrationList = gasConcentrationService.listDatabyTimeSlotAndPosition(startTime,endTime,reqDto.getPosition()); if (CollectionUtils.isEmpty(gasConcentrationList)) return success; GasCategory gasCategory = gasCategoryService.findById(reqDto.getGasName()); if (gasCategory == null){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"暂无该气体实时数据"); } String fileNamePrefix = "gasValue"; String fileName = fileNamePrefix + reqDto.getGasName(); if (reqDto.getGasName() < 10){ fileName = fileNamePrefix +"0"+ reqDto.getGasName(); } String fileNameTemp = fileName; List respDTOS = gasConcentrationList.stream().map(gasConcentration -> { GasLineChartRespDTO gasLineChartRespDTO = new GasLineChartRespDTO(); BeanUtils.copyProperties(gasCategory, gasLineChartRespDTO); BeanUtils.copyProperties(gasConcentration, gasLineChartRespDTO); gasLineChartRespDTO.setGasName(reqDto.getGasName()); Field[] fields = gasConcentration.getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); // 设置字段可访问,即使是私有字段 if (field.getName().equals(fileNameTemp)) { Double value = null; try { value = (Double) field.get(gasConcentration); gasLineChartRespDTO.setGasValue(value); } catch (IllegalAccessException e) { logger.info("【警告】气体浓度折线图反射获取气体浓度失败"); } } } return gasLineChartRespDTO; }).collect(Collectors.toList()); success.setData(respDTOS); return success; } @Override public Result gasPage(PageQuery pageQuery) { if (pageQuery == null || pageQuery.getPageIndex() == null || pageQuery.getPageSize() == null){ throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL,"分页参数不能为空"); } if (pageQuery == null || pageQuery.getSearchParams() == null || pageQuery.getSearchParams().getGasName() == null) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"参数不能为空"); GasPageQuery searchParams = pageQuery.getSearchParams(); Integer gasName = searchParams.getGasName(); LocalDateTime startTime = searchParams.getStartTime(); LocalDateTime endTime = searchParams.getEndTime(); if (startTime == null || endTime == null){ searchParams.setStartTime(zeroTime); searchParams.setEndTime(nowTime); } SearchResult> searchResult = new SearchResult<>(); searchResult.setPageIndex(pageQuery.getPageIndex()); searchResult.setPageSize(pageQuery.getPageSize()); searchResult.setSuccess(); Page pageResult = gasConcentrationService.listDatabyTimeSlotAndPositionAndPage(pageQuery); if (CollectionUtils.isEmpty(pageResult.getContent())) return searchResult; searchResult.setTotal(pageResult.getTotalElements()); searchResult.setPages(pageResult.getTotalPages()); GasCategory gasCategory = gasCategoryService.findById(gasName); if (gasCategory == null){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"暂无该气体实时数据"); } String fileNamePrefix = "gasValue"; String fileName = fileNamePrefix + gasName; if (gasName < 10){ fileName = fileNamePrefix +"0"+ gasName; } String fileNameTemp = fileName; List respDTOS = pageResult.getContent().stream().map(gasConcentration -> { GasPageRespDTO gasLineChartRespDTO = new GasPageRespDTO(); BeanUtils.copyProperties(gasCategory, gasLineChartRespDTO); BeanUtils.copyProperties(gasConcentration, gasLineChartRespDTO); gasLineChartRespDTO.setGasName(gasName); Field[] fields = gasConcentration.getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); // 设置字段可访问,即使是私有字段 if (field.getName().equals(fileNameTemp)) { Double value = null; try { value = (Double) field.get(gasConcentration); gasLineChartRespDTO.setGasValue(value); } catch (IllegalAccessException e) { logger.info("【警告】气体浓度分页-反射获取气体浓度失败"); } } } return gasLineChartRespDTO; }).collect(Collectors.toList()); searchResult.setData(respDTOS); return searchResult; } @Override public Result gasFluxLineChart(GasFluxLineChartReqDTO reqDto) { Result success = Result.success(); if (reqDto == null || reqDto.getGasName() == null) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"参数不能为空"); LocalDateTime startTime = reqDto.getStartTime(); LocalDateTime endTime = reqDto.getEndTime(); if (startTime == null || endTime == null){ startTime = zeroTime; endTime = nowTime; } List gasFluxes = gasFluxService.listDatabyTimeSlotAndAreaId(startTime,endTime,reqDto.getAreaId()); if (CollectionUtils.isEmpty(gasFluxes)) return success; GasCategory gasCategory = gasCategoryService.findById(reqDto.getGasName()); if (gasCategory == null){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"暂无该气体通量数据"); } List regions = regionService.findAll(); if (CollectionUtils.isEmpty(regions)){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"无法获取区域信息"); } Map regionMap = regions.stream().collect(Collectors.toMap(Region::getId, r -> r)); String fileNamePrefix = "gasValue"; String fileName = fileNamePrefix + reqDto.getGasName(); if (reqDto.getGasName() < 10){ fileName = fileNamePrefix +"0"+ reqDto.getGasName(); } String fileNameTemp = fileName; List respDTOS = gasFluxes.stream().map(gasFlux -> { GasFluxLineChartRespDTO gasFluxLineChartRespDTO = new GasFluxLineChartRespDTO(); BeanUtils.copyProperties(gasCategory, gasFluxLineChartRespDTO); BeanUtils.copyProperties(gasFlux, gasFluxLineChartRespDTO); if (regionMap.get(gasFlux.getAreaId()) != null){ Region region = regionMap.get(gasFlux.getAreaId()); BeanUtils.copyProperties(region,gasFluxLineChartRespDTO,"name"); gasFluxLineChartRespDTO.setRegionName(region.getName()); } gasFluxLineChartRespDTO.setGasName(reqDto.getGasName()); Field[] fields = gasFlux.getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); // 设置字段可访问,即使是私有字段 if (field.getName().equals(fileNameTemp)) { Double value = null; try { value = (Double) field.get(gasFlux); gasFluxLineChartRespDTO.setGasValue(value); } catch (IllegalAccessException e) { logger.info("【警告】气体通量折线图反射获取气体浓度失败"); } } } return gasFluxLineChartRespDTO; }).collect(Collectors.toList()); success.setData(respDTOS); return success; } @Override public Result gasFluxPage(PageQuery pageQuery) { if (pageQuery == null || pageQuery.getPageIndex() == null || pageQuery.getPageSize() == null){ throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL,"分页参数不能为空"); } if (pageQuery == null || pageQuery.getSearchParams() == null || pageQuery.getSearchParams().getGasName() == null) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"参数不能为空"); GasFluxPageQuery searchParams = pageQuery.getSearchParams(); Integer gasName = searchParams.getGasName(); LocalDateTime startTime = searchParams.getStartTime(); LocalDateTime endTime = searchParams.getEndTime(); if (startTime == null || endTime == null){ searchParams.setStartTime(zeroTime); searchParams.setEndTime(nowTime); } SearchResult> searchResult = new SearchResult<>(); searchResult.setPageIndex(pageQuery.getPageIndex()); searchResult.setPageSize(pageQuery.getPageSize()); searchResult.setSuccess(); Page pageResult = gasFluxService.listDatabyTimeSlotAndPage(pageQuery); if (CollectionUtils.isEmpty(pageResult.getContent())) return searchResult; searchResult.setTotal(pageResult.getTotalElements()); searchResult.setPages(pageResult.getTotalPages()); GasCategory gasCategory = gasCategoryService.findById(gasName); if (gasCategory == null){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"暂无该气体实时数据"); } List regions = regionService.findAll(); if (CollectionUtils.isEmpty(regions)){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"无法获取区域信息"); } Map regionMap = regions.stream().collect(Collectors.toMap(Region::getId, r -> r)); String fileNamePrefix = "gasValue"; String fileName = fileNamePrefix + gasName; if (gasName < 10){ fileName = fileNamePrefix +"0"+ gasName; } String fileNameTemp = fileName; List respDTOS = pageResult.getContent().stream().map(gasFlux -> { GasFluxPageRespDTO gasFluxPageRespDTO = new GasFluxPageRespDTO(); BeanUtils.copyProperties(gasCategory, gasFluxPageRespDTO); BeanUtils.copyProperties(gasFlux, gasFluxPageRespDTO); if (regionMap.get(gasFlux.getAreaId()) != null){ Region region = regionMap.get(gasFlux.getAreaId()); BeanUtils.copyProperties(region,gasFluxPageRespDTO,"name"); gasFluxPageRespDTO.setRegionName(region.getName()); } gasFluxPageRespDTO.setGasName(pageQuery.getSearchParams().getGasName()); Field[] fields = gasFlux.getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); // 设置字段可访问,即使是私有字段 if (field.getName().equals(fileNameTemp)) { Double value = null; try { value = (Double) field.get(gasFlux); gasFluxPageRespDTO.setGasValue(value); } catch (IllegalAccessException e) { logger.info("【警告】气体通量分页数据-反射获取气体浓度失败"); } } } return gasFluxPageRespDTO; }).collect(Collectors.toList()); searchResult.setData(respDTOS); return searchResult; } @Override public Result gasAtmosphereLineChart(GasAtmosphereLineChartReqDTO reqDto) { Result success = Result.success(); if (reqDto == null || StringUtils.isEmpty(reqDto.getAtmosphere())) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"参数不能为空"); LocalDateTime startTime = reqDto.getStartTime(); LocalDateTime endTime = reqDto.getEndTime(); if (startTime == null || endTime == null){ startTime = zeroTime; endTime = nowTime; } List gasConcentrations = gasConcentrationService.listDatabyTimeSlot(startTime,endTime); if (CollectionUtils.isEmpty(gasConcentrations)) return success; List respDTOS = gasConcentrations.stream().map(gasConcentration -> { GasAtmosphereLineChartRespDTO gasAtmosphereLineChartRespDTO = new GasAtmosphereLineChartRespDTO(); gasAtmosphereLineChartRespDTO.setTime(gasConcentration.getTime()); Field[] fields = gasConcentration.getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); if (field.getName().equals(reqDto.getAtmosphere())) { try { Object value = field.get(gasConcentration); gasAtmosphereLineChartRespDTO.setValue(value); } catch (IllegalAccessException e) { logger.info("【警告】气象折线图反射获取气体浓度失败"); } } } return gasAtmosphereLineChartRespDTO; }).collect(Collectors.toList()); success.setData(respDTOS); return success; } @Override public Result gasAtmospherePage(PageQuery pageQuery) { if (pageQuery == null || pageQuery.getPageIndex() == null || pageQuery.getPageSize() == null){ throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL,"分页参数不能为空"); } if (pageQuery == null || pageQuery.getSearchParams() == null || StringUtils.isEmpty(pageQuery.getSearchParams().getAtmosphere())) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"参数不能为空"); GasAtmospherePageQuery searchParams = pageQuery.getSearchParams(); String atmosphere = searchParams.getAtmosphere(); LocalDateTime startTime = searchParams.getStartTime(); LocalDateTime endTime = searchParams.getEndTime(); if (startTime == null || endTime == null){ searchParams.setStartTime(zeroTime); searchParams.setEndTime(nowTime); } SearchResult> searchResult = new SearchResult<>(); searchResult.setPageIndex(pageQuery.getPageIndex()); searchResult.setPageSize(pageQuery.getPageSize()); searchResult.setSuccess(); Page pageResult = gasConcentrationService.gasAtmospherePage(pageQuery); if (CollectionUtils.isEmpty(pageResult.getContent())) return searchResult; searchResult.setTotal(pageResult.getTotalElements()); searchResult.setPages(pageResult.getTotalPages()); List respDTOS = pageResult.getContent().stream().map(gasConcentration -> { GasAtmospherePageRespDTO gasAtmospherePageRespDTO = new GasAtmospherePageRespDTO(); gasAtmospherePageRespDTO.setTime(gasConcentration.getTime()); Field[] fields = gasConcentration.getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); // 设置字段可访问,即使是私有字段 if (field.getName().equals(atmosphere)) { try { Object value = field.get(gasConcentration); gasAtmospherePageRespDTO.setValue(value); } catch (IllegalAccessException e) { logger.info("【警告】气象折线图反射获取气体浓度失败"); } } } return gasAtmospherePageRespDTO; }).collect(Collectors.toList()); searchResult.setData(respDTOS); return searchResult; } }