package com.qunzhixinxi.hnqz.admin.util;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.*;
import com.deepoove.poi.data.style.TableStyle;
import com.deepoove.poi.util.BytePictureUtils;
import com.qunzhixinxi.hnqz.admin.entity.XlsTask;
import org.apache.poi.xwpf.usermodel.BreakType;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

public class ExportReport {
//	static String basePath="http://172.17.0.1:9999";

	/**
	 * 患者教育服务报告
	 *
	 * @param map
	 */
	public static File main_bulidTemplateHCP(Map<String, Object> map, String fileName) {
		//模板、文件、图片路径
		String workPath = (String) OsEnvUtils.getEachEnvPaths().get("EXCEL_FILE_PATH");
		String outWorkPath = OsEnvUtils.getEachEnvPaths().get("EXCEL_FILE_PATH") + "out/";

		String templateName = "doc_template_hcp.docx";
		Map<String, Object> datas = new HashMap<String, Object>() {
			{
				// 项目名称-年
				put("rule_year", bulidObjectToStr(map.get("rule_year")));
				// 项目名称-月
				put("rule_month", bulidObjectToStr(map.get("rule_month")));
				// 项目名称-日区间
				put("rule_days", bulidObjectToStr(map.get("rule_days")));

				put("wt_ent", bulidObjectToStr(map.get("wt_ent")));
				put("report_users", bulidObjectToStr(map.get("report_users")));
				put("report_ent", bulidObjectToStr(map.get("report_ent")));
				put("year", bulidObjectToStr(map.get("year")));
				put("month", bulidObjectToStr(map.get("month")));
				put("day", bulidObjectToStr(map.get("day")));
				put("totalscorePackageScore", bulidObjectToStr(map.get("totalscorePackageScore")));
				put("totalSubmitScore", bulidObjectToStr(map.get("totalSubmitScore")));

				put("scorePackageName", bulidObjectToStr(map.get("scorePackageName")));
				put("service_date", bulidObjectToStr(map.get("service_date")));
				put("scoreId", bulidObjectToStr(map.get("scoreId")));

				TableStyle tableStyleHeader = new TableStyle();
				tableStyleHeader.setAlign(STJc.Enum.forInt(2));
				tableStyleHeader.setBackgroundColor("cccccc");

				TableStyle tableStyle = new TableStyle();
				tableStyle.setAlign(STJc.Enum.forInt(2));

				//自定义表格
				RowRenderData header = RowRenderData.build(new TextRenderData("序号"), new TextRenderData("医生姓名"),
						new TextRenderData("处方单编号"), new TextRenderData("开具处方时间"), new TextRenderData("患教服务费"));
				header.setRowStyle(tableStyle);
				// 表格
				List<RowRenderData> ztList = new ArrayList<>();
				if (null != map.get("prescriptionTable")) {
					List<Map<String, Object>> ztMapList = (List<Map<String, Object>>) map.get("prescriptionTable");

					if (ztMapList.size() > 0) {
						for (Map<String, Object> map : ztMapList) {
							RowRenderData ryrow0 = RowRenderData.build(bulidObjectToStr(map.get("number")),
									bulidObjectToStr(map.get("doctorName")), bulidObjectToStr(map.get("prescriptionNo")),
									bulidObjectToStr(map.get("prescriptionTime")), bulidObjectToStr(map.get("hcpServiceCharge")));
							ryrow0.setRowStyle(tableStyle);
							ztList.add(ryrow0);
						}
					}
				}
				put("prescriptionTable", new MiniTableRenderData(header, ztList));

				// 处方详情
				if (null != map.get("prescriptionList")) {
					put("prescriptionList", map.get("prescriptionList"));
				}
			}
		};

		return generateWord(datas, workPath + templateName, outWorkPath, fileName);
	}

