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
package com.xiaopeng.map.controller;
 
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaopeng.api.model.operation.XpElectronicFence;
import com.xiaopeng.api.model.operation.XpStation;
import com.xiaopeng.api.query.operation.XpStationQuery;
import com.xiaopeng.api.vo.account.LoginUser;
import com.xiaopeng.api.vo.operation.XpStationVo;
import com.xiaopeng.common.core.domain.R;
import com.xiaopeng.common.core.exception.ServiceException;
import com.xiaopeng.common.core.utils.WebUtils;
import com.xiaopeng.common.core.web.page.PageInfo;
import com.xiaopeng.common.security.service.TokenService;
import com.xiaopeng.map.exportExcel.XpStationExportExcel;
import com.xiaopeng.map.service.XpElectronicFenceService;
import com.xiaopeng.map.service.XpPointService;
import com.xiaopeng.map.service.XpStationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
 
 
@RestController
@Slf4j
@Api(tags = "场站管理")
@RequestMapping("/api/v1/station")
public class XpStationManagementController {
 
 
    @Resource
    private XpStationService xpStationService;
 
    @Resource
    private TokenService tokenService;
    @Resource
    private XpPointService xpPointService;
    @Resource
    private XpElectronicFenceService xpElectronicFenceService;
 
 
    @ApiOperation(value = "场站管理已经启动列表", tags = {"管理后台-运营-场站管理"})
    @GetMapping("/findListEnable")
    public R<List<XpStation>> findListByCityCode(@RequestParam String cityCode) {
        XpStationQuery query = new XpStationQuery();
        query.setCityCode(cityCode);
        return R.ok( xpStationService.findList(query));
    }
 
 
    @ApiOperation(value = "场站管理列表", tags = {"管理后台-运营-场站管理"})
    @PostMapping("/findList")
    public R<List<XpStation>> findList(@RequestBody XpStationQuery query) {
        List<XpStation> list = xpStationService.findList(query);
        return R.ok(list);
    }
 
    @ApiOperation(value = "场站管理分页列表", tags = {"管理后台-运营-场站管理"})
    @PostMapping("/pageList")
    public R<PageInfo<XpStation>> pageList(@RequestBody XpStationQuery query) {
        PageInfo<XpStation> pageList = xpStationService.pageList(query);
        return R.ok(pageList);
    }
 
    @GetMapping("/getById/{id}")
    @ApiOperation(value = "详情场站管理", tags = {"管理后台-运营-场站管理"})
    public R getById(@PathVariable Long id) {
        XpStation bean = xpStationService.getById(id);
        if (bean == null) {
            return R.fail("参数错误");
        }
        XpStationVo beanVo = new XpStationVo();
        BeanUtils.copyProperties(bean, beanVo);
        beanVo.setXpPointList(xpPointService.listByStationId(bean.getId()));
 
        XpElectronicFence electronicFence = xpElectronicFenceService.getById(bean.getElectronicId());
 
        beanVo.setFenceEffectType(electronicFence.getEffectType());
        beanVo.setFenceEffectTime(electronicFence.getEffectTime());
        beanVo.setFenceFailureType(electronicFence.getFailureType());
        beanVo.setFenceFailureTime(electronicFence.getFailureTime());
 
        return R.ok(beanVo);
    }
 
