导出

package com.jxre.bigdata.service_impl.sys.data_statistics.project_statistics;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriUtils;

import com.alibaba.druid.util.StringUtils;
import com.jxre.bigdata.dao.CTProjectBuildInfoGridAcceptDao;
import com.jxre.bigdata.entity.PovertyDistributedPowerStationEntity;
import com.jxre.bigdata.exception.ExcelException;
import com.jxre.bigdata.service.sys.data_statistics.project_statistics.InstallRatioStatisticsService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class InstallRatioStatisticsServiceImpl implements InstallRatioStatisticsService {

    @Autowired
    public CTProjectBuildInfoGridAcceptDao dao;
    /**
     * 风电项目类型
     */
    public static String PROJECT_TYPE_WIND = "1";
    /**
     * 光伏项目类型
     */
    public static String PROJECT_TYPE_LIGHT = "2";
    /**
     * 生物质项目类型
     */
    public static String PROJECT_TYPE_BIOMASS = "3";
    
    
    /**
     * 获取项目信息表中所有市以及并网受理情况表中所有并网时间的年份
     */
    @Override
    public Map<String,Object> findCityAndYear() {
        
        Map<String,Object> resultMap = new HashMap<String,Object>();
        //获取查询条件中的年份
        List<Map<String,Object>> yearList = getYear();
        //获取查询条件中的城市
        List<Map<String,Object>> cityList = getCity();
        
        resultMap.put("city", cityList);
        resultMap.put("year", yearList);
        return resultMap;
    }
    
    /**
     * 获取项目建设信息(并网受理情况)表中并网时间的年份
     * @return
     */
    private List<Map<String,Object>> getYear(){
        
        List<Map<String,Object>> yearList = new ArrayList<Map<String,Object>>();
        for(String year : dao.selectYearByGroup()) {
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("year", year);
            yearList.add(map);
        }
        return yearList;
    }
    
    /**
     * 获取项目信息表中城市
     * @return
     */
    private List<Map<String,Object>> getCity(){
        
        List<Map<String,Object>> cityList = new ArrayList<Map<String,Object>>();
        for(String city : dao.selectAllCityNameByGroup()) {
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("city", city);
            cityList.add(map);
        }
        return cityList;
    }
    
    /**
     * 获取各市/县的各项目类型的并网装机容量以及
     * 每个市/县的各项目类型的并网装机容量占比
     */
    @Override
    public Map<String, Object> findCityEachProjectTypeGridCapacity(Map<String,String> map) {
        
        log.info("接收参数map(应包含year和area):"+map);
        Map<String,Object> resultMap = new HashMap<String,Object>();
        log.info("当参数中的area为空时,执行if下两个方法,获取柱状图以及表格的数据;当area不为空时,执行else下两个方法,获取柱状图以及表格的数据");
        log.info("---查询元素"+map.get("area")); 
        if(StringUtils.isEmpty(map.get("area"))) {
            //查询各市的各项目类型并网装机容量
            resultMap.put("HistogramData",findHistogramDataEveryCity(map.get("year")));
            resultMap.put("tableData", findTableDataEveryCity(map.get("year")));
        }else {
            //查询各县的各项目类型并网装机容量
            resultMap.put("HistogramData", findHistogramDataEveryCounty(map.get("year"), map.get("area")));
            resultMap.put("tableData", findTableDataEveryCounty(map.get("year"), map.get("area")));
        }
        return resultMap;
    }
    
    /**
     * 获取每个市对应的柱状图所需的风电、光伏、生物质的并网装机容量
     */
    private Map<String, Object> findHistogramDataEveryCity(String year) {
        
        log.info("接收参数year:"+year);
        Map<String,Object> resultMap = new HashMap<String,Object>();
        Map<String,Object> paramsMap = new HashMap<String,Object>();
        log.info("如果参数year为空,设置powerTime为2018,否则powerTime即为参数year的值");
        if(StringUtils.isEmpty(year)) {
            paramsMap.put("powerTime", 2018);
        }else {
            paramsMap.put("powerTime", year);
        }
        //获取项目信息表中所有城市名称
        List<String> cityList = dao.selectAllCityNameByGroup();
        //查询风电类型的并网装机
        List<Double> windDataList = findWindProjectTypeGCEveryCity(year,cityList,paramsMap);
        //查询光伏类型的并网装机
        List<Double> lightDataList = findLightProjectTypeGCEveryCity(year,cityList,paramsMap);
        //查询生物质类型的并网装机
        List<Double> biomassDataList = findBiomassProjectTypeGCEveryCity(year,cityList,paramsMap);
        
        resultMap.put("cityList", cityList);
        resultMap.put("windDataList", windDataList);
        resultMap.put("lightDataList", lightDataList);
        resultMap.put("biomassDataList", biomassDataList);
        return resultMap;
    }
    
    /**
     * 查询每个城市的风电类型的并网装机
     * @param year
     * @param cityList
     * @return
     */
    private List<Double> findWindProjectTypeGCEveryCity(String year,List<String> cityList,Map<String,Object> paramsMap){
        
        log.info("接收参数year,cityList,paramsMap:"+year+","+cityList+","+paramsMap);
        List<Double> windDataList = new ArrayList<Double>();
        paramsMap.put("type", PROJECT_TYPE_WIND);
        log.info("循环遍历每个城市,查询每个城市的风电类型并网装机容量");
        for(int i=0;i<cityList.size();i++) {
            paramsMap.put("cityName", cityList.get(i));
            windDataList.add(dao.selectAllCityEachProjectTypeOfGridCapacity(paramsMap));
        }
        return windDataList;
    }
    
    /**
     *查询每个城市的光伏类型的并网装机
     * @param year
     * @param cityList
     * @return
     */
    private List<Double> findLightProjectTypeGCEveryCity(String year,List<String> cityList,Map<String,Object> paramsMap){
        
        log.info("接收参数year,cityList,paramsMap:"+year+","+cityList+","+paramsMap);
        List<Double> lightDataList = new ArrayList<Double>();
        paramsMap.put("type", PROJECT_TYPE_LIGHT);
        log.info("开始遍历每个城市,获取其光伏类型的并网装机");
        for(int i=0;i<cityList.size();i++) {
            paramsMap.put("cityName", cityList.get(i));
            lightDataList.add(dao.selectAllCityEachProjectTypeOfGridCapacity(paramsMap));
        }
        return lightDataList;
    }
    
    /**
     * 查询每个城市的生物质类型的并网装机
     * @param year
     * @param cityList
     * @return
     */
    private List<Double> findBiomassProjectTypeGCEveryCity(String year,List<String> cityList,Map<String,Object> paramsMap){
        
        log.info("接收参数year,cityList,paramsMap:"+year+","+cityList+","+paramsMap);
        List<Double> biomassDataList = new ArrayList<Double>();
        paramsMap.put("type", PROJECT_TYPE_BIOMASS);
        log.info("开始遍历每个城市,获取其生物质类型的并网装机");
        for(int i=0;i<cityList.size();i++) {
            paramsMap.put("cityName", cityList.get(i));
            biomassDataList.add(dao.selectAllCityEachProjectTypeOfGridCapacity(paramsMap));
        }
        return biomassDataList;
    }
    
    /**
     * 获取表格中每一行是一个城市的并网装机数据
     */
    private List<Map<String,Object>> findTableDataEveryCity(String year) {
        
        log.info("接收参数year:"+year);
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        Map<String,Object> histogramMap = findHistogramDataEveryCity(year);
        List<String> cityList = (List<String>) histogramMap.get("cityList");
        List<Double> windDataList = (List<Double>) histogramMap.get("windDataList");
        List<Double> lightDataList = (List<Double>) histogramMap.get("lightDataList");
        List<Double> biomassDataList = (List<Double>) histogramMap.get("biomassDataList");
        log.info("获取参数cityList:"+cityList+",windDataList:"+windDataList+",lightDataList:"+lightDataList+",biomassDataList:"+biomassDataList);
        
        for(int i=0;i<cityList.size();i++) {
            //获取每个城市的总并网装机数
            double allGridDevice = windDataList.get(i)+lightDataList.get(i)+biomassDataList.get(i);
            
            String allGridDevice2 = windDataList.get(i)+lightDataList.get(i)+biomassDataList.get(i).toString();
            //获取风电占比
            String windRatio = (windDataList.get(i)/allGridDevice*100)+"%";
            //获取光伏占比
            String lightRatio = (lightDataList.get(i)/allGridDevice*100)+"%";
            //获取生物质占比
            String biomassRatio = (biomassDataList.get(i)/allGridDevice*100)+"%";
            //存放表格每一行数据
            Map<String,Object> tableMap = new HashMap<String,Object>();
            
            tableMap.put("area", cityList.get(i));
            tableMap.put("allGridCapacity", allGridDevice);
            tableMap.put("allGridCapacity2", allGridDevice2);
            tableMap.put("windDevice", windDataList.get(i).toString());
            tableMap.put("windRatio", windRatio);
            tableMap.put("lightDevice", lightDataList.get(i).toString());
            tableMap.put("lightRatio", lightRatio);
            tableMap.put("biomassDevice", biomassDataList.get(i).toString());
            tableMap.put("biomassRatio", biomassRatio);
            
            resultList.add(tableMap);
        }
        return resultList;
    }
    
    /**
     * 查询当前城市各个县对应的柱状图所需的风电、光伏、生物质的并网装机数据
     */
    private Map<String, Object> findHistogramDataEveryCounty(String year,String city) {
        
        log.info("接收参数year:"+year+",city:"+city);
        Map<String,Object> resultMap = new HashMap<String,Object>();
        Map<String,Object> paramsMap = new HashMap<String,Object>();
        List<String> countyList = dao.selectAllCountyNameByCity(city);
        log.info("如果year为空,给powerTime设置2018,否则就按当前值");
        if(StringUtils.isEmpty(year)) {
            paramsMap.put("powerTime", 2018);
        }else {
            paramsMap.put("powerTime", year);
        }
        
        //查询风电类型的并网装机
        List<Double> windDataList = findWindProjectTypeGCEveryCounty(countyList,paramsMap);
        //查询光伏类型的并网装机
        List<Double> lightDataList = findLightProjectTypeGCEveryCounty(countyList,paramsMap);
        //查询生物质类型的并网装机
        List<Double> biomassDataList = findBiomassProjectTypeGCEveryCounty(countyList,paramsMap);
        
        resultMap.put("cityList", countyList);
        resultMap.put("windDataList", windDataList);
        resultMap.put("lightDataList", lightDataList);
        resultMap.put("biomassDataList", biomassDataList);
    
        return resultMap;
    }
    
    /**
     * 查询每个县的生物质类型的并网装机
     * @param city
     * @param projectTypeData
     * @param paramsMap
     * @return
     */
    private List<Double> findBiomassProjectTypeGCEveryCounty(List<String> countyList,Map<String,Object> paramsMap){
        
        log.info("接收参数countyList:"+countyList+",paramsMap:"+paramsMap);
        List<Double> biomassDataList = new ArrayList<Double>();
        paramsMap.put("type", PROJECT_TYPE_BIOMASS);
        for(int i=0;i<countyList.size();i++) {
            paramsMap.put("countyName", countyList.get(i));
            biomassDataList.add(dao.selectAllCityEachProjectTypeOfGridCapacity(paramsMap));
        }
        log.info("查询每个县的生物质类型的并网装机方法执行完毕!");
        return biomassDataList;
    }
    
    /**
     * 查询每个县的光伏类型的并网装机
     * @param city
     * @param projectTypeData
     * @param paramsMap
     * @return
     */
    private List<Double> findLightProjectTypeGCEveryCounty(List<String> countyList,Map<String,Object> paramsMap){
        
        log.info("接收参数countyList:"+countyList+",paramsMap:"+paramsMap);
        List<Double> lightDataList = new ArrayList<Double>();
        paramsMap.put("type", PROJECT_TYPE_LIGHT);
        for(int i=0;i<countyList.size();i++) {
            paramsMap.put("countyName", countyList.get(i));
            lightDataList.add(dao.selectAllCityEachProjectTypeOfGridCapacity(paramsMap));
        }
        log.info("查询每个县的生物质类型的并网装机方法执行完毕!");
        return lightDataList;
    }
    
    /**
     * 查询每个县的风电类型的并网装机
     * @param city
     * @param projectTypeData
     * @param paramsMap
     * @return
     */
    private List<Double> findWindProjectTypeGCEveryCounty(List<String> countyList,Map<String,Object> paramsMap){
        
        log.info("接收参数countyList:"+countyList+",paramsMap:"+paramsMap);
        List<Double> windDataList = new ArrayList<Double>();
        paramsMap.put("type", PROJECT_TYPE_WIND);
        for(int i=0;i<countyList.size();i++) {
            paramsMap.put("countyName", countyList.get(i));
            windDataList.add(dao.selectAllCityEachProjectTypeOfGridCapacity(paramsMap));
        }
        log.info("查询每个县的风电类型的并网装机方法执行完毕!");
        return windDataList;
    }
    
    /**
     * 查询表格中每一行是一个县的并网装机数据
     */
    private List<Map<String,Object>> findTableDataEveryCounty(String year,String city) {
        
        log.info("接收参数year:"+year+",city:"+city);
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        Map<String,Object> histogramMap = findHistogramDataEveryCounty(year,city);
        List<String> CountyList = (List<String>) histogramMap.get("cityList");
        List<Double> windDataList = (List<Double>) histogramMap.get("windDataList");
        List<Double> lightDataList = (List<Double>) histogramMap.get("lightDataList");
        List<Double> biomassDataList = (List<Double>) histogramMap.get("biomassDataList");
        log.info("获取参数CountyList:"+CountyList+",windDataList:"+windDataList+",lightDataList:"+lightDataList+",biomassDataList:"+biomassDataList);
        
        for(int i=0;i<CountyList.size();i++) {
            //获取每个县的总并网装机数
            double allGridDevice = windDataList.get(i)+lightDataList.get(i)+biomassDataList.get(i);
            String allGridDevice2 = windDataList.get(i)+lightDataList.get(i)+biomassDataList.get(i).toString();
            //获取风电占比
            String windRatio = (windDataList.get(i)/allGridDevice*100)+"%";
            //获取光伏占比
            String lightRatio = (lightDataList.get(i)/allGridDevice*100)+"%";
            //获取生物质占比
            String biomassRatio = (biomassDataList.get(i)/allGridDevice*100)+"%";
            //存放表格每一行数据
            Map<String,Object> tableMap = new HashMap<String,Object>();
            
            tableMap.put("area", CountyList.get(i));
            tableMap.put("allGridCapacity", allGridDevice);
            tableMap.put("allGridCapacity2", allGridDevice2);
            tableMap.put("windDevice", windDataList.get(i).toString());
            tableMap.put("windRatio", windRatio);
            tableMap.put("lightDevice", lightDataList.get(i).toString());
            tableMap.put("lightRatio", lightRatio);
            tableMap.put("biomassDevice", biomassDataList.get(i).toString());
            tableMap.put("biomassRatio", biomassRatio);
            
            resultList.add(tableMap);
        }
        return resultList;
    }
    //文件导出
    @Override
    public Object downloadExcel(Map<String, String> map) {
        System.out.println("文件导出map="+map.get("area")+"=="+map.get("year"));
         Map<String, Object> m=this.findCityEachProjectTypeGridCapacity(map);
         List<HashMap<String, String>> ms=(List<HashMap<String, String>>) m.get("tableData"); 
         String[] titles = { "区域", "总并网装机", "风电装机", "风电占比","光伏装机","光伏占比","生物质装机","生物质占比"}; 
         System.out.println("文件导出数据组="+ms);
        return educeExcel(titles, ms); 
         //return null;
    }

    /**
     * 
     * @param titles 第一列名
     * @param list 向单元格插入数据
     * @return
     */
    private Object educeExcel(String[] titles,List<HashMap<String, String>> list){ 
        //创建Excel对象
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                HSSFWorkbook workbook = new HSSFWorkbook();
                //创建工作表单
                HSSFSheet sheet = workbook.createSheet("装机占比统计");  
                
                //创建HSSFRow对象 (行)第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
                HSSFRow row = sheet.createRow(0);     
                row.setHeightInPoints(20);// 设备标题的高度
                //创建HSSFCell对象  (单元格)
                HSSFCell cell=null; 
                //设置第一列单元格的列
                for(int i = 0; i < titles.length; i++){
                    cell = row.createCell(i);//列索引从0开始
                    cell.setCellValue(titles[i]);//列名1
                };
                 CellStyle  style =  workbook.createCellStyle();
                 style.setFillPattern(FillPatternType.SOLID_FOREGROUND);  
                 style.setFillForegroundColor(IndexedColors.RED.getIndex());     
                 CellStyle textStyle = workbook.createCellStyle();
                 DataFormat format = workbook.createDataFormat();
                 textStyle.setDataFormat(format.getFormat("@"));
                //设置单元格的值  
                for (int i = 0; i < list.size(); i++) {
                    row = sheet.createRow(i+1);
                    //返回电站业主表中数据(企业名称,企业性质,企业地址,企业能源类型)
                    HashMap<String, String> example = list.get(i);

                    // 第六步,创建单元格,并设置值 
                    String  name = null;
                        name =  example.get("area");
                
                    row.createCell(0).setCellValue(name); 
                    String nature =null;
                        nature = example.get("allGridCapacity2");
                    row.createCell(1).setCellValue(nature); 
                    String address=null;
                    if(example.get("lightRatio") !=null){
                        address = example.get("lightRatio");
                    }
                    row.createCell(2).setCellValue(address); 
                    String a1=null;
                    
                    a1 = example.get("biomassDevice").indexOf("NaN%")!=-1?"0":example.get("biomassDevice");

                    row.createCell(3).setCellValue(a1);
                    String a2=null;
                    
                    a2 = example.get("biomassRatio");

                    row.createCell(4).setCellValue(a2);
                    String a3=null;
                    
                    a3 = example.get("windDevice").indexOf("NaN%")!=-1?"0":example.get("windDevice");

                    row.createCell(5).setCellValue(a3);
                    String a4=null;
                    
                    a4 = example.get("lightDevice");

                    row.createCell(6).setCellValue(a4);
                    String a5=null;
                    
                    a5 = example.get("windRatio").indexOf("NaN%")!=-1?"0":example.get("windRatio");

                    row.createCell(7).setCellValue(a5);
                    System.out.println("数据a1="+a1+"a2"+a2+"a3"+a3+"a4"+a4+"");
                }
                
                //输出Excel文件  
                try {
                    workbook.write(os);
                    workbook.close();
                    String filename_enc = UriUtils.encode(
                            "数据导出.xls", "UTF-8");
                      ResponseEntity<byte[]>response = ResponseEntity
                            .ok()
                            .contentType(
                                    MediaType
                                            .parseMediaType("application/octet-stream"))
                            .header("Access-Control-Expose-Headers",
                                    "Content-Disposition")
                            .header("Content-Disposition",
                                    "attachment; filename*=UTF-8''" + filename_enc) // 例子:"attachment;
                            .body(os.toByteArray()); // filename*=UTF-8''%EC%82%AC%EB%B3%B8%20-DSC02499.jpg"
                      return response;
                } catch (Exception e) {    
                    throw new ExcelException("服务器忙");
                     
                }    
            
    }
    
}
 

猜你喜欢

转载自blog.csdn.net/weixin_41722928/article/details/84339680