swb
2024-06-27 db153ac7fbbd132e0b850c1ebc93122da5fe99e0
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
153
package com.moral.api.service.impl;
 
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.entity.DustldDTO;
import com.moral.api.entity.HistorySecondCruiser;
import com.moral.api.entity.SecondCruiserSort;
import com.moral.api.entity.SysDictData;
import com.moral.api.mapper.HistorySecondCruiserMapper;
import com.moral.api.mapper.ManageCoordinateDetailMapper;
import com.moral.api.mapper.OrganizationMapper;
import com.moral.api.mapper.SecondCruiserSortMapper;
import com.moral.api.service.HistorySecondCruiserService;
import com.moral.api.service.SysDictTypeService;
import com.moral.util.DateUtils;
 
/**
 * Description //todo
 *
 * @author swb
 * @ClassName HistorySecondCruiserServiceImpl
 * @date 2024.06.25 14:41
 */
@Service
@Slf4j
public class HistorySecondCruiserServiceImpl extends ServiceImpl<HistorySecondCruiserMapper, HistorySecondCruiser> implements HistorySecondCruiserService {
    @Autowired
    private SysDictTypeService sysDictTypeService;
 
    @Autowired
    private HistorySecondCruiserMapper historySecondCruiserMapper;
    @Autowired
    private OrganizationMapper organizationMapper;
    @Autowired
    private ManageCoordinateDetailMapper manageCoordinateDetailMapper;
    @Autowired
    private SecondCruiserSortMapper secondCruiserSortMapper;
 
 
    @Override
    @Transactional
    public void sort(String startTime, String endTime) {
 
        if (ObjectUtils.isEmpty(startTime)&&ObjectUtils.isEmpty(endTime)){
            //当前时间的前一天
            startTime = DateUtils.getDateStringOfDay(-1, DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
            //获取当前时间
            endTime = DateUtils.getCurDate(DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
        }
        SysDictData list = sysDictTypeService.listOne("ZHC", "dustld");
        List<HistorySecondCruiser> result = historySecondCruiserMapper.getResult(startTime, endTime);
 
        //
        ArrayList<SecondCruiserSort> list1 = new ArrayList<>();
        if (!ObjectUtils.isEmpty(result)){
            for (HistorySecondCruiser historySecondCruiser : result) {
                String mac = historySecondCruiser.getMac();
                Integer organizationId = historySecondCruiser.getOrganizationId();
                List<Integer> orgList = organizationMapper.orgIdSpecialDevList(organizationId, mac);
                if(CollectionUtils.isEmpty(orgList)){
                    return;
                }
 
                HashMap<String, Object> params = new HashMap<>();
                params.put("mac",mac);
                params.put("startTime",startTime);
                params.put("endTime",endTime);
                List<Map<String, Object>> dusts = historySecondCruiserMapper.getDusts(params);
                Map<String, List<DustldDTO>> collect = manageCoordinateDetailMapper.CompareTo(orgList.get(0)).stream().collect(Collectors.groupingBy(o -> o.getName()));
                if (ObjectUtils.isEmpty(dusts) || ObjectUtils.isEmpty(collect)){
                    return;
                }
                Set<String> strings = collect.keySet();
                for (String string : strings) {
                    SecondCruiserSort secondCruiserSort = new SecondCruiserSort();
                    ArrayList<Double> doubleArrayList = new ArrayList<>();
                    List<DustldDTO> dustldDTOS = collect.get(string);
                    for (DustldDTO dustldDTO : dustldDTOS) {
                        String flyLat = dustldDTO.getFlyLat();
                        String flyLon = dustldDTO.getFlyLon();
                        if (flyLon==null && flyLat==null){
                            continue;
                        }
                        double latDouble1 = Double.parseDouble(flyLat);
                        double lonDouble1 = Double.parseDouble(flyLon);
                        for (Map<String, Object> dust : dusts) {
                            String flyLat1 = Objects.nonNull(dust.get("flyLat")) ? dust.get("flyLat").toString() :"0";
                            String flyLon1 = Objects.nonNull(dust.get("flyLon")) ? dust.get("flyLon").toString() :"0";
                            double latDouble = Double.parseDouble(flyLat1);
                            double lonDouble = Double.parseDouble(flyLon1);
//                    String flyLon1 = dust.get("flyLon").toString();
                            if (latDouble1==latDouble && lonDouble1==lonDouble){
                                Double dustld = Objects.nonNull(dust.get("dustld"))?Double.parseDouble(dust.get("dustld").toString()):0d;
                                if(list.getDataValue().contains(",")){
                                    List<String> resultStr = Arrays.asList(list.getDataValue().split(","));
                                    if(resultStr.size() % 2 ==0){
                                        dustld = numAvg(resultStr,BigDecimal.valueOf(dustld)).doubleValue();
                                    }
                                }else {
                                    BigDecimal dataValue = Objects.nonNull(list.getDataValue())?BigDecimal.valueOf(Double.parseDouble(list.getDataValue())):BigDecimal.ZERO;
                                    dustld = BigDecimal.valueOf(dustld).add(dataValue).doubleValue();
                                }
                                doubleArrayList.add(dustld);
                                break;
                            }
                        }
                    }
                    if (ObjectUtils.isEmpty(doubleArrayList)){
                        continue;
                    }
                    Double ListAva = doubleArrayList.stream() .collect(Collectors.averagingDouble(Double::doubleValue));
                    double rsAvg = new BigDecimal(ListAva/1000).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
                    secondCruiserSort.setRoad(string);
                    secondCruiserSort.setValue(rsAvg);
                    secondCruiserSort.setMac(mac);
                    secondCruiserSort.setOrganizationId(organizationId);
                    list1.add(secondCruiserSort);
                }
            }
        }
        if (!ObjectUtils.isEmpty(list1)){
            secondCruiserSortMapper.insertAll(list1);
        }
 
    }
 
    private BigDecimal numAvg(List<String> list , BigDecimal num){
        int nums = 1;
        for (int i=0;i<list.size();i=i+2){
            if(num.compareTo(BigDecimal.valueOf(Double.parseDouble(list.get(i))))>= 0 ){
                return num.add(BigDecimal.valueOf(Double.parseDouble(list.get(i+1))));
            }
            nums+=2;
        }
        return num;
    }
}