	/**
	 * 内部企业服务报告
	 *
	 * @param map
	 */
	public static File main_bulidTemplate1(Map<String, Object> map, Integer type,String basePath) {
		//模板、文件、图片路径
		String workPath = "/excel/";
		String outWorkPath = "/excel/out/";

		String userType= bulidObjectToStr(map.get("userType"));
		String templateName = "";
		if (null != type && 1 == type) {
			templateName = "doc_template1.docx";

		} else {
			if((!userType.equals(""))&&userType.equals("3")){//全职学术推广员
				templateName = "doc_template12.docx";
			}else {
				templateName = "doc_template11.docx";
			}

		}
		Map<String, Object> datas = new HashMap<String, Object>() {
			{
				//文本
				put("rule_year", bulidObjectToStr(map.get("rule_year")));
				put("rule_month", bulidObjectToStr(map.get("rule_month")));
				put("wt_ent", bulidObjectToStr(map.get("wt_ent")));
				put("report_users", bulidObjectToStr(map.get("report_users")));
				put("report_ent", bulidObjectToStr(map.get("report_ent")));
				put("year", bulidObjectToStr(map.get("year")));
				put("month", bulidObjectToStr(map.get("month")));
				put("day", bulidObjectToStr(map.get("day")));
				put("tMoney", bulidObjectToStr(map.get("tMoney")));
				put("totalscorePackageScore", bulidObjectToStr(map.get("totalscorePackageScore")));
				put("totalSubmitScore", bulidObjectToStr(map.get("totalSubmitScore")));
				put("submitAmount", bulidObjectToStr(map.get("submitAmount")));
				put("settleAmount", bulidObjectToStr(map.get("settleAmount")));
				put("totalscorePackageNum", bulidObjectToStr(map.get("totalscorePackageNum")));
				put("totalSubmitNum", bulidObjectToStr(map.get("totalSubmitScore")));

				put("scorePackageName", bulidObjectToStr(map.get("scorePackageName")));
				put("drugEnt", bulidObjectToStr(map.get("drugEnt")));
				put("service_date", bulidObjectToStr(map.get("service_date")));
				put("scoreId", bulidObjectToStr(map.get("scoreId")));


				TableStyle tableStyleHeader = new TableStyle();
				tableStyleHeader.setAlign(STJc.Enum.forInt(2));
				tableStyleHeader.setBackgroundColor("cccccc");


				TableStyle tableStyle = new TableStyle();
				tableStyle.setAlign(STJc.Enum.forInt(2));


				//自定义表格
				RowRenderData header = RowRenderData.build(new TextRenderData("任务类型"),
						new TextRenderData("活动次数"),
						new TextRenderData("合计积分"));
				header.setRowStyle(tableStyle);

				List<RowRenderData> ztList = new ArrayList<>();

				if (null != map.get("ztTable")) {
					List<Map<String, Object>> ztMapList = (List<Map<String, Object>>) map.get("ztTable");

					if (ztMapList.size() > 0) {
						for (Map<String, Object> map : ztMapList) {
							RowRenderData ryrow0 = RowRenderData.build(bulidObjectToStr(map.get("taskTypeName"))
									, bulidObjectToStr(map.get("taskCount"))
									, bulidObjectToStr(map.get("taskSum")));
							ryrow0.setRowStyle(tableStyle);
							ztList.add(ryrow0);
						}
					}
				}

				put("ztTable", new MiniTableRenderData(header, ztList));


				//自定义表格
				RowRenderData headerRy = RowRenderData.build(new TextRenderData("代表姓名"),
						new TextRenderData("任务计数"),
						new TextRenderData("累计积分"));
				headerRy.setRowStyle(tableStyleHeader);

				List<RowRenderData> ryList = new ArrayList<>();

				if (null != map.get("ryTable")) {
					Map<String, Object> ryMapList = (Map<String, Object>) map.get("ryTable");

					if (ryMapList.size() > 0) {
						Iterator iterator = ryMapList.keySet().iterator();
						Integer totalCount = 0;
						Integer totalSum = 0;
						while (iterator.hasNext()) {
							String key = iterator.next().toString();
							if (!key.contains("_total")) {
								String[] strs = key.split("_");
								if (strs.length == 3) {
									try {
										totalCount += Integer.parseInt(ryMapList.get(key + "_total") + "");
										totalSum += Integer.parseInt(ryMapList.get(key) + "");
									} catch (Exception ex) {
										ex.printStackTrace();
									}

									RowRenderData ryrow0 = RowRenderData.build(bulidObjectToStr(strs[0])
											, bulidObjectToStr(bulidObjectToStr(ryMapList.get(key + "_total")))
											, bulidObjectToStr(bulidObjectToStr(ryMapList.get(key))));
									ryrow0.setRowStyle(tableStyle);
									ryList.add(ryrow0);
								}
							}
						}
						RowRenderData ryrow0 = RowRenderData.build(bulidObjectToStr("合计")
								, bulidObjectToStr(bulidObjectToStr(totalCount))
								, bulidObjectToStr(totalSum));
						ryrow0.setRowStyle(tableStyle);
						ryList.add(ryrow0);
					}
				}
				put("ryTable", new MiniTableRenderData(headerRy, ryList));

				//自定义表格
				RowRenderData headerMx = RowRenderData.build(new TextRenderData("完成时间"),
						new TextRenderData("任务类型"),
						new TextRenderData("分值"),
						new TextRenderData("姓名"),
						new TextRenderData("所属组织"));
				headerMx.setRowStyle(tableStyle);
				List<RowRenderData> mxList = new ArrayList<>();

				if (null != map.get("mxTable")) {
					List<XlsTask> ztMapList = (List<XlsTask>) map.get("mxTable");
					for (XlsTask xlsTask : ztMapList) {
						RowRenderData mxrow0 = RowRenderData.build(bulidObjectToStr(xlsTask.getCreateTime()),
								bulidObjectToStr(xlsTask.getTaskType()),
								bulidObjectToStr(xlsTask.getScore()),
								bulidObjectToStr(xlsTask.getUsername()),
								bulidObjectToStr(null != xlsTask.getAgentDeptName() ? xlsTask.getAgentDeptName() : xlsTask.getDeptName())
						);
						mxrow0.setRowStyle(tableStyle);
						mxList.add(mxrow0);
					}

				}


				put("mxTable", new MiniTableRenderData(headerMx, mxList));


				//自定义表格
				RowRenderData headerRwMx = RowRenderData.build(new TextRenderData("序号"),
						new TextRenderData("任务时间"),
						new TextRenderData("任务名称"),
						new TextRenderData("分值"),
						new TextRenderData("代表姓名"));
				headerRwMx.setRowStyle(tableStyle);
				List<RowRenderData> rwmxList = new ArrayList<>();

				if (null != map.get("rwmxTable")) {
					List<Map<String, Object>> ztMapList = (List<Map<String, Object>>) map.get("rwmxTable");
					int i = 1;
					for (Map<String, Object> xlsTask : ztMapList) {
						RowRenderData mxrow0 = RowRenderData.build(bulidObjectToStr(i++),
								bulidObjectToStr(bulidObjectToStr(xlsTask.get("taskTime"))),
								bulidObjectToStr(bulidObjectToStr(xlsTask.get("taskType"))),
								bulidObjectToStr(bulidObjectToStr(xlsTask.get("score"))),
								bulidObjectToStr(bulidObjectToStr(xlsTask.get("taskUser"))));
						mxrow0.setRowStyle(tableStyle);
						rwmxList.add(mxrow0);
					}

				}

				put("rwmxTable", new MiniTableRenderData(headerRwMx, rwmxList));
			}
		};
		if(null!=basePath){
			List<Map<String, Object>> infoList= (List<Map<String, Object>>) map.get("infoList");
			Map<String, Object> data2=main_common(infoList,basePath,type);
			datas.putAll(data2);
		}


		return generateWord(datas, workPath + templateName, outWorkPath);
	}

