|
@@ -1,17 +1,20 @@
|
|
|
package com.qizhen.healsphere.service.impl;
|
|
|
|
|
|
import com.qizhen.healsphere.repository.mapper.BacteriaGenusMapper;
|
|
|
+import com.qizhen.healsphere.repository.mapper.EpifluMapper;
|
|
|
import com.qizhen.healsphere.repository.mapper.StrainMapper;
|
|
|
-import com.qizhen.healsphere.web.param.BacteriaGenus;
|
|
|
-import com.qizhen.healsphere.web.param.Strain;
|
|
|
+import com.qizhen.healsphere.web.param.*;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.Year;
|
|
|
+import java.time.ZoneId;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
+
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
public class ZYApiServiceImpl {
|
|
@@ -22,6 +25,9 @@ public class ZYApiServiceImpl {
|
|
|
@Autowired
|
|
|
private StrainMapper strainMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private EpifluMapper epifluMapper;
|
|
|
+
|
|
|
public List<BacteriaGenus> getBacteriaInfo(String input) {
|
|
|
log.info("Calling getBacteriaInfo with input: {}", input);
|
|
|
// 将输入字符串按逗号分割成列表
|
|
@@ -29,10 +35,10 @@ public class ZYApiServiceImpl {
|
|
|
|
|
|
// 查询 strain 表
|
|
|
List<Strain> strains = strainMapper.findByParentNames(names);
|
|
|
- List<String> pnames = strains.stream().map(Strain::getParentName).collect(Collectors.toList()); // 查询 bacteriagenus 表
|
|
|
- List<BacteriaGenus> bacteriaGenera = bacteriaGenusMapper.findByGenericNames(pnames);
|
|
|
-
|
|
|
+ List<String> pnames = strains.stream().map(Strain::getParentName).collect(Collectors.toList());
|
|
|
|
|
|
+ // 查询 bacteriagenus 表
|
|
|
+ List<BacteriaGenus> bacteriaGenera = bacteriaGenusMapper.findByGenericNames(pnames);
|
|
|
|
|
|
// 将 strains 按 parentName 分组
|
|
|
Map<String, List<Strain>> strainMap = strains.stream()
|
|
@@ -48,4 +54,165 @@ public class ZYApiServiceImpl {
|
|
|
|
|
|
return bacteriaGenera;
|
|
|
}
|
|
|
+
|
|
|
+ public List<String> getCladeList(String subType) {
|
|
|
+ return epifluMapper.findDistinctCladeBySubtype(subType);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Integer getCount(String subType) {
|
|
|
+ return epifluMapper.selectCountBySubtype(subType);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map<String,Double> getCumulativeList(String subtype) {
|
|
|
+ Date maxCollectionDate = epifluMapper.findMaxCollectionDateBySubtype(subtype);
|
|
|
+ // 更安全的日期转换方式
|
|
|
+ LocalDate startDate = maxCollectionDate.toInstant()
|
|
|
+ .atZone(ZoneId.of("UTC")) // 明确指定时区为UTC
|
|
|
+ .toLocalDate()
|
|
|
+ .withDayOfYear(1);
|
|
|
+ LocalDate endDate = startDate.plusMonths(12).minusDays(1);
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
+
|
|
|
+ Map<String, Double> cumulative = new TreeMap<>(); // 使用 TreeMap 保持键的有序性
|
|
|
+ for (int i = 0; i < 12; i++) {
|
|
|
+ LocalDate monthStartDate = startDate.plusMonths(i);
|
|
|
+ LocalDate monthEndDate = monthStartDate.plusMonths(1).minusDays(1);
|
|
|
+ String monthKey = monthStartDate.format(formatter);
|
|
|
+ //如果要把逻辑改成每个月占比,把下面的 startDate改成monthStartDate
|
|
|
+ int countInMonth = epifluMapper.selectCountBySubtypeAndDateRange(subtype, startDate, monthEndDate);
|
|
|
+ int countInYear = epifluMapper.selectCountBySubtypeAndDateRange(subtype, startDate, endDate);
|
|
|
+ double percentage = (double) countInMonth / countInYear * 100;
|
|
|
+ cumulative.put(monthKey, Math.round(percentage * 10.0) / 10.0); // 保留小数点后一位
|
|
|
+ }
|
|
|
+ return cumulative;
|
|
|
+ }
|
|
|
+// public Map<String, Integer> getInEachContinentMap(String subtype) {
|
|
|
+// List<String> continentList = Arrays.asList("Asia", "Europe", "Africa", "North America", "South America", "Oceania");
|
|
|
+// Date maxCollectionDate = epifluMapper.findMaxCollectionDateBySubtype(subtype);
|
|
|
+// // 更安全的日期转换方式
|
|
|
+// LocalDate startDate = maxCollectionDate.toInstant()
|
|
|
+// .atZone(ZoneId.of("UTC")) // 明确指定时区为UTC
|
|
|
+// .toLocalDate()
|
|
|
+// .withDayOfYear(1);
|
|
|
+// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
+//
|
|
|
+// Map<String, Integer> inEachContinent = new TreeMap<>(); // 使用 TreeMap 保持键的有序性
|
|
|
+// for (String continent : continentList) {
|
|
|
+// for (int i = 0; i < 12; i+=3) {
|
|
|
+// LocalDate SeasonStartDate = startDate.plusMonths(i);
|
|
|
+// LocalDate seasonEndDate = SeasonStartDate.plusMonths(3).minusDays(1);
|
|
|
+// String SeasonKey = SeasonStartDate.format(formatter);
|
|
|
+// int counInSeason = epifluMapper.selectCountBySubtypeAndDateRangeWithContinent(subtype, SeasonStartDate, seasonEndDate, continent);
|
|
|
+// inEachContinent.put(SeasonKey, counInSeason);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// return inEachContinent;
|
|
|
+// }
|
|
|
+public Map<String, Object> getInEachContinentMap(String subtype) {
|
|
|
+ List<String> continentList = Arrays.asList("Asia", "Europe", "Africa",
|
|
|
+ "North America", "South America", "Oceania");
|
|
|
+ // 颜色数组
|
|
|
+ List<String> colors = Arrays.asList("#67a71f", "#e4ab04", "#7f79b7",
|
|
|
+ "#1a9e76", "#dc5d04", "#a5761a");
|
|
|
+
|
|
|
+ Date maxCollectionDate = epifluMapper.findMaxCollectionDateBySubtype(subtype);
|
|
|
+ LocalDate startDate = maxCollectionDate.toInstant()
|
|
|
+ .atZone(ZoneId.of("UTC"))
|
|
|
+ .toLocalDate()
|
|
|
+ .withDayOfYear(1);
|
|
|
+
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
+ Map<String, Object> inEachContinent = new HashMap<>();
|
|
|
+
|
|
|
+ // 1. 准备x轴数据 - 所有季度标签
|
|
|
+ List<String> xAxis = new ArrayList<>();
|
|
|
+ for (String continent : continentList) {
|
|
|
+ for (int i = 0; i < 12; i += 3) {
|
|
|
+ LocalDate seasonStart = startDate.plusMonths(i);
|
|
|
+ xAxis.add(seasonStart.format(formatter));
|
|
|
+ }
|
|
|
+ xAxis.add(" "); // 每大洲后加空字符串
|
|
|
+ }
|
|
|
+ // 移除最后一个多余的空格
|
|
|
+ if (!xAxis.isEmpty() && xAxis.get(xAxis.size()-1).equals(" ")) {
|
|
|
+ xAxis.remove(xAxis.size() - 1);
|
|
|
+ }
|
|
|
+ inEachContinent.put("x", xAxis);
|
|
|
+
|
|
|
+ // 2. 准备y轴数据 - 改为List<Object>以支持混合类型
|
|
|
+ List<Object> yAxis = new ArrayList<>();
|
|
|
+ int colorIndex = 0;
|
|
|
+ int itemCount = 0;
|
|
|
+
|
|
|
+ for (String continent : continentList) {
|
|
|
+ for (int i = 0; i < 12; i += 3) {
|
|
|
+ LocalDate seasonStart = startDate.plusMonths(i);
|
|
|
+ LocalDate seasonEnd = seasonStart.plusMonths(3).minusDays(1);
|
|
|
+
|
|
|
+ int count = epifluMapper.selectCountBySubtypeAndDateRangeWithContinent(
|
|
|
+ subtype, seasonStart, seasonEnd, continent);
|
|
|
+
|
|
|
+ Map<String, Object> dataItem = new HashMap<>();
|
|
|
+ dataItem.put("value", count);
|
|
|
+
|
|
|
+ Map<String, String> itemStyle = new HashMap<>();
|
|
|
+ itemStyle.put("color", colors.get(colorIndex));
|
|
|
+ dataItem.put("itemStyle", itemStyle);
|
|
|
+
|
|
|
+ yAxis.add(dataItem);
|
|
|
+ itemCount++;
|
|
|
+
|
|
|
+ // 每4个数据项后插入空字符串并切换颜色
|
|
|
+ if (itemCount % 4 == 0) {
|
|
|
+ yAxis.add(" "); // 插入空字符串分隔符
|
|
|
+ colorIndex = (colorIndex + 1) % colors.size();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 移除最后可能多余的空字符串
|
|
|
+ if (!yAxis.isEmpty() && yAxis.get(yAxis.size()-1) instanceof String) {
|
|
|
+ yAxis.remove(yAxis.size() - 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ inEachContinent.put("y", yAxis);
|
|
|
+ return inEachContinent;
|
|
|
}
|
|
|
+
|
|
|
+ public List<GeoInfo> getGeoInfoList(String input) {
|
|
|
+ List<String> continentList = Arrays.asList("Asia", "Europe", "Africa",
|
|
|
+ "North America", "South America", "Oceania");
|
|
|
+ List<GeoInfo> geoInfoList = new ArrayList<>();
|
|
|
+
|
|
|
+ for (String continent : continentList) {
|
|
|
+ GeoInfo geoInfo = new GeoInfo();
|
|
|
+
|
|
|
+ // 1. 获取clade统计数据
|
|
|
+ List<CladeCountResult> cladeCountResult = epifluMapper.selectCladeCountByContinent(continent, input);
|
|
|
+
|
|
|
+ // 2. 转换数据格式
|
|
|
+ List<GeoDataItem> dataItems = cladeCountResult.stream()
|
|
|
+ .map(result -> new GeoDataItem(result.getClade(), result.getCount()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 3. 设置中心坐标(注意这里传入的是Double数组)
|
|
|
+ ContinentCoordinates coordinates = ContinentCoordinates.fromName(continent.toUpperCase().replace(" ", "_"));
|
|
|
+ geoInfo.setContinentName(continent);
|
|
|
+ geoInfo.setCenter(new Double[]{
|
|
|
+ Double.parseDouble(coordinates.getLongitude()),
|
|
|
+ Double.parseDouble(coordinates.getLatitude())
|
|
|
+ });
|
|
|
+
|
|
|
+ // 4. 设置数据
|
|
|
+ geoInfo.setData(dataItems);
|
|
|
+
|
|
|
+ geoInfoList.add(geoInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ return geoInfoList;
|
|
|
+ }
|
|
|
+ //获取所有的subType值
|
|
|
+ public List<String> findDistinctSubtype(){
|
|
|
+ return epifluMapper.findDistinctSubtype();
|
|
|
+ }
|
|
|
+}
|