puzhibing
2026-09-23 488f2ca889255b55c085d3acc02ed1dd0e029a66
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
package com.ruoyi.other.service.impl;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.exception.ServiceException;
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.mapper.TFleetMapper;
import com.ruoyi.other.service.TFleetService;
import com.ruoyi.other.service.TGroupCompanyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
 
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 车队管理 服务实现类
 * </p>
 *
 * @author mxcd
 * @since 2026-09-22
 */
@Service
public class TFleetServiceImpl extends ServiceImpl<TFleetMapper, TFleet> implements TFleetService {
 
    private static final int ENABLED = 1;
    private static final int DISABLED = 0;
 
    @Resource
    private TGroupCompanyService groupCompanyService;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importFleets(List<FleetImportDTO> items) {
        if (items == null || items.isEmpty()) {
            throw new ServiceException("导入数据不能为空");
        }
        Set<String> fleetNames = new HashSet<>();
        Set<String> companyNames = new HashSet<>();
        int row = 1;
        for (FleetImportDTO item : items) {
            row++;
            if (item == null || !StringUtils.hasText(item.getFleetName())) {
                throw new ServiceException("第" + row + "行车队名称不能为空");
            }
            String fleetName = item.getFleetName().trim();
            if (fleetName.length() > 128) {
                throw new ServiceException("第" + row + "行车队名称不能超过128个字符");
            }
            if (!StringUtils.hasText(item.getCompanyName())) {
                throw new ServiceException("第" + row + "行所属公司不能为空");
            }
            parseStatus(item.getStatus(), row);
            if (!fleetNames.add(fleetName) || lambdaQuery().eq(TFleet::getFleetName, fleetName).count() > 0) {
                throw new ServiceException("第" + row + "行车队名称已存在或重复");
            }
            companyNames.add(item.getCompanyName().trim());
        }
        // 批量查询公司名称 -> ID
        Map<String, Integer> companyMap = groupCompanyService.lambdaQuery()
                .in(TGroupCompany::getName, companyNames)
                .list()
                .stream()
                .collect(Collectors.toMap(TGroupCompany::getName, TGroupCompany::getId, (a, b) -> a));
        row = 1;
        for (FleetImportDTO item : items) {
            row++;
            String companyName = item.getCompanyName().trim();
            if (!companyMap.containsKey(companyName)) {
                throw new ServiceException("第" + row + "行所属公司不存在:" + companyName);
            }
        }
        LocalDateTime now = LocalDateTime.now();
        for (FleetImportDTO item : items) {
            TFleet fleet = new TFleet();
            fleet.setCompanyId(companyMap.get(item.getCompanyName().trim()));
            fleet.setFleetName(item.getFleetName().trim());
            fleet.setContactName(trim(item.getContactName()));
            fleet.setContactPhone(trim(item.getContactPhone()));
            fleet.setStatus(parseStatus(item.getStatus(), 0));
            fleet.setDelFlag(false);
            fleet.setCreateTime(now);
            save(fleet);
        }
        return "成功导入" + items.size() + "条车队数据";
    }
 
    @Override
    public List<FleetExportDTO> selectExportList(FleetQueryDTO query) {
        FleetQueryDTO safe = query == null ? new FleetQueryDTO() : query;
        List<TFleet> fleets = lambdaQuery()
                .like(StringUtils.hasText(safe.getFleetName()), TFleet::getFleetName, safe.getFleetName())
                .eq(safe.getCompanyId() != null, TFleet::getCompanyId, safe.getCompanyId())
                .eq(safe.getStatus() != null, TFleet::getStatus, safe.getStatus())
                .orderByDesc(TFleet::getId)
                .list();
        if (fleets == null || fleets.isEmpty()) {
            return new ArrayList<>();
        }
        Set<Integer> companyIds = fleets.stream()
                .map(TFleet::getCompanyId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        Map<Integer, String> nameMap = companyIds.isEmpty() ? Collections.emptyMap()
                : groupCompanyService.listByIds(companyIds).stream()
                        .collect(Collectors.toMap(TGroupCompany::getId, TGroupCompany::getName, (a, b) -> a));
        return fleets.stream().map(item -> {
            FleetExportDTO dto = new FleetExportDTO();
            dto.setFleetName(item.getFleetName());
            dto.setCompanyName(nameMap.get(item.getCompanyId()));
            // VIN码车数/可充电站数/月充电/当前可用余额后端暂无数据源,导出留空,与列表"-"占位一致
            dto.setStatus(item.getStatus() == null ? null : String.valueOf(item.getStatus()));
            return dto;
        }).collect(Collectors.toList());
    }
 
    private String trim(String value) {
        return value == null ? null : value.trim();
    }
 
    private Integer parseStatus(String value, int row) {
        if (!StringUtils.hasText(value) || "1".equals(value.trim()) || "启用".equals(value.trim())) {
            return ENABLED;
        }
        if ("0".equals(value.trim()) || "停用".equals(value.trim())) {
            return DISABLED;
        }
        throw new ServiceException((row > 0 ? "第" + row + "行" : "") + "启用状态错误");
    }
 
}