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
package com.xiaopeng.operation.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaopeng.api.model.operation.XpOpenCity;
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.common.security.utils.SecurityUtils;
import com.xiaopeng.operation.dto.RoutePlanAddDTO;
import com.xiaopeng.operation.dto.RoutePlanEditDTO;
import com.xiaopeng.operation.mapper.XpOpenCityMapper;
import com.xiaopeng.operation.mapper.XpRoutePlanMapper;
import com.xiaopeng.operation.query.RoutePlanPageListQuery;
import com.xiaopeng.operation.service.IXpRoutePlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.List;
 
/**
 * <p>
 * 路线规划配置 服务实现类
 * </p>
 *
 * @author mitao
 * @since 2026-02-03
 */
@Service
public class XpRoutePlanServiceImpl extends ServiceImpl<XpRoutePlanMapper, XpRoutePlan> implements IXpRoutePlanService {
 
    @Resource
    private XpOpenCityMapper openCityMapper;
 
    @Override
    public PageInfo<XpRoutePlan> pageList(RoutePlanPageListQuery query) {
        // 创建分页对象
        PageInfo<XpRoutePlan> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        // 调用Mapper查询
        List<XpRoutePlan> list = this.baseMapper.pageList(pageInfo, query);
        // 设置结果
        pageInfo.setRecords(list);
        return pageInfo;
    }
 
    @Override
    public void add(RoutePlanAddDTO dto) {
        // 1. 根据openCityId查询城市信息
        XpOpenCity openCity = openCityMapper.selectById(dto.getOpenCityId());
        if (openCity == null) {
            throw new ServiceException("开通城市不存在");
        }
        long count = this.count(new QueryWrapper<XpRoutePlan>().eq("open_city_id", openCity.getId())
                .eq("is_deleted", 0));
        if(count >= 3){
            throw new ServiceException("同一个城市只能添加三个路线规划");
        }
        count = this.count(new QueryWrapper<XpRoutePlan>().eq("open_city_id", openCity.getId())
                .eq("plan_name", dto.getPlanName()).eq("is_deleted", 0));
        if(count > 0){
            throw new ServiceException("路线规划名称不能重复");
        }
 
        // 2. 创建路线规划配置对象
        XpRoutePlan routePlan = new XpRoutePlan();
        routePlan.setOpenCityId(dto.getOpenCityId());
        routePlan.setPlanName(dto.getPlanName());
        routePlan.setRemarks(dto.getRemarks());
        routePlan.setRoutePlanRule(dto.getRoutePlanRule());
 
        // 3. 填充省市区信息
        routePlan.setProvinceName(openCity.getProvinceName());
        routePlan.setProvinceCode(openCity.getProvinceCode());
        routePlan.setCityName(openCity.getCityName());
        routePlan.setCityCode(openCity.getCityCode());
        routePlan.setDistrictName(openCity.getDistrictName());
        routePlan.setDistrictCode(openCity.getDistrictCode());
        routePlan.setAuditStatus(2);
        // 5. 保存到数据库
        this.save(routePlan);
    }
 
    @Override
    public void edit(RoutePlanEditDTO dto) {
        // 1. 查询原记录是否存在
        XpRoutePlan routePlan = this.getById(dto.getId());
        if (routePlan == null) {
            throw new ServiceException("路线规划配置不存在");
        }
 
        // 2. 根据openCityId查询城市信息
        XpOpenCity openCity = openCityMapper.selectById(dto.getOpenCityId());
        if (openCity == null) {
            throw new ServiceException("开通城市不存在");
        }
        long count = this.count(new QueryWrapper<XpRoutePlan>().eq("open_city_id", openCity.getId())
                .eq("plan_name", dto.getPlanName()).eq("is_deleted", 0).ne("id", dto.getId()));
        if(count > 0){
            throw new ServiceException("路线规划名称不能重复");
        }
 
        // 3. 更新字段
        routePlan.setOpenCityId(dto.getOpenCityId());
        routePlan.setPlanName(dto.getPlanName());
        routePlan.setRemarks(dto.getRemarks());
        routePlan.setRoutePlanRule(dto.getRoutePlanRule());
 
        // 4. 更新省市区信息
        routePlan.setProvinceName(openCity.getProvinceName());
        routePlan.setProvinceCode(openCity.getProvinceCode());
        routePlan.setCityName(openCity.getCityName());
        routePlan.setCityCode(openCity.getCityCode());
        routePlan.setDistrictName(openCity.getDistrictName());
        routePlan.setDistrictCode(openCity.getDistrictCode());
        routePlan.setAuditStatus(2);
//        routePlan.setUpdateBy(SecurityUtils.getLoginUser().getSysUser().getNickName());
        // 5. 保存更新
        this.updateById(routePlan);
    }
 
    @Override
    public void deleteById(Long id) {
        // 1. 检查记录是否存在
        XpRoutePlan routePlan = this.getById(id);
        if (routePlan == null) {
            throw new ServiceException("路线规划配置不存在");
        }
 
        // 2. 逻辑删除(MyBatis-Plus自动处理)
        this.removeById(id);
    }
}