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
package com.ruoyi.system.controller;
 
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.dataInterchange.api.feignClient.UPExgMsgRealLocationClient;
import com.ruoyi.dataInterchange.api.vo.GnssDataVo;
import com.ruoyi.system.api.model.Car;
import com.ruoyi.system.api.model.Warn;
import com.ruoyi.system.query.CarWarnInfoResp;
import com.ruoyi.system.query.CarWarnListReq;
import com.ruoyi.system.query.CarWarnListResp;
import com.ruoyi.system.query.WarnResp;
import com.ruoyi.system.service.ICarService;
import com.ruoyi.system.service.IWarnService;
import com.ruoyi.system.util.GDMapGeocodingUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
/**
 * @author zhibing.pu
 * @Date 2025/3/27 14:38
 */
@RestController
@RequestMapping("/warn")
public class WarnController {
    
    
    @Resource
    private IWarnService warnService;
    
    @Resource
    private ICarService carService;
    
    
    @Resource
    private UPExgMsgRealLocationClient upExgMsgRealLocationClient;
    
    
    @GetMapping("/getCarWarnList")
    @ApiOperation(value = "获取车辆预警列表数据", tags = {"车辆管理", "预警记录", "首页"})
    public R<PageInfo<CarWarnListResp>> getCarWarnList(CarWarnListReq carWarnListReq) {
        PageInfo<CarWarnListResp> pageInfo = warnService.getCarWarnList(carWarnListReq);
        return R.ok(pageInfo);
    }
    
    
    @GetMapping("/exportCarWarnList")
    @ApiOperation(value = "导出车辆预警列表数据", tags = {"预警记录"})
    public void exportCarWarnList(CarWarnListReq carWarnListReq, HttpServletResponse response) {
        carWarnListReq.setPageSize(99999);
        PageInfo<CarWarnListResp> pageInfo = warnService.getCarWarnList(carWarnListReq);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), CarWarnListResp.class, pageInfo.getRecords());
        response.setCharacterEncoding("utf-8");
        ServletOutputStream outputStream = null;
        try {
            String fileName = URLEncoder.encode("预警记录.xls", "utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                workbook.close();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    
    @GetMapping("/getCarWarnInfo")
    @ApiOperation(value = "获取车辆预警详情数据", tags = {"车辆管理", "预警记录"})
    public R<CarWarnInfoResp> getCarWarnInfo(String vehicleNumber) {
        Car car = carService.getOne(new LambdaQueryWrapper<Car>().eq(Car::getVehicleNumber, vehicleNumber));
        if (null == car) {
            return R.ok();
        }
        String time = LocalDateTime.now().minusMinutes(15).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        List<Warn> list = warnService.list(new LambdaQueryWrapper<Warn>().eq(Warn::getCarId, car.getId()).lt(Warn::getStartTime, time).orderByDesc(Warn::getCreateTime));
        CarWarnInfoResp carWarnInfoResp = new CarWarnInfoResp();
        carWarnInfoResp.setVehicleNumber(vehicleNumber);
        GnssDataVo gnssDataVo = upExgMsgRealLocationClient.getVehicleSpeed(vehicleNumber).getData();
        carWarnInfoResp.setSpeed(new BigDecimal(gnssDataVo.getVec1()));
        Map<String, String> geocode = null;
        try {
            String longitude = new BigDecimal(gnssDataVo.getLon()).divide(new BigDecimal(1000000)).toString();
            String latitude = new BigDecimal(gnssDataVo.getLat()).divide(new BigDecimal(1000000)).toString();
            geocode = GDMapGeocodingUtil.geocode(longitude, latitude);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        String address = geocode.get("address");
        carWarnInfoResp.setNowAddress(address);
        List<WarnResp> warnList = new ArrayList<>();
        for (Warn warn : list) {
            WarnResp warnResp = new WarnResp();
            warnResp.setWarnType(warn.getWarnType());
            warnResp.setWarnTime(warn.getStartTime());
            warnResp.setSpeed(warn.getSpeed());
            warnResp.setLon(new BigDecimal(warn.getLongitude()));
            warnResp.setLat(new BigDecimal(warn.getLatitude()));
            warnResp.setAddress(warn.getAddress());
            warnList.add(warnResp);
        }
        carWarnInfoResp.setWarnList(warnList);
        return R.ok(carWarnInfoResp);
    }
    
    @GetMapping("/getWarnGroupCount")
    @ApiOperation(value = "获取预警情况统计", tags = {"首页"})
    public R<List<Map<String, Object>>> getWarnGroupCount() {
        List<Map<String, Object>> warnGroupCount = warnService.getWarnGroupCount();
        return R.ok(warnGroupCount);
    }
    
    @GetMapping("/getWarnGroupCountTop10")
    @ApiOperation(value = "获取预警排行榜前10", tags = {"首页"})
    public R<List<Map<String, Object>>> getWarnGroupCountTop10() {
        List<Map<String, Object>> warnGroupCountTop10 = warnService.getWarnGroupCountTop10();
        return R.ok(warnGroupCountTop10);
    }
}