package com.jiayue.ipfcst.console.controller;

import cn.hutool.core.date.DateUtil;
import com.jiayue.ipfcst.aop.SaveValidate;
import com.jiayue.ipfcst.common.core.util.JsonBeanUtil;
import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.common.data.constant.enums.ElectricFieldStatrsEnum;
import com.jiayue.ipfcst.common.data.constant.enums.ElectricFieldTypeEnum;
import com.jiayue.ipfcst.common.data.constant.enums.EquipmentTypeEnum;
import com.jiayue.ipfcst.common.data.constant.enums.ProvinceEnum;
import com.jiayue.ipfcst.common.data.entity.*;
import com.jiayue.ipfcst.common.data.entity.gathertunnelinfo.*;
import com.jiayue.ipfcst.common.data.entity.sendertunnelinfo.Sender104TcpTunnelInfo;
import com.jiayue.ipfcst.common.data.entity.sendertunnelinfo.SenderCdtRtuTunnelInfo;
import com.jiayue.ipfcst.common.data.entity.sendertunnelinfo.SenderModbusRtuTunnelInfo;
import com.jiayue.ipfcst.common.data.entity.sendertunnelinfo.SenderModbusTcpTunnelInfo;
import com.jiayue.ipfcst.console.service.*;
import com.jiayue.ipfcst.dataexchange.service.EquipmentAttributeService;
import com.jiayue.ipfcst.dataexchange.service.ProtocolGatherDataPointService;
import com.jiayue.ipfcst.dataexchange.service.ProtocolSenderDataPointService;
import com.jiayue.ipfcst.dataexchange.service.TunnelInfoService;
import com.jiayue.ipfcst.fileupload.service.UploadFileChannelService;
import com.jiayue.ipfcst.fileupload.service.UploadFileCodeService;
import com.jiayue.ipfcst.fileupload.service.UploadObjectService;
import com.jiayue.ipfcst.fileupload.service.UploadURLService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
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
    PvModuleModelService pvModuleModelService;

    @Autowired
    OverHaulPlanService overHaulPlanService;

    @Autowired
    InverterInfoService inverterInfoService;

    @Autowired
    WeatherStationInfoService weatherStationInfoService;

    @Autowired
    WindTowerInfoService windTowerInfoService;

    @Autowired
    WindTurbineInfoService windTurbineInfoService;

    @Autowired
    QuartzService quartzService;

    @Autowired
    InitJobClassService initJobClassService;

    @Autowired
    SysParameterService sysParameterService;

    @Autowired
    UploadObjectService uploadObjectService;

    @Autowired
    UploadFileChannelService uploadFileChannelService;

    @Autowired
    UploadFileCodeService uploadFileCodeService;

    @Autowired
    UploadURLService uploadURLService;

    @Autowired
    TunnelInfoService tunnelInfoService;

    @Autowired
    EquipmentAttributeService equipmentAttributeService;

    @Autowired
    ProtocolSenderDataPointService protocolSenderDataPointService;

    @Autowired
    ProtocolGatherDataPointService protocolGatherDataPointService;

  @Autowired
  AGC_AVCInfoService agcAvcInfoService;

  @Autowired
  BoosterStationInfoService boosterStationInfoService;

  @Autowired
  SubsidiaryEquipmentInfoService subsidiaryEquipmentInfoService;

    /**
     * 新增场站信息
     *
     * @param electricField 场站实体参数
     * @return 执行结果
     */
    @PostMapping(value = "/")
    @SaveValidate
    public ResponseVO saveElectricField(@RequestBody ElectricField electricField) {
        try {
            electricFieldService.update(electricField);
            return ResponseVO.success();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(" 保存场站信息异常");
            return ResponseVO.fail();
        }

    }


    /**
     * 更新场站信息
     *
     * @param electricField 场站实体参数
     * @return 执行结果
     */
    @PutMapping(value = "/")
    @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 = "/")
    public ResponseVO getElectricField() {
        try {
            ElectricField electricField = this.electricFieldService.get();
            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();
        }

    }

    /**
     * 功能描述: <br>
     * 〈返回上报类型〉
     *
     * @param: []
     * @Return: com.jiayue.ipfcst.console.dto.ResponseBean
     * @Author: YH
     * @Date: 2020/3/3 14:08
     */
    @GetMapping(value = "/getProvince")
    public ResponseVO getProvince() {
        List<Map<String, Object>> provinceType = new ArrayList<>();
        Map<String, Object> map;
        try {
            for (ProvinceEnum e : ProvinceEnum.values()) {
                map = new HashMap<>();
                map.put("label", e.getMessage());
                map.put("key", e.name());
                map.put("value", e.name());
                provinceType.add(map);
            }
            return ResponseVO.success(provinceType);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(" 获取场站上报类型异常");
            return ResponseVO.fail(provinceType);
        }

    }


    /**
     * 功能描述: <br>
     * 〈返回场站类型〉
     *
     * @param: []
     * @Return: com.jiayue.ipfcst.console.dto.ResponseBean
     * @Author: YH
     * @Date: 2020/3/3 14:08
     */
    @GetMapping(value = "/getElType")
    public ResponseVO getElType() {
        List<Map<String, Object>> elType = new ArrayList<>();
        Map<String, Object> 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);
        }

    }

    /**
     * 获取设备枚举类型
     *
     * @return
     */
    @GetMapping(value = "/getEquipmentType")
    public ResponseVO getEquipmentType() {
        List<Map<String, Object>> equipmentType = new ArrayList<>();
        Map<String, Object> map;
        try {
            for (EquipmentTypeEnum e : EquipmentTypeEnum.values()) {
                map = new HashMap<>();
                map.put("label", e.getMessage());
                map.put("key", e.name());
                map.put("value", e.name());
                equipmentType.add(map);
            }
            return ResponseVO.success(equipmentType);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(" 获取场站类型异常");
            return ResponseVO.fail(equipmentType);
        }

    }


    @GetMapping(value = "/getElectricFieldStatrsEnum")
    public ResponseVO getElectricFieldStatrsEnum() {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map;
        try {
            for (ElectricFieldStatrsEnum e : ElectricFieldStatrsEnum.values()) {
                map = new HashMap<>();
                map.put("label", e.getMessage());
                map.put("key", e.name());
                map.put("value", e.name());
                list.add(map);
            }
            return ResponseVO.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(" 获取场站状态类型异常");
            return ResponseVO.fail(list);
        }

    }

    @PostMapping(value = "/dataDownload")
    public void dataDownload(HttpServletRequest request,
                             HttpServletResponse response) {
        BufferedOutputStream bos = null;
        try {
            response.setCharacterEncoding("UTF-8");
            StringBuilder templateContent = new StringBuilder();
            //查询场站信息
            ElectricField electricField = electricFieldService.getElectricField();
            // 添加第一行为场站编码
            String header = electricField.getStationCode();
            templateContent.append(header + "\r\n");
            //查询所有配置参数信息
            List<String> valList = queryAllParamsFromDb(electricField);

            //遍历上面查询到的所有配置数据，存入模板中
            valList.forEach(str->{
              templateContent.append(str + "\r\n");
            });

            byte[] templateContentBytes = templateContent.toString().getBytes("UTF-8");
            long fileLength = templateContentBytes.length + 3;
            response.setContentType("application/x-msdownload;charset=UTF-8");


            response.setHeader("Content-disposition",
                    "attachment; filename=" + URLEncoder.encode(electricField.getStationCode() + "-" + DateUtil.today() + ".rb", "UTF-8"));
            response.setHeader("Content-Length", String.valueOf(fileLength));
            bos = new BufferedOutputStream(response.getOutputStream());
            bos.write(new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF});
            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);
                }
        }
    }

  /**
   * 查询场站所有配置信息存入list
   * 如有新增设备信息及表需要添加到此方法中
   * @param electricField
   * @return
   */
    @SneakyThrows
  private List<String> queryAllParamsFromDb(ElectricField electricField) {
      String header = electricField.getStationCode();
      List<String> valList = new ArrayList<>();
    //场站信息
      List<ElectricField> electricFields = new ArrayList<>();
    electricFields.add(electricField);
      String val = "ElectricField:::" + JsonBeanUtil.beanToJson(electricFields);
    valList.add(val);

    //光伏组件信息
    List<PvModuleModel> pvModuleModelList = pvModuleModelService.getAll();
    if (null != pvModuleModelList && pvModuleModelList.size() > 0) {
      for (PvModuleModel bean : pvModuleModelList) {
        bean.setStationCode(header);
      }
      val = "PvModuleModel:::" + JsonBeanUtil.beanToJson(pvModuleModelList);
      valList.add(val);
    }
    //检修计划
    List<OverhaulPlan> overhaulPlanList = overHaulPlanService.getAll();
    if (null != overhaulPlanList && overhaulPlanList.size() > 0) {
      for (OverhaulPlan bean : overhaulPlanList) {
        bean.setStationCode(header);
      }
      val = "OverhaulPlan:::" + JsonBeanUtil.beanToJson(overhaulPlanList);
      valList.add(val);
    }
    //逆变器信息
    List<InverterInfo> inverterInfoList = inverterInfoService.getAll();
    if (null != inverterInfoList && inverterInfoList.size() > 0) {
      for (InverterInfo bean : inverterInfoList) {
        bean.setStationCode(header);
      }
      val = "InverterInfo:::" + JsonBeanUtil.beanToJson(inverterInfoList);
      valList.add(val);
    }
    //气象站数据
    List<WeatherStationInfo> weatherStationInfoList = weatherStationInfoService.getAll();
    if (null != weatherStationInfoList && weatherStationInfoList.size() > 0) {
      for (WeatherStationInfo bean : weatherStationInfoList) {
        bean.setStationCode(header);
      }
      val = "WeatherStationInfo:::" + JsonBeanUtil.beanToJson(weatherStationInfoList);
      valList.add(val);
    }
    //测风塔
    List<WindTowerInfo> windTowerInfoList = windTowerInfoService.getAll();
    if (null != windTowerInfoList && windTowerInfoList.size() > 0) {
      for (WindTowerInfo bean : windTowerInfoList) {
        bean.setStationCode(header);
      }
      val = "WindTowerInfo:::" + JsonBeanUtil.beanToJson(windTowerInfoList);
      valList.add(val);
    }
    //风机
    List<WindTurbineInfo> windTurbineInfoList = windTurbineInfoService.getAll();
    if (null != windTurbineInfoList && windTurbineInfoList.size() > 0) {
      for (WindTurbineInfo bean : windTurbineInfoList) {
        bean.setStationCode(header);
      }
      val = "WindTurbineInfo:::" + JsonBeanUtil.beanToJson(windTurbineInfoList);
      valList.add(val);
    }
    //初始化定时任务
    List<InitJobClass> initJobClassList = initJobClassService.findAll();
    if (null != initJobClassList && initJobClassList.size() > 0) {
      for (InitJobClass bean : initJobClassList) {
        bean.setStationCode(header);
      }
      val = "InitJobClass:::" + JsonBeanUtil.beanToJson(initJobClassList);
      valList.add(val);
    }
    //定时任务
    List<Quartz> quartzList = quartzService.getAll();
    if (null != quartzList && quartzList.size() > 0) {
      for (Quartz bean : quartzList) {
        bean.setStationCode(header);
      }
      val = "Quartz:::" + JsonBeanUtil.beanToJson(quartzList);
      valList.add(val);
    }
    //系统参数
    List<SysParameter> sysParameterList = sysParameterService.getAll();
    if (null != sysParameterList && sysParameterList.size() > 0) {
      for (SysParameter bean : sysParameterList) {
        bean.setStationCode(header);
      }
      val = "SysParameter:::" + JsonBeanUtil.beanToJson(sysParameterList);
      valList.add(val);
    }

    //文件上报部分
    List<UploadObject> uploadObjectList = uploadObjectService.get();
    if (null != uploadObjectList && uploadObjectList.size() > 0) {
      for (UploadObject bean : uploadObjectList) {
        bean.setStationCode(header);
      }
      val = "UploadObject:::" + JsonBeanUtil.beanToJson(uploadObjectList);
      valList.add(val);
    }

    //上报通道
    List<UploadFileChannel> uploadFileChannels = uploadFileChannelService.get();
    if (null != uploadFileChannels && uploadFileChannels.size() > 0) {
      for (UploadFileChannel bean : uploadFileChannels) {
        bean.setStationCode(header);
      }
      val = "UploadFileChannel:::" + JsonBeanUtil.beanToJson(uploadFileChannels);
      valList.add(val);
    }
    //上报文件编码
    List<UploadFileCode> uploadFileCodes = uploadFileCodeService.get();
    if (null != uploadFileCodes && uploadFileCodes.size() > 0) {
      for (UploadFileCode bean : uploadFileCodes) {
        bean.setStationCode(header);
      }
      val = "UploadFileCode:::" + JsonBeanUtil.beanToJson(uploadFileCodes);
      valList.add(val);
    }
    //上报文件路径
    List<UploadURL> uploadURLS = uploadURLService.findAll();
    if (null != uploadURLS && uploadURLS.size() > 0) {
      for (UploadURL bean : uploadURLS) {
        bean.setStationCode(header);
      }
      val = "UploadURL:::" + JsonBeanUtil.beanToJson(uploadURLS);
      valList.add(val);
    }

    //数据接入部分
    List<EquipmentAttribute> equipmentAttributes = equipmentAttributeService.getAll();
    if (null != equipmentAttributes && equipmentAttributes.size() > 0) {
      for (EquipmentAttribute bean : equipmentAttributes) {
        bean.setStationCode(header);
      }
      val = "EquipmentAttribute:::" + JsonBeanUtil.beanToJson(equipmentAttributes);
      valList.add(val);
    }

    //文件解析通道信息
    List<FileParseTunnelInfo> fileParseTunnelInfos = tunnelInfoService.getAllFileParseTunnel();
    if (null != fileParseTunnelInfos && fileParseTunnelInfos.size() > 0) {
      for (FileParseTunnelInfo bean : fileParseTunnelInfos) {
        bean.setStationCode(header);
      }
      val = "FileParseTunnelInfo:::" + JsonBeanUtil.beanToJson(fileParseTunnelInfos);
      valList.add(val);
    }

    //104tcp 接入通道
    List<Gather104TcpTunnelInfo> gather104TcpTunnelInfos = tunnelInfoService.getAllGather104TcpTunnel();
    if (null != gather104TcpTunnelInfos && gather104TcpTunnelInfos.size() > 0) {
      for (Gather104TcpTunnelInfo bean : gather104TcpTunnelInfos) {
        bean.setStationCode(header);
      }
      val = "Gather104TcpTunnelInfo:::" + JsonBeanUtil.beanToJson(gather104TcpTunnelInfos);
      valList.add(val);
    }

    //cdtRtu接入通道
    List<GatherCdtRtuTunnelInfo> gatherCdtRtuTunnelInfos = tunnelInfoService.getAllGatherCdtRtuTunnel();
    if (null != gatherCdtRtuTunnelInfos && gatherCdtRtuTunnelInfos.size() > 0) {
      for (GatherCdtRtuTunnelInfo bean : gatherCdtRtuTunnelInfos) {
        bean.setStationCode(header);
      }
      val = "GatherCdtRtuTunnelInfo:::" + JsonBeanUtil.beanToJson(gatherCdtRtuTunnelInfos);
      valList.add(val);
    }
    //modbusRtu接入通道
    List<GatherModbusRtuTunnelInfo> gatherModbusRtuTunnelInfos =
      tunnelInfoService.getAllGatherModbusRtuTunnel();
    if (null != gatherModbusRtuTunnelInfos && gatherModbusRtuTunnelInfos.size() > 0) {
      for (GatherModbusRtuTunnelInfo bean : gatherModbusRtuTunnelInfos) {
        bean.setStationCode(header);
      }
      val = "GatherModbusRtuTunnelInfo:::" + JsonBeanUtil.beanToJson(gatherModbusRtuTunnelInfos);
      valList.add(val);
    }

    //modbusTcp接入通道
    List<GatherModbusTcpTunnelInfo> gatherModbusTcpTunnelInfos =
      tunnelInfoService.getAllGatherModbusTcpTunnel();
    if (null != gatherModbusTcpTunnelInfos && gatherModbusTcpTunnelInfos.size() > 0) {
      for (GatherModbusTcpTunnelInfo bean : gatherModbusTcpTunnelInfos) {
        bean.setStationCode(header);
      }
      val = "GatherModbusTcpTunnelInfo:::" + JsonBeanUtil.beanToJson(gatherModbusTcpTunnelInfos);
      valList.add(val);
    }

    //ModbusRtuWithTcp 接入通道
    List<GatherModbusRtuWithTcpServerTunnelInfo> gatherModbusRtuWithTcpServerTunnelInfos =
      tunnelInfoService.getAllGatherModbusRtuWithTcpServerTunnel();
    if (null != gatherModbusRtuWithTcpServerTunnelInfos && gatherModbusRtuWithTcpServerTunnelInfos.size() > 0) {
      for (GatherModbusRtuWithTcpServerTunnelInfo bean : gatherModbusRtuWithTcpServerTunnelInfos) {
        bean.setStationCode(header);
      }
      val = "GatherModbusRtuWithTcpServerTunnelInfo:::" + JsonBeanUtil.beanToJson(gatherModbusRtuWithTcpServerTunnelInfos);
      valList.add(val);
    }

    //104tcp转发通道
    List<Sender104TcpTunnelInfo> sender104TcpTunnelInfos = tunnelInfoService.getAllSender104TcpTunnel();
    if (null != sender104TcpTunnelInfos && sender104TcpTunnelInfos.size() > 0) {
      for (Sender104TcpTunnelInfo bean : sender104TcpTunnelInfos) {
        bean.setStationCode(header);
      }
      val = "Sender104TcpTunnelInfo:::" + JsonBeanUtil.beanToJson(sender104TcpTunnelInfos);
      valList.add(val);
    }

    //CdtRtu转发通道
    List<SenderCdtRtuTunnelInfo> senderCdtRtuTunnelInfos = tunnelInfoService.getAllSenderCdtRtuTunnel();
    if (null != senderCdtRtuTunnelInfos && senderCdtRtuTunnelInfos.size() > 0) {
      for (SenderCdtRtuTunnelInfo bean : senderCdtRtuTunnelInfos) {
        bean.setStationCode(header);
      }
      val = "SenderCdtRtuTunnelInfo:::" + JsonBeanUtil.beanToJson(senderCdtRtuTunnelInfos);
      valList.add(val);
    }
    //ModbusRtu 转发通道
    List<SenderModbusRtuTunnelInfo> senderModbusRtuTunnelInfos =
      tunnelInfoService.getAllSenderModbusRtuTunnel();
    if (null != senderModbusRtuTunnelInfos && senderModbusRtuTunnelInfos.size() > 0) {
      for (SenderModbusRtuTunnelInfo bean : senderModbusRtuTunnelInfos) {
        bean.setStationCode(header);
      }
      val = "SenderModbusRtuTunnelInfo:::" + JsonBeanUtil.beanToJson(senderModbusRtuTunnelInfos);
      valList.add(val);
    }
    //ModbusTcp 转发通道
    List<SenderModbusTcpTunnelInfo> senderModbusTcpTunnelInfos =
      tunnelInfoService.getAllSenderModbusTcpTunnel();
    if (null != senderModbusTcpTunnelInfos && senderModbusTcpTunnelInfos.size() > 0) {
      for (SenderModbusTcpTunnelInfo bean : senderModbusTcpTunnelInfos) {
        bean.setStationCode(header);
      }
      val = "SenderModbusTcpTunnelInfo:::" + JsonBeanUtil.beanToJson(senderModbusTcpTunnelInfos);
      valList.add(val);
    }

    //转发点表
    List<ProtocolSenderDataPoint> protocolSenderDataPoints = protocolSenderDataPointService.getAll();
    if (null != protocolSenderDataPoints && protocolSenderDataPoints.size() > 0) {
      for (ProtocolSenderDataPoint bean : protocolSenderDataPoints) {
        bean.setStationCode(header);
      }
      val = "ProtocolSenderDataPoint:::" + JsonBeanUtil.beanToJson(protocolSenderDataPoints);
      valList.add(val);
    }
    //接入点表
    List<ProtocolGatherDataPoint> protocolGatherDataPoints = protocolGatherDataPointService.getAll();
    if (null != protocolGatherDataPoints && protocolGatherDataPoints.size() > 0) {
      for (ProtocolGatherDataPoint bean : protocolGatherDataPoints) {
        bean.setStationCode(header);
      }
      val = "ProtocolGatherDataPoint:::" + JsonBeanUtil.beanToJson(protocolGatherDataPoints);
      valList.add(val);
    }

    // -------------------------20220615 新增agc/avc 升压站 南方电网设备-----------------------------------
      //gac/avc
      List<AGC_AVCInfo> agcAvcInfoList = agcAvcInfoService.getAll();
      if (null != agcAvcInfoList && agcAvcInfoList.size() > 0) {
        for (AGC_AVCInfo bean : agcAvcInfoList) {
          bean.setStationCode(header);
        }
        val = "AgcAvcInfo:::" + JsonBeanUtil.beanToJson(agcAvcInfoList);
        valList.add(val);
      }

      //升压站
      List<BoosterStationInfo> boosterStationInfoList = boosterStationInfoService.getAll();
      if (null != boosterStationInfoList && boosterStationInfoList.size() > 0) {
        for (BoosterStationInfo bean : boosterStationInfoList) {
          bean.setStationCode(header);
        }
        val = "BoosterStationInfo:::" + JsonBeanUtil.beanToJson(boosterStationInfoList);
        valList.add(val);
      }

      //南方电网设备
      List<SubsidiaryEquipmentInfo> subsidiaryEquipmentInfoList = subsidiaryEquipmentInfoService.getAll();
      if (null != subsidiaryEquipmentInfoList && subsidiaryEquipmentInfoList.size() > 0) {
        for (SubsidiaryEquipmentInfo bean : subsidiaryEquipmentInfoList) {
          bean.setStationCode(header);
        }
        val = "SubsidiaryEquipmentInfo:::" + JsonBeanUtil.beanToJson(subsidiaryEquipmentInfoList);
        valList.add(val);
      }

    return valList;
  }


  @RequestMapping(value = "/export")
    public void export(HttpServletResponse response) {
        BufferedOutputStream bos = null;
        try {
            StringBuilder templateContent = new StringBuilder();
            response.setCharacterEncoding("UTF-8");
            ElectricField electricField = electricFieldService.get();
            String header = "\"场站编号\"" + "," + "\"场站状态\"" + "," + "\"场站名称\"" + "," + "\"场站标识\"" + "," + "\"别名\"" + "," + "\"装机容量\"" + "," + "\"并网设备数\"" + "," + "\"场站经度\"" + "," + "\"场站纬度\"" + "," + "\"场站类型\"" + "," + "\"上报省调\"" + "," + "\"入库时间\"" + "," + "\"场站海拔\"" + "," + "\"场站所属公司\"" + "," + "\"场站位置\"" + "," + "\"场站面积\"" + "," + "\"合同结束日期\"" + "\r\n";

            StringBuilder content = new StringBuilder();

            content.append(electricField.getStationCode() + ",");
            content.append(electricField.getStationStatusEnum().getMessage() + ",");
            content.append(electricField.getName() + ",");
            content.append(electricField.getSign() + ",");
            content.append(electricField.getNetSubstationName() + ",");
            content.append(electricField.getCapacity() + ",");
            content.append(electricField.getGridCE() + ",");
            content.append(electricField.getLongitude() + ",");
            content.append(electricField.getLatitude() + ",");
            content.append(electricField.getElectricFieldTypeEnum().getMessage() + ",");
            content.append(electricField.getProvinceEnum().getMessage() + ",");
            content.append(electricField.getInterval() + ",");
            content.append(electricField.getAltitude() + ",");
            content.append(electricField.getCompany() + ",");
            content.append(electricField.getLocation() + ",");
            content.append(electricField.getArea() + ",");
            content.append(electricField.getBackupC()+ ",");



            templateContent.append(header);
            templateContent.append(content.toString());
            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("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();
            templateContent = null;

        } 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);
                }
        }

    }

}
