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> findListByCityCode(@RequestParam String cityCode) { XpStationQuery query = new XpStationQuery(); query.setCityCode(cityCode); return R.ok( xpStationService.findList(query)); } @ApiOperation(value = "场站管理列表", tags = {"管理后台-运营-场站管理"}) @PostMapping("/findList") public R> findList(@RequestBody XpStationQuery query) { List list = xpStationService.findList(query); return R.ok(list); } @ApiOperation(value = "场站管理分页列表", tags = {"管理后台-运营-场站管理"}) @PostMapping("/pageList") public R> pageList(@RequestBody XpStationQuery query) { PageInfo 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("参数错误"); } xpStationService.validate(vo); XpStation bean = xpStationService.getById(vo.getId()); if(bean == null){ return R.fail("参数错误"); } //场站状态 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 list = xpStationService.findList(query); List 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("点位导出失败"); } } }