    /**
     * 新增电子围栏
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增场站管理", tags = {"管理后台-运营-场站管理"})
    public R add(@Valid @RequestBody XpStationVo vo) {
 
        xpStationService.validate(vo);
 
        LoginUser loginUser = tokenService.getLoginUser();
 
        vo.setCreateBy(loginUser.getUsername());
        vo.setCreateTime(LocalDateTime.now());
        vo.setUpdateBy(loginUser.getUsername());
        vo.setUpdateTime(LocalDateTime.now());
 
        return xpStationService.add(vo);
    }
 
    /**
     * 编辑电子围栏
     */
    @PostMapping("/edit")
    @ApiOperation(value = "编辑场站管理" , tags = {"管理后台-运营-场站管理"})
    public R edit(@Valid @RequestBody XpStationVo vo) {
        if(vo.getId() == null){
            return R.fail("参数错误");
        }
        XpStation bean = xpStationService.getById(vo.getId());
        if(bean == null){
            return R.fail("参数错误");
        }
        xpStationService.validate(vo);
        //场站状态 0=草稿 1=待启用 2=启用中 3=已停用 4=失效
//        if(bean.getStatus() == 2){
//            return R.fail("场站在启动中不能编辑");
//        }
 
        LoginUser loginUser = tokenService.getLoginUser();
 
        vo.setUpdateBy(loginUser.getUsername());
        vo.setUpdateTime(LocalDateTime.now());
 
        return xpStationService.edit(vo);
    }
 
 
    /**
     * 启用中状态: 操作项展示为 [停用]。 停用
     *     -  操作项展示为 [停用]。点击后弹出二次确认框,“当前场站包含12个点位,确认一起停用?
     *       - 是:确认后场站以及子点位状态流转至“已停用”。
     *       - 否:子点位维持当前状态,但自动退回为“独立点位”。
     */
    @ApiOperation(value = "停用" , tags = {"管理后台-运营-场站管理"})
    @GetMapping("/updateStatus/{id}")
    public R updateStatus(@PathVariable Long id, @RequestParam Boolean isEnable, @RequestParam Boolean pointStop) {
        XpStation bean = xpStationService.getById(id);
        if (bean == null) {
            throw new ServiceException("场站不存在,请刷新重试");
        }
        if(isEnable){
            xpStationService.updateStatusEnable(bean);
        }else{
//            if(bean.getStatus() == 2){//场站状态 0=草稿 1=待启用 2=启用中 3=已停用 4=失效
//                return R.fail("场站状态启动中,不能停用");
//            }
            //场站状态 0=草稿 1=待测试 2=测试中 3=测试不通过 4=可运营 5=待生效 6=启用中 7=已停用 8=失效
            xpStationService.updateStatusStop(bean,pointStop);
        }
 
        return R.ok();
    }
    /**
     *       - 删除拦截逻辑:
     *         - 测试阶段(待测试、测试中等):点击直接删除。
     *         - 历史运营阶段(已停用、失效):点击弹出二次确认框“当前场站包含12个点位,确认一起删除?”
     *           - 是:点位统一一起删除
     *           - 否:不自动删除点位,点位自动退回为[待测试]的“独立点位”,并自动继承父级运营区的运营时段
     * @param id
     * @param pointDelete
     * @return
     */
    @ApiOperation(value = "删除场站管理", tags = {"管理后台-运营-场站管理"})
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable Long id,@RequestParam Boolean pointDelete) {
        XpStation bean = xpStationService.getById(id);
        if (bean == null) {
            throw new ServiceException("场站不存在,请刷新重试");
        }
        // 场站状态  0=草稿 1=待启用 2=启用中 3=已停用 4=失效
        // 只有状态为已停用(7)时才能删除
        if (bean.getStatus() == 2 ) {
            throw new ServiceException("场站启用中,无法删除");
        }
        boolean b = xpStationService.removeById(bean,pointDelete);
        if (b){
            return R.ok();
        }
        return R.fail("删除失败");
    }
 
 
 
    @ApiOperation(value = "导出", tags = {"管理后台-运营-场站管理"})
    @PostMapping("/exportExcel")
    public void exportExcel(@RequestBody XpStationQuery query) {
        if (query.getIds().isEmpty()) {
            return;
        }
        List<XpStation> list = xpStationService.findList(query);
 
        List<XpStationExportExcel> exportList = new ArrayList<>(list.size());
        for (XpStation xpStation : list) {
            XpStationExportExcel excel = new XpStationExportExcel();
            BeanUtils.copyProperties(xpStation, excel);
            excel.setStatusName(xpStation.getStatusName());
            excel.setCreateTimeStr(xpStation.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            excel.setUpdateTimeStr(xpStation.getUpdateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            exportList.add(excel);
        }
 
        HttpServletResponse response = WebUtils.response();
        if (response == null) {
            throw new ServiceException("导出失败:未获取到响应对象");
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        try {
            String fileName = URLEncoder.encode("场站列表.xls", StandardCharsets.UTF_8.name());
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
            try (Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), XpStationExportExcel.class, exportList);
                 ServletOutputStream outputStream = response.getOutputStream()) {
                workbook.write(outputStream);
            }
        } catch (IOException e) {
            log.error("点位导出失败 ids={}", query.getIds(), e);
            throw new ServiceException("点位导出失败");
        }
    }
}