puzhibing
2026-09-30 67d25f3d9eb0124c7aab544bf8f77031d6d8adc3
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package com.ruoyi.other.controller;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.account.api.model.TVinManagement;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.Logical;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.other.api.domain.TFleet;
import com.ruoyi.other.api.domain.TGroupCompany;
import com.ruoyi.other.api.dto.FleetExportDTO;
import com.ruoyi.other.api.dto.FleetImportDTO;
import com.ruoyi.other.api.dto.FleetQueryDTO;
import com.ruoyi.other.service.CompanyAccountAccessService;
import com.ruoyi.other.service.TFleetService;
import com.ruoyi.other.service.TGroupCompanyService;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
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.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 车队管理 前端控制器
 * </p>
 *
 * @author mxcd
 * @since 2026-09-22
 */
@RestController
@RequestMapping("/fleet")
public class TFleetController {
 
    @Resource
    private TFleetService fleetService;
 
    @Resource
    private TGroupCompanyService groupCompanyService;
 
    @Resource
    private CompanyAccountAccessService companyAccountAccessService;
 
    /**
     * 分页查询车队
     */
    @RequiresPermissions(value = {"/fleet/list", "/vinManagement/list", "/chargingCard/list"}, logical = Logical.OR)
    @GetMapping("/page")
    @ApiOperation(value = "分页查询车队", tags = {"管理后台-车队管理"})
    public AjaxResult<PageInfo<TFleet>> page(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                             @RequestParam(value = "size", defaultValue = "10") Integer size,
                                             @RequestParam(value = "fleetName", required = false) String fleetName,
                                             @RequestParam(value = "companyId", required = false) Integer companyId,
                                             @RequestParam(value = "status", required = false) Integer status) {
        // 公司账号强制按所属公司过滤,覆盖前端传入的公司筛选
        companyId = companyAccountAccessService.resolveCompanyFilter(companyId);
        PageInfo<TFleet> page = new PageInfo<>(current, size);
        fleetService.lambdaQuery()
                .like(StringUtils.hasText(fleetName), TFleet::getFleetName, fleetName)
                .eq(companyId != null, TFleet::getCompanyId, companyId)
                .eq(status != null, TFleet::getStatus, status)
                .orderByDesc(TFleet::getId)
                .page(page);
        fillCompanyName(page.getRecords());
        // 填充VIN车辆数/可充电站数/累计三列聚合
        fleetService.fillAggregates(page.getRecords());
        return AjaxResult.success(page);
    }
 
    /**
     * 添加车队
     */
    @RequiresPermissions(value = {"/fleet/list"}, logical = Logical.OR)
    @PostMapping("/add")
    @ApiOperation(value = "添加车队", tags = {"管理后台-车队管理"})
    @Log(title = "【车队管理】添加车队", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody TFleet fleet) {
        // 公司账号新增车队时强制归属本公司
        fleet.setCompanyId(companyAccountAccessService.resolveCompanyFilter(fleet.getCompanyId()));
        if (fleet.getCompanyId() == null) {
            return AjaxResult.error("所属公司不能为空");
        }
        if (!StringUtils.hasText(fleet.getFleetName())) {
            return AjaxResult.error("车队名称不能为空");
        }
        fleet.setId(null);
        fleet.setDelFlag(false);
        if (fleet.getStatus() == null) {
            fleet.setStatus(1);
        }
        fleetService.save(fleet);
        return AjaxResult.success();
    }
 
    /**
     * 编辑车队
     */
    @RequiresPermissions(value = {"/fleet/list"}, logical = Logical.OR)
    @PostMapping("/edit")
    @ApiOperation(value = "编辑车队", tags = {"管理后台-车队管理"})
    @Log(title = "【车队管理】编辑车队", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@RequestBody TFleet fleet) {
        if (fleet.getId() == null) {
            return AjaxResult.error("车队ID不能为空");
        }
        TFleet existing = fleetService.getById(fleet.getId());
        if (existing == null) {
            return AjaxResult.error("车队不存在或已删除");
        }
        // 公司账号越权校验:只能编辑本公司车队,且不允许改挂到其他公司
        companyAccountAccessService.assertCompanyVisible(existing.getCompanyId());
        fleet.setCompanyId(companyAccountAccessService.resolveCompanyFilter(fleet.getCompanyId()));
        fleetService.updateById(fleet);
        return AjaxResult.success();
    }
 