	/**
	 * 定量包
	 *
	 * @param map
	 */
	public static File main_bulidTemplate2(Map<String, Object> map, Integer type, String basePath) {
		//模板、文件、图片路径
		String workPath = "/excel/";
		String outWorkPath = "/excel/out/";

		String userType= bulidObjectToStr(map.get("userType"));//区分全职兼职学术推广员模版
		String templateName = "";
		if (null != type && 1 == type) {
			templateName = "doc_template1-1.docx";
		} else if(null != type && 2 == type) {
			if((!userType.equals(""))&&userType.equals("3")){//全职学术推广员
				templateName = "doc_template12-1.docx";
			}else {
				templateName = "doc_template11-1.docx";
			}
		}else if(null != type && 3 == type) {
			templateName = "doc_template2-1.docx";
		}
		Map<String, Object> datas = new HashMap<String, Object>() {
			{
				//文本
				put("rule_year", bulidObjectToStr(map.get("rule_year")));
				put("rule_month", bulidObjectToStr(map.get("rule_month")));
				put("wt_ent", bulidObjectToStr(map.get("wt_ent")));
				put("report_users", bulidObjectToStr(map.get("report_users")));
				put("report_ent", bulidObjectToStr(map.get("report_ent")));
				put("year", bulidObjectToStr(map.get("year")));
				put("month", bulidObjectToStr(map.get("month")));
				put("day", bulidObjectToStr(map.get("day")));
				put("tMoney", bulidObjectToStr(map.get("tMoney")));
				put("totalscorePackageScore", bulidObjectToStr(map.get("totalscorePackageScore")));
				put("totalSubmitScore", bulidObjectToStr(map.get("totalSubmitScore")));
				put("submitAmount", bulidObjectToStr(map.get("submitAmount")));
				put("settleAmount", bulidObjectToStr(map.get("settleAmount")));
				put("totalscorePackageTaskNum", bulidObjectToStr(map.get("totalscorePackageTaskNum")));
				put("totalSubmitTaskNum", bulidObjectToStr(map.get("totalSubmitTaskNum")));

				put("scoreId", bulidObjectToStr(map.get("scoreId")));
				put("drugEnt", bulidObjectToStr(map.get("drugEnt")));
				put("service_date", bulidObjectToStr(map.get("service_date")));
				put("scoreId", bulidObjectToStr(map.get("scoreId")));


				TableStyle tableStyleHeader = new TableStyle();
				tableStyleHeader.setAlign(STJc.Enum.forInt(2));
				tableStyleHeader.setBackgroundColor("cccccc");


				TableStyle tableStyle = new TableStyle();
				tableStyle.setAlign(STJc.Enum.forInt(2));


				//自定义表格
				RowRenderData header = RowRenderData.build(new TextRenderData("任务类型"),
						new TextRenderData("活动次数"));
				header.setRowStyle(tableStyle);

				List<RowRenderData> ztList = new ArrayList<>();

				if (null != map.get("ztTable")) {
					List<Map<String, Object>> ztMapList = (List<Map<String, Object>>) map.get("ztTable");

					if (ztMapList.size() > 0) {
						for (Map<String, Object> map : ztMapList) {
							RowRenderData ryrow0 = RowRenderData.build(bulidObjectToStr(map.get("taskTypeName"))
									, bulidObjectToStr(map.get("taskCount")));
							ryrow0.setRowStyle(tableStyle);
							ztList.add(ryrow0);
						}
					}
				}

				put("ztTable", new MiniTableRenderData(header, ztList));


				//自定义表格
				RowRenderData headerRy = RowRenderData.build(new TextRenderData("代表姓名"),
						new TextRenderData("任务计数"));
				headerRy.setRowStyle(tableStyleHeader);

				List<RowRenderData> ryList = new ArrayList<>();

				if (null != map.get("ryTable")) {
					Map<String, Object> ryMapList = (Map<String, Object>) map.get("ryTable");

					if (ryMapList.size() > 0) {
						Iterator iterator = ryMapList.keySet().iterator();
						Integer totalCount = 0;
						Integer totalSum = 0;
						while (iterator.hasNext()) {
							String key = iterator.next().toString();
							if (!key.contains("_total")) {
								String[] strs = key.split("_");
								if (strs.length == 3) {
									try {
										totalCount += Integer.parseInt(ryMapList.get(key + "_total") + "");
										totalSum += Integer.parseInt(ryMapList.get(key)==null?"0":ryMapList.get(key) + "");
									} catch (Exception ex) {
										ex.printStackTrace();
									}

									RowRenderData ryrow0 = RowRenderData.build(bulidObjectToStr(strs[0])
											, bulidObjectToStr(bulidObjectToStr(ryMapList.get(key + "_total"))));
									ryrow0.setRowStyle(tableStyle);
									ryList.add(ryrow0);
								}
							}
						}
						RowRenderData ryrow0 = RowRenderData.
								build(bulidObjectToStr("合计")
								, bulidObjectToStr(bulidObjectToStr(totalCount)));
						ryrow0.setRowStyle(tableStyle);
						ryList.add(ryrow0);
					}
				}
				put("ryTable", new MiniTableRenderData(headerRy, ryList));

				//自定义表格
				RowRenderData headerMx = RowRenderData.build(new TextRenderData("完成时间"),
						new TextRenderData("任务类型"),
						new TextRenderData("个数"),
						new TextRenderData("姓名"),
						new TextRenderData("所属组织"));
				headerMx.setRowStyle(tableStyle);
				List<RowRenderData> mxList = new ArrayList<>();

				if (null != map.get("mxTable")) {
					List<XlsTask> ztMapList = (List<XlsTask>) map.get("mxTable");
					for (XlsTask xlsTask : ztMapList) {
						RowRenderData mxrow0 = RowRenderData.build(bulidObjectToStr(xlsTask.getCreateTime()),
								bulidObjectToStr(xlsTask.getTaskType()),
								bulidObjectToStr(xlsTask.getTaskNum()),
								bulidObjectToStr(xlsTask.getUsername()),
								bulidObjectToStr(null != xlsTask.getAgentDeptName() ? xlsTask.getAgentDeptName() : xlsTask.getDeptName())
						);
						mxrow0.setRowStyle(tableStyle);
						mxList.add(mxrow0);
					}

				}


				put("mxTable", new MiniTableRenderData(headerMx, mxList));


				//自定义表格
				RowRenderData headerRwMx = RowRenderData.build(new TextRenderData("序号"),
						new TextRenderData("任务时间"),
						new TextRenderData("任务名称"),
						new TextRenderData("个数"),
						new TextRenderData("代表姓名"));
				headerRwMx.setRowStyle(tableStyle);
				List<RowRenderData> rwmxList = new ArrayList<>();

				if (null != map.get("rwmxTable")) {
					List<Map<String, Object>> ztMapList = (List<Map<String, Object>>) map.get("rwmxTable");
					int i = 1;
					for (Map<String, Object> xlsTask : ztMapList) {
						RowRenderData mxrow0 = RowRenderData.build(bulidObjectToStr(i++),
								bulidObjectToStr(bulidObjectToStr(xlsTask.get("taskTime"))),
								bulidObjectToStr(bulidObjectToStr(xlsTask.get("taskType"))),
								bulidObjectToStr(bulidObjectToStr(xlsTask.get("taskNum"))),
								bulidObjectToStr(bulidObjectToStr(xlsTask.get("taskUser"))));
						mxrow0.setRowStyle(tableStyle);
						rwmxList.add(mxrow0);
					}

				}
				put("rwmxTable", new MiniTableRenderData(headerRwMx, rwmxList));

			}
		};

		if(null!=basePath){
			List<Map<String, Object>> infoList= (List<Map<String, Object>>) map.get("infoList");
			Map<String, Object> data2=main_common(infoList,basePath,type);
			datas.putAll(data2);
		}
		return generateWord(datas, workPath + templateName, outWorkPath);
	}

