puzhibing
2026-05-17 993a6e6ea8ae047b9e5fffd249ae7adfcd650d3a
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
package com.xiaopeng.operation.service.impl;
 
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaopeng.api.feignClient.vehicleConvoy.XpVehicleClient;
import com.xiaopeng.api.model.account.SysUser;
import com.xiaopeng.api.model.operation.XpTargetedService;
import com.xiaopeng.api.model.operation.XpTargetedServiceVehicle;
import com.xiaopeng.api.model.operation.XpTargetedServiceWhitelist;
import com.xiaopeng.api.model.operation.XpWhitelist;
import com.xiaopeng.api.model.vehicleConvoy.XpVehicle;
import com.xiaopeng.api.query.operation.XpTargetedServiceQuery;
import com.xiaopeng.api.vo.operation.XpTargetedServiceVehicleVo;
import com.xiaopeng.api.vo.operation.XpTargetedServiceVo;
import com.xiaopeng.common.core.domain.R;
import com.xiaopeng.common.core.exception.ServiceException;
import com.xiaopeng.common.core.utils.SensitiveDataCryptoUtil;
import com.xiaopeng.common.core.web.page.PageInfo;
import com.xiaopeng.operation.mapper.XpTargetedServiceMapper;
import com.xiaopeng.operation.service.IXpWhitelistService;
import com.xiaopeng.operation.service.XpTargetedServiceService;
import com.xiaopeng.operation.service.XpTargetedServiceVehicleService;
import com.xiaopeng.operation.service.XpTargetedServiceWhitelistService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
 
@Service
public class XpTargetedServiceServiceImpl extends ServiceImpl<XpTargetedServiceMapper, XpTargetedService> implements XpTargetedServiceService {
 
    @Resource
    private XpTargetedServiceVehicleService xpTargetedServiceVehicleService;
    @Resource
    private XpTargetedServiceWhitelistService xpTargetedServiceWhitelistService;
 
    @Resource
    XpVehicleClient xpVehicleClient;
    @Resource
    XpTargetedServiceMapper xpTargetedServiceMapper;
 
    @Resource
    private IXpWhitelistService xpWhitelistService;
 
 
    public List<XpTargetedServiceVehicle> listByTargetedServiceId(Long targetedServiceId) {
        if (targetedServiceId == null) {
            return Collections.emptyList();
        }
        List<XpTargetedServiceVehicle> list = xpTargetedServiceMapper.listVehicle(targetedServiceId);
 
        if(!list.isEmpty()){
            List<Long> vehicleIds = new ArrayList<>();
            for (XpTargetedServiceVehicle xpTargetedServiceVehicle : list) {
                vehicleIds.add(xpTargetedServiceVehicle.getVehicleId());
            }
            R<List<XpVehicle>> listR = xpVehicleClient.listByIds(vehicleIds);
            if(!listR.getData().isEmpty()){
                List<XpVehicle> vehicleList = listR.getData();
                Map<Long, XpVehicle> vehicleMap = vehicleList.stream()
                        .collect(Collectors.toMap(XpVehicle::getId, vehicle -> vehicle));
 
                for (XpTargetedServiceVehicle entity : list) {
 
                    entity.setVehicleCode(vehicleMap.get(entity.getVehicleId()).getVehicleCode());
                    entity.setPlateNumber(vehicleMap.get(entity.getVehicleId()).getPlateNumber());
                    entity.setVehicleModelName(vehicleMap.get(entity.getVehicleId()).getVehicleModelName());
                    entity.setFleetName(vehicleMap.get(entity.getVehicleId()).getFleetName());
                }
            }
        }
        return list;
    }
 
    @Override
    public PageInfo<XpTargetedServiceVo> pageList(XpTargetedServiceQuery query) {
        PageInfo<XpTargetedServiceVo> pageInfo = new PageInfo<>(query.getPageNum(),query.getPageSize());
        List<XpTargetedServiceVo> list = this.baseMapper.pageList(pageInfo,query);
        pageInfo.setRecords(list);
        return pageInfo;
    }
    
    @Override
    public XpTargetedServiceVo getById(Long id) {
        XpTargetedService entity = baseMapper.selectById(id);
        if (entity == null) {
            return null;
        }
        XpTargetedServiceVo vo = new XpTargetedServiceVo();
        // 拷贝基础属性
        org.springframework.beans.BeanUtils.copyProperties(entity, vo);
        
        // 查询关联的车辆列表
        List<XpTargetedServiceVehicle> vehicleList = this.listByTargetedServiceId(id);
        vo.setVehicleList(vehicleList);
        
        // 查询关联的白名单列表
        List<XpTargetedServiceWhitelist> whitelistList = baseMapper.listWhitelist(id);
        vo.setWhitelistList(whitelistList);
        
        return vo;
    }
 
