package cn.report.test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.report.template.poi.XWPFTemplate;
import cn.report.template.poi.data.MergeCellRule;
import cn.report.template.poi.data.RowRenderData;
import cn.report.template.poi.data.Rows;
import cn.report.template.poi.data.TableRenderData;
import cn.report.template.poi.data.Tables;
import cn.util.core.IsKit;


/**
 * https://juejin.cn/post/7039954144668418056
 * */
public class POITLTest {

	public static void main(String[] args) throws IOException {
		/**OK 
		测试图表的插入：多系列多类别折线图
		Map<String, Object> elephant = new HashMap<String, Object>();

		//将数据存储为了后边生成图样式
		List<String> devname2 = new ArrayList<String>();
		List<Integer> useRate2 = new ArrayList<Integer>();
		List<Integer> useYear2 = new ArrayList<Integer>();

		for(int i = 0; i < 3; i++){
			devname2.add("电灯_"+i);//X坐标
			useRate2.add(i+3);
			useYear2.add(i+1);
		}
		ChartMultis cm = Charts.ofMultiSeries("多系列条形图",devname2.toArray(new String[devname2.size()]));
		List<Map<String,Object>> rlist = new ArrayList<Map<String,Object>>();
		//数据与图形模板要匹配
		Map<String,Object> map = new HashMap<>();
		map.put("r", "成年象");
		map.put("c", useYear2);
		rlist.add(map);
		map = new HashMap<>();
		map.put("r", "幼7象");
		map.put("c",useRate2);
		rlist.add(map);
		map = new HashMap<>();
		map.put("r", "幼象");
		map.put("c", useYear2);
		rlist.add(map);
		if(rlist.size() > 0) {
			//Collections.reverse(rlist);
			 for(int i = 0;i < rlist.size(); i ++){
				 Map<String,Object> mapT = rlist.get(i);
				 String t = TypeUtil.objToStr(mapT.get("r"));
				 List<Integer> t1 = TypeUtil.cast(mapT.get("c"));
				 System.out.println("1:"+t);
				 System.out.println("2:"+t1);
				 cm.addSeries(t,t1.toArray(new Integer[t1.size()]));
		     }
		}else {
			cm.addSeries("",new Integer[] {0});
		}
		ChartMultiSeriesRenderData chart = cm.create();
		elephant.put("name", "大象");
		elephant.put("chart", chart);
		Map<String, Object> giraffe = new HashMap<String, Object>() {
			{
				put("name", "长颈鹿");
				//	put("picture", Pictures.ofLocal("C:/bjtu/template/lu.png").size(100, 120).create());
				put("chart",
						Charts.ofMultiSeries("长颈鹿生存现状", new String[] { "2018年", "2019年", "2020年" })
						.addSeries("成7年鹿", new Integer[] { 500, 600, 700 })
						.addSeries("幼鹿", new Integer[] { 200, 300, 400 })
						.create());

			}
		};
		List<Map<String, Object>> animals = Arrays.asList(elephant, giraffe);
		XWPFTemplate.compile("C:/bjtu/template/picture.docx").render(new HashMap<String, Object>() {
			{
				put("animals", animals);
			}
		}).writeToFile("C:/bjtu/template/out_picture.docx");
		*/
	}


