kaiyu
2021-09-08 9cc9f173f8db89135610eba2c8af79c17b24dfdf
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.moral.api.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.HistorySecondUav;
import com.moral.api.entity.Organization;
import com.moral.api.mapper.HistorySecondUavMapper;
import com.moral.api.pojo.dto.uav.UAVQueryTimeSlotDTO;
import com.moral.api.pojo.form.uav.UAVQueryTimeSlotForm;
import com.moral.api.service.HistorySecondUavService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.service.OrganizationService;
import com.moral.api.service.SpecialDeviceService;
import com.moral.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.*;
import java.util.function.Predicate;
 
/**
 * <p>
 * 无人机秒数据表 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-08-31
 */
@Service
public class HistorySecondUavServiceImpl extends ServiceImpl<HistorySecondUavMapper, HistorySecondUav> implements HistorySecondUavService {
 
    @Autowired
    HistorySecondUavMapper historySecondUavMapper;
    @Autowired
    OrganizationService organizationService;
    @Autowired
    SpecialDeviceService specialDeviceService;
 
    @Override
    public List<Date> queryDate(Integer organizationId) {
        //构造查询条件
        QueryWrapper<HistorySecondUav> queryWrapper = new QueryWrapper<>();
        //获取子组织id
        List<Organization> children = organizationService.getChildrenOrganizationsById(organizationId);
        List<Integer> childrenId = new ArrayList<>();
        for (Organization child : children) {
            childrenId.add(child.getId());
        }
        childrenId.add(organizationId);
        queryWrapper.in("organization_id",childrenId);
        //设置查询时间范围为180天
        Date endDate = new Date();
        Date startDate = DateUtils.addDays(endDate, -180);
        queryWrapper.between("batch",startDate,endDate);
        //设置查询字段
        queryWrapper.select("DISTINCT batch");
        //查询结果
        List<HistorySecondUav> historySecondUavs = historySecondUavMapper.selectList(queryWrapper);
        //结果转为Date集合
        List<Date> result = new ArrayList<>();
        for (HistorySecondUav historySecondUav : historySecondUavs) {
            result.add(historySecondUav.getBatch());
        }
        return result;
    }
 
    @Override
    public List<UAVQueryTimeSlotDTO> queryTimeSlot(UAVQueryTimeSlotForm form) {
        //取参
        Integer organizationId = form.getOrganizationId();
        Date startDate = form.getStartDate();
        Date endDate = form.getEndDate();
        QueryWrapper<HistorySecondUav> wrapper = new QueryWrapper<>();
        //获取子组织id
        List<Organization> children = organizationService.getChildrenOrganizationsById(organizationId);
        List<Integer> childrenId = new ArrayList<>();
        for (Organization child : children) {
            childrenId.add(child.getId());
        }
        childrenId.add(organizationId);
        wrapper.in("organization_id",childrenId);
        //查询根据batch查,因为可能会有跨天飞行的情况。
        wrapper.between("batch",startDate,endDate);
        //设置查询字段
        wrapper.select("mac,time,batch");
        //查询结果
        List<HistorySecondUav> historySecondUavs = historySecondUavMapper.selectList(wrapper);
        //根据batch进行分批
        Map<String,List<HistorySecondUav>> batchMap = new LinkedHashMap<>();//key为batch的string
        for (HistorySecondUav historySecondUav : historySecondUavs) {
            //获取batch对应的数据集合
            List<HistorySecondUav> list = batchMap.get(historySecondUav.getBatch().toString());
            if(list!=null){
                list.add(historySecondUav);
            }else{
                ArrayList<HistorySecondUav> newList = new ArrayList<>();
                newList.add(historySecondUav);
                batchMap.put(historySecondUav.getBatch().toString(),newList);
            }
        }
        //去除少于30条数据的集合
        batchMap.values().removeIf(new Predicate<List<HistorySecondUav>>() {
            @Override
            public boolean test(List<HistorySecondUav> historySecondUavs) {
                if(historySecondUavs.size()<=30)
                    return true;
                return false;
            }
        });
        //根据mac进行分类
        Map<String,List<Map<String,List<HistorySecondUav>>>> macBatchMap = new LinkedHashMap<>();//key为mac
        //遍历batchMap将mac提取出来,作为macBatchMap的key,batch和数据map的数据集合放入value
        batchMap.forEach((key,value)->{
            String mac = value.get(0).getMac();
            List<Map<String, List<HistorySecondUav>>> maps = macBatchMap.get(mac);
            if(maps!=null){
                Map<String,List<HistorySecondUav>> map = new LinkedHashMap<>();
                map.put(key,value);
                maps.add(map);
            }else{
                List<Map<String,List<HistorySecondUav>>> list = new ArrayList<>();
                Map<String,List<HistorySecondUav>> map = new LinkedHashMap<>();
                map.put(key,value);
                list.add(map);
                macBatchMap.put(value.get(0).getMac(),list);
            }
        });
        //封装返回数据
        List<UAVQueryTimeSlotDTO> dtos = new ArrayList<>();
        macBatchMap.forEach((key,value)->{
            UAVQueryTimeSlotDTO dto = new UAVQueryTimeSlotDTO();
            List<Map<String,Date>> timeSlots = new ArrayList<>();
            dto.setMac(key);
            //根据mac查询设备名称
            dto.setName((String) specialDeviceService.getSpecialDeviceMapByMac(key).get("name"));
            //获取时间段
            value.forEach(listValue->{
                listValue.forEach((mKey,mValue)->{
                    Date slotStartDate = mValue.get(0).getTime();
                    Date slotEndDate = mValue.get(mValue.size()-1).getTime();
                    Map<String,Date> dateMap = new HashMap<>();
                    dateMap.put("startTime",slotStartDate);
                    dateMap.put("endTime",slotEndDate);
                    timeSlots.add(dateMap);
                });
            });
            dto.setTimeSlot(timeSlots);
            dtos.add(dto);
        });
      return dtos;
    }
 
}