    @Transactional
    @Override
    public boolean add(XpTargetedServiceVo dto, SysUser sysUser){
        dto.setCreateTime(LocalDateTime.now());
        dto.setCreateBy(sysUser.getNickName());
        dto.setUpdateTime(LocalDateTime.now());
        dto.setUpdateBy(sysUser.getNickName());
        boolean b = save(dto);
        if(!b){
            throw new ServiceException("保存失败");
        }
 
        for (XpTargetedServiceVehicle entity : dto.getVehicleList()) {
            entity.setTargetedServiceId(dto.getId());
        }
         b = xpTargetedServiceVehicleService.saveBatch(dto.getVehicleList());
        if(!b){
            throw new ServiceException("保存失败");
        }
        for (XpTargetedServiceWhitelist entity : dto.getWhitelistList()) {
            entity.setTargetedServiceId(dto.getId());
        }
         b = xpTargetedServiceWhitelistService.saveBatch(dto.getWhitelistList());
        if(!b){
            throw new ServiceException("保存失败");
        }
        return true;
    }
 
 
    @Transactional
    @Override
    public boolean edit(XpTargetedServiceVo dto, SysUser sysUser){
        dto.setUpdateTime(LocalDateTime.now());
        dto.setUpdateBy(sysUser.getNickName());
        boolean b = updateById(dto);
        if(!b){
            throw new ServiceException("保存失败");
        }
 
        xpTargetedServiceVehicleService.deleteByTargetedServiceId(dto.getId());
        xpTargetedServiceWhitelistService.removeByTargetedServiceId(dto.getId());
 
        for (XpTargetedServiceVehicle entity : dto.getVehicleList()) {
            entity.setTargetedServiceId(dto.getId());
        }
        b = xpTargetedServiceVehicleService.saveBatch(dto.getVehicleList());
        if(!b){
            throw new ServiceException("保存失败");
        }
        for (XpTargetedServiceWhitelist entity : dto.getWhitelistList()) {
            entity.setTargetedServiceId(dto.getId());
        }
        b = xpTargetedServiceWhitelistService.saveBatch(dto.getWhitelistList());
        if(!b){
            throw new ServiceException("保存失败");
        }
        return true;
    }
    
    @Override
    public boolean updateStatus(Long id, Integer status, SysUser sysUser) {
        XpTargetedService entity = new XpTargetedService();
        entity.setId(id);
        entity.setStatus(status);
        entity.setUpdateBy(sysUser.getNickName());
        return this.updateById(entity);
    }
 
    @Override
    @Transactional
    public R deleteByIds(List<Long> ids, SysUser sysUser) {
//        List<XpTargetedService> list = new ArrayList<>();
//        for (Long id : ids) {
//            list.add(new XpTargetedService(){{
//                setId(id);
//                setUpdateBy(sysUser.getNickName());
//                setUpdateTime(LocalDateTime.now());
//                setDeleted(true);
//            }});
//
//        }
//        this.updateBatchById(list);
 
        removeBatchByIds(ids);
        return R.ok();
    }
 
    /**
     * 定向服务 根据 用户id 和 城市编码 查询 定向服务的 数据和 车辆信息
     * @param appUserId
     * @param cityCode
     * @return
     */
    @Override
    public List<String> findSetVehicleVinByUserIdAndCityCode(Long appUserId, String cityCode) {
        List<XpTargetedServiceVehicleVo> list = findListByUserIdAndCityCode(appUserId, cityCode);
        if(list.isEmpty()){
            return null;
        }
        return list.stream()
                .map(XpTargetedServiceVehicleVo::getVehicleVin)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
 
    }
 
    @Override
    public List<XpTargetedServiceVehicleVo> findListByUserIdAndCityCode(Long appUserId, String cityCode) {
 
        List<XpTargetedServiceVehicleVo> listInfoVehicle = baseMapper.findListInfoVehicle(appUserId, cityCode);
 
        if(!listInfoVehicle.isEmpty()){
            // 提取车辆ID列表
            List<Long> vehicleIds = listInfoVehicle.stream()
                    .map(XpTargetedServiceVehicleVo::getVehicleId)
                    .filter(Objects::nonNull)
                    .distinct()
                    .collect(Collectors.toList());
 
            R<List<XpVehicle>> listR = xpVehicleClient.listByIds(vehicleIds);
            if(!listR.getData().isEmpty()){
                List<XpVehicle> vehicleList = listR.getData();
                Map<Long, XpVehicle> vehicleMap = vehicleList.stream()
                        .collect(Collectors.toMap(XpVehicle::getId, vehicle -> vehicle));
 
                for (XpTargetedServiceVehicleVo beanVo : listInfoVehicle) {
                    XpVehicle remoteVehicle = vehicleMap.get(beanVo.getVehicleId());
                    if (remoteVehicle != null) {
                        String vin = SensitiveDataCryptoUtil.decryptIfPossible(remoteVehicle.getVehicleVin());
                        beanVo.setVehicleVin(vin);
                    }
                }
            }
            return listInfoVehicle;
        }
        return null;
    }
 
    @Override
    public Integer getTargetedServiceWeight(Long appUserId, String cityCode) {
        List<XpWhitelist> one = xpWhitelistService.list(new QueryWrapper<XpWhitelist>().eq("app_user_id", appUserId).eq("status", 1)
                .eq("is_deleted", 0).last(" and expire_time >= DATE(NOW()) or expire_time is null"));
        if(!one.isEmpty()){
            List<Long> collect = one.stream().map(XpWhitelist::getId).collect(Collectors.toList());
            List<XpTargetedServiceWhitelist> xpTargetedServiceWhitelists = xpTargetedServiceWhitelistService.list(new QueryWrapper<XpTargetedServiceWhitelist>().in("whitelist_id", collect));
            for (XpTargetedServiceWhitelist xpTargetedServiceWhitelist : xpTargetedServiceWhitelists) {
                XpTargetedService targetedServiceVo = this.getOne(new QueryWrapper<XpTargetedService>().eq("id", xpTargetedServiceWhitelist.getTargetedServiceId())
                        .eq("status", 1).eq("is_deleted", 0).eq("city_code", cityCode)
                        .last(" and (is_long_valid = 1 or now() between start_time and end_time)"));
                if(null != targetedServiceVo){
                    return xpTargetedServiceWhitelist.getWeight();
                }
            }
        }
        return null;
    }
}