package com.jiayue.ipfcst.console.controller; import com.jiayue.ipfcst.aop.SaveValidate; import com.jiayue.ipfcst.common.core.web.vo.ResponseVO; import com.jiayue.ipfcst.common.data.constant.enums.ElectricFieldTypeEnum; import com.jiayue.ipfcst.common.data.entity.ElectricField; import com.jiayue.ipfcst.console.service.ElectricFieldService; import com.jiayue.ipfcst.console.service.OverHaulPlanService; import com.jiayue.ipfcst.console.service.SysParameterService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import java.io.BufferedOutputStream; import java.io.IOException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 场站信息restful接口 * * @author tl * @version 3.0 * @since 2020/6/28 10:12 */ @RestController @RequestMapping("/electricField") @Slf4j public class ElectricFieldController { @Autowired ElectricFieldService electricFieldService; @Autowired SysParameterService sysParameterService; @Autowired OverHaulPlanService overHaulPlanService; /** * 新增场站信息 * * @param electricField 场站实体参数 * @return 执行结果 */ @PostMapping(value = "/") @SaveValidate public ResponseVO saveElectricField(@RequestBody ElectricField electricField) { try { electricFieldService.add(electricField); return ResponseVO.success(); } catch (Exception e) { e.printStackTrace(); log.error(" 保存场站信息异常"); return ResponseVO.fail(); } } /** * 更新场站信息 * * @param electricField 场站实体参数 * @return 执行结果 */ @PostMapping(value = "/updateElectricField") @SaveValidate public ResponseVO updateElectricField(@RequestBody ElectricField electricField) { try { this.electricFieldService.update(electricField); return ResponseVO.success(); } catch (Exception e) { e.printStackTrace(); log.error(" 更新场站信息异常"); return ResponseVO.fail(); } } /** * 获取场站信息 * * @return 所有场站信息 */ @GetMapping(value = "/getElectricField") public ResponseVO getElectricField() { try { List electricFieldList = this.electricFieldService.getAll(); return ResponseVO.success(electricFieldList); } catch (Exception e) { e.printStackTrace(); log.error(" 获取场站信息异常"); return ResponseVO.success(null); } } /** * 获取场站信息 * * @return 所有场站信息 */ @GetMapping(value = "/getElectricField/{stationCode}") public ResponseVO getElectricFieldByStationCode(@PathVariable String stationCode) { try { ElectricField electricField = this.electricFieldService.findByStationCode(stationCode); return ResponseVO.success(electricField); } catch (Exception e) { e.printStackTrace(); log.error(" 获取场站信息异常"); return ResponseVO.success(null); } } @DeleteMapping(value = "/{stationCode}") @SaveValidate public ResponseVO deleteElectricField(@PathVariable String stationCode) { try { electricFieldService.delete(stationCode); return ResponseVO.success(); } catch (Exception e) { e.printStackTrace(); log.error(" 删除场站信息异常"); return ResponseVO.fail(); } } /** * 功能描述:
* 〈返回场站类型〉 * * @param: [] * @Return: com.jiayue.ipfcst.console.dto.ResponseBean * @Author: YH * @Date: 2020/3/3 14:08 */ @GetMapping(value = "/getElType") public ResponseVO getElType() { List> elType = new ArrayList<>(); Map map; try { for (ElectricFieldTypeEnum e : ElectricFieldTypeEnum.values()) { map = new HashMap<>(); map.put("label", e.getMessage()); map.put("key", e.name()); map.put("value", e.name()); elType.add(map); } return ResponseVO.success(elType); } catch (Exception e) { e.printStackTrace(); log.error(" 获取场站类型异常"); return ResponseVO.fail(elType); } } //导出所有场站 @RequestMapping(value = "/export") public void export(HttpServletResponse response) { BufferedOutputStream bos = null; try { StringBuilder templateContent = new StringBuilder(); response.setCharacterEncoding("UTF-8"); List electricFieldList = electricFieldService.getAll(); String header = "\"场站编号\"" + "," + "\"场站名称\"" + "," + "\"场站标识\"" + "," + "\"别名\"" + "," + "\"装机容量\"" + "," + "\"并网设备数\"" + "," + "\"场站经度\"" + "," + "\"场站纬度\"" + "," + "\"场站类型\"" + "," + "\"入库时间\"" + "," + "\"场站海拔\"" + "," + "\"场站所属公司\"" + "," + "\"场站位置\"" + "," + "\"场站面积\"" + "\r\n"; StringBuilder content = new StringBuilder(); for (ElectricField electricField : electricFieldList) { content.append(electricField.getStationCode()).append(","); content.append(electricField.getName()).append(","); content.append(electricField.getSign()).append(","); content.append(electricField.getNetSubstationName()).append(","); content.append(electricField.getCapacity()).append(","); content.append(electricField.getGridCE()).append(","); content.append(electricField.getLongitude()).append(","); content.append(electricField.getLatitude()).append(","); content.append(electricField.getElectricFieldTypeEnum().getMessage()).append(","); content.append(electricField.getInterval()).append(","); content.append(electricField.getAltitude()).append(","); content.append(electricField.getCompany()).append(","); content.append(electricField.getLocation()).append(","); content.append(electricField.getArea()).append(","); content.append("\r\n"); } templateContent.append(header); templateContent.append(content); response.setContentType("application/x-msdownload;charset=UTF-8");// 文件下载必须配置为application/x-msdownload response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode("场站信息数据.csv", "UTF-8"));// 中文文件名必须使用URLEncoder.encode进行转码 byte[] templateContentBytes = templateContent.toString().getBytes(StandardCharsets.UTF_8); bos = new BufferedOutputStream(response.getOutputStream());// 向response中写入文件流 bos.write(new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF});// 指定csv文件用UTF-8字符集打开 bos.write(templateContentBytes); response.flushBuffer(); } catch (Exception e) { log.error("系统错误:" + e.getMessage(), e); throw new RuntimeException(e); } finally { if (bos != null) try { bos.close(); } catch (IOException e) { log.error("系统错误:" + e.getMessage(), e); } } } }