wlzboy
4 天以前 cfe0b79fbea0fb1d7a5a796e71ada7d3b7812046
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
package com.ruoyi.web.controller.system;
 
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.VehicleMileageStats;
import com.ruoyi.system.service.IVehicleMileageStatsService;
 
/**
 * 车辆里程统计Controller
 */
@RestController
@RequestMapping("/system/mileageStats")
public class VehicleMileageStatsController extends BaseController {
    
    private static final Logger logger = LoggerFactory.getLogger(VehicleMileageStatsController.class);
    
    @Autowired
    private IVehicleMileageStatsService vehicleMileageStatsService;
 
    /**
     * 查询车辆里程统计列表
     */
    @PreAuthorize("@ss.hasPermi('system:mileageStats:list')")
    @GetMapping("/list")
    public TableDataInfo list(VehicleMileageStats vehicleMileageStats) {
        startPage();
        List<VehicleMileageStats> list = vehicleMileageStatsService.selectVehicleMileageStatsList(vehicleMileageStats);
        return getDataTable(list);
    }
 
    /**
     * 导出车辆里程统计列表
     */
    @PreAuthorize("@ss.hasPermi('system:mileageStats:export')")
    @Log(title = "车辆里程统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VehicleMileageStats vehicleMileageStats) {
        List<VehicleMileageStats> list = vehicleMileageStatsService.selectVehicleMileageStatsList(vehicleMileageStats);
        ExcelUtil<VehicleMileageStats> util = new ExcelUtil<VehicleMileageStats>(VehicleMileageStats.class);
        util.exportExcel(response, list, "车辆里程统计数据");
    }
 
    /**
     * 获取车辆里程统计详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:mileageStats:query')")
    @GetMapping(value = "/{statsId}")
    public AjaxResult getInfo(@PathVariable("statsId") Long statsId) {
        return success(vehicleMileageStatsService.selectVehicleMileageStatsById(statsId));
    }
 
    /**
     * 新增车辆里程统计
     */
    @PreAuthorize("@ss.hasPermi('system:mileageStats:add')")
    @Log(title = "车辆里程统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VehicleMileageStats vehicleMileageStats) {
        return toAjax(vehicleMileageStatsService.insertVehicleMileageStats(vehicleMileageStats));
    }
 
    /**
     * 修改车辆里程统计
     */
    @PreAuthorize("@ss.hasPermi('system:mileageStats:edit')")
    @Log(title = "车辆里程统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VehicleMileageStats vehicleMileageStats) {
        return toAjax(vehicleMileageStatsService.updateVehicleMileageStats(vehicleMileageStats));
    }
 
    /**
     * 删除车辆里程统计
     */
    @PreAuthorize("@ss.hasPermi('system:mileageStats:remove')")
    @Log(title = "车辆里程统计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{statsIds}")
    public AjaxResult remove(@PathVariable Long[] statsIds) {
        return toAjax(vehicleMileageStatsService.deleteVehicleMileageStatsByIds(statsIds));
    }
 
    /**
     * 手动触发计算指定车辆指定日期的里程统计
     */
    @PreAuthorize("@ss.hasPermi('system:mileageStats:calculate')")
    @Log(title = "车辆里程统计", businessType = BusinessType.OTHER)
    @PostMapping("/calculate")
    public AjaxResult calculate(Long vehicleId, String statDate) {
        try {
            if (statDate == null || statDate.trim().isEmpty()) {
                return error("统计日期不能为空");
            }
            
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            sdf.setLenient(false); // 严格解析日期
            Date date = sdf.parse(statDate.trim());
//            logger.info("---> 手动计算车辆里程汇总,车辆ID: {}, 日期: {}", vehicleId, date);
            VehicleMileageStats stats = vehicleMileageStatsService.calculateAndSaveMileageStats(vehicleId, date);
            // 修复String到Date转换问题,添加更好的错误处理
            if (stats != null) {
                return AjaxResult.success("里程统计计算成功", stats);
            } else {
                return error("该车辆在指定日期无GPS数据");
            }
        } catch (Exception e) {
            logger.error("里程统计计算失败 - 车辆ID: {}, 日期: {}", vehicleId, statDate, e);
            return error("里程统计计算失败:" + e.getMessage());
        }
    }
 
    /**
     * 批量计算指定日期所有车辆的里程统计
     */
    @PreAuthorize("@ss.hasPermi('system:mileageStats:batch')")
    @Log(title = "车辆里程统计", businessType = BusinessType.OTHER)
    @PostMapping("/batchCalculate")
    public AjaxResult batchCalculate(String statDate) {
        try {
            if (statDate == null || statDate.trim().isEmpty()) {
                return error("统计日期不能为空");
            }
            
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            sdf.setLenient(false); // 严格解析日期
            Date date = sdf.parse(statDate.trim());
            
            int count = vehicleMileageStatsService.batchCalculateMileageStats(date);
            return success("批量里程统计完成,成功统计 " + count + " 辆车");
        } catch (Exception e) {
            logger.error("批量里程统计失败 - 日期: {}", statDate, e);
            return error("批量里程统计失败:" + e.getMessage());
        }
    }
}