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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package com.xiaopeng.operation.service.impl;
 
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaopeng.api.model.operation.XpOpenCity;
import com.xiaopeng.api.model.operation.XpPricing;
import com.xiaopeng.api.model.operation.XpRoutePlan;
import com.xiaopeng.common.core.exception.ServiceException;
import com.xiaopeng.common.core.web.page.PageInfo;
import com.xiaopeng.operation.constant.I18nKeyConstant;
import com.xiaopeng.operation.dto.OpenCityAddDTO;
import com.xiaopeng.operation.dto.OpenCityEditDTO;
import com.xiaopeng.operation.mapper.XpOpenCityMapper;
import com.xiaopeng.operation.query.OpenCityPageQuery;
import com.xiaopeng.operation.service.IXpPricingItemService;
import com.xiaopeng.operation.service.IXpRoutePlanService;
import com.xiaopeng.operation.service.XpOpenCityService;
import com.xiaopeng.operation.service.XpPricingService;
import com.xiaopeng.operation.vo.OpenCityListVO;
import com.xiaopeng.operation.vo.XpOpenCityNextVo;
import com.xiaopeng.operation.vo.XpOpenCityVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 开通城市 服务实现类
 * </p>
 *
 * @author liu
 * @since 2026-02-03
 */
@Service
public class XpOpenCityServiceImpl extends ServiceImpl<XpOpenCityMapper, XpOpenCity> implements XpOpenCityService {
    @Autowired
    private IXpRoutePlanService xpRoutePlanService;
 
    @Autowired
    private XpPricingService xpPricingService;
 
 
    @Override
    public XpOpenCityVo citiesList() {
        XpOpenCityVo xpOpenCityVo = new XpOpenCityVo();
        List<XpOpenCity> xpOpenCities = this.baseMapper.selectList(new LambdaQueryWrapper<XpOpenCity>().eq(XpOpenCity::getStatus, 1));
        if(!xpOpenCities.isEmpty()){
            List<XpOpenCityNextVo> xpOpenCityNextVos = new ArrayList<>();
            for (XpOpenCity xpOpenCity : xpOpenCities) {
                XpOpenCityNextVo xpOpenCityNextVo = new XpOpenCityNextVo();
                xpOpenCityNextVo.setName(xpOpenCity.getCityName());
                xpOpenCityNextVo.setAdcode(xpOpenCity.getCityCode());
                xpOpenCityNextVo.setId(xpOpenCity.getId());
                xpOpenCityNextVos.add(xpOpenCityNextVo);
            }
            xpOpenCityVo.setCities(xpOpenCityNextVos);
            xpOpenCityVo.setTotal(xpOpenCities.size());
        }
        return xpOpenCityVo;
    }
 
    @Override
    public List<OpenCityListVO> getOpenCityListForAdmin(Integer type) {
        // 1. 查询所有启用状态的开通城市
        List<XpOpenCity> xpOpenCities = this.baseMapper.selectList(
                new LambdaQueryWrapper<XpOpenCity>()
                        .eq(XpOpenCity::getStatus, 1)
                        .orderByAsc(XpOpenCity::getId)
        );
        List<Long> existsOpenCityIds = new ArrayList<>();
        //2.根据type过滤已配置的开通城市  类型:1-路线规划配置使用 2-定价管理配置使用
        if (Integer.valueOf(1).equals(type)) {
            //路径规划
            existsOpenCityIds = xpRoutePlanService.list().stream().map(XpRoutePlan::getOpenCityId).collect(Collectors.toList());
        } else if (Integer.valueOf(2).equals(type)){
            //定价管理
            existsOpenCityIds = xpPricingService.list().stream().map(XpPricing::getCityId).collect(Collectors.toList());
        }
        if (CollUtil.isNotEmpty(existsOpenCityIds)) {
            List<Long> finalExistsOpenCityIds = existsOpenCityIds;
            xpOpenCities = xpOpenCities.stream().filter(item -> !finalExistsOpenCityIds.contains(item.getId())).collect(Collectors.toList());
        }
        // 2. 转换为VO,处理区/县级拼接逻辑
        return xpOpenCities.stream()
                .map(city -> {
                    OpenCityListVO vo = new OpenCityListVO();
                    vo.setId(city.getId());
 
                    // 关键逻辑:判断是否为区/县级配置
                    if (StringUtils.isNotBlank(city.getDistrictName())) {
                        // 区/县级:拼接市名 + 区/县名
                        vo.setName(city.getCityName() + city.getDistrictName());
                        vo.setCode(city.getDistrictCode());
                    } else {
                        // 城市级:只返回市名
                        vo.setName(city.getCityName());
                        vo.setCode(city.getCityCode());
                    }
 
                    return vo;
                })
                .collect(Collectors.toList());
    }
 
