xuhy
2026-04-22 746517aca9d0b7ae334c70b3041a9b1de795addb
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
package com.xiaopeng.operation.service.impl;
 
 
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.vehicleConvoy.XpVehicle;
import com.xiaopeng.api.query.operation.XpTargetedServiceQuery;
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.web.page.PageInfo;
import com.xiaopeng.operation.mapper.XpTargetedServiceMapper;
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.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
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;
 
 
    public List<XpTargetedServiceVehicle> listByTargetedServiceId(Long targetedServiceId) {
        if (targetedServiceId == null) {
            return Collections.emptyList();
        }
        List<XpTargetedServiceVehicle> list = xpTargetedServiceMapper.listVehicle(targetedServiceId);
 
        if(!list.isEmpty()){
            List<Long> ids = new ArrayList<>();
            for (XpTargetedServiceVehicle xpTargetedServiceVehicle : list) {
                ids.add(xpTargetedServiceVehicle.getVehicleId());
            }
            R<List<XpVehicle>> listR = xpVehicleClient.listByIds(ids);
            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();
    }
}