	/**
	 * 合并详情--定额，定量
	 *
	 * @param mapList
	 */
	public static Map<String, Object> main_common(List<Map<String, Object>> mapList, String basePath,Integer type) {
		//模板、文件、图片路径
			if (mapList.size() > 0) {
				if (type!=3) {
					for (Map<String, Object> map : mapList) {
						Integer imgWidth = 0;
						Integer imgHeight = 0;
						if (null == map.get("imgWidth")) {
							imgWidth = 585;
						} else {
							imgWidth = Integer.parseInt(map.get("imgWidth") + "");
						}

						if (null == map.get("imgHeight")) {
							imgHeight = 900;
						} else {
							imgHeight = Integer.parseInt(map.get("imgHeight") + "");
						}

						try {
							BufferedImage bufferedImage = (BufferedImage) map.get("taskBaseImg");
							if (bufferedImage.getWidth() > bufferedImage.getHeight()) {
								imgWidth = 585;
								imgHeight = 300;

							}
							map.put("taskBaseImg", new PictureRenderData(imgWidth, imgHeight, ".png",
									bufferedImage
							));
						} catch (Exception ex) {
							map.put("taskBaseImg", new PictureRenderData(imgWidth, imgHeight, ".png",
									BytePictureUtils.getUrlBufferedImage(
											basePath + map.get("taskBaseImg")
									)
							));

						}

						List<Map<String, Object>> infoList = new ArrayList<>();
						if (null != map.get("imgList")) {
							List<String> strListImg = (List<String>) map.get("imgList");
							if (strListImg.size() > 0) {
								for (String str : strListImg) {
									Map<String, Object> stringObjectMap = new HashMap<>();
									try {
										if (null != str && !"null".equals(str)) {
											BufferedImage bufferedImage = BytePictureUtils.getUrlBufferedImage(
													basePath + str
											);
											Integer dimgWidth = 585;
											Integer dimgHeight = 900;
											if (bufferedImage.getWidth() > bufferedImage.getHeight()) {
												dimgHeight = sfImage(bufferedImage.getWidth(), 585, bufferedImage.getHeight());

											}

											stringObjectMap.put("taskBaseImgInfo",
													new PictureRenderData(dimgWidth, dimgHeight, ".png", bufferedImage
													)
											);

											stringObjectMap.put("huanhangInfo", sfBr(bufferedImage.getWidth(), 585, bufferedImage.getHeight())

											);
											infoList.add(stringObjectMap);
										}
									} catch (Exception ex) {
										ex.printStackTrace();
									}
								}
							}

							map.put("imgList", infoList);

						}
					}
				}
			}


			Map<String, Object> datas = new HashMap<String, Object>() {
				{
					//文本
					put("infoList", mapList);
					if (type==3) {
						put("rule_year", bulidObjectToStr("2021"));
						TableStyle tableStyleHeader = new TableStyle();
						tableStyleHeader.setAlign(STJc.Enum.forInt(2));
						tableStyleHeader.setBackgroundColor("cccccc");

						TableStyle tableStyle = new TableStyle();
						tableStyle.setAlign(STJc.Enum.forInt(2));
						//自定义表格
						RowRenderData header = RowRenderData.build(new TextRenderData("序号"),
								new TextRenderData("任务类型"),
								new TextRenderData("前端系统"),
								new TextRenderData("项目名称"),
								new TextRenderData("项目编码"));
						header.setRowStyle(tableStyle);
						List<RowRenderData> ztList = new ArrayList<>();
						if (null != mapList) {
							if (mapList.size() > 0) {
								for (Map<String, Object> map : mapList) {
									RowRenderData ryrow0 = RowRenderData.build(bulidObjectToStr(map.get("taskIndex"))
											, bulidObjectToStr(map.get("taskTypeName"))
											, bulidObjectToStr(map.get("temp1"))
											, bulidObjectToStr(map.get("temp2"))
											, bulidObjectToStr(map.get("temp3")));
									ryrow0.setRowStyle(tableStyle);
									ztList.add(ryrow0);
								}
							}
						}
						put("infoTable", new MiniTableRenderData(header, ztList));
					}
				}
			};

		return datas;
	}



