/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.manager.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import io.swagger.annotations.*;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import lombok.SneakyThrows;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springblade.common.utils.MathUtil;
import org.springblade.common.utils.SnowFlakeUtil;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;

import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.*;
import org.springblade.manager.entity.*;
import org.springblade.manager.service.*;
import org.springblade.manager.vo.*;
import org.springblade.manager.wrapper.ExcelTabWrapper;
import org.springblade.resource.feign.IOSSClient;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.boot.ctrl.BladeController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 清表基础数据表 控制器
 *
 * @author BladeX
 * @since 2022-05-18
 */
@RestController
@AllArgsConstructor
@RequestMapping("/exceltab")
@Api(value = "清表基础数据表", tags = "清表基础数据表接口")
public class ExcelTabController extends BladeController {

	// excel 基本信息表
	private final IExcelTabService excelTabService;
	// wes 基本信息表
	private final IWbsTreeService wbsTreeService;
	// excel 解析结构
	private final IExctabCellService exctabCellService;

	private final IOSSClient iossClient;

	// 私有项目wbs
	private final IWbsTreePrivateService wbsTreePrivateService;

	// 清表表格解析信息
	private final ILinkdataInfoService linkdataInfoService;

	// 元素信息表-
	private final IWbsFormElementService wbsFormElementService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入excelTab")
	public R<ExcelTabVO> detail(ExcelTab excelTab) {
		ExcelTab detail = excelTabService.getOne(Condition.getQueryWrapper(excelTab));
		return R.data(ExcelTabWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 清表基础数据表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入excelTab")
	public R<IPage<ExcelTabVO>> list(ExcelTab excelTab, Query query) {
		IPage<ExcelTab> pages = excelTabService.page(Condition.getPage(query), Condition.getQueryWrapper(excelTab));
		return R.data(ExcelTabWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 清表基础数据表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入excelTab")
	public R<IPage<ExcelTabVO>> page(ExcelTabVO excelTab, Query query) {
		IPage<ExcelTabVO> pages = excelTabService.selectExcelTabPage(Condition.getPage(query), excelTab);
		return R.data(pages);
	}

	/**
	 * 新增 清表基础数据表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入excelTab")
	public R save(@Valid @RequestBody ExcelTab excelTab) {
		if(Func.isEmpty(excelTab.getParentId())) {
			excelTab.setTenantId(AuthUtil.getTenantId());
			excelTab.setParentId(BladeConstant.TOP_PARENT_ID);
			excelTab.setIsDeleted(BladeConstant.DB_NOT_DELETED);
			excelTabService.save(excelTab);
			excelTab.setAlias(excelTab.getId()+"");
		}

		if (excelTab.getParentId() > 0) {
			ExcelTab parent = excelTabService.getById(excelTab.getParentId());
			if (Func.toLong(parent.getParentId()) == Func.toLong(excelTab.getId())) {
				throw new ServiceException("父节点不可选择自身!");
			}
			excelTab.setTenantId(parent.getTenantId());
			String ancestors = parent.getAlias() + StringPool.COMMA + excelTab.getParentId();
			excelTab.setAlias(ancestors);
			excelTab.setIsDeleted(BladeConstant.DB_NOT_DELETED);
		}
		return R.status(excelTabService.saveOrUpdate(excelTab));
	}

	/**
	 * 修改 清表基础数据表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入excelTab")
	public R update(@Valid @RequestBody ExcelTab excelTab) {
		return R.status(excelTabService.updateById(excelTab));
	}

	/**
	 * 新增或修改 清表基础数据表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入excelTab")
	public R submit(@Valid @RequestBody ExcelTab excelTab) {
		if(Func.isEmpty(excelTab.getParentId())) {
			excelTab.setTenantId(AuthUtil.getTenantId());
			excelTab.setParentId(BladeConstant.TOP_PARENT_ID);
			excelTab.setIsDeleted(BladeConstant.DB_NOT_DELETED);
			excelTab.setFileType(1); // 祖节点
			excelTabService.save(excelTab);
			excelTab.setAlias(excelTab.getId()+"");
		}

		if (excelTab.getParentId() > 0) {
			ExcelTab parent = excelTabService.getById(excelTab.getParentId());
			if (Func.toLong(parent.getParentId()) == Func.toLong(excelTab.getId())) {
				throw new ServiceException("父节点不可选择自身!");
			}
			excelTab.setTenantId(parent.getTenantId());
			String ancestors = parent.getAlias() + StringPool.COMMA + excelTab.getParentId();
			excelTab.setAlias(ancestors);
			excelTab.setIsDeleted(BladeConstant.DB_NOT_DELETED);
		}
		return R.status(excelTabService.saveOrUpdate(excelTab));
	}

	
	/**
	 * 删除 清表基础数据表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(excelTabService.deleteLogic(Func.toLongList(ids)));
	}


	/**
	 * 清表模版树
	 */
	@GetMapping("/tab-lazytree")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "清表清表树形结构", notes = "清表清表树形结构")
	public R<List<ExceTabTreVO>> tabLazyTree(Long parentId,Long modeId, BladeUser bladeUser) {
		List<ExceTabTreVO> tree = excelTabService.tabLazyTree( bladeUser.getTenantId(), modeId,parentId);
		return R.data(tree);
	}

	/**
	 * 上传文件
	 *
	 * @param file 文件
	 * @return ObjectStat
	 */
	@SneakyThrows
	@PostMapping("/put-file-attach")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "清表上传", notes = "清表上传")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(name = "file", value = "文件源", required = true),
			@ApiImplicitParam(name = "nodeId", value = "节点id", required = true)
	})
	public R putFileAttach(@RequestParam("file") MultipartFile file, Long nodeId) {

		ExcelTab detail = excelTabService.getById(nodeId);
		// 删除excel文件
		R<BladeFile> bladeFile = iossClient.addFileInfo(file);
		BladeFile bladeFile1 = bladeFile.getData();
		String filecode = SnowFlakeUtil.getId()+"";
		String thmlUrl = "/Users/hongchuangyanfa/Desktop/"+filecode+".html";
		// 解析excel
		Workbook wb = new Workbook();
		wb.loadFromMHtml(file.getInputStream());
		//获取工作表
		Worksheet sheet = wb.getWorksheets().get(0);
		sheet.saveToHtml(thmlUrl);
		detail.setExtension(bladeFile1.getOriginalName());
		detail.setFileUrl(bladeFile1.getLink());
		detail.setFileType(3); // 表示为清表信息  1 表示祖节点  2 表示为节点信息 3 表示清表
		detail.setHtmlUrl(thmlUrl);
		excelTabService.saveOrUpdate(detail);
		// 解析html
		explianHtml(thmlUrl);
		return R.success("上传成功");
	}



