/* * Copyright (C) 2023 niushuai233 niushuai.cc * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cc.niushuai.gasweb.controller; import cc.niushuai.gasweb.entity.CH2O; import cc.niushuai.gasweb.entity.ChartResp; import cc.niushuai.gasweb.entity.Gas; import cc.niushuai.gasweb.repository.CH2ORepository; import cc.niushuai.gasweb.repository.GasRepository; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateUtil; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 图表请求入口 * * @author niushuai233 * @date 2024/6/7 10:33 * @since 0.0.1 */ @Tag(name = "图表请求入口") @Slf4j @Controller @RequestMapping("/chart") public class ChartController { @Resource private GasRepository gasRepository; @Resource private CH2ORepository ch2ORepository; @Operation(summary = "test") @GetMapping("/test") @ResponseBody public String test() { return "hello test"; } @Operation(summary = "根据类型获取不同表的数据") @PostMapping("/fetch") @ResponseBody public Map fetch(String startDateTime, String endDateTime) { Map map = new HashMap<>(); fetchGas(startDateTime, endDateTime, map); fetchCH2O(startDateTime, endDateTime, map); return map; } private void fetchCH2O(String startDateTime, String endDateTime, Map map) { try { List allList = ch2ORepository.findAllByTimeBetweenOrderByIdAsc(DateUtil.parseDateTime(startDateTime), DateUtil.parseDateTime(endDateTime)); if (CollUtil.isNotEmpty(allList)) { ChartResp ch2o = new ChartResp("CH2O浓度"); for (CH2O ch2O : allList) { String dateTime = DateUtil.formatDateTime(ch2O.getTime()); ch2o.getData().add(new ChartResp.Data(dateTime, ch2O.getCh2o())); } map.put("ch2o", ch2o); } } catch (Exception e) { log.error("fetch ch2o failed", e); map.put("ch2oError", new ChartResp("fetch error: " + e.getMessage())); } } private void fetchGas(String startDateTime, String endDateTime, Map map) { try { List allList = gasRepository.findAllByTimeBetweenOrderByIdAsc(DateUtil.parseDateTime(startDateTime), DateUtil.parseDateTime(endDateTime)); if (CollUtil.isNotEmpty(allList)) { ChartResp co = new ChartResp("CO浓度"); ChartResp co2 = new ChartResp("CO2浓度"); ChartResp ch4 = new ChartResp("CH4浓度"); ChartResp temperature = new ChartResp("环境温度"); ChartResp rp2024_temperature = new ChartResp("rp2024温度"); for (Gas gas : allList) { String dateTime = DateUtil.formatDateTime(gas.getTime()); co.getData().add(new ChartResp.Data(dateTime, gas.getCo())); co2.getData().add(new ChartResp.Data(dateTime, gas.getCo2())); ch4.getData().add(new ChartResp.Data(dateTime, gas.getCh4())); temperature.getData().add(new ChartResp.Data(dateTime, gas.getTemperature())); rp2024_temperature.getData().add(new ChartResp.Data(dateTime, gas.getRp2040Temperature())); } map.put("co", co); map.put("co2", co2); map.put("ch4", ch4); map.put("temperature", temperature); map.put("rp2024_temperature", rp2024_temperature); } } catch (Exception e) { log.error("fetch gas failed", e); map.put("gasError", new ChartResp("fetch error: " + e.getMessage())); } } }