	/**
	 * 内部企业服务报告
	 *
	 * @param mapList
	 */
	public static File main_bulidTemplate2(List<Map<String, Object>> mapList, String basePath) {
		//模板、文件、图片路径
		String workPath = "/excel/";
		String outWorkPath = "/excel/out/";

		String templateName = "doc_template2.docx";

		if (mapList.size() > 0) {
			for (Map<String, Object> map : mapList) {
				Integer imgWidth = 0;
				Integer imgHeight = 0;
				if (null == map.get("imgWidth")) {
					imgWidth = 585;
				} else {
					imgWidth = Integer.parseInt(map.get("imgWidth") + "");
				}

				if (null == map.get("imgHeight")) {
					imgHeight = 900;
				} else {
					imgHeight = Integer.parseInt(map.get("imgHeight") + "");
				}

				try {
					BufferedImage bufferedImage = (BufferedImage) map.get("taskBaseImg");
					if (bufferedImage.getWidth() > bufferedImage.getHeight()) {
						imgWidth = 585;
						imgHeight = 300;

					}
					map.put("taskBaseImg", new PictureRenderData(imgWidth, imgHeight, ".png",
							bufferedImage
					));
				} catch (Exception ex) {
					map.put("taskBaseImg", new PictureRenderData(imgWidth, imgHeight, ".png",
							BytePictureUtils.getUrlBufferedImage(
									basePath + map.get("taskBaseImg")
							)
					));
				}

				List<Map<String, Object>> infoList = new ArrayList<>();
				if (null != map.get("imgList")) {
					List<String> strListImg = (List<String>) map.get("imgList");
					if (strListImg.size() > 0) {
						for (String str : strListImg) {
							Map<String, Object> stringObjectMap = new HashMap<>();
							try {
								if (null != str && !"null".equals(str)) {
									BufferedImage bufferedImage = BytePictureUtils.getUrlBufferedImage(
											basePath + str
									);
									Integer dimgWidth = 585;
									Integer dimgHeight = 900;
									if (bufferedImage.getWidth() > bufferedImage.getHeight()) {
										dimgHeight = sfImage(bufferedImage.getWidth(), 585, bufferedImage.getHeight());

									}

									stringObjectMap.put("taskBaseImgInfo",
											new PictureRenderData(dimgWidth, dimgHeight, ".png", bufferedImage
											)
									);

									stringObjectMap.put("huanhangInfo", sfBr(bufferedImage.getWidth(), 585, bufferedImage.getHeight())

									);
									infoList.add(stringObjectMap);
								}
							} catch (Exception ex) {
								ex.printStackTrace();
							}
						}
					}

					map.put("imgList", infoList);

				}
			}
		}

		Map<String, Object> datas = new HashMap<String, Object>() {
			{
				//文本
				put("infoList", mapList);
			}
		};

		return generateWord(datas, workPath + templateName, outWorkPath);
	}