	/**
	 * 清表编辑 wbs 下拉框选择
	 */
	@GetMapping("/getWbsTypeList")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "清表编辑 wbs 下拉框选择", notes = "传入excelTab")
	public R getWbsTypeList() {
		List<ExcelTabWbsTypeVO> wbsTypeList= excelTabService.getWbsTypeList();
		return R.data(wbsTypeList);
	}

	/**
	 * 清表编辑 - 下拉框 树节点
	 */
	@GetMapping("/lazy-tree")
	@ApiOperationSupport(order = 11)
	@ApiOperation(value = "懒加载节点树形结构", notes = "传入wbsId，parentId，请求头token")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(name = "wbsId", value = "wbsId", required = true),
			@ApiImplicitParam(name = "parentId", value = "父级id", required = true),
			@ApiImplicitParam(name = "wbsType", value = "type", required = true)
	})
	public R<List<WbsTreeVO>> getLazyTreeById(String wbsId, Long parentId, BladeUser bladeUser, Integer wbsType) {
		if(wbsType==1){
			List<WbsTreeVO> tree = excelTabService.lazyTree(wbsId, Func.toStrWithEmpty(null, bladeUser.getTenantId()), parentId);
			if (tree != null && tree.size() > 0) {
				return R.data(tree);
			}
		}

		return R.fail(200, "未查询到信息");
	}

	/**
	 * 查询当前节点下所有表单（根据节点ID查询当前表单）
	 */
	@GetMapping("/selectByNodeTable")
	@ApiOperationSupport(order = 12)
	@ApiOperation(value = "查询当前节点下所有表单", notes = "传入父节点id")
	public R<List<WbsNodeTableVO>> selectByNodeTable(@ApiParam(value = "树节点Id", required = true) @RequestParam String id,
										 @ApiParam(value = "wbsType", required = true) @RequestParam Integer wbsType) {
		if(wbsType==1){
			List<WbsNodeTableVO> rs = wbsTreeService.selectByNodeTable(id);
			if (!("").equals(rs) && rs.size() > 0) {
				return R.data(rs);
			}
		}
		return R.fail(200, "未查询到数据");
	}


	/**
	 * 清表编辑 - 下拉框 树节点
	 */
	@PostMapping("/sava-dataInfo")
	@ApiOperationSupport(order = 13)
	@ApiOperation(value = "编辑-保存", notes = "编辑-保存")
	public R<List<WbsTreeVO>> savaDataInfo(@Valid @RequestBody WbsExclTabParmVO wbsExclTabParmVO, BladeUser bladeUser) {
		// 保存节点信息
		ExcelTab excelTab = new ExcelTab();
		if(wbsExclTabParmVO.getId()!=null){// 修改
			excelTab = excelTabService.getById(wbsExclTabParmVO.getId());
			excelTab.setName(wbsExclTabParmVO.getNodeName());
			excelTab.setTabType(wbsExclTabParmVO.getTabType());
		}else{ // 新增
			ExcelTab parent = excelTabService.getById(wbsExclTabParmVO.getParentId());
			excelTab.setParentId(wbsExclTabParmVO.getParentId());
			excelTab.setTabType(wbsExclTabParmVO.getTabType());
			excelTab.setName(wbsExclTabParmVO.getNodeName());
			excelTab.setIsDeleted(BladeConstant.DB_NOT_DELETED);
			excelTab.setFileType(2);
			String ancestors = parent.getAlias() + StringPool.COMMA + excelTab.getParentId();
			excelTab.setAlias(ancestors);
		}
		excelTabService.saveOrUpdate(excelTab);
		return R.success("添加成功!");
	}

	/**
	 * 关联清表树
	 */
	@GetMapping("/tab-lazytree-all")
	@ApiOperationSupport(order = 14)
	@ApiOperation(value = "关联清表树all", notes = "关联清表树all")
	public R<List<ExceTabTreVO>> tabLazyTreeall(Long modeId,String name, BladeUser bladeUser) {
		List<ExceTabTreVO> tree = excelTabService.tabLazyTreeAll( bladeUser.getTenantId(), modeId, name);
		return R.data(tree);
	}


	/**
	 * 关联清表-保存
	 */
	@GetMapping("/save-linkTab")
	@ApiOperationSupport(order = 14)
	@ApiOperation(value = "关联清表-保存", notes = "关联清表-保存")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(name = "exceTabId", value = "清表id", required = true),
			@ApiImplicitParam(name = "tabId", value = "表Id", required = true)
	})
	public R<List<ExceTabTreVO>> saveLinkeTab(Long exceTabId,Long tabId, BladeUser bladeUser) {
		// 查询私有项目信息
		WbsTreePrivate wbsTree = new WbsTreePrivate();
		wbsTree.setPKeyId(tabId);
		WbsTreePrivate aPrivate = wbsTreePrivateService.getOne(Condition.getQueryWrapper(wbsTree));

		UpdateWrapper<WbsTreePrivate> updateWrapper = new UpdateWrapper<>();
		updateWrapper.in("p_key_id",tabId);
		updateWrapper.set("is_link_table",1);
		updateWrapper.set("excel_id",exceTabId);

		wbsTreePrivateService.update(updateWrapper);
		// 获取excel 基本信息
		ExcelTab excelTab = excelTabService.getById(exceTabId);
		// 解析
		try {
			getExcelHtml1(excelTab.getHtmlUrl(),aPrivate.getId()+"");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return R.success("关联成功");
	}




	// 匹配
	public void getExcelHtml1(String thmlUrl,String  TabId) throws IOException{
		List<WbsFormElement> dataList = wbsFormElementService.selectElementListByFid(TabId);

		File file1 = ResourceUtil.getFile(thmlUrl);
		FileInputStream fileInputStream = new FileInputStream(file1);
		String htmlString =   IoUtil.readToString(fileInputStream);
		Document doc = Jsoup.parse(htmlString);
		Element table = doc.select("table").first();
		Elements trs = table.select("tr");

		// 循环解析
		// 标题坐标
		List<Map<String,String>> zikey = new ArrayList<>();

		int index_id = 0;
		// 循环解析
		for(int i = 0 ;i < trs.size() ;i++) {
			Element tr = trs.get(i);
			boolean index_stat = Boolean.parseBoolean(tr.attr("index_stat"));

			if(index_stat && index_id==0){
				index_id = 1;
				zikey = new ArrayList<>();
			}

			if(!index_stat && index_id==1 && zikey.size()>=1){
				index_id = 2;
				zikey = new ArrayList<>();
			}

			Elements tds = tr.select("td");
			for (int j = 0; j < tds.size(); j++) {
				Element data = tds.get(j);
				String textInfo  = data.text();
				int x1 = Integer.parseInt( data.attr("x1"));
				int x2 = Integer.parseInt( data.attr("x2"));
				int y1 = Integer.parseInt( data.attr("y1"));
				int y2 = Integer.parseInt( data.attr("y2"));


				if(!textInfo.isEmpty() && !textInfo.equals("/")){
					Map<String,String> dataInfo =new HashMap<String,String>();
					dataInfo.put("name",textInfo);
					dataInfo.put("x1",data.attr("x1"));
					dataInfo.put("x2",data.attr("x2"));
					dataInfo.put("y1",data.attr("y1"));
					dataInfo.put("y2",data.attr("y2"));
					zikey.add(dataInfo);
				}else if(data.html().indexOf("input")>=0){

					List arrayList = new ArrayList<>();


					for(int k=zikey.size()-1;k>=0 ;k--){
						String name = zikey.get(k).get("name").replaceAll("[^\u4E00-\u9FA5]", "");
						int xx1 = Integer.parseInt( zikey.get(k).get("x1"));
						int xx2 = Integer.parseInt( zikey.get(k).get("x2"));
						int yy1 = Integer.parseInt( zikey.get(k).get("y1"));
						int yy2 = Integer.parseInt( zikey.get(k).get("y2"));

						// 左匹配
						if( yy1<=y1 && yy2>=y2 && xx2<x1 && StringUtils.isNotEmpty(name)){
							arrayList.add(name);
							if(!index_stat){
								break;
							}
						}

						//向 上 匹配
						if(index_stat){
							if( xx1<=x1 && xx2 >=x2 && yy2<y1 ){
								arrayList.add(name);
							}
						}
					}

					Set<String> resultSet = new HashSet<>();
					MathUtil.getCombination(arrayList, resultSet);
					double maxInfo = 0;
					if(!resultSet.isEmpty()){

						for(WbsFormElement wbsFormElement : dataList){
							for(String bx : resultSet){
								double dataxx =  MathUtil.sim(bx,wbsFormElement.getEName().replaceAll("[^\u4E00-\u9FA5]", ""));
								if(maxInfo < dataxx){
									System.out.println("x1-x2="+x1+","+x2+"y1-y2="+y1+","+y2+bx+"="+dataxx+"_jieh="+resultSet);
									data.empty().text(bx);
									maxInfo = dataxx;
								}
							}
						}
					}
					System.out.println(maxInfo);
				}
			}
		}


		//
		// for (Map<String,String> d:zikey){
		// System.out.println(d.get("name")+"x1"+d.get("x1"));
		//  }

		fileInputStream.close();

		File writefile = new File(thmlUrl);
		FileUtil.writeToFile(writefile, doc.html(), Boolean.parseBoolean("UTF-8"));
		System.out.println("完成");

	}



	/**
	 * 清表生成html
	 */
	@GetMapping("/get-excel-html")
	@ApiOperationSupport(order = 15)
	@ApiOperation(value = "清表生成html", notes = "清表生成html")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(name = "excelId", value = "excelId", required = true)
	})
	public R getExcelHtml(Long excelId) throws IOException {
		ExcelTab excelTab = excelTabService.getById(excelId);
		if(excelTab ==null ){
			return R.fail("该数据下无此节点!");
		}
		if(excelTab.getHtmlUrl()==null){
			return R.fail("请上传清表!");
		}

		File file1 = ResourceUtil.getFile(excelTab.getHtmlUrl());
		FileInputStream fileInputStream = new FileInputStream(file1);
		String htmlString =   IoUtil.readToString(fileInputStream);
		// 解析 style
		Document doc = Jsoup.parse(htmlString);
		Element table = doc.select("table").first();
		doc.select("Col").remove();
		fileInputStream.close();
		return R.data(table+"");
	}



	public static void explianHtml(String htmlURl) throws IOException {

		File file1 = ResourceUtil.getFile(htmlURl);
		FileInputStream inputStream = new FileInputStream(file1);

		// 测试
		String htmlString =  IoUtil.readToString(inputStream);

		// 样式集合
		Map<String ,String > styleMap = new HashMap<>();
		// 解析 style
		Document doc = Jsoup.parse(htmlString);
		Element style = doc.select("style").first();

		Matcher cssMatcher = Pattern.compile("(\\w+)\\s*[{]([^}]+)[}]").matcher(style.html());
		while (cssMatcher.find()) {
			styleMap.put(cssMatcher.group(1),cssMatcher.group(2));
		}
		// 解析 总行和总列
		Element table = doc.select("table").first();
		Elements trs = table.select("tr");

		// 获取总行列数
		int maxCol = doc.select("Col").size();
		String [] rowData = new String[trs.size()+1];

		int s_index = 0;
		boolean index_stat = false;
		// 横向计算
		for(int i = 0 ;i < trs.size() ;i++) {
			Element tr = trs.get(i);
			Elements tds = tr.select("td");

			int x_width = 0 ;
			int y_width = 0 ;
			//区域计算
			for (int j = 0; j < tds.size(); j++) {
				Element data = tds.get(j);
				String keyId = data.attr("class");

				int colspan = data.attr("COLSPAN").equals("") ? 1 : Integer.parseInt(data.attr("COLSPAN"));
				String classInfo = styleMap.get(keyId);
				data.removeAttr("class");
				data.attr("style",styleMap.get(keyId).replaceAll("break-word","inherit"));
				// 第一列
				if(j==0 && classInfo.indexOf("border-left-style")>=0 && classInfo.indexOf("border-top-style")>=0 && classInfo.indexOf("border-bottom-style")>=0 && !index_stat){
					x_width +=colspan;
				}else if(j == tds.size()-1 && classInfo.indexOf("border-right-style")>=0 && classInfo.indexOf("border-top-style")>=0 && classInfo.indexOf("border-bottom-style")>=0 && !index_stat && j>=1){
					x_width +=colspan;
				}else if(classInfo.indexOf("border-top-style")>=0 && classInfo.indexOf("border-bottom-style")>=0 && !index_stat && j>=1){
					x_width +=colspan;
				}

				// 计算退出
				if(j==0 && classInfo.indexOf("border-left-style")<0 && classInfo.indexOf("border-top-style")>=0 && classInfo.indexOf("border-bottom-style")<0 && index_stat && classInfo.indexOf("border-right-style")<0){
					y_width +=colspan;
				}else if(j == tds.size()-1 && classInfo.indexOf("border-right-style")<0 && classInfo.indexOf("border-top-style")>=0 && classInfo.indexOf("border-bottom-style")<0 && index_stat && j>=1){
					y_width +=colspan;
				}else if(classInfo.indexOf("border-top-style")>=0 && classInfo.indexOf("border-bottom-style")<0 &&index_stat && j>=1){
					y_width +=colspan;
				}
			}

			if(x_width == maxCol && !index_stat && s_index==0){
				s_index = i + 1;
				index_stat = true ;
			}

			if(y_width == maxCol && index_stat && s_index>0){
				s_index = i + 1;
				index_stat = false ;
			}

			tr.attr("index_stat",index_stat+"");

			int x= 0;
			for (int j = 0; j < tds.size(); j++) {
				Element data = tds.get(j);

				int colspan = data.attr("COLSPAN").equals("") ? 0 : Integer.parseInt(data.attr("COLSPAN"));
				int rowspan = data.attr("ROWSPAN").equals("") ? 0 : Integer.parseInt(data.attr("ROWSPAN"));

				Map<String, String> textObject = new HashMap<>();
				// 计算
				int x1 =0;
				int x2 =0;
				int y1 =0;
				int y2 =0;
				//x
				// X 坐标
				if(colspan==0){
					x1 = x+1;
					x2 = x+1;
					x= x +1;
				}else{
					x1 = x+1;
					x2 = x+colspan;
					x = x+colspan;
				}
				String textInfo = data.text().trim();

				// y 坐标
				y1=i+1;
				if(rowspan==0){
					y2 = i+1;
				}else{
					y2 = i+rowspan;
					for(int k=0;k<rowspan-1;k++){
						String dataInfo = rowData[k+2+i];
						if(dataInfo==null){
							dataInfo = x1+":"+x2;
						}else{
							String [] dataInfo2 = dataInfo.split(",");
							String dataInfo3 = dataInfo2[dataInfo2.length-1];
							int mx1 = Integer.parseInt(dataInfo3.split(":")[0]);
							int mx2 = Integer.parseInt(dataInfo3.split(":")[1]);
							if(mx2+1==x1){
								dataInfo = dataInfo+","+mx1+":"+x2;
							}else{
								dataInfo = dataInfo+","+x1+":"+x2;
							}
						}
						rowData[k+2+i] = dataInfo;
					}
				}

				String getRowInfo = rowData[y1];
				if(getRowInfo!=null){
					String [] dataInfo2 = getRowInfo.split(",");
					if(getRowInfo.indexOf("1")>=0 && j==0){
						for(int m =0;m<dataInfo2.length ;m++){
							int mx1 = Integer.parseInt(dataInfo2[m].split(":")[0]);
							int mx2 = Integer.parseInt(dataInfo2[m].split(":")[1]);
							x = mx2;
						}
						x1 = x+x1;
						x2 = x+x2;
						x = x+1 ; //计算自己
					}else{
						for(int m =0;m<dataInfo2.length ;m++){
							int mx1 = Integer.parseInt(dataInfo2[m].split(":")[0]);
							int mx2 = Integer.parseInt(dataInfo2[m].split(":")[1]);
							if(x2+1 == mx1){
								x = mx2 ;
							}
						}
					}
				}


				// System.out.println(StringUtil.trimAllWhitespace(textInfo)+""+StringUtil.trimAllWhitespace(textInfo).length());
				data.text(textInfo.replaceAll(" ",""));



				// 设置td 属性
				data.attr("trIndex",i+"");
				data.attr("tdIndex",j+"");
				data.attr("x1",x1+"");
				data.attr("x2",x2+"");
				data.attr("y1",y1+"");
				data.attr("y2",y2+"");
				String parm = i+","+j+","+x1+","+x2+","+y1+","+y2;
				// 设置文本信息
				if(textInfo.indexOf("年")>=0 && textInfo.indexOf("月")>=0&& textInfo.indexOf("日")>=0){
					data.empty().append("<el-date-picker style='width:100%;height:100%' trIndex="+i+" tdIndex="+j+"  x1="+x1+" x2="+x2+" y1="+y1+" y2="+y2+"  type='date' format='yyyy 年 MM 月 dd 日' placeholder='请选择时间'> </el-date-picker>");
				}

				if(textInfo.isEmpty() || textInfo.equals("/") || textInfo.equals("-")){
					if(index_stat){
						if(rowspan>=1){
							data.empty().append("<el-input type='textarea' @contextmenu.prevent.native='RightClick("+parm+")' trIndex="+i+" tdIndex="+j+"  x1="+x1+" x2="+x2+" y1="+y1+" y2="+y2+" style='width:100%;height:100%'   :rows="+rowspan*2+" placeholder=''> </el-input>");
						}else{
							data.empty().append("<el-input type='text' @contextmenu.prevent.native='RightClick("+parm+")' trIndex="+i+" tdIndex="+j+"  x1="+x1+" x2="+x2+" y1="+y1+" y2="+y2+" style='width:100%;height:100%' placeholder=''> </el-input>");
						}
					}else{
						if(j==0 ){
							if(colspan ==maxCol && i>=1 && i<=2){
								if(rowspan>=1){
									data.empty().append("<el-input type='textarea' @contextmenu.prevent.native='RightClick("+parm+")' trIndex="+i+" tdIndex="+j+"  x1="+x1+" x2="+x2+" y1="+y1+" y2="+y2+" style='width:100%;height:100%'   :rows="+rowspan*2+" placeholder=''> </el-input>");
								}else{
									data.empty().append("<el-input type='text' @contextmenu.prevent.native='RightClick("+parm+")' trIndex="+i+" tdIndex="+j+"  x1="+x1+" x2="+x2+" y1="+y1+" y2="+y2+" style='width:100%;height:100%' placeholder=''> </el-input>");
								}
							}
						}else{
							Element bforData = tds.get(j-1);
							if(!bforData.text().isEmpty()){
								if(rowspan>=1){
									data.empty().append("<el-input type='textarea' @contextmenu.prevent.native='RightClick("+parm+")' trIndex="+i+" tdIndex="+j+"  x1="+x1+" x2="+x2+" y1="+y1+" y2="+y2+" style='width:100%;height:100%'   :rows="+rowspan*2+" placeholder=''> </el-input>");
								}else{
									data.empty().append("<el-input type='text' @contextmenu.prevent.native='RightClick("+parm+")' trIndex="+i+" tdIndex="+j+"  x1="+x1+" x2="+x2+" y1="+y1+" y2="+y2+" style='width:100%;height:100%' placeholder=''> </el-input>");
								}
							}
						}
					}
				}
			}
		}

		// 写入

		FileUtil.writeToFile(file1, doc.html(), Boolean.parseBoolean("UTF-8"));
		inputStream.close();
		System.out.println("完成");
	}
}
