Pu Zhibing
2025-04-07 4109495b9c51a4bbd8b0a7c3c69093909d2e33e1
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.ruoyi.system.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.dataInterchange.api.feignClient.UPWarnMsgAdptInfoClient;
import com.ruoyi.dataInterchange.api.model.enums.WarnType;
import com.ruoyi.dataInterchange.api.vo.UPWarnMsgAdptInfoVo;
import com.ruoyi.system.api.model.Car;
import com.ruoyi.system.api.model.Driver;
import com.ruoyi.system.api.model.Enterprise;
import com.ruoyi.system.api.model.Warn;
import com.ruoyi.system.mapper.WarnMapper;
import com.ruoyi.system.query.CarWarnListReq;
import com.ruoyi.system.query.CarWarnListResp;
import com.ruoyi.system.service.ICarService;
import com.ruoyi.system.service.IDriverService;
import com.ruoyi.system.service.IEnterpriseService;
import com.ruoyi.system.service.IWarnService;
import com.ruoyi.system.util.GDMapGeocodingUtil;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
 
 
/**
 * @author zhibing.pu
 * @Date 2025/3/27 14:36
 */
@Service
public class WarnServiceImpl extends ServiceImpl<WarnMapper, Warn> implements IWarnService {
    
    @Resource
    private UPWarnMsgAdptInfoClient upWarnMsgAdptInfoClient;
    
    @Resource
    private IEnterpriseService enterpriseService;
    
    @Resource
    private IDriverService driverService;
    
    @Resource
    private ICarService carService;
    
    
    /**
     * 存储新的报警数据
     */
    @Override
    public void taskSaveNewWarn() {
        Warn warn = this.getOne(new LambdaQueryWrapper<Warn>().orderByDesc(Warn::getCreateTime).last(" limit 1"));
        Long createTime = -1L;
        if (null != warn) {
            createTime = warn.getCreateTime().toEpochSecond(ZoneOffset.ofHours(8));
        }
        List<UPWarnMsgAdptInfoVo> list = upWarnMsgAdptInfoClient.findByCreateTimeAfter(createTime).getData();
        if (null == list || list.isEmpty()) {
            return;
        }
        List<Enterprise> list1 = enterpriseService.list();
        List<Driver> driverList = driverService.list(new LambdaQueryWrapper<Driver>().eq(Driver::getStatus, 1));
        List<Car> carList = carService.list();
        List<Warn> warns = new ArrayList<>();
        for (UPWarnMsgAdptInfoVo vo : list) {
            warn = new Warn();
            Optional<Car> carOptional = carList.stream().filter(s -> s.getVehicleNumber().equals(vo.getVehicleNo())).findFirst();
            if (carOptional.isPresent()) {
                warn.setCarId(carOptional.get().getId());
            }
            Optional<Driver> optional = driverList.stream().filter(s -> s.getVehicleNumber().equals(vo.getVehicleNo())).findFirst();
            if (optional.isPresent()) {
                warn.setDriverId(optional.get().getId());
            }
            Enterprise enterprise = list1.stream().filter(s -> s.getCode().equals(vo.getInferiorPlatformId().toString())).findFirst().get();
            warn.setEnterpriseId(enterprise.getId());
            warn.setStartTime(LocalDateTime.ofInstant(Instant.ofEpochMilli(vo.getCreateTime() * 1000), ZoneOffset.ofHours(8)).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            warn.setEndTime(LocalDateTime.ofInstant(Instant.ofEpochMilli(vo.getCreateTime() * 1000), ZoneOffset.ofHours(8)).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            warn.setWarnType(WarnType.getWarnTypeName(vo.getWarnType()));
            warn.setWarnNumber(1);
            warn.setSpeed(new BigDecimal(vo.getSpeed()));
            warn.setLongitude(new BigDecimal(vo.getLongitude()).divide(new BigDecimal(1000000)).toString());
            warn.setLatitude(new BigDecimal(vo.getLatitude()).divide(new BigDecimal(1000000)).toString());
            Map<String, String> geocode = null;
            try {
                geocode = GDMapGeocodingUtil.geocode(warn.getLongitude(), warn.getLatitude());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            warn.setAddress(geocode.get("address"));
            switch (vo.getResult()) {
                case 0x00:
                    warn.setTreatmentState("处理中");
                    break;
                case 0x01:
                    warn.setTreatmentState("已处理完毕");
                    break;
                case 0x02:
                    warn.setTreatmentState("不作处理");
                    break;
                case 0x03:
                    warn.setTreatmentState("将来处理");
                    break;
            }
            warn.setCreateTime(LocalDateTime.now());
            warn.setObjectId(vo.getInfoId());
            warns.add(warn);
        }
        if (warns.size() > 0) {
            this.saveBatch(warns);
        }
    }
    
    
    /**
     * 定时任务修改预警信息处理结果
     */
    @Override
    public void taskUpdateWarnStatus() {
        List<Warn> warnList = this.list(new LambdaQueryWrapper<Warn>().eq(Warn::getTreatmentState, "处理中"));
        for (Warn warn : warnList) {
            UPWarnMsgAdptInfoVo vo = upWarnMsgAdptInfoClient.findByInfoId(warn.getObjectId()).getData();
            if (null != vo && null != warn && vo.getResult() != 0x00) {
                switch (vo.getResult()) {
                    case 0x00:
                        warn.setTreatmentState("处理中");
                        break;
                    case 0x01:
                        warn.setTreatmentState("已处理完毕");
                        break;
                    case 0x02:
                        warn.setTreatmentState("不作处理");
                        break;
                    case 0x03:
                        warn.setTreatmentState("将来处理");
                        break;
                }
                this.updateById(warn);
            }
        }
        
    }
    
    
    /**
     * 获取车辆预警
     *
     * @param carWarnListReq
     * @return
     */
    @Override
    public PageInfo<CarWarnListResp> getCarWarnList(CarWarnListReq carWarnListReq) {
        PageInfo<CarWarnListResp> pageInfo = new PageInfo<>(carWarnListReq.getPageCurr(), carWarnListReq.getPageCurr());
        return this.baseMapper.getCarWarnList(pageInfo, carWarnListReq);
    }
    
    
    /**
     * 获取预警汇总统计最高的10类报警数据
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getWarnGroupCount() {
        List<Map<String, Object>> list = this.baseMapper.getWarnGroup10Count();
        List<String> collect = new ArrayList<>();
        for (Map<String, Object> map : list) {
            collect.add(map.get("warnType").toString());
        }
        if (!collect.isEmpty()) {
            List<Map<String, Object>> warnGroupCount = this.baseMapper.getWarnGroupCount(collect);
            list.addAll(warnGroupCount);
        }
        return list;
    }
    
    
    /**
     * 获取预警汇总前10排行榜
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getWarnGroupCountTop10() {
        return this.baseMapper.getWarnGroupCountTop10();
    }
}