whyclxw
2022-01-10 00b61a61b2a90c85aeae872e351ddf4bb2b516cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whyc.dto.BattInfAndEnduranceDTO;
import com.whyc.dto.Response;
import com.whyc.mapper.BattEnduranceMapper;
import com.whyc.pojo.BattEndurance;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.*;
 
@Service
public class BattEnduranceService {
    @Resource
    private BattEnduranceMapper mapper;
 
    public Response getEndurance(int uId){
        List<BattEndurance> enduranceList = mapper.endurance(uId);
        List<Integer> times = Arrays.asList(1,2,3,5,8);
        Map resultMap = enduranceAnalysis(enduranceList,times);
        return new Response().set(1,resultMap,"查询成功");
    }
 
    public Response enableEndurance(){
        mapper.enableEndurance();
        return new Response().set(1,true,"启动续航线程成功");
    }
 
    /**按时间阶段统计续航*/
    private static Map enduranceAnalysis(List<BattEndurance> enduranceList, List<Integer> times) {
        Map<String,Integer> map = new LinkedHashMap<>();
 
        //初始化续航分类
        for (int i = 0; i < times.size(); i++) {
            if(i ==0 ){
                map.put("续航"+times.get(0)+"小时内",0);
            }
            else if(i == times.size()-1){
                map.put("续航"+times.get(i-1)+"小时到"+times.get(i)+"小时",0);
                map.put("续航"+times.get(times.size()-1)+"小时以上",0);
            }else {
                map.put("续航" + times.get(i-1) + "小时到" + times.get(i) + "小时", 0);
            }
        }
 
        for (int i = 0; i < enduranceList.size(); i++) {
            //每一个具体的续航进行分组
            if(enduranceList.get(i).getEnduranceActualTimelong()<times.get(0)*60){
                map.put("续航"+times.get(0)+"小时内", Optional.ofNullable(map.get("续航"+times.get(0)+"小时内")).orElse(0)+1);
            }
            else if(enduranceList.get(i).getEnduranceActualTimelong()>times.get(times.size()-1)*60){
                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).getEnduranceActualTimelong() <= times.get(j)*60) {
                        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 getBattInfEnduranceByTimeCode(int uId,int timeCode){
        List<BattInfAndEnduranceDTO> list = mapper.getBattInfEnduranceByTimeCode(uId,timeCode);
        return new Response().set(1,list,"查询成功");
    }
 
 
    public Response getEnduranceTimeByDeviceId(int deviceId){
        QueryWrapper<BattEndurance> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deviceid",deviceId);
        BattEndurance battEndurance = mapper.selectOne(queryWrapper);
        float timeLong = 0.0f;
        if (battEndurance==null){
            timeLong = -1f;
        }else {
            timeLong = battEndurance.getEnduranceActualTimelong();
        }
        return new Response().set(1,timeLong,"查询成功");
    }
 
 
 
}