	/**
	 * 结算包--导出详情
	 *
	 * @param mapList
	 */
	public static File main_bulidTemplate2_1(List<Map<String, Object>> mapList, String basePath) {
		//模板、文件、图片路径
		String workPath = "/excel/";
		String outWorkPath = "/excel/out/";
		String templateName = "doc_template2-1.docx";


		Map<String, Object> datas = new HashMap<String, Object>() {
			{
				put("rule_year", bulidObjectToStr("2021"));
				TableStyle tableStyleHeader = new TableStyle();
				tableStyleHeader.setAlign(STJc.Enum.forInt(2));
				tableStyleHeader.setBackgroundColor("cccccc");

				TableStyle tableStyle = new TableStyle();
				tableStyle.setAlign(STJc.Enum.forInt(2));
				//自定义表格
				RowRenderData header = RowRenderData.build(new TextRenderData("序号"),
						new TextRenderData("任务类型"),
						new TextRenderData("前端系统"),
						new TextRenderData("项目名称"),
						new TextRenderData("项目编码"));
				header.setRowStyle(tableStyle);
				List<RowRenderData> ztList = new ArrayList<>();
				if (null != mapList) {
					if (mapList.size() > 0) {
						for (Map<String, Object> map : mapList) {
							RowRenderData ryrow0 = RowRenderData.build(bulidObjectToStr(map.get("taskIndex"))
									, bulidObjectToStr(map.get("taskTypeName"))
									, bulidObjectToStr(map.get("temp1"))
									, bulidObjectToStr(map.get("temp2"))
									, bulidObjectToStr(map.get("temp3")));
							ryrow0.setRowStyle(tableStyle);
							ztList.add(ryrow0);
						}
					}
				}
				put("infoTable", new MiniTableRenderData(header, ztList));
			}
		};

		return generateWord(datas, workPath + templateName, outWorkPath);
	}