    /**
     * 删除车队(支持逗号分隔批量删除)
     */
    @RequiresPermissions(value = {"/fleet/list"}, logical = Logical.OR)
    @DeleteMapping("/del")
    @ApiOperation(value = "删除车队", tags = {"管理后台-车队管理"})
    @Log(title = "【车队管理】删除车队", businessType = BusinessType.DELETE)
    public AjaxResult del(@RequestParam("ids") String ids) {
        if (!StringUtils.hasText(ids)) {
            return AjaxResult.error("车队ID不能为空");
        }
        List<Integer> idList = java.util.Arrays.stream(ids.split(","))
                .map(String::trim)
                .filter(StringUtils::hasText)
                .map(Integer::valueOf)
                .collect(Collectors.toList());
        if (idList.isEmpty()) {
            return AjaxResult.error("车队ID不能为空");
        }
        // 公司账号越权校验:只能删除本公司车队
        List<TFleet> records = fleetService.listByIds(idList);
        if (records == null || records.size() != idList.size()) {
            return AjaxResult.error("部分车队不存在或已删除");
        }
        for (TFleet record : records) {
            companyAccountAccessService.assertCompanyVisible(record.getCompanyId());
        }
        fleetService.deleteFleets(idList);
        return AjaxResult.success();
    }
 
    /**
     * 切换车队启用状态
     */
    @RequiresPermissions(value = {"/fleet/list"}, logical = Logical.OR)
    @PostMapping("/status")
    @ApiOperation(value = "切换车队启用状态", tags = {"管理后台-车队管理"})
    @Log(title = "【车队管理】切换车队状态", businessType = BusinessType.UPDATE)
    public AjaxResult status(@RequestParam("id") Integer id, @RequestParam("status") Integer status) {
        if (id == null) {
            return AjaxResult.error("车队ID不能为空");
        }
        TFleet record = fleetService.getById(id);
        if (record == null) {
            return AjaxResult.error("车队不存在或已删除");
        }
        // 公司账号越权校验:只能操作本公司车队
        companyAccountAccessService.assertCompanyVisible(record.getCompanyId());
        TFleet update = new TFleet();
        update.setId(id);
        update.setStatus(status);
        fleetService.updateById(update);
        return AjaxResult.success();
    }
 
    /**
     * 导入车队(Excel)
     */
    @RequiresPermissions(value = {"/fleet/import"}, logical = Logical.OR)
    @PostMapping("/import")
    @ApiOperation(value = "导入车队", tags = {"管理后台-车队管理"})
    @Log(title = "【车队管理】导入车队", businessType = BusinessType.IMPORT)
    public AjaxResult<String> importData(@RequestPart("file") MultipartFile file) throws Exception {
        List<FleetImportDTO> items = new ExcelUtil<>(FleetImportDTO.class).importExcel(file.getInputStream());
        return AjaxResult.success(fleetService.importFleets(items));
    }
 
    /**
     * 下载车队导入模板
     */
    @RequiresPermissions(value = {"/fleet/import"}, logical = Logical.OR)
    @GetMapping("/import-template")
    @ApiOperation(value = "下载车队导入模板", tags = {"管理后台-车队管理"})
    public void template(HttpServletResponse response) {
        new ExcelUtil<>(FleetImportDTO.class).importTemplateExcel(response, "车队导入模板");
    }
 
    /**
     * 导出车队(Excel,按筛选条件)
     */
    @RequiresPermissions(value = {"/fleet/export"}, logical = Logical.OR)
    @PutMapping("/export")
    @ApiOperation(value = "导出车队", tags = {"管理后台-车队管理"})
    @Log(title = "【车队管理】导出车队", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, @RequestBody(required = false) FleetQueryDTO query) {
        if (query == null) {
            query = new FleetQueryDTO();
        }
        // 公司账号强制按所属公司过滤,覆盖前端传入的公司筛选
        query.setCompanyId(companyAccountAccessService.resolveCompanyFilter(query.getCompanyId()));
        new ExcelUtil<>(FleetExportDTO.class).exportExcel(response, fleetService.selectExportList(query), "车队数据");
    }
 