    @Override
    public PageInfo<XpOpenCity> getPageList(OpenCityPageQuery query) {
        LambdaQueryWrapper<XpOpenCity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(query.getCityName())) {
            lambdaQueryWrapper.or(ew -> ew.like(StringUtils.isNotBlank(query.getCityName()), XpOpenCity::getProvinceName, query.getCityName()))
                    .or(ew -> ew.like(StringUtils.isNotBlank(query.getCityName()), XpOpenCity::getCityName, query.getCityName()))
                    .or(ew -> ew.like(StringUtils.isNotBlank(query.getCityName()), XpOpenCity::getDistrictName, query.getCityName()));
        }
        if (Objects.nonNull(query.getStatus())) {
            lambdaQueryWrapper.eq(XpOpenCity::getStatus, query.getStatus());
        }
        lambdaQueryWrapper.orderByDesc(XpOpenCity::getCreateTime);
        PageInfo<XpOpenCity> pageInfo = page(new PageInfo<>(query.getPageNum(), query.getPageSize()), lambdaQueryWrapper);
        return pageInfo;
    }
 
    @Override
    public void add(OpenCityAddDTO dto) {
        checkIfExist(dto, null);
        XpOpenCity xpOpenCity = BeanUtil.copyProperties(dto, XpOpenCity.class);
        save(xpOpenCity);
    }
 
    /**
     * 校验开通城市是否已添加
     * @param dto
     * @param excludeOpenCityId
     */
    private void checkIfExist(OpenCityAddDTO dto, Long excludeOpenCityId) {
        Long count = lambdaQuery().eq(XpOpenCity::getProvinceCode, dto.getProvinceCode())
                .eq(XpOpenCity::getCityCode, dto.getCityCode())
                .eq(StringUtils.isNotBlank(dto.getDistrictCode()), XpOpenCity::getDistrictCode, dto.getDistrictCode())
                .ne(Objects.nonNull(excludeOpenCityId), XpOpenCity::getId, excludeOpenCityId)
                .count();
        if (count > 0) {
            throw new ServiceException("该城市已存在", I18nKeyConstant.OPEN_CITY_EXISTS);
        }
    }
 
    @Override
    public void edit(OpenCityEditDTO dto) {
        checkIfExist(dto, dto.getId());
        XpOpenCity xpOpenCity = BeanUtil.copyProperties(dto, XpOpenCity.class);
        updateById(xpOpenCity);
    }
 
    @Override
    public void deleteById(Long id) {
        removeById(id);
    }
 
    @Override
    public void upDown(Long id, Integer status) {
        lambdaUpdate().eq(XpOpenCity::getId, id).set(XpOpenCity::getStatus, status).update();
    }
 
    @Override
    public String getNameWithCityName(Long cityId) {
        String cityName = "";
        XpOpenCity openCity = getById(cityId);
        if (Objects.nonNull(openCity)) {
            if (StringUtils.isNotBlank(openCity.getDistrictCode())) {
                cityName = openCity.getCityName() + ">" + openCity.getDistrictName();
            } else {
                cityName = openCity.getCityName();
            }
        }
        return cityName;
    }
 
 
    /**
     * 根据省、市、区县编码查询开通城市
     * @param provinceCode
     * @param cityCode
     * @param districtCode
     * @return
     */
    @Override
    public XpOpenCity getXpOpenCity(String provinceCode, String cityCode, String districtCode) {
        //先根据区编码获取,没有再根据市编码获取,没有再根据省编码获取
        XpOpenCity xpOpenCity = this.baseMapper.selectOne(new LambdaQueryWrapper<XpOpenCity>()
                .eq(XpOpenCity::getDistrictCode, districtCode));
        if (Objects.isNull(xpOpenCity)) {
            xpOpenCity = this.baseMapper.selectOne(new LambdaQueryWrapper<XpOpenCity>()
                    .eq(XpOpenCity::getCityCode, cityCode).eq(XpOpenCity::getDistrictCode, ""));
        }
        if (Objects.isNull(xpOpenCity)) {
            xpOpenCity = this.baseMapper.selectOne(new LambdaQueryWrapper<XpOpenCity>()
                    .eq(XpOpenCity::getProvinceCode, provinceCode)
                    .eq(XpOpenCity::getCityCode, "")
                    .eq(XpOpenCity::getDistrictCode, ""));
        }
        return xpOpenCity;
    }
}