	/**
	 * 众包结算
	 *
	 * @param map
	 */
	public static File main_bulidTemplate3(Map<String, Object> map) {
		//模板、文件、图片路径
		String workPath = "/excel/";
		String outWorkPath = "/excel/out/";

		String templateName = "doc_template3.docx";
		Map<String, Object> datas = new HashMap<String, Object>() {
			{
				//文本
				put("rule_year", bulidObjectToStr(map.get("rule_year")));
				put("rule_month", bulidObjectToStr(map.get("rule_month")));
				put("wt_ent", bulidObjectToStr(map.get("wt_ent")));
				put("report_users", bulidObjectToStr(map.get("report_users")));
				put("report_ent", bulidObjectToStr(map.get("report_ent")));
				put("year", bulidObjectToStr(map.get("year")));
				put("month", bulidObjectToStr(map.get("month")));
				put("day", bulidObjectToStr(map.get("day")));
				put("tMoney", bulidObjectToStr(map.get("tMoney")));

				put("scoreId", bulidObjectToStr(map.get("scoreId")));
				put("drugEnt", bulidObjectToStr(map.get("drugEnt")));
				put("service_date", bulidObjectToStr(map.get("service_date")));


				TableStyle tableStyleHeader = new TableStyle();
				tableStyleHeader.setAlign(STJc.Enum.forInt(2));
				tableStyleHeader.setBackgroundColor("cccccc");


				TableStyle tableStyle = new TableStyle();
				tableStyle.setAlign(STJc.Enum.forInt(2));


				//自定义表格
				RowRenderData header = RowRenderData.build(new TextRenderData("任务类型"),
						new TextRenderData("活动次数"),
						new TextRenderData("合计积分"));
				header.setRowStyle(tableStyle);

				List<RowRenderData> ztList = new ArrayList<>();

				if (null != map.get("ztTable")) {
					List<Map<String, Object>> ztMapList = (List<Map<String, Object>>) map.get("ztTable");

					if (ztMapList.size() > 0) {
						for (Map<String, Object> map : ztMapList) {
							RowRenderData ryrow0 = RowRenderData.build(bulidObjectToStr(map.get("taskTypeName"))
									, bulidObjectToStr(map.get("taskCount"))
									, bulidObjectToStr(map.get("taskSum")));
							ryrow0.setRowStyle(tableStyle);
							ztList.add(ryrow0);
						}
					}
				}

				put("ztTable", new MiniTableRenderData(header, ztList));


				//自定义表格
				RowRenderData headerJs = RowRenderData.build(new TextRenderData("承接人"),
						new TextRenderData("众包任务名称"),
						new TextRenderData("承接时间"),
						new TextRenderData("任务计数"),
						new TextRenderData("累计积分"));
				headerJs.setRowStyle(tableStyleHeader);

				List<RowRenderData> jsList = new ArrayList<>();

				if (null != map.get("jsTable")) {
					List<Map<String, Object>> ryMapList = (List<Map<String, Object>>) map.get("jsTable");

					if (ryMapList.size() > 0) {
						for (Map<String, Object> mapOne : ryMapList) {
							RowRenderData ryrow0 = RowRenderData.build(bulidObjectToStr(mapOne.get("realname"))
									, bulidObjectToStr(mapOne.get("score_name"))
									, bulidObjectToStr(mapOne.get("pcreate_time"))
									, bulidObjectToStr(mapOne.get("score_count"))
									, bulidObjectToStr(mapOne.get("score"))
							);
							ryrow0.setRowStyle(tableStyle);
							jsList.add(ryrow0);
						}
					}
				}
				put("jsTable", new MiniTableRenderData(headerJs, jsList));


				//自定义表格
				RowRenderData headerJsUser = RowRenderData.build(new TextRenderData("代表姓名"),
						new TextRenderData("身份证号"),
						new TextRenderData("电话"),
						new TextRenderData("任务计数"),
						new TextRenderData("累计积分"),
						new TextRenderData("积分单价"),
						new TextRenderData("任务单价"));
				headerJsUser.setRowStyle(tableStyleHeader);
				List<RowRenderData> jsUserList = new ArrayList<>();

				if (null != map.get("jsUserTable")) {
					List<Map<String, Object>> ryMapList = (List<Map<String, Object>>) map.get("jsUserTable");

					if (ryMapList.size() > 0) {
						for (Map<String, Object> mapOne : ryMapList) {
							RowRenderData ryrow0 = RowRenderData.build(bulidObjectToStr(mapOne.get("realname"))
									, bulidObjectToStr(mapOne.get("idcard"))
									, bulidObjectToStr(mapOne.get("phone"))
									, bulidObjectToStr(mapOne.get("score_count"))
									, bulidObjectToStr(mapOne.get("score"))
							);
							ryrow0.setRowStyle(tableStyle);
							jsUserList.add(ryrow0);
						}
					}
				}
				put("jsUserTable", new MiniTableRenderData(headerJsUser, jsUserList));

//
//				//自定义有序列表
//				put("testText", new NumbericRenderData(NumbericRenderData.FMT_DECIMAL, new ArrayList<TextRenderData>() {
//					{
//						add(new TextRenderData("Plug-in grammar"));
//						add(new TextRenderData("Supports word text, header..."));
//						add(new TextRenderData("Not just templates, but also style templates"));
//					}
//				}));
//
//				//网落图片
//				put("picture", new PictureRenderData(200, 150, ".jpg", BytePictureUtils.getUrlBufferedImage("https://gss3.bdstatic.com/7Po3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike116%2C5%2C5%2C116%2C38/sign=61c551093f6d55fbd1cb7e740c4b242f/d8f9d72a6059252d937820d3369b033b5ab5b9fd.jpg")));
//				//本地图片
//				put("picture2", new PictureRenderData(200, 150, ".jpg", BytePictureUtils.getLocalByteArray(new File(workPath + "c1.jpg"))));

			}
		};

		return generateWord(datas, workPath + templateName, outWorkPath);
	}


