package com.whyc.service;
|
|
import com.whyc.dto.BatteryInfo;
|
import com.whyc.dto.DataAnalysisFloatDTO;
|
import com.whyc.dto.Response;
|
import com.whyc.mapper.BatteryDataMapper;
|
import com.whyc.mapper.BatteryResDataMapper;
|
import com.whyc.mapper.BatteryTestDataMapper;
|
import com.whyc.pojo.BatteryEndurance;
|
import com.whyc.pojo.BatteryRTState;
|
import com.whyc.util.MathUtil;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import java.util.*;
|
|
@Service
|
public class BatteryDataService {
|
|
@Resource
|
private BatteryDataMapper mapper;
|
|
@Resource
|
private BatteryResDataMapper resMapper;
|
|
@Resource
|
private BatteryTestDataMapper testDataMapper;
|
|
public Response getEndurance(Integer userId) {
|
List<BatteryEndurance> enduranceList = mapper.endurance(userId);
|
List<Integer> times = Arrays.asList(1, 2, 3);
|
Map resultMap = enduranceAnalysis(enduranceList,times);
|
return new Response<Map>().set(1,resultMap);
|
}
|
|
/**按时间阶段统计续航*/
|
private static Map enduranceAnalysis(List<BatteryEndurance> enduranceList, List<Integer> times) {
|
Map<String,Integer> map = new HashMap<>();
|
|
for (int i = 0; i < enduranceList.size(); i++) {
|
//每一个具体的续航进行分组
|
if(enduranceList.get(i).getEnduranceActualTime()<times.get(0)){
|
map.put("续航"+times.get(0)+"小时内",Optional.ofNullable(map.get("续航"+times.get(0)+"小时内")).orElse(0)+1);
|
}
|
else if(enduranceList.get(i).getEnduranceActualTime()>times.get(times.size()-1)){
|
map.put("续航"+times.get(times.size()-1)+"小时以上",Optional.ofNullable(map.get("续航"+times.get(times.size()-1)+"小时以上")).orElse(0)+1);
|
}else {
|
for (int j = 0; j < times.size(); j++) {
|
if (enduranceList.get(i).getEnduranceActualTime() <= times.get(j)) {
|
map.put("续航" + times.get(j - 1) + "小时到" + times.get(j) + "小时", Optional.ofNullable(map.get("续航" + times.get(j - 1) + "小时到" + times.get(j) + "小时")).orElse(0) + 1);
|
break;
|
}
|
}
|
}
|
}
|
//排序
|
TreeMap<String, Integer> maps = new TreeMap<>(new Comparator<String>() {
|
@Override
|
public int compare(String o1, String o2) {
|
return o1.compareTo(o2);
|
}
|
});
|
|
maps.putAll(map);
|
map.clear();
|
|
return maps;
|
}
|
|
public Response getBatteryCap(Integer userId) {
|
List<BatteryRTState> batteryRTStates = mapper.getBatteryCap(userId);
|
List<Float> percents = Arrays.asList(0.8f, 0.85f,0.9f,0.95f);
|
Map resultMap = batteryCapAnalysis(batteryRTStates,percents);
|
return new Response<Map>().set(1,resultMap);
|
}
|
|
private Map batteryCapAnalysis(List<BatteryRTState> batteryRTStates, List<Float> percents) {
|
Map<String,Integer> map = new HashMap<>();
|
|
for (int i = 0; i < batteryRTStates.size(); i++) {
|
//每一个具体的续航进行分组
|
if((Float)MathUtil.divide(batteryRTStates.get(i).getBatteryRealCap(),batteryRTStates.get(i).getMonCapStd(),true)<percents.get(0)){
|
map.put(percents.get(0)+"以下",Optional.ofNullable(map.get(percents.get(0)+"以下")).orElse(0)+1);
|
}
|
else if((Float)MathUtil.divide(batteryRTStates.get(i).getBatteryRealCap(),batteryRTStates.get(i).getMonCapStd(),true)>percents.get(percents.size()-1)){
|
map.put(percents.get(percents.size()-1)+"以上",Optional.ofNullable(map.get(percents.get(percents.size()-1)+"以上")).orElse(0)+1);
|
}else {
|
for (int j = 0; j < percents.size(); j++) {
|
if ((Float)MathUtil.divide(batteryRTStates.get(i).getBatteryRealCap(),batteryRTStates.get(i).getMonCapStd(),true) <= percents.get(j)) {
|
map.put(percents.get(j - 1) + "到" + percents.get(j), Optional.ofNullable(map.get(percents.get(j - 1) + "到" + percents.get(j))).orElse(0) + 1);
|
break;
|
}
|
}
|
}
|
}
|
//排序
|
TreeMap<String, Integer> maps = new TreeMap<>(new Comparator<String>() {
|
@Override
|
public int compare(String o1, String o2) {
|
return o1.compareTo(o2);
|
}
|
});
|
|
maps.putAll(map);
|
map.clear();
|
|
return maps;
|
}
|
|
public Response getMonVol(Integer userId) {
|
HashMap<String, Object> resMap = new HashMap<>();
|
List<BatteryInfo> batteryInfos = testDataMapper.getStationAndBatteryGroupIds(userId);
|
for (BatteryInfo temp:batteryInfos) {
|
//根据BattGroupIds查询到对应的数值
|
String[] batteryGroupIds = temp.getBatteryGroupIds().split(",");
|
Integer[] ids = new Integer[batteryGroupIds.length];
|
for (int i = 0; i < batteryGroupIds.length; i++) {
|
ids[i] = Integer.parseInt(batteryGroupIds[i]);
|
}
|
DataAnalysisFloatDTO dataStatistics = testDataMapper.getStatisticsByBattGroupIds(ids,"mon_vol");
|
resMap.put(temp.getStationName(),dataStatistics);
|
}
|
return new Response<>().set(1,resMap);
|
}
|
|
public Response getMonTemp(Integer userId) {
|
HashMap<String, Object> resMap = new HashMap<>();
|
List<BatteryInfo> batteryInfos = testDataMapper.getStationAndBatteryGroupIds(userId);
|
for (BatteryInfo temp:batteryInfos) {
|
//根据BattGroupIds查询到对应的数值
|
String[] batteryGroupIds = temp.getBatteryGroupIds().split(",");
|
Integer[] ids = new Integer[batteryGroupIds.length];
|
for (int i = 0; i < batteryGroupIds.length; i++) {
|
ids[i] = Integer.parseInt(batteryGroupIds[i]);
|
}
|
DataAnalysisFloatDTO dataStatistics = testDataMapper.getStatisticsByBattGroupIds(ids,"mon_tmp");
|
resMap.put(temp.getStationName(),dataStatistics);
|
}
|
return new Response<>().set(1,resMap);
|
}
|
|
public Response getMonRes(Integer userId) {
|
HashMap<String, Object> resMap = new HashMap<>();
|
List<BatteryInfo> batteryInfos = resMapper.getResStationAndBatteryGroupIds(userId);
|
for (BatteryInfo temp:batteryInfos) {
|
//根据BattGroupIds查询到对应的数值
|
String[] batteryGroupIds = temp.getBatteryGroupIds().split(",");
|
Integer[] ids = new Integer[batteryGroupIds.length];
|
for (int i = 0; i < batteryGroupIds.length; i++) {
|
ids[i] = Integer.parseInt(batteryGroupIds[i]);
|
}
|
DataAnalysisFloatDTO dataStatistics = resMapper.getResStatisticsByBattGroupIds(ids);
|
resMap.put(temp.getStationName(),dataStatistics);
|
}
|
return new Response<>().set(1,resMap);
|
}
|
|
public Response getMonCap(Integer userId) {
|
HashMap<String, Object> resMap = new HashMap<>();
|
List<BatteryInfo> batteryInfos = testDataMapper.getStationAndBatteryGroupIds(userId);
|
for (BatteryInfo temp:batteryInfos) {
|
String[] batteryGroupIds = temp.getBatteryGroupIds().split(",");
|
Integer[] ids = new Integer[batteryGroupIds.length];
|
for (int i = 0; i < batteryGroupIds.length; i++) {
|
ids[i] = Integer.parseInt(batteryGroupIds[i]);
|
}
|
//查询标准
|
List<BatteryInfo> batteryStdList = testDataMapper.getVolAndCapStd(ids);
|
//查询最值
|
List<BatteryInfo> batteryStatisticList = testDataMapper.getCapStatisticsByBattGroupIds(ids);
|
|
}
|
return new Response<>().set(1,resMap);
|
|
}
|
|
|
public static void main(String[] args) {
|
BatteryEndurance endurance = new BatteryEndurance();
|
endurance.setEnduranceActualTime(1.5);
|
|
BatteryEndurance endurance2 = new BatteryEndurance();
|
endurance2.setEnduranceActualTime(1.5);
|
|
BatteryEndurance endurance3 = new BatteryEndurance();
|
endurance3.setEnduranceActualTime(2.5);
|
|
BatteryEndurance endurance4 = new BatteryEndurance();
|
endurance4.setEnduranceActualTime(3.5);
|
|
List<BatteryEndurance> batteryEnduranceList = Arrays.asList(endurance, endurance2, endurance3, endurance4);
|
|
List<Integer> timeList = Arrays.asList(1, 2, 3);
|
|
Map map = enduranceAnalysis(batteryEnduranceList, timeList);
|
|
}
|
|
}
|