    /**
     * 查询车队详情(查看按钮使用)
     */
    @RequiresPermissions(value = {"/fleet/list"}, logical = Logical.OR)
    @GetMapping("/detail")
    @ApiOperation(value = "查询车队详情", tags = {"管理后台-车队管理"})
    public AjaxResult<TFleet> detail(@RequestParam("id") Integer id) {
        if (id == null) {
            return AjaxResult.error("车队ID不能为空");
        }
        TFleet fleet = fleetService.getById(id);
        if (fleet == null) {
            return AjaxResult.error("车队不存在或已删除");
        }
        // 公司账号越权校验:只能查看本公司车队
        companyAccountAccessService.assertCompanyVisible(fleet.getCompanyId());
        if (fleet.getCompanyId() != null) {
            TGroupCompany company = groupCompanyService.getById(fleet.getCompanyId());
            if (company != null) {
                fleet.setCompanyName(company.getName());
            }
        }
        // 详情亦填充5列聚合(保持与列表一致)
        fleetService.fillAggregates(java.util.Collections.singletonList(fleet));
        return AjaxResult.success(fleet);
    }
 
    /**
     * 分页查询车队下的VIN码车辆(查看车队抽屉使用)
     */
    @RequiresPermissions(value = {"/fleet/list"}, logical = Logical.OR)
    @GetMapping("/vin-page")
    @ApiOperation(value = "分页查询车队下VIN码车辆", tags = {"管理后台-车队管理"})
    public AjaxResult<Page<TVinManagement>> vinPage(@RequestParam("id") Integer id,
                                                    @RequestParam(value = "current", defaultValue = "1") Integer current,
                                                    @RequestParam(value = "size", defaultValue = "10") Integer size) {
        TFleet fleet = fleetService.getById(id);
        if (fleet == null) {
            return AjaxResult.error("车队不存在或已删除");
        }
        // 公司账号越权校验:只能查看本公司车队下的VIN
        companyAccountAccessService.assertCompanyVisible(fleet.getCompanyId());
        return AjaxResult.success(fleetService.selectVinPage(id, current, size));
    }
 
    /**
     * 按公司ID批量回填公司名称
     */
    private void fillCompanyName(List<TFleet> records) {
        if (records == null || records.isEmpty()) {
            return;
        }
        List<Integer> companyIds = records.stream()
                .map(TFleet::getCompanyId)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
        if (companyIds.isEmpty()) {
            return;
        }
        Map<Integer, String> nameMap = groupCompanyService.listByIds(companyIds).stream()
                .collect(Collectors.toMap(TGroupCompany::getId, TGroupCompany::getName, (a, b) -> a));
        records.forEach(item -> item.setCompanyName(nameMap.get(item.getCompanyId())));
    }
 
    /**
     * 远程接口:车队详情(VIN码车辆所属车队校验使用,内部调用)
     */
    @PostMapping("/remote/get/{id}")
    @ApiOperation(value = "远程-车队详情", tags = {"内部调用"})
    public R<TFleet> remoteGet(@PathVariable("id") Integer id) {
        if (id == null) {
            return R.fail("车队ID不能为空");
        }
        return R.ok(fleetService.getById(id));
    }
 
    /**
     * 远程接口:按ID批量查询车队(VIN列表回填车队名称使用,内部调用)
     */
    @PostMapping("/remote/list-by-ids")
    @ApiOperation(value = "远程-按ID批量查询车队", tags = {"内部调用"})
    public R<List<TFleet>> remoteListByIds(@RequestBody List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return R.ok(new ArrayList<>());
        }
        List<Integer> distinct = ids.stream().filter(Objects::nonNull).distinct().collect(Collectors.toList());
        if (distinct.isEmpty()) {
            return R.ok(new ArrayList<>());
        }
        return R.ok(fleetService.listByIds(distinct));
    }
 
}