	/**1. 单系列饼图

	Integer[] r = {0,0,0,0};

	ChartSingleSeriesRenderData issueServerityPie = Charts

			.ofSingleSeries("问题严重等级分布", new String[] { "1", "2","3","4" })

			.series("饼图",  r)

			.create();

	//2. 多系列单类别条形图（循环添加rlist中的数据，系列数为rlist.size()）

	public ChartMultiSeriesRenderData bar(Integer id) {

		List<Map<String,Long>> rlist = getById(id);

		ChartMultis cm = Charts.ofMultiSeries("多系列条形图", new String[] { " " });

		if(rlist.size() > 0) {

			Collections.reverse(rlist);

			rlist.forEach(r -> cm.addSeries(StringKit.of(r.get("r")), new Integer[] {NumberKit.parseInt(r.get("c"))}));

		}else {

			cm.addSeries("",new Integer[] {0});

		}

		ChartMultiSeriesRenderData chart = cm.create();

		return chart;

	}


	//多系列多类别折线图(intlist1 ,intlist2 ,intlist3 ,intlist4 总共有4个系例，类别数为任意intlist的size，每个系列的类别数必须相同

	public ChartMultiSeriesRenderData line(Integer id) {

		List<Data> datalist = getById(id);

		List<String> orderlist = new ArrayList<>();

		List<Integer> intlist1 = new ArrayList<>();

		List<Integer> intlist2 = new ArrayList<>();

		List<Integer> intlist3 = new ArrayList<>();

		List<Integer> intlist4 = new ArrayList<>();

		if(datalist .size() > 0) {

			Collections.reverse(datalist );

			datalist .forEach(r -> {

				orderlist.add(r.getOrdernumber().toString());

				intlist1.add(NumberKit.parseInt(r.getNew_issue_count()));

				intlist2.add(NumberKit.parseInt(r.getExist_issue_count()));

				intlist3.add(NumberKit.parseInt(r.getFixed_issue_count()));

				intlist4.add(NumberKit.parseInt(r.getDisable_issue_count()));

			});

		}else {

			orderlist.add("0");

			intlist1.add(0);

			intlist2.add(0);

			intlist3.add(0);

			intlist4.add(0);

		}

		ChartMultiSeriesRenderData  chart = Charts.ofComboSeries("问题趋势图",orderlist.toArray(new String[orderlist.size()]))

				.addLineSeries("1",intlist1.toArray(new Integer[intlist1.size()]))

				.addLineSeries("2",intlist2.toArray(new Integer[intlist2.size()]))

				.addLineSeries("3",intlist3.toArray(new Integer[intlist3.size()]))

				.addLineSeries("4",intlist4.toArray(new Integer[intlist4.size()]))

				.create();

		return chart;

	}

*/
	

	/**设置表格宽度 setTableRenderDataAndColWidth
	 * 表格赋值，
	 * 设置列宽和合并单元格
	 *
	 * @param rowRenderDataArray
	 * @param cellLength
	 * @return
	 */
	private static TableRenderData setTableRenderDataAndColWidth(RowRenderData[] rowRenderDataArray, Integer cellLength) {
		//table赋值set方法需要list
		List<RowRenderData> RowRenderDataList = Arrays.asList(rowRenderDataArray);
		//设置列宽：
		double[] colWidthsCm = new double[cellLength];
		for (int i = 0; i < cellLength; i++) {
			// "章节号", "项目", "内容" 设置为 2 ；这几列宽度大
			if (i == 1 || i == 2 || i == 3) {
				colWidthsCm[i] = 2D;
			} else {
				colWidthsCm[i] = 1D;
			}
		}
		//18.450000762939453D A4纸张
		TableRenderData check_table = Tables.ofWidth(18.450000762939453D, colWidthsCm).center().create();
		check_table.setRows(RowRenderDataList);
		//合并单元格
		MergeCellRule.MergeCellRuleBuilder mergeCellRuleBuilder = mergeCell(cellLength);
		check_table.setMergeRule(mergeCellRuleBuilder.build());

		return check_table;
	}
	/**
	 * 表格 合并单元格
	 *
	 * @param cellLength
	 * @return
	 */
	private static MergeCellRule.MergeCellRuleBuilder mergeCell(Integer cellLength) {
		/**
		 * 设置表格合并规则 从0开始
		 * 1.起始行 MergeCellRule.Grid.of(i, j) i: 行 j: 列
		 * 2.结束行 MergeCellRule.Grid.of(i, j) i: 行 j: 列
		 */
		//合并单元格
		//合并到【备注】前一列
		MergeCellRule.MergeCellRuleBuilder mergeCellRuleBuilder = MergeCellRule.builder();
		mergeCellRuleBuilder.map(MergeCellRule.Grid.of(0, 0), MergeCellRule.Grid.of(1, 0));//序号合并 上下行合并
		mergeCellRuleBuilder.map(MergeCellRule.Grid.of(0, 1), MergeCellRule.Grid.of(1, 1));//章节号合并
		mergeCellRuleBuilder.map(MergeCellRule.Grid.of(0, 2), MergeCellRule.Grid.of(1, 2));//项目合并
		mergeCellRuleBuilder.map(MergeCellRule.Grid.of(0, 3), MergeCellRule.Grid.of(1, 3));//内容合并
		mergeCellRuleBuilder.map(MergeCellRule.Grid.of(0, 4), MergeCellRule.Grid.of(0, cellLength - 2));//验证类型合并 第一行
		mergeCellRuleBuilder.map(MergeCellRule.Grid.of(0, cellLength - 1), MergeCellRule.Grid.of(1, cellLength - 1));//备注合并
		return mergeCellRuleBuilder;
	}
	/**
	 * 自定义表格宽度和表格合并
	 * https://blog.csdn.net/qunwuhui/article/details/121974873
	 * 在word模板文件中，生成表格位置添加标签：{{#check_table}}
	 * */
	public static void main888(String[] args) throws IOException {

		Map<String, Object> datas = new HashMap<String, Object>();
		// create table
		//word的 表格
		List tableList = new ArrayList<>();
		tableList.add("数据1");
		tableList.add("数据2");
		tableList.add("数据3");
		tableList.add("数据4");
		tableList.add("数据5");
		tableList.add("数据6");
		//验证类型集合
		List<String> typeList = Arrays.asList("MC0","MC1",
				"MC2","MC3","MC4","MC5","MC6",
				"MC7","MC8","MC9");
		//表头是两行
		//第一行数据是固定内容 "序号", "章节号", "项目", "内容", "验证方法", "备注"
		List<String> headerCellListTemp1 = Arrays.asList("序号", "章节号", "项目", "内容", "验证方法");
		List<String> headerCellList1 = new ArrayList<>(headerCellListTemp1);
		//验证方法，可能是多列数据  第一列是验证方法，后面是赋空字符串，用于合并单元格;
		//数据类型：验证方法 "" "" ""
		//验证类型长度
		int typeListSize = typeList.size();
		for (int k = 0; k < typeListSize - 1; k++) {
			headerCellList1.add(" "); //赋空字符串
		}
		headerCellList1.add("备注");

		String[] headerCell1 = headerCellList1.toArray(new String[headerCellList1.size()]);

		RowRenderData header1 = Rows.of(headerCell1).center().create();


		//headerCell2 :验证方法对应列，是动态内容
		List<String> headerCellListTemp2 = Arrays.asList("序号", "章节号", "项目", "内容");
		List<String> headerCellList2 = new ArrayList<>(headerCellListTemp2);
		// 验证方法对应列
		headerCellList2.addAll(typeList);
		//备注对应的列 赋空值
		headerCellList2.add(" ");

		String[] headerCell2 = headerCellList2.toArray(new String[headerCellList2.size()]);
		RowRenderData header2 = Rows.of(headerCell2).center().create();
		//表格 列的数量
		int cellLength = headerCell1.length;

		//无数据只有表头数据 tableList 无值
		if (IsKit.isEmpty(tableList)) {
			RowRenderData[] RowRenderDataHeader = new RowRenderData[2];
			RowRenderDataHeader[0] = header1;
			RowRenderDataHeader[1] = header2;

			TableRenderData check_table = setTableRenderDataAndColWidth(RowRenderDataHeader, cellLength);


			// table 只有表头数据
			datas.put("check_table", check_table);
		} else {
			int length = 2 + tableList.size();
			//行数据 数组
			RowRenderData[] RowRenderData = new RowRenderData[length];

			RowRenderData[0] = header1;
			RowRenderData[1] = header2;

			for (int i = 0; i < tableList.size(); i++) {

				String index = Integer.toString(i + 1);
				List<String> tempList = Arrays.asList("A", "B", "C",
						"D", "E", "F", "G", "H", "I", "G");

				String[] cellString = new String[cellLength];
				cellString[0] = index; //序号
				cellString[1] = "5.1.1";//标题序号
				cellString[2] = "项目"; // 项目
				cellString[3] = "说明"; //内容 ： 描述
				//验证方法对应数据赋值
				if (headerCell2.length > 4) {
					for (int j = 0; j < tempList.size(); j++) {
						cellString[j + 4] = tempList.get(j);
					}
				}
				//备注
				cellString[cellLength - 1] = "备注";
				RowRenderData rowRenderData = Rows.of(cellString).
						center().create();
				//行数据赋值
				RowRenderData[2 + i] = rowRenderData;

			}
			TableRenderData check_table = setTableRenderDataAndColWidth(RowRenderData, cellLength);
			// table
			datas.put("check_table", check_table);

		}
		XWPFTemplate.compile("C:/bjtu/temp/Test.docx")
		.render(datas).writeToFile("C:/bjtu/temp/Ttest1.docx");
	}


}