	/**
	 * 通过word模板并生成word文档
	 *
	 * @param paramData    参数数据
	 * @param templatePath word模板地址加模板文件名字
	 * @param outFilePath  输出文件地址（不带文件名字）
	 * @return 生成的word文件
	 */
	public static File generateWord(Map<String, Object> paramData, String templatePath, String outFilePath) {
		String outFileName = "word_" + System.currentTimeMillis() + "_" + new Random(100).toString() + ".doc";
		return generateWord(paramData, templatePath, outFilePath, outFileName);
	}


	/**
	 * 通过word模板并生成word文档
	 *
	 * @param paramData    参数数据
	 * @param templatePath word模板地址加模板文件名字
	 * @param outFilePath  输出文件地址（不带文件名字）
	 * @param outFileName  输出文件名字
	 * @return 生成的word文件
	 */
	public static File generateWord(Map<String, Object> paramData, String templatePath, String outFilePath, String outFileName) {
		//解析word模板
		XWPFTemplate template = XWPFTemplate.compile(templatePath).render(paramData);
		//输出文件
		FileOutputStream out = null;
		File outFile = new File(outFilePath + outFileName);
		try {
			out = new FileOutputStream(outFile);
			template.write(out);
			out.flush();
		} catch (IOException e) {

			//log.error("生成word写入文件失败", e);
		} finally {
			if (template != null) {
				try {
					template.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return outFile;
	}

	public static String bulidObjectToStr(Object obj) {
		if (null != obj) {
			return obj + "";
		}
		return "";
	}

	public static File main_bulidTemplateInfo(Map<String, Object> map) {

		return null;
	}


	private static Integer sfImage(Integer yWidth, Integer xWidth, Integer yHeight) {
		if (xWidth == 0) {
			return 0;
		}

//		return yHeight / (yWidth / xWidth);
		return yHeight * xWidth / yWidth ;

	}

	private static String sfBr(Integer yWidth, Integer xWidth, Integer yHeight) {
		if (xWidth == 0) {
			return "";
		}

		if (yHeight > 600) {
			return "";
		}

//		Integer num = (900 - yHeight / (yWidth / xWidth)) / 10;
		Integer num = (900 - yHeight * xWidth / yWidth ) / 10;
		String brStr = "";
		for (int i = 0; i < num; i++) {
			brStr += "\n";
		}

		return brStr;

	}

}
