package com.ruoyi.common.utils.chartForWord;
import com.ruoyi.common.utils.BarChart;
import com.ruoyi.common.utils.chartForWord.CustomXWPFDocument;
import org.apache.poi.xwpf.usermodel.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 生成动态word
 * @author  shuaihua zang
 * @date  2022/10/13
 */

public class GenerateWord {

    public static void makeWord(HttpServletResponse response,HttpServletRequest request,int pg2_size,int pg3_size)throws Exception {

        //创建文本对象
        CustomXWPFDocument docxDocument = new CustomXWPFDocument();
        //secondRun.setColor("FFC0CB");
        //secondRun.setUnderline(UnderlinePatterns.SINGLE);
        //paragraph_2_title_run.addCarriageReturn();
        //paragraphX2.setIndentationFirstLine(420);//首行缩进
        //paragraph_2_title.setStyle("2");

        //创建标题
        BarChart.setTitle(docxDocument,"Evaluation report of planting monitoring in ITBA Nature Reserve");

        //创建第一个段落的标题
        BarChart.createFirstLevelTopic(docxDocument, "1.Background information");
        //创建第一个段落的内容
        BarChart.createParagraphAndInsertWord(docxDocument, "Date time:${ct_pg1_1}");
        BarChart.createParagraphAndInsertWord(docxDocument, "Evaluation area:${ct_pg1_2}");

        //创建第二段落的标题
        BarChart.createFirstLevelTopic(docxDocument, "2.Results of remote sensing monitoring of seeding success rate");
        //循环判断有多少个资源
        for (int i = 0; i < pg2_size; i++) {
            //创建第二段落的内容
            BarChart.insertPic(docxDocument, "C:\\Users\\xkrs\\Desktop\\pic\\1.png");
            BarChart.setPicTitle(docxDocument, "Spatial distribution data of successful seeding regions");
            BarChart.insertPic(docxDocument, "C:\\Users\\xkrs\\Desktop\\pic\\2.png");
            BarChart.setPicTitle(docxDocument, "Spatial distribution data of seeding success rate");
            BarChart.createParagraphAndInsertWord(docxDocument, "${ct_pg2_wz_1}.The total planting area in ${ct_pg2_wz_2} area was ${ct_pg2_wz_3} hm2, the vegetation survival area was ${ct_pg2_wz_4} hm2, and the seeding success rate was ${ct_pg2_wz_5} %.");
        }
        //第三段的标题
        BarChart.createFirstLevelTopic(docxDocument, "3.Results of remote sensing monitoring of vegetation health");
        for(int i = 1; i <=pg3_size; i=i+2) {
            int j=i+1;
            //第三段的内容
            //第三段的第一个小标题
            BarChart.createSecondLevelTopic(docxDocument, "3."+i+".Monitoring results of medium resolution data");
            //第三段的第一个小标题的内容
            BarChart.insertPic(docxDocument, "C:\\Users\\xkrs\\Desktop\\pic\\2.png");
            BarChart.setPicTitle(docxDocument, "Spatial distribution data of vegetation planting area");
            BarChart.insertPic(docxDocument, "C:\\Users\\xkrs\\Desktop\\pic\\1.png");
            BarChart.setPicTitle(docxDocument, "Spatial distribution data of vegetation health status");
            //插入柱状图表
            String[] xAxisData = new String[]{"Health", "Normal", "Not-Health"};
            Integer[] yAxisData = new Integer[]{1000, 700, 1200};
            //BarChart.drawTable(docxDocument,xAxisData,yAxisData);


            BarChart.setPicTitle(docxDocument, "Statistical data on vegetation health");
            BarChart.createParagraphAndInsertWord(docxDocument, "2022-06-04.Based on the monitoring results of high resolution satellite images, the area of healthy vegetation growth in 01 region was 1202.8 hm2, accounting for 40.7%. The area with normal vegetation growth was 749.9 hm2, accounting for 25.4%. The area of unhealthy vegetation growth was 1000.2 hm2, accounting for 33.9%.");
            //第三段的第二个小标题
            BarChart.createSecondLevelTopic(docxDocument, "3."+j+".Monitoring results of high resolution data");
            //第三段的第二个小标题的内容
            BarChart.insertPic(docxDocument, "C:\\Users\\xkrs\\Desktop\\pic\\1.png");
            BarChart.setPicTitle(docxDocument, "Spatial distribution data of vegetation planting area");
            BarChart.insertPic(docxDocument, "C:\\Users\\xkrs\\Desktop\\pic\\2.png");
            BarChart.setPicTitle(docxDocument, "Spatial distribution data of vegetation health status");
            //此处插入图表
            String[] xAxisData1 = new String[]{"Health", "Normal", "Not-Health"};
            Integer[] yAxisData1 = new Integer[]{1000, 700, 1200};
            //BarChart.drawTable(docxDocument,xAxisData1,yAxisData1);
            BarChart.setPicTitle(docxDocument, "Statistical data on vegetation health");
            BarChart.createParagraphAndInsertWord(docxDocument, "2022-06-04.Based on the monitoring results of high resolution satellite images, the area of healthy vegetation growth in 01 region was 1202.8 hm2, accounting for 40.7%. The area with normal vegetation growth was 749.9 hm2, accounting for 25.4%. The area of unhealthy vegetation growth was 1000.2 hm2, accounting for 33.9%.");

        }

        //第四个标题
        BarChart.createFirstLevelTopic(docxDocument,"4.Results of remote sensing monitoring of planting suitability");
        //第四个标题的内容
        BarChart.insertPic(docxDocument,"C:\\Users\\xkrs\\Desktop\\pic\\1.png");
        BarChart.setPicTitle(docxDocument,"Spatial distribution data of planting suitability");
        //此处插入图表
        String[] xAxisData2 = new String[]{"Health", "Normal", "Not-Health"};
        Integer[] yAxisData2 = new Integer[]{1000, 700, 1200};
        //BarChart.drawTable(docxDocument,xAxisData2,yAxisData2);
        BarChart.setPicTitle(docxDocument,"Spatial distribution data of planting suitability");
        BarChart.createParagraphAndInsertWord(docxDocument,"In 01 region, the area of very suitable region was 1202.8 hm2, accounting for 40.7%. The area of suitable grade was 749.9 hm2, accounting for 25.4%. The area of unsuitable area was 1000.2 hm2, accounting for 33.9%.");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/msword");
        //文件名
        String fileName = "Evaluation report of planting monitoring in ITBA Nature Reserve - V2.0.docx";
        response.setHeader("Content-Disposition","attachment;fileName="+new

        String(fileName.getBytes("UTF-8"), "ISO-8859-1"));

        ServletOutputStream responseOutputStream = response.getOutputStream();
        docxDocument.write(responseOutputStream);
        responseOutputStream.flush();
        responseOutputStream.close();

}

}
