/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-01-29
 * 描述            ：质保书发运物料信息表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Qms.qcm.service.impl;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import javax.imageio.ImageIO;

import market.Com.mapper.ComBasePhyItemMapper;
import market.Com.vo.ComBasePhyItem;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.com.service.impl.ComBaseConfigServiceImpl;
import market.Qms.common.DataHelper;
import market.Qms.common.MergeHSSFSheet;
import market.Qms.common.QRCodeUtil;
import market.Qms.common.ReturnMsg;
import market.Qms.function.StringFormat;
import market.Qms.qcm.mapper.QcmJhySampleConsignMMapper;
import market.Qms.qcm.mapper.QcmZbsBillMapper;
import market.Qms.qcm.mapper.QcmZbsDataCicMapper;
import market.Qms.qcm.mapper.QcmZbsDataMatMapper;
import market.Qms.qcm.mapper.QcmZbsDataOrdMapper;
import market.Qms.qcm.mapper.QcmZbsDataPicMapper;
import market.Qms.qcm.mapper.QcmZbsLanguageMapper;
import market.Qms.qcm.mapper.QcmZbsOrgMapper;
import market.Qms.qcm.mapper.QcmZbsTmplItemMapper;
import market.Qms.qcm.mapper.QcmZbsTmplMapper;
import market.Qms.qcm.service.QcmZbsBillService;
import market.Qms.qcm.vo.QcmJhySampleConsignM;
import market.Qms.qcm.vo.QcmZbsBigItem;
import market.Qms.qcm.vo.QcmZbsBill;
import market.Qms.qcm.vo.QcmZbsChemObject;
import market.Qms.qcm.vo.QcmZbsDataCic;
import market.Qms.qcm.vo.QcmZbsDataMat;
import market.Qms.qcm.vo.QcmZbsDataOrd;
import market.Qms.qcm.vo.QcmZbsDataPic;
import market.Qms.qcm.vo.QcmZbsLanguage;
import market.Qms.qcm.vo.QcmZbsObject;
import market.Qms.qcm.vo.QcmZbsObjectQlty;
import market.Qms.qcm.vo.QcmZbsOrg;
import market.Qms.qcm.vo.QcmZbsSampleNo;
import market.Qms.qcm.vo.QcmZbsTmpl;
import market.Qms.qcm.vo.QcmZbsTmplItem;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
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.util.CellRangeAddress;

import com.alibaba.druid.stat.TableStat.Condition;
import com.aspose.cells.License;
import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;
import com.google.zxing.oned.CodaBarReader;
import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path = "QcmZbsBillService")
public class QcmZbsBillServiceImpl extends BaseServiceImpl<QcmZbsBill, Object> implements QcmZbsBillService {

	public ResultModel doInsertBill(CommonPage<QcmZbsBill> sum) throws Exception {

		ResultModel rm = new ResultModel();
		String userName = this.getSession().getAttribute(HomeService.SESSION_USER_NAME).toString();
		try {
			List<QcmZbsBill> bills = sum.getObjectList();
			for (QcmZbsBill bill : bills) {

				bill.setCreate_time(new Date());
				bill.setCreate_name(userName);
				this.getMapper(QcmZbsBillMapper.class).insert(bill);
			}
			rm.setState("200");
			rm.setMsgInfo("质保书数据源信息生成成功");

		} catch (Exception e) {
			throw new Exception("生成质保书数据失败");
		}

		return rm;

	}

	public ResultModel removeInsertBill(CommonPage<QcmZbsBill> sum) throws Exception {
		ResultModel rm = new ResultModel();
		String userName = this.getSession().getAttribute(HomeService.SESSION_USER_NAME).toString();
		try {
			QcmZbsBill bls = sum.getObject();
			List<QcmZbsBill> bills = this.getMapper(QcmZbsBillMapper.class).findWithCondition(null,
					"BILL_NO='" + bls.getBill_no() + "'", null, 0, 0);
			for (QcmZbsBill bill : bills) {
				if ("1".equals(bill.getIs_create_book())) {
					rm.setState("500");
					rm.setMsgInfo("发运单号[" + bill.getBill_no() + "]向下的子板[" + bill.getObject_no()
							+ "],已经生成了质保书，请联系质量人员删除质保书后撤销");
					return rm;
				}

			}

			// 否则删除质保书数据源
			for (QcmZbsBill bill : bills) {
				this.getMapper(QcmZbsBillMapper.class).delete(bill.getBill_no(), bill.getObject_no());

			}
			rm.setState("200");
			rm.setMsgInfo("质保书数据源撤销成功");

		} catch (Exception e) {
			throw new Exception("撤销质保书数据失败");
		}

		return rm;

	}

	/**
	 * 查询表头项分组物料信息 参数：发货号： 合同号： 可以为空
	 */
	public ResultModel selectBillHead(CommonPage<QcmZbsBill> sum) {
		ResultModel rm = new ResultModel();
		try {

			ReturnMsg<List<QcmZbsBill>> msg = new ReturnMsg<>();
			List<QcmZbsBill> list = new ArrayList<QcmZbsBill>();
			QcmZbsBill bill = sum.getObject();
			if (bill == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入参数");
				return rm;
			}

			String bill_no = bill.getBill_no();
			String contract_no = bill.getContract_no();
			String data_sources = bill.getData_sources();
			String order_unit_name = bill.getOrder_unit_name();
			String starTime = sum.getStartTime();
			String endTime = sum.getEndTime();
			String steel_name = bill.getSteel_name();
			Integer pageIndex = sum.getPageSize() * (sum.getPageIndex() - 1);
			Integer pageSize = sum.getPageSize() * (sum.getPageIndex() - 1) + sum.getPageSize();
			String object_no=bill.getObject_no();
			//卷号查询处理
			String object_nos=null;
			String contructNos=null;
			List<String> bills2 = new ArrayList<>();
			List<String> contructNos2 = new ArrayList<>();
			if (object_no!=null && !"".equals(object_no)) {
				List<String> bills=this.getMapper(QcmZbsBillMapper.class).findBills(object_no);
				if (DataHelper.checkIsNull(bills)) {
					for(String item : bills) {
						bills2.add(item.split("#")[0]);
						contructNos2.add(item.split("#")[1]);
					}
					object_nos=DataHelper.getSetConditionList(bills2);
					// 条件增加合同号：防止出现卷号查询只查询出发运号 20220330 
					contructNos=DataHelper.getSetConditionList(contructNos2);
				}
			}
			if (object_nos!=null && !"".equals(object_nos)) {
				bill_no=null;
			}
			if (StringUtils.isNotEmpty(object_no)) {
				if (StringUtils.isEmpty(object_nos) || "null".equals(object_nos)) {
					msg.setTotal(0);
					rm.setData(msg);
					rm.setState("200");
					return rm;
				}			
			}

			List<QcmZbsBill> bills = this.getMapper(QcmZbsBillMapper.class).findHead(bill_no, contract_no, starTime,
					endTime, data_sources, pageIndex, pageSize, steel_name, order_unit_name,object_nos, contructNos);
			
			String condition = "";
			if (StringUtils.isNotEmpty(bill_no)) {
				condition += " and t.bill_no like '%"+bill_no+"%'";
			}
			if (StringUtils.isNotEmpty(order_unit_name)) {
				condition += " and t.order_unit_name like '%"+order_unit_name+"%'";
			}
			if (StringUtils.isNotEmpty(contract_no)) { 
				condition += " and t.contract_no like '%"+condition+"%'";
			}
			if (StringUtils.isNotEmpty(steel_name)) {
				condition += " and t.steel_name like '%"+steel_name+"%'";
			}
			if (StringUtils.isNotEmpty(object_no)) {
				condition += " and t.bill_no in ("+object_nos+")";
				// contructNos
				condition += " and t.CONTRACT_NO in ("+contructNos+")";
			}
			int total = this.getMapper(QcmZbsBillMapper.class).findHeadCount(condition);
			msg.setResult(bills);
			msg.setSuccess(true);
			msg.setTotal(total);
			rm.setData(msg);
			rm.setState("200");

		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败");
			e.printStackTrace();
			return rm;
		}

		return rm;

	}

	/**
	 * 根据表头项查询
	 */

	public ResultModel selectBillByHead(CommonPage<QcmZbsBill> sum) {
		ResultModel rm = new ResultModel();
		try {
			QcmZbsBill bill = sum.getObject();
			String bill_no = bill.getBill_no();
			String steel_name = bill.getSteel_name();
			String prod_code = bill.getProd_code();
			String delivery_state_code = bill.getDelivery_state_code();
			String contract_no = bill.getContract_no();
			String car_no = bill.getCar_no();
			String reciver_unit_no = bill.getReciver_unit_no();
			String cut_edge_type_no = bill.getCut_edge_type_no();
			String fixed_length_type_no = bill.getFixed_length_type_no();
			String batch_no = bill.getBatch_no();
			List<QcmZbsBill> bills = new ArrayList<>();
			if ("0".equals(bill.getIs_batch())) {
				bills = this.getMapper(QcmZbsBillMapper.class).findByNull(bill_no, steel_name, prod_code,
						delivery_state_code, contract_no, car_no, reciver_unit_no, cut_edge_type_no,
						fixed_length_type_no);

			} else {
				if ("1".equals(bill.getIs_length()) && "1".equals(bill.getIs_width())) {
					bills = this.getMapper(QcmZbsBillMapper.class).findByAllCondition(bill_no, steel_name, prod_code,
							delivery_state_code, contract_no, car_no, reciver_unit_no, cut_edge_type_no,
							fixed_length_type_no);

				} else if ("1".equals(bill.getIs_length()) && !"1".equals(bill.getIs_width())) {
					bills = this.getMapper(QcmZbsBillMapper.class).findByLength(bill_no, steel_name, prod_code,
							delivery_state_code, contract_no, car_no, reciver_unit_no, cut_edge_type_no,
							fixed_length_type_no);

				} else if (!"1".equals(bill.getIs_length()) && "1".equals(bill.getIs_width())) {
					bills = this.getMapper(QcmZbsBillMapper.class).findByWidth(bill_no, steel_name, prod_code,
							delivery_state_code, contract_no, car_no, reciver_unit_no, cut_edge_type_no,
							fixed_length_type_no);

				} else {
					bills = this.getMapper(QcmZbsBillMapper.class).findByWidth(bill_no, steel_name, prod_code,
							delivery_state_code, contract_no, car_no, reciver_unit_no, cut_edge_type_no,
							fixed_length_type_no);

				}
			}
			rm.setData(bills);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败");
			return rm;
		}

		return rm;
	}

	public ResultModel printBookInfo(CommonPage<QcmZbsDataOrd> sum) throws Exception {
		ResultModel rm = new ResultModel();
		try {

			// 获取订单信息
			QcmZbsDataOrd ord = sum.getObject();
			if (ord == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入需要打印的质保书");
				return rm;
			}
			// 获取模板信息
			String model_no = ord.getTmpl_no();
			if (model_no == null || "".equals(model_no)) {
				rm.setState("500");
				rm.setMsgInfo("请传入模板编号");
				return rm;
			}
			// 模板配置数据
			QcmZbsTmpl model = this.getMapper(QcmZbsTmplMapper.class).findById(model_no);

			// 获取表头配置文件
			List<QcmZbsTmplItem> head = this.getMapper(QcmZbsTmplItemMapper.class).findWithCondition(null,
					"TMPL_NO='" + model_no + "' and div_type='0'", "ITEM_STR_ROW,to_number (ITEM_STR_COL)", 0, 0);

			InputStream is = new ByteArrayInputStream(model.getTmpl_cert());
			HSSFWorkbook wb = new HSSFWorkbook(is);
			// 创建HSSFSheet对象
			HSSFSheet sheet = wb.getSheetAt(0);
			// 设置字体
			// Sheet样式
			HSSFCellStyle sheetStyle = wb.createCellStyle();
			HSSFFont headfont = wb.createFont();
			headfont.setFontName("黑体");
			HSSFCellStyle headstyle = wb.createCellStyle();
			headstyle.setFont(headfont);
			// sheetStyle.Alignment = HorizontalAlignment.Center;

			// 获取
			// 获取表头配置项,表头填充逻辑
			// 获取最大表单最大列数
			Integer max_row = sheet.getLastRowNum();
			int jian_ge = 0;
			for (int i = 0; i < head.size(); i++) {
				if (head.get(i).getItem_str_row() != head.get(i + 1).getItem_str_row()) {
					jian_ge = StringFormat.getNumber(head.get(i + 1).getItem_str_row())
							- StringFormat.getNumber(head.get(i).getItem_str_row());
					break;
				}
			}

			for (int i = 0; i < head.size(); i++) {
				int item_str_row = StringFormat.getNumber(head.get(i).getItem_str_row());
				int item_str_col = StringFormat.getNumber(head.get(i).getItem_str_col());
				int val_str_col = StringFormat.getNumber(head.get(i).getVal_str_col());

				// 填充名称
				String value = this.getMapper(QcmZbsDataOrdMapper.class).findValue(head.get(i).getItem_code(),
						ord.getCret_no());
				HSSFRow row2 = sheet.getRow(item_str_row);
				HSSFCell cell = row2.getCell(item_str_col);
				cell.setCellValue(head.get(i).getItem_name());

				// 填充值
				cell = row2.getCell(val_str_col);
				cell.setCellValue(value);
				// 合并行
				// 合并名称单元格
				sheet.addMergedRegion(new CellRangeAddress(item_str_row, item_str_row, item_str_col, val_str_col - 1));
				System.out.println(item_str_row);
				System.out.println(item_str_col);
				System.out.println(val_str_col - 1);

				// sheet.addMergedRegion(new
				// CellRangeAddress(item_str_row,item_str_row+jian_ge,item_str_col,val_str_col-1));

				if (i == 0) {
					sheet.addMergedRegion(new CellRangeAddress(item_str_row, item_str_row, val_str_col,
							(StringFormat.getNumber(head.get(i + 1).getItem_str_col()) - 1)));
				}

				if (i != 0 && i != head.size() - 1) {

					if (!head.get(i).getItem_str_row().equals(head.get(i - 1).getItem_str_row())) {

						if (!head.get(i).getItem_str_row().equals(head.get(i + 1).getItem_str_row())) {
							sheet.addMergedRegion(
									new CellRangeAddress(item_str_row, item_str_row, val_str_col, max_row));

						} else {

							sheet.addMergedRegion(new CellRangeAddress(item_str_row, item_str_row, val_str_col,
									(StringFormat.getNumber(head.get(i + 1).getItem_str_col()) - 1)));

						}

					} else {
						if (!head.get(i).getItem_str_row().equals(head.get(i + 1).getItem_str_row())) {
							sheet.addMergedRegion(
									new CellRangeAddress(item_str_row, item_str_row, val_str_col, max_row));

						} else {

							sheet.addMergedRegion(new CellRangeAddress(item_str_row, item_str_row, val_str_col,
									(StringFormat.getNumber(head.get(i + 1).getItem_str_col()) - 1)));

						}

					}

				}

				if (i == head.size() - 1) {
					sheet.addMergedRegion(new CellRangeAddress(item_str_row, item_str_row, val_str_col, max_row));

				}

			}

			// 获取物料配置
			List<QcmZbsTmplItem> materia = this.getMapper(QcmZbsTmplItemMapper.class).findWithCondition(null,
					"TMPL_NO='" + model_no + "' and div_type='1'", "ITEM_STR_ROW,to_number(ITEM_STR_COL)", 0, 0);

			// 获取成分、材质、物料开始行是不是同一行
			// String materia_row=materia.get(0).getItem_str_row();
			String chem_row = model.getCic_row_title();
			String qlty_row = model.getPic_row_title();

			// 获取成分信息
			List<QcmZbsDataCic> cics = this.getMapper(QcmZbsDataCicMapper.class).findWithCondition(null,
					"CRET_NO='" + ord.getCret_no() + "'", "MATERIAL_NO,ASSAY_TYPE,CHEM_CODE", 0, 0);
			// 提取所有元素
			TreeMap<String, String> allChem = new TreeMap<String, String>();
			for (QcmZbsDataCic qcmZbsDataCic : cics) {
				allChem.put(qcmZbsDataCic.getChem_code(), qcmZbsDataCic.getChem_name());
			}

			// 判断模板元素项是否满足
			String[] chem_name = new String[allChem.size()];
			Set<String> set = allChem.keySet();

			int count = 0;
			for (String c : set) {
				chem_name[count++] = allChem.get(c);
			}
			for (QcmZbsDataCic qcmZbsDataCic : cics) {
				allChem.put(qcmZbsDataCic.getChem_code(), qcmZbsDataCic.getChem_name());
			}

			// 获取成分元素开始行
			Integer cic_row_title = StringFormat.getNumber(model.getCic_row_title());
			Integer cic_col_title = StringFormat.getNumber(model.getCic_col_title());
			if (qlty_row.equals(chem_row)) {

			} else {
				// 如果成分实绩开始行和物料开始行不同，暂不考虑

				// 相同的情况下
				// 配置成分模板

				HSSFRow row3 = sheet.getRow(cic_row_title - 1);
				HSSFCell cel3 = row3.getCell(cic_col_title);
				cel3.setCellValue("化学成分 CHEMICAL COMPOSITION %");
				sheet.addMergedRegion(
						new CellRangeAddress(cic_row_title - 1, cic_row_title - 1, cic_col_title, max_row));

				for (String c : chem_name) {
					HSSFRow row2 = sheet.getRow(cic_row_title);
					HSSFCell cell = row2.getCell(cic_col_title);
					if (cic_col_title > max_row) {
						cic_row_title++;
						cic_col_title = StringFormat.getNumber(model.getCic_col_title());
					} else {
						cell.setCellValue(c);
						cic_col_title++;
					}

				}

				// 填充物料信息
				for (int i = 0; i < materia.size(); i++) {
					if (materia.get(i).getItem_name().equals("厚度") || materia.get(i).getItem_name().equals("宽度")
							|| materia.get(i).getItem_name().equals("长度")
							|| materia.get(i).getItem_name().equals("重量")) {
						HSSFRow row4 = sheet.getRow(StringFormat.getNumber(materia.get(i).getItem_str_row()) - 1);
						System.out.println(StringFormat.getNumber(materia.get(i).getItem_str_row()) - 1);
						HSSFCell cel4 = row4.getCell(StringFormat.getNumber(materia.get(i).getItem_str_col()));
						cel4.setCellValue("尺寸及重量 MATERIAL DESCRIPTION");

					}

					HSSFRow row4 = sheet.getRow(StringFormat.getNumber(materia.get(i).getItem_str_row()));
					System.out.println(StringFormat.getNumber(materia.get(i).getItem_str_row()));
					HSSFCell cel4 = row4.getCell(StringFormat.getNumber(materia.get(i).getItem_str_col()));
					cel4.setCellValue(materia.get(i).getItem_name());

					if (i == materia.size() - 1) {
						sheet.addMergedRegion(
								new CellRangeAddress(StringFormat.getNumber(materia.get(i).getItem_str_row()),
										StringFormat.getNumber(materia.get(i).getItem_str_row()),
										StringFormat.getNumber(materia.get(i).getItem_str_col()), cic_col_title - 3));

					} else {
						int l_row = StringFormat.getNumber(materia.get(i + 1).getItem_str_col());
						if (l_row - 1 != StringFormat.getNumber(materia.get(i).getItem_str_col())) {
							sheet.addMergedRegion(
									new CellRangeAddress(StringFormat.getNumber(materia.get(i).getItem_str_row()),
											StringFormat.getNumber(materia.get(i).getItem_str_row()),
											StringFormat.getNumber(materia.get(i).getItem_str_col()), l_row - 1));

						}

					}

				}

			}

			// 根据质保书编号查询物料信息
			List<QcmZbsDataMat> mats = this.getMapper(QcmZbsDataMatMapper.class).findWithCondition(null,
					"CRET_NO='" + ord.getCret_no() + "'", null, 0, 0);

			// 获取检验大项代码集合
			TreeSet<String> bigItem = new TreeSet<>();

			for (QcmZbsDataMat mat : mats) {
				// 材质分组
				List<QcmZbsDataPic> pics = this.getMapper(QcmZbsDataPicMapper.class).findWithCondition(null,
						"CRET_NO='" + mat.getCret_no() + "' and MATERIAL_NO='" + mat.getMaterial_no() + "'",
						"SMP_NO,SEQ", 0, 0);

				for (QcmZbsDataPic p : pics) {
					bigItem.add(p.getPhy_code_l());
				}
			}

			TreeMap<String, List<QcmZbsDataPic>> qltyItems = new TreeMap<>();

			for (String item : bigItem) {

				List<QcmZbsDataPic> picT = null;

				for (QcmZbsDataMat mat : mats) {
					// 材质分组
					/*
					 * List<QcmZbsDataPic> pics =
					 * this.getMapper(QcmZbsDataPicMapper.class).
					 * findWithCondition(null, "CRET_NO='" + mat.getCret_no() +
					 * "' and MATERIAL_NO='" + mat.getMaterial_no() + "'",
					 * "SMP_NO,SEQ", 0, 0);
					 */

					picT = new ArrayList<>();
					List<QcmZbsDataPic> picR = this.getMapper(QcmZbsDataPicMapper.class).findWithCondition(null,
							"CRET_NO='" + mat.getCret_no() + "' and MATERIAL_NO='" + mat.getMaterial_no()
									+ "' and PHY_CODE_L='" + item + "'",
							"SMP_NO,SEQ", 0, 0);

					for (int i = 0; i < picR.size(); i++) {
						String items = picR.get(i).getPhy_code_l() + picR.get(i).getPhy_code_m()
								+ picR.get(i).getPhy_code_s() + picR.get(i).getItem_code_d()
								+ picR.get(i).getItem_code_s() + picR.get(i).getItem_code_t();

						if (i == 0) {
							picT.add(picR.get(i));
							continue;
						}

						for (int j = 0; j < picT.size(); j++) {
							String newItems = picT.get(j).getPhy_code_l() + picT.get(j).getPhy_code_m()
									+ picT.get(j).getPhy_code_s() + picT.get(j).getItem_code_d()
									+ picT.get(j).getItem_code_s() + picT.get(j).getItem_code_t();

							if (items.equals(newItems)) {
								break;
							}

							if (j == picT.size() - 1) {
								picT.add(picR.get(i));
								break;
							}

						}

					}

				}
				List<QcmZbsDataPic> T = new ArrayList<>();
				// 获取当前检验大项的检验项目的温度
				for (QcmZbsDataPic t : picT) {

					if (t.getItem_code_t() != null && !"".equals(t.getItem_code_t())) {
						QcmZbsDataPic r = new QcmZbsDataPic();
						r.setPhy_code_l(t.getPhy_code_l());
						r.setPhy_name_l(t.getPhy_name_l());
						r.setPhy_code_s("I01");
						r.setPhy_name_s("温度 T");
						r.setPhy_unit("℃");

						T.add(r);
						break;

					}

				}

				// 获取当前检验大项的检验项目的尺寸
				for (QcmZbsDataPic t : picT) {

					if (t.getItem_code_s() != null && !"".equals(t.getItem_code_s())) {
						QcmZbsDataPic r = new QcmZbsDataPic();
						r.setPhy_code_l(t.getPhy_code_l());
						r.setPhy_name_l(t.getPhy_name_l());
						r.setPhy_code_s("I02");
						r.setPhy_name_s("尺寸");
						r.setPhy_unit("mm");
						T.add(r);
						break;

					}

				}
				// 获取所有的小项

				T.addAll(picT);

				qltyItems.put(item, T);

			}

			// 质保书保存对象
			List<QcmZbsObject> targetBook = new ArrayList<>();
			for (QcmZbsDataMat mat : mats) {
				QcmZbsObject obj = new QcmZbsObject();
				List<QcmZbsChemObject> tarChem = new ArrayList<>();
				int chem_size = 0;

				QcmZbsChemObject ch;
				// 获取mat对象的所有属性名、以及值
				Field[] field = mat.getClass().getDeclaredFields();
				TreeMap<String, Object> heat_info = new TreeMap<String, Object>();
				for (Field f : field) {
					heat_info.put(f.getName(), getFieldValueByName(f.getName(), mat));
				}

				// 获取成品成分信息
				List<QcmZbsDataCic> chemP = this.getMapper(QcmZbsDataCicMapper.class)
						.findWithCondition(null, "CRET_NO='" + ord.getCret_no() + "' and MATERIAL_NO='"
								+ mat.getMaterial_no() + "' and ASSAY_TYPE='P'", "MATERIAL_NO,ASSAY_TYPE,CHEM_CODE", 0,
						0);

				List<QcmZbsDataCic> chemL = this.getMapper(QcmZbsDataCicMapper.class)
						.findWithCondition(null, "CRET_NO='" + ord.getCret_no() + "' and MATERIAL_NO='"
								+ mat.getMaterial_no() + "' and ASSAY_TYPE='L'", "MATERIAL_NO,ASSAY_TYPE,CHEM_CODE", 0,
						0);

				// 成品成分
				if (chemP != null && chemP.size() > 0) {
					ch = new QcmZbsChemObject();
					String[] chemValue = new String[chem_name.length];
					int a = 0;
					for (String chemName : chem_name) {

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

							if (chemP.get(i).getChem_name().equals(chemName)) {
								chemValue[a++] = chemP.get(i).getChem_value();
								break;
							}

							if (i == chemP.size() - 1) {
								chemValue[a++] = null;
							}
						}

					}

					ch.setChemValue(chemValue);
					ch.setHeat_info(heat_info);
					ch.setAssay_type("P");
					tarChem.add(ch);
					chem_size++;
				}
				// 熔炼成分
				if (chemL != null && chemL.size() > 0) {
					ch = new QcmZbsChemObject();
					String[] chemValue = new String[chem_name.length];
					int a = 0;
					for (String chemName : chem_name) {

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

							if (chemL.get(i).getChem_name().equals(chemName)) {
								chemValue[a++] = chemL.get(i).getChem_value();
								break;
							}

							if (i == chemL.size() - 1) {
								chemValue[a++] = null;
							}
						}

					}

					ch.setChemValue(chemValue);
					ch.setHeat_info(heat_info);
					ch.setAssay_type("L");
					tarChem.add(ch);
					chem_size++;
				}

				// 材质分组
				List<QcmZbsDataPic> pics = this.getMapper(QcmZbsDataPicMapper.class).findWithCondition(null,
						"CRET_NO='" + mat.getCret_no() + "' and MATERIAL_NO='" + mat.getMaterial_no() + "'",
						"SMP_NO,SEQ", 0, 0);

				// 封装材质项目值
				List<QcmZbsDataPic> RQ = null;
				TreeMap<String, List<QcmZbsDataPic>> qltyList = new TreeMap<>();
				for (int i = 0; i < pics.size(); i++) {

					if (i == 0) {
						RQ = new ArrayList<>();
						RQ.add(pics.get(i));
						continue;
					}

					if (!pics.get(i).getSmp_no().equals(pics.get(i - 1).getSmp_no())) {

						// 封装是否具有温度、尺寸
						for (QcmZbsDataPic q : RQ) {
							if (q.getItem_code_t() != null && !"".equals(q.getItem_code_t())) {
								QcmZbsDataPic r = new QcmZbsDataPic();
								r.setPhy_code_l(q.getPhy_code_l());
								r.setPhy_name_l(q.getPhy_name_l());
								r.setPhy_code_s("I01");
								r.setPhy_name_s("温度 T");
								r.setPhy_unit("℃");
								r.setVal1(q.getItem_name_t());
								System.out.println(q.getItem_name_t());
								RQ.add(r);
								break;
							}
						}

						for (QcmZbsDataPic q : RQ) {
							if (q.getItem_code_s() != null && !"".equals(q.getItem_code_s())) {
								QcmZbsDataPic r = new QcmZbsDataPic();
								r.setPhy_code_l(q.getPhy_code_l());
								r.setPhy_name_l(q.getPhy_name_l());
								r.setPhy_code_s("I02");
								r.setPhy_name_s("尺寸");
								r.setPhy_unit("mm");
								r.setVal1(q.getItem_name_s());
								System.out.println(q.getItem_name_s());
								RQ.add(r);
								break;
							}
						}

						qltyList.put(pics.get(i - 1).getSmp_no(), RQ);
						RQ = new ArrayList<>();
						RQ.add(pics.get(i));
						continue;
					}

					RQ.add(pics.get(i));
					if (i == pics.size() - 1) {

						// 封装是否具有温度、尺寸
						for (QcmZbsDataPic q : RQ) {
							if (q.getItem_code_t() != null && !"".equals(q.getItem_code_t())) {
								QcmZbsDataPic r = new QcmZbsDataPic();
								r.setPhy_code_l(q.getPhy_code_l());
								r.setPhy_name_l(q.getPhy_name_l());
								r.setPhy_code_s("I01");
								r.setPhy_name_s("温度 T");
								r.setPhy_unit("℃");
								r.setVal1(q.getItem_name_t());
								System.out.println(q.getItem_name_t());
								RQ.add(r);
								break;
							}
						}

						for (QcmZbsDataPic q : RQ) {
							if (q.getItem_code_s() != null && !"".equals(q.getItem_code_s())) {
								QcmZbsDataPic r = new QcmZbsDataPic();
								r.setPhy_code_l(q.getPhy_code_l());
								r.setPhy_name_l(q.getPhy_name_l());
								r.setPhy_code_s("I02");
								r.setPhy_name_s("尺寸");
								r.setPhy_unit("mm");
								r.setVal1(q.getItem_name_s());
								System.out.println(q.getItem_name_s());
								RQ.add(r);
								break;
							}
						}

						qltyList.put(pics.get(i).getSmp_no(), RQ);

					}

				}

				// 将成分添加到对象
				obj.setMaterial_no(mat.getMaterial_no());
				obj.setQlty_size(qltyList.size());
				obj.setQltyMap(qltyList);
				obj.setChemList(tarChem);
				obj.setChem_size(chem_size);
				targetBook.add(obj);

			}

			// 填充材质项目行
			// 材质大项开始列
			Integer pic_col_item_l = StringFormat.getNumber(model.getPic_col_item_l());
			// 材质大项开始行
			Integer pic_row_item_l = StringFormat.getNumber(model.getPic_row_item_l());
			// 材质检验项目开始行
			Integer pic_row_item_s = StringFormat.getNumber(model.getPic_row_item_s());
			// 材质检验项目开始列
			Integer pic_col_item_s = StringFormat.getNumber(model.getPic_col_item_s());

			Set<String> keys = qltyItems.keySet();
			for (String key : keys) {
				List<QcmZbsDataPic> pics = qltyItems.get(key);
				Integer bigStr = pic_col_item_s;
				HSSFRow row2 = sheet.getRow(pic_row_item_l);
				HSSFCell cel2 = row2.getCell(bigStr);
				cel2.setCellValue(pics.get(0).getPhy_name_l());
				System.out.println(pics.get(0).getPhy_name_l());
				for (QcmZbsDataPic p : pics) {
					HSSFRow row3 = sheet.getRow(pic_row_item_s);
					HSSFCell cel3 = row3.getCell(pic_col_item_s);
					cel3.setCellValue(p.getPhy_name_s());

					row3 = sheet.getRow(pic_row_item_s + 1);
					cel3 = row3.getCell(pic_col_item_s);
					cel3.setCellValue(p.getPhy_unit());
					pic_col_item_s++;

				}
				// 合并单元格
				sheet.addMergedRegion(new CellRangeAddress(pic_row_item_l, pic_row_item_l, bigStr, pic_col_item_s - 1));

			}

			OutputStream os = new FileOutputStream("E:\\通用普通模板成品.xls");
			wb.write(os);
			os.flush();
			os.close();
			rm.setMsgInfo("质保书生成成功");

		} catch (Exception e) {
			e.printStackTrace();
		}

		return rm;
	}

	/**
	 * 根据属性名获取属性值
	 */
	private Object getFieldValueByName(String fieldName, Object o) {
		try {
			String firstLetter = fieldName.substring(0, 1).toUpperCase();
			String getter = "get" + firstLetter + fieldName.substring(1);
			Method method = o.getClass().getMethod(getter, new Class[] {});
			Object value = method.invoke(o, new Object[] {});
			return value;
		} catch (Exception e) {
			// log.error(e.getMessage(),e);
			return null;
		}
	}

	/**
	 * 打印质保书代码
	 */

	public ResultModel printBookInfo2(CommonPage<QcmZbsDataOrd> sum) throws Exception {
		ResultModel rm = new ResultModel();
		try {

			// 获取订单信息
			QcmZbsDataOrd ord = sum.getObject();
			if (ord == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入需要打印的质保书");
				return rm;
			}
			// 获取模板信息
			String model_no = ord.getTmpl_no();
			if (model_no == null || "".equals(model_no)) {
				rm.setState("500");
				rm.setMsgInfo("请传入模板编号");
				return rm;
			}
			// 模板配置数据
			QcmZbsTmpl model = this.getMapper(QcmZbsTmplMapper.class).findById(model_no);

			// 获取表头配置文件
			List<QcmZbsTmplItem> head = this.getMapper(QcmZbsTmplItemMapper.class).findWithCondition(null,
					"TMPL_NO='" + model_no + "' and DIV_TYPE='0'", null, 0, 0);

			// 获取成分信息
			List<QcmZbsDataCic> cics = this.getMapper(QcmZbsDataCicMapper.class).findWithCondition(null,
					"CRET_NO='" + ord.getCret_no() + "'", "MATERIAL_NO,ASSAY_TYPE,CHEM_CODE", 0, 0);
			// 提取所有元素
			TreeMap<String, String> allChem = new TreeMap<String, String>();
			for (QcmZbsDataCic qcmZbsDataCic : cics) {
				allChem.put(qcmZbsDataCic.getChem_code(), qcmZbsDataCic.getChem_name());
			}

			// 判断模板元素项是否满足
			String[] chem_name = new String[allChem.size()];
			Set<String> set = allChem.keySet();
			int count = 0;
			for (String c : set) {
				chem_name[count++] = allChem.get(c);
			}

			// 根据质保书编号查询物料信息
			List<QcmZbsDataMat> mats = this.getMapper(QcmZbsDataMatMapper.class).findWithCondition(null,
					"CRET_NO='" + ord.getCret_no() + "'", null, 0, 0);

			// 获取检验大项代码集合
			TreeSet<String> bigItem = new TreeSet<>();

			for (QcmZbsDataMat mat : mats) {
				// 材质分组
				List<QcmZbsDataPic> pics = this.getMapper(QcmZbsDataPicMapper.class).findWithCondition(null,
						"CRET_NO='" + mat.getCret_no() + "' and MATERIAL_NO='" + mat.getMaterial_no() + "'",
						"SMP_NO,SEQ", 0, 0);

				for (QcmZbsDataPic p : pics) {
					bigItem.add(p.getPhy_code_l());
				}
			}

			TreeMap<String, List<QcmZbsDataPic>> qltyItems = new TreeMap<>();

			for (String item : bigItem) {

				List<QcmZbsDataPic> picT = null;

				for (QcmZbsDataMat mat : mats) {
					// 材质分组
					picT = new ArrayList<>();
					List<QcmZbsDataPic> picR = this.getMapper(QcmZbsDataPicMapper.class).findWithCondition(null,
							"CRET_NO='" + mat.getCret_no() + "' and MATERIAL_NO='" + mat.getMaterial_no()
									+ "' and PHY_CODE_L='" + item + "'",
							"SMP_NO,SEQ", 0, 0);

					for (int i = 0; i < picR.size(); i++) {
						int value = 0;
						String items = picR.get(i).getPhy_code_l() + picR.get(i).getPhy_code_m()
								+ picR.get(i).getPhy_code_s() + picR.get(i).getItem_code_d()
								+ picR.get(i).getItem_code_s() + picR.get(i).getItem_code_t();

						if (i == 0) {
							if (picR.get(i).getVal1() != null && !"".equals(picR.get(i).getVal1())) {
								value++;
							}
							if (picR.get(i).getVal2() != null && !"".equals(picR.get(i).getVal2())) {
								value++;
							}
							if (picR.get(i).getVal3() != null && !"".equals(picR.get(i).getVal3())) {
								value++;
							}
							if (picR.get(i).getAvg_val() != null && !"".equals(picR.get(i).getAvg_val())) {
								value++;
							}
							picR.get(i).setValue(value);
							picT.add(picR.get(i));
							continue;
						}

						for (int j = 0; j < picT.size(); j++) {
							String newItems = picT.get(j).getPhy_code_l() + picT.get(j).getPhy_code_m()
									+ picT.get(j).getPhy_code_s() + picT.get(j).getItem_code_d()
									+ picT.get(j).getItem_code_s() + picT.get(j).getItem_code_t();

							if (items.equals(newItems)) {
								if (picR.get(i).getVal1() != null && !"".equals(picR.get(i).getVal1())) {
									value++;
								}
								if (picR.get(i).getVal2() != null && !"".equals(picR.get(i).getVal2())) {
									value++;
								}
								if (picR.get(i).getVal3() != null && !"".equals(picR.get(i).getVal3())) {
									value++;
								}
								if (picR.get(i).getAvg_val() != null && !"".equals(picR.get(i).getAvg_val())) {
									value++;
								}
								if (value > picT.get(j).getValue()) {
									picT.get(j).setValue(value);
								}
								break;
							}

							if (j == picT.size() - 1) {
								if (picR.get(i).getVal1() != null && !"".equals(picR.get(i).getVal1())) {
									value++;
								}
								if (picR.get(i).getVal2() != null && !"".equals(picR.get(i).getVal2())) {
									value++;
								}
								if (picR.get(i).getVal3() != null && !"".equals(picR.get(i).getVal3())) {
									value++;
								}
								if (picR.get(i).getAvg_val() != null && !"".equals(picR.get(i).getAvg_val())) {
									value++;
								}
								picR.get(i).setValue(value);
								picT.add(picR.get(i));
								break;
							}

						}

					}

				}
				List<QcmZbsDataPic> T = new ArrayList<>();
				// 获取当前检验大项的检验项目的温度
				for (QcmZbsDataPic t : picT) {

					if (t.getItem_code_t() != null && !"".equals(t.getItem_code_t())) {
						QcmZbsDataPic r = new QcmZbsDataPic();
						r.setPhy_code_l(t.getPhy_code_l());
						r.setPhy_name_l(t.getPhy_name_l());
						r.setPhy_code_s("I01");
						r.setPhy_name_s("温度 T");
						r.setPhy_unit("℃");
						r.setValue(1);
						T.add(r);
						break;

					}

				}

				// 获取当前检验大项的检验项目的尺寸
				for (QcmZbsDataPic t : picT) {

					if (t.getItem_code_s() != null && !"".equals(t.getItem_code_s())) {
						QcmZbsDataPic r = new QcmZbsDataPic();
						r.setPhy_code_l(t.getPhy_code_l());
						r.setPhy_name_l(t.getPhy_name_l());
						r.setPhy_code_s("I02");
						r.setPhy_name_s("尺寸");
						r.setPhy_unit("mm");
						r.setValue(1);
						T.add(r);
						break;

					}

				}
				// 获取所有的小项

				T.addAll(picT);

				qltyItems.put(item, T);

			}

			// 质保书保存对象
			List<QcmZbsObject> targetBook = new ArrayList<>();

			for (QcmZbsDataMat mat : mats) {
				QcmZbsObject obj = new QcmZbsObject();
				List<QcmZbsChemObject> tarChem = new ArrayList<>();
				int chem_size = 0;

				QcmZbsChemObject ch;
				// 获取mat对象的所有属性名、以及值
				Field[] field = mat.getClass().getDeclaredFields();
				TreeMap<String, Object> heat_info = new TreeMap<String, Object>();
				for (Field f : field) {
					heat_info.put(f.getName(), getFieldValueByName(f.getName(), mat));
				}

				// 获取成品成分信息
				List<QcmZbsDataCic> chemP = this.getMapper(QcmZbsDataCicMapper.class)
						.findWithCondition(null, "CRET_NO='" + ord.getCret_no() + "' and MATERIAL_NO='"
								+ mat.getMaterial_no() + "' and ASSAY_TYPE='P'", "MATERIAL_NO,ASSAY_TYPE,CHEM_CODE", 0,
						0);

				List<QcmZbsDataCic> chemL = this.getMapper(QcmZbsDataCicMapper.class)
						.findWithCondition(null, "CRET_NO='" + ord.getCret_no() + "' and MATERIAL_NO='"
								+ mat.getMaterial_no() + "' and ASSAY_TYPE='L'", "MATERIAL_NO,ASSAY_TYPE,CHEM_CODE", 0,
						0);

				// 成品成分
				if (chemP != null && chemP.size() > 0) {
					ch = new QcmZbsChemObject();
					String[] chemValue = new String[chem_name.length];
					int a = 0;
					for (String chemName : chem_name) {

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

							if (chemP.get(i).getChem_name().equals(chemName)) {
								chemValue[a++] = chemP.get(i).getChem_value();
								break;
							}

							if (i == chemP.size() - 1) {
								chemValue[a++] = null;
							}
						}

					}

					ch.setChemValue(chemValue);
					ch.setHeat_info(heat_info);
					ch.setAssay_type("P");
					tarChem.add(ch);
					chem_size++;
				}
				// 熔炼成分
				if (chemL != null && chemL.size() > 0) {
					ch = new QcmZbsChemObject();
					String[] chemValue = new String[chem_name.length];
					int a = 0;
					for (String chemName : chem_name) {

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

							if (chemL.get(i).getChem_name().equals(chemName)) {
								chemValue[a++] = chemL.get(i).getChem_value();
								break;
							}

							if (i == chemL.size() - 1) {
								chemValue[a++] = null;
							}
						}

					}

					ch.setChemValue(chemValue);
					ch.setHeat_info(heat_info);
					ch.setAssay_type("L");
					tarChem.add(ch);
					chem_size++;
				}

				// 材质分组
				List<QcmZbsDataPic> pics = this.getMapper(QcmZbsDataPicMapper.class).findWithCondition(null,
						"CRET_NO='" + mat.getCret_no() + "' and MATERIAL_NO='" + mat.getMaterial_no() + "'",
						"SMP_NO,SEQ", 0, 0);

				// 封装材质项目值
				List<QcmZbsDataPic> RQ = null;
				TreeMap<String, List<QcmZbsDataPic>> qltyList = new TreeMap<>();
				for (int i = 0; i < pics.size(); i++) {

					if (i == 0) {
						RQ = new ArrayList<>();
						RQ.add(pics.get(i));

						if (i == pics.size() - 1) {
							// 封装温度
							encapTemp(RQ);
							// 封装尺寸
							encapSize(RQ);

							qltyList.put(pics.get(i).getSmp_no(), RQ);
						}

						continue;
					}

					if (!pics.get(i).getSmp_no().equals(pics.get(i - 1).getSmp_no())) {
						// 封装温度
						encapTemp(RQ);
						// 封装尺寸
						encapSize(RQ);

						qltyList.put(pics.get(i - 1).getSmp_no(), RQ);
						RQ = new ArrayList<>();
						RQ.add(pics.get(i));
						continue;
					}

					RQ.add(pics.get(i));
					if (i == pics.size() - 1) {
						// 封装温度
						encapTemp(RQ);
						// 封装尺寸
						encapSize(RQ);

						qltyList.put(pics.get(i).getSmp_no(), RQ);

					}

				}

				// 将成分添加到对象
				obj.setMaterial_no(mat.getMaterial_no());
				obj.setQlty_size(qltyList.size());
				obj.setQltyMap(qltyList);
				obj.setChemList(tarChem);
				obj.setChem_size(chem_size);
				targetBook.add(obj);

			}

			// 填充excel 生成质保书
			// 获取质保书模板
			// 暂时用流读入模板
			// InputStream is = new FileInputStream("F:\\1号质保书.xls");
			InputStream is = new ByteArrayInputStream(model.getTmpl_cert());
			HSSFWorkbook wb = new HSSFWorkbook(is);
			// 创建HSSFSheet对象
			HSSFSheet sheet = wb.getSheetAt(0);

			// 获取
			// 获取表头配置项,表头填充逻辑
			for (QcmZbsTmplItem he : head) {

				String value = this.getMapper(QcmZbsDataOrdMapper.class).findValue(he.getItem_code(), ord.getCret_no());
				HSSFRow row2 = sheet.getRow(StringFormat.getNumber(he.getItem_str_row()));
				System.out.println(StringFormat.getNumber(he.getItem_str_row()));
				HSSFCell cel2 = row2.getCell(StringFormat.getNumber(he.getItem_str_col_e()));
				System.out.println(StringFormat.getNumber(he.getItem_str_col_e()));
				if (value == null || "".equals(value)) {
					cel2.setCellValue("");
				} else {
					cel2.setCellValue(value);
					System.out.println(value);
				}

			}

			// 填充成分项目
			// 获取成分模板开始行
			Integer cic_row_title = StringFormat.getNumber(model.getCic_row_title());
			Integer cic_col_title = StringFormat.getNumber(model.getCic_col_title());
			// 获取最大表单最大列数

			// Integer max_row = sheet.getPhysicalNumberOfRows();
			Integer max_col = 36;
			// System.out.println(sheet.getPhysicalNumberOfRows());
			for (String c : chem_name) {
				HSSFRow row2 = sheet.getRow(cic_row_title);

				HSSFCell cel2 = row2.getCell(cic_col_title);

				if (cic_col_title >= max_col) {
					cic_row_title++;
					cic_col_title = StringFormat.getNumber(model.getCic_col_title());
				} else {
					// System.out.println("111" + c);
					if (c != null && !"".equals(c)) {
						cel2.setCellValue(c);
						cic_col_title++;
					} else {
						cel2.setCellValue("");
						cic_col_title++;
					}

				}

			}

			// 获取语种代码
			String lng_code = model.getLng_code();
			// 根据语言代码查询语言
			List<QcmZbsLanguage> laugnagesBig = this.getMapper(QcmZbsLanguageMapper.class).findWithCondition(null,
					"LNG_CODE='" + lng_code + "' and TYPE_CODE='41150203'", null, 0, 0);
			List<QcmZbsLanguage> laugnagesItem = this.getMapper(QcmZbsLanguageMapper.class).findWithCondition(null,
					"LNG_CODE='" + lng_code + "' and TYPE_CODE='41150204'", null, 0, 0);

			// 填充材质项目行
			// 材质大项开始列
			Integer pic_col_item_l = StringFormat.getNumber(model.getPic_col_item_l());
			// 材质大项开始行
			Integer pic_row_item_l = StringFormat.getNumber(model.getPic_row_item_l());
			// 材质检验项目开始行
			Integer pic_row_item_s = StringFormat.getNumber(model.getPic_row_item_s());
			// 材质检验项目开始列
			Integer pic_col_item_s = StringFormat.getNumber(model.getPic_col_item_s());
			// 值单位开始行
			Integer pic_row_item_u = StringFormat.getNumber(model.getPic_row_item_u());
			// 值单位开始列
			Integer pic_col_item_u = StringFormat.getNumber(model.getPic_col_item_u());
			Set<String> keys = qltyItems.keySet();
			for (String key : keys) {
				List<QcmZbsDataPic> pics = qltyItems.get(key);
				// 大项开始列
				Integer bigStr = pic_col_item_s;
				// 设置单元格，大项中文开始行
				HSSFRow row2 = sheet.getRow(pic_row_item_l);
				HSSFCell cel2 = row2.getCell(bigStr);
				cel2.setCellValue(pics.get(0).getPhy_name_l());
				// 填充外文
				for (QcmZbsLanguage l : laugnagesBig) {
					if (key.equals(l.getItem_code())) {
						row2 = sheet.getRow(pic_row_item_l + 1);
						cel2 = row2.getCell(bigStr);
						cel2.setCellValue(l.getVal());
						break;
					}
				}
				// System.out.println(pics.get(0).getPhy_name_l());

				for (QcmZbsDataPic p : pics) {
					int itemStr = pic_col_item_s;
					HSSFRow row3 = sheet.getRow(pic_row_item_s);
					HSSFCell cel3 = row3.getCell(pic_col_item_s);
					cel3.setCellValue(p.getPhy_name_s());
					if ("I01".equals(p.getPhy_code_s())) {
						// 填值单位
						row3 = sheet.getRow(pic_row_item_u);
						cel3 = row3.getCell(pic_col_item_s);
						cel3.setCellValue(p.getPhy_unit());

						pic_col_item_s++;
						continue;
					}

					if ("I02".equals(p.getPhy_code_s())) {

						// 填值单位
						row3 = sheet.getRow(pic_row_item_u);
						cel3 = row3.getCell(pic_col_item_s);
						cel3.setCellValue(p.getPhy_unit());

						pic_col_item_s++;
						continue;
					}

					// 填充项目对应的英文
					for (QcmZbsLanguage l : laugnagesItem) {
						if (l.getItem_code().equals(p.getPhy_code_s())) {
							row3 = sheet.getRow(pic_row_item_s + 1);
							cel3 = row3.getCell(pic_col_item_s);
							cel3.setCellValue(l.getEng_abbr());

							if (p.getValue() == 1) {

								// 填值单位
								row3 = sheet.getRow(pic_row_item_u);
								cel3 = row3.getCell(pic_col_item_s);
								cel3.setCellValue(p.getPhy_unit());

								row3 = sheet.getRow(pic_row_item_s + 2);
								cel3 = row3.getCell(pic_col_item_s++);
								cel3.setCellValue(l.getItem_type());

							} else {

								row3 = sheet.getRow(pic_row_item_u);
								cel3 = row3.getCell(pic_col_item_s);
								cel3.setCellValue(p.getPhy_unit());

								if (p.getValue() == 2) {

									row3 = sheet.getRow(pic_row_item_s + 2);
									cel3 = row3.getCell(pic_col_item_s++);
									cel3.setCellValue(1);

									row3 = sheet.getRow(pic_row_item_s + 2);
									cel3 = row3.getCell(pic_col_item_s++);
									cel3.setCellValue("AVE");

								}
								if (p.getValue() == 3) {
									row3 = sheet.getRow(pic_row_item_s + 2);
									cel3 = row3.getCell(pic_col_item_s++);
									cel3.setCellValue(1);

									row3 = sheet.getRow(pic_row_item_s + 2);
									cel3 = row3.getCell(pic_col_item_s++);
									cel3.setCellValue(2);

									row3 = sheet.getRow(pic_row_item_s + 2);
									cel3 = row3.getCell(pic_col_item_s++);
									cel3.setCellValue("AVE");
								}
								if (p.getValue() == 4) {

									row3 = sheet.getRow(pic_row_item_s + 2);
									cel3 = row3.getCell(pic_col_item_s++);
									cel3.setCellValue(1);

									row3 = sheet.getRow(pic_row_item_s + 2);
									cel3 = row3.getCell(pic_col_item_s++);
									cel3.setCellValue(2);

									row3 = sheet.getRow(pic_row_item_s + 2);
									cel3 = row3.getCell(pic_col_item_s++);
									cel3.setCellValue(3);

									row3 = sheet.getRow(pic_row_item_s + 2);
									cel3 = row3.getCell(pic_col_item_s++);
									cel3.setCellValue("AVE");

								}

								// 合并项目单元格
								sheet.addMergedRegion(new CellRangeAddress(pic_row_item_s, pic_row_item_s, itemStr,
										pic_col_item_s - 1));

								//
								sheet.addMergedRegion(new CellRangeAddress(pic_row_item_s + 1, pic_row_item_s + 1,
										itemStr, pic_col_item_s - 1));

							}

							break;
						}
					}
					// 合并值单位
					if (pic_col_item_s - 1 > itemStr) {
						sheet.addMergedRegion(
								new CellRangeAddress(pic_row_item_u, pic_row_item_u, itemStr, pic_col_item_s - 1));

					}

				}

				// 合并大项单元格
				if (pic_col_item_s - 1 > bigStr) {
					sheet.addMergedRegion(
							new CellRangeAddress(pic_row_item_l, pic_row_item_l, bigStr, pic_col_item_s - 1));

				}

				// 合并大项英文名
				if (pic_col_item_s - 1 > bigStr) {
					sheet.addMergedRegion(
							new CellRangeAddress(pic_row_item_l + 1, pic_row_item_l + 1, bigStr, pic_col_item_s - 1));

				}

			}

			// 获取成分配置项
			List<QcmZbsTmplItem> chemConfigure = this.getMapper(QcmZbsTmplItemMapper.class).findWithCondition(null,
					"TMPL_NO='" + model_no + "' and DIV_TYPE='1'", null, 0, 0);

			// 填充成分、材质实绩
			Integer cic_row_val = StringFormat.getNumber(model.getCic_row_val());
			Integer pic_row_val = StringFormat.getNumber(model.getPic_row_val());

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

				List<QcmZbsChemObject> cic = targetBook.get(i).getChemList();
				for (QcmZbsChemObject o : cic) {
					Integer cic_col_val = StringFormat.getNumber(model.getCic_col_val());
					int meter_star = cic_row_val;

					// 填充成分值
					for (String ch : o.getChemValue()) {

						HSSFRow row2 = sheet.getRow(cic_row_val);
						HSSFCell cell = row2.getCell(cic_col_val);
						if (cic_col_val >= max_col) {
							cic_row_val++;
							cic_col_val = StringFormat.getNumber(model.getCic_col_val());
						} else {
							cell.setCellValue(ch);
							cic_col_val++;
						}

					}

					// 填充成分配置
					TreeMap<String, Object> tree = o.getHeat_info();
					Set<String> se = tree.keySet();
					for (QcmZbsTmplItem c : chemConfigure) {

						for (String key : se) {
							if (c.getItem_code().equals(key.toUpperCase())) {
								Object value = tree.get(key);
								// 获取单元格
								HSSFRow row1 = sheet.getRow(meter_star);
								System.out.println(c.getItem_str_col());
								HSSFCell cell = row1.getCell(StringFormat.getNumber(c.getItem_str_col_e()));
								System.out.println((String) value);
								cell.setCellValue((String) value);

							}

						}

					}

					// 行加一
					cic_row_val++;

				}

				// 填充材质值
				Set<String> qs = targetBook.get(i).getQltyMap().keySet();

				// 获取所有项目
				// key是代表试样号smp_no
				Set<String> qi = qltyItems.keySet();
				for (String key : qs) {
					Integer pic_col_val = StringFormat.getNumber(model.getPic_col_val());
					List<QcmZbsDataPic> pic = targetBook.get(i).getQltyMap().get(key);

					// k拿到所有检验大项代码

					for (String k : qi) {
						List<QcmZbsDataPic> pi = qltyItems.get(k);

						for (QcmZbsDataPic p : pi) {
							String oldItem = p.getPhy_code_l() + p.getPhy_code_m() + p.getPhy_code_s()
									+ p.getItem_code_d() + p.getItem_code_s() + p.getItem_code_t();
							for (int j = 0; j < pic.size(); j++) {
								String newItem = pic.get(j).getPhy_code_l() + pic.get(j).getPhy_code_m()
										+ pic.get(j).getPhy_code_s() + pic.get(j).getItem_code_d()
										+ pic.get(j).getItem_code_s() + pic.get(j).getItem_code_t();

								if (oldItem.equals(newItem)) {
									// 材质值
									HSSFRow row1 = sheet.getRow(pic_row_val);
									HSSFCell cel1 = row1.getCell(pic_col_val);
									if (p.getValue() == 1) {
										cel1.setCellValue(pic.get(j).getVal1());
										pic_col_val++;
									} else if (p.getValue() == 2) {
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getVal1());
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getAvg_val());
										pic_col_val++;
									} else if (p.getValue() == 3) {
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getVal1());
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getVal2());
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getAvg_val());
										pic_col_val++;
									} else if (p.getValue() == 4) {
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getVal1());
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getVal2());
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getVal3());
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getAvg_val());

									}

									break;
								}

								if (j == pic.size() - 1) {
									// 材质值
									HSSFRow row1 = sheet.getRow(pic_row_val);
									HSSFCell cel1 = row1.getCell(pic_col_val);
									cel1.setCellValue("");
									pic_col_val++;
									break;
								}

							}

						}

					}

					/*
					 * for (QcmZbsDataPic p : pic) { //材质值 HSSFRow row1 =
					 * sheet.getRow(pic_row_val); HSSFCell cel1 =
					 * row1.getCell(pic_col_val);
					 * cel1.setCellValue(p.getVal1()); pic_col_val++; }
					 */
					// 卷号
					HSSFRow row2 = sheet.getRow(pic_row_val);
					HSSFCell cel2 = row2.getCell(1);
					cel2.setCellValue(targetBook.get(i).getMaterial_no());
					// 试样类型
					QcmJhySampleConsignM qm = this.getMapper(QcmJhySampleConsignMMapper.class).findById(key);
					row2 = sheet.getRow(pic_row_val);
					cel2 = row2.getCell(2);

					if (qm != null) {
						cel2.setCellValue(qm.getSmp_type_name());
					} else {
						cel2.setCellValue("");
					}

					pic_row_val++;

				}

			}

			/*
			 * String path =
			 * this.getRequest().getServletContext().getRealPath("/") + "excel"
			 * + File.separator; // 临时保存路径 File dir = new File(path); if
			 * (!dir.exists()) { new File(path).mkdir(); }
			 * 
			 * String book_num = path + "第[" + ord.getCret_no() + "]号质保书.xls";
			 */

			OutputStream os = new FileOutputStream("E:\\通用普通模板成品.xls");
			wb.write(os);
			os.flush();
			os.close();
			rm.setData("/download?filename=" + "第[" + ord.getCret_no() + "]号质保书.xls");
			// rm.setData(bytess);
			rm.setState("200");
			rm.setMsgInfo("质保书生成成功");

			// ouputStream.close();

		} catch (Exception e) {
			rm.setState("500");
			e.printStackTrace();
			rm.setMsgInfo("生成质保书失败");
			return rm;
		}

		return rm;
	}

	private void encapSize(List<QcmZbsDataPic> RQ) {
		for (QcmZbsDataPic q : RQ) {
			if (q.getItem_code_s() != null && !"".equals(q.getItem_code_s())) {
				QcmZbsDataPic r = new QcmZbsDataPic();
				r.setPhy_code_l(q.getPhy_code_l());
				r.setPhy_name_l(q.getPhy_name_l());
				r.setPhy_code_s("I02");
				r.setPhy_name_s("尺寸");
				r.setPhy_unit("mm");
				r.setVal1(q.getItem_name_s());
				// System.out.println(q.getItem_name_s());
				RQ.add(r);
				break;
			}
		}
	}

	private void encapTemp(List<QcmZbsDataPic> RQ) {
		for (QcmZbsDataPic q : RQ) {
			if (q.getItem_code_t() != null && !"".equals(q.getItem_code_t())) {
				QcmZbsDataPic r = new QcmZbsDataPic();
				r.setPhy_code_l(q.getPhy_code_l());
				r.setPhy_name_l(q.getPhy_name_l());
				r.setPhy_code_s("I01");
				r.setPhy_name_s("温度 T");
				r.setPhy_unit("℃");
				r.setVal1(q.getItem_name_t());
				// System.out.println(q.getItem_name_t());
				RQ.add(r);
				break;
			}
		}
	}

	private Integer judgeIsValue(HSSFRow row1, Integer cic_row_val, int number) {

		HSSFCell cel1 = row1.getCell(cic_row_val);
		if (cel1.getStringCellValue() != null && !"".equals(cel1.getStringCellValue())) {
			cel1 = row1.getCell(number);
			cic_row_val++;
			return judgeIsValue(row1, cic_row_val, number);
		} else {
			return cic_row_val;
		}

	}

	public ResultModel printBookEdition(CommonPage<QcmZbsDataOrd> sum) throws Exception {
		ResultModel rm = new ResultModel();
		try {
			List<QcmZbsDataOrd> ords = sum.getObjectList();
			// 生成质保书的保存路径
			List<String> tarRoot = new ArrayList<String>();
			for (QcmZbsDataOrd ord : ords) {
				String data=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
				data=data.substring(0, 16);
				String model_no = ord.getTmpl_no();
				ord = this.getMapper(QcmZbsDataOrdMapper.class).findById(ord.getCret_no());
				// 模板配置数据
				QcmZbsTmpl model = this.getMapper(QcmZbsTmplMapper.class).findById(model_no);
				// 成分表头信息
				List<QcmZbsDataCic> cicTitle = new ArrayList<QcmZbsDataCic>();
				createChemTitle(ord, cicTitle);
				// 材质表头信息
				List<QcmZbsObjectQlty> picTitle = new ArrayList<QcmZbsObjectQlty>();
				createQltyTitle(ord, picTitle);
				// 封装质保书对象实体类

				List<QcmZbsObject> targetBook = new ArrayList<>();
				createZbsObject(ord, targetBook, cicTitle);
				// 获取模板配置，判断成分、材质总共可以占有的行数
				// 填充成分、材质实绩
				Integer cic_row_val = model.getCic_row_val() == null ? null
						: StringFormat.getNumber(model.getCic_row_val());
				Integer cic_col_val = model.getCic_col_val() == null ? null
						: StringFormat.getNumber(model.getCic_col_val());
				Integer pic_row_val = model.getPic_row_val() == null ? null
						: StringFormat.getNumber(model.getPic_row_val());
				Integer cic_col_title = model.getCic_col_title() == null ? null
						: StringFormat.getNumber(model.getCic_col_title());
				Integer cic_row_title = model.getCic_row_title() == null ? null
						: StringFormat.getNumber(model.getCic_row_title());
				Integer pic_row_item_l = model.getPic_row_item_l() == null ? null
						: StringFormat.getNumber(model.getPic_row_item_l());
				Integer pic_col_item_l = model.getPic_row_val() == null ? null
						: StringFormat.getNumber(model.getPic_col_item_l());
				Integer remark_row = model.getPic_row_val() == null ? null
						: StringFormat.getNumber(model.getRemark_row());
				Integer pic_row_item_u = model.getPic_row_val() == null ? null
						: StringFormat.getNumber(model.getPic_row_item_u());

				// 判断成分和材质是否在同一行
				String sameRow = "0";
				// 创建文件夹
				Integer page = 1;

				String path = this.getRequest().getServletContext().getRealPath("/") + "excel" + File.separator; // 临时保存路径
				File dir = new File(path);
				if (!dir.exists()) {
					new File(path).mkdir();
				}
				//创建二维码
				createErCode(path,ord.getCret_no());
				String book_num = null;
				// 获取excel对象
				InputStream is = new ByteArrayInputStream(model.getTmpl_cert());
				HSSFWorkbook wb = new HSSFWorkbook(is);
				// 创建HSSFSheet对象
				HSSFSheet sheet = wb.getSheetAt(0);
				HSSFWorkbook targetWb =null;
				HSSFSheet targetSheet =null;
				//成分、材质最大行数
				int max_chem=pic_row_item_l-cic_row_val;
				int max_qlty=remark_row-pic_row_item_u-1;
				
				int oneChemNum = 1;
				int oneQltyNum = 1;
				int chem_row = 1;
				;
				if (cic_row_val == pic_row_val) {

				} else {
					oneChemNum = (sheet.getRow(StringFormat.getNumber(model.getCic_col_title()))
							.getPhysicalNumberOfCells() - StringFormat.getNumber(model.getCic_col_val()));
					max_chem = pic_row_item_l - cic_row_val;
				}
				chem_row = cicTitle.size() % oneChemNum == 0 ? cicTitle.size() / oneChemNum
						: cicTitle.size() / oneChemNum + 1;
				int count_chem = 0;
				int count_qlty = 0;
				//成分、材质开始行
				int chem_str_row=cic_row_val;
				int qlty_str_row=pic_row_val;

				// 获取表头配置项 // 获取表头配置文件
				List<QcmZbsTmplItem> head = this.getMapper(QcmZbsTmplItemMapper.class).findWithCondition(null,
						"TMPL_NO='" + model_no + "' and DIV_TYPE='0'", null, 0, 0);
				// 填充行计数
				int seq=1;
				for (int i = 0; i < targetBook.size(); i++) {
					 chem_str_row=cic_row_val;
				     qlty_str_row=pic_row_val;
					  int chem_size=targetBook.get(i).getChem_size()*chem_row;
					  int qlty_size=targetBook.get(i).getQlty_size();
					  if (chem_size==0) {
						  chem_size=1;
					  }
					  if (qlty_size==0) {
						  qlty_size=1;
					}
					  count_chem+=chem_size;
					  count_qlty+=qlty_size;
					// 当第一个卷进来时
					if (i == 0) {
						// count_chem=chem_size;
						// count_qlty=qlty_size;
						// 创建表头信息
						addCePicture(sheet,model,ord,path,wb);
						addErPicture(sheet,model,path,wb,ord.getCret_no());
						addPicture(sheet,model,path,wb);
						addHead(head, sheet, ord,data);
						addChemTitle(cic_col_title, cic_row_title, pic_col_item_l, cicTitle, sameRow, sheet);
						addQltyTitle(model, picTitle, sameRow, sheet);
						addQltyAndChem(model, targetBook.get(i), sameRow, picTitle, sheet, chem_str_row, qlty_str_row,seq);
						
						
						if (i ==targetBook.size()-1) {
							//book_num = path + "第[" + ord.getCret_no() + "]" + page.toString() + "号质保书.xls";
							//OutputStream os = new FileOutputStream(book_num);
							
							ByteArrayOutputStream baos = new ByteArrayOutputStream();
							if (targetWb==null) {
								targetWb=wb;
								targetSheet=targetWb.getSheetAt(0);
							}else {
								//拼接excel
								MergeHSSFSheet copySheetUtil=new MergeHSSFSheet();
								copySheetUtil.mergeHSSFSheet(wb, targetSheet, sheet, true, 1);
								targetWb.setPrintArea(0, targetSheet.getRow(0).getFirstCellNum(), targetSheet.getRow(0).getLastCellNum()-1, targetSheet.getFirstRowNum(), targetSheet.getLastRowNum());
								//targetWb.setPrintArea(0, targetSheet.getRow(0).getFirstCellNum(), targetSheet.getRow(0).getLastCellNum(), targetSheet.getFirstRowNum(), targetSheet.getLastRowNum());

							}
							targetWb.write(baos);
							baos.flush();
							byte[] bytes = baos.toByteArray();
							ord.setCret_xls(bytes);
							//插入PDF
							ByteArrayInputStream in=new ByteArrayInputStream(bytes);
							if (!getLicense()) {
						           // return;
						        }
							Workbook pdfwb = new Workbook(in);// 原始excel路径
							ByteArrayOutputStream outPdf=new ByteArrayOutputStream();
							pdfwb.save(outPdf, SaveFormat.PDF);
							byte[] pdf=outPdf.toByteArray();
							ord.setCret_pdf(pdf);
							
							this.getMapper(QcmZbsDataOrdMapper.class).update(ord);
							baos.close();
							
							//wb.write(os);
							//os.flush();
							//os.close();
							// 填充EXCEL的成分材质
//							tarRoot.add(
//									"/download?filename=" + "第[" + ord.getCret_no() + "]" + page.toString() + "号质保书.xls");
							continue;

						}
						
						//判断下一个卷可不可以放下
						int next_chem_size=targetBook.get(i+1).getChem_size()*chem_row;
						int next_qlty_size=targetBook.get(i+1).getQlty_size();
						if (next_chem_size==0) {
							next_chem_size=1;
						}
						if (next_qlty_size==0) {
							next_qlty_size=1;
						}
						if (next_chem_size+count_chem>max_chem||next_qlty_size+count_qlty>max_qlty) {
							//关闭流
							book_num = path + "第[" + ord.getCret_no() + "]" + page.toString() + "号质保书.xls";
							//OutputStream os = new FileOutputStream(book_num);
							if (1==page) {
								targetWb=wb;
								targetSheet=targetWb.getSheetAt(0);
							}else {
								MergeHSSFSheet copySheetUtil=new MergeHSSFSheet();
								copySheetUtil.mergeHSSFSheet(targetWb, targetSheet, sheet, true, 1);
								targetWb.setPrintArea(0, targetSheet.getRow(0).getFirstCellNum(), targetSheet.getRow(0).getLastCellNum()-1, targetSheet.getFirstRowNum(), targetSheet.getLastRowNum());
							}

//							wb.write(os);
//							os.flush();
//							os.close();
							// 填充EXCEL的成分材质
//							tarRoot.add(
//									"/download?filename=" + "第[" + ord.getCret_no() + "]" + page.toString() + "号质保书.xls");

							//统计置为0
							count_chem = 0;
						    count_qlty = 0;
						    page++;
						    //行复原
						    cic_row_val = model.getCic_row_val() == null ? null
									: StringFormat.getNumber(model.getCic_row_val());
				
						    pic_row_val = model.getPic_row_val() == null ? null
									: StringFormat.getNumber(model.getPic_row_val());
						    seq=1;
						    //重新引入模板
						    is = new ByteArrayInputStream(model.getTmpl_cert());
							wb = new HSSFWorkbook(is);
							sheet = wb.getSheetAt(0);
						}else{
							seq+=1;
							//材质成分行到下一个卷的开始行
							cic_row_val+=chem_size;
							pic_row_val+=qlty_size;
						}
						
						continue;
					}
					//填充成分材质代码
					addQltyAndChem(model, targetBook.get(i), sameRow, picTitle, sheet, chem_str_row, qlty_str_row,seq);
					if (count_chem<=max_chem&&count_qlty<=max_qlty&&i!= targetBook.size()-1) {
						//判断下一个卷可不可以放下
						int next_chem_size=targetBook.get(i+1).getChem_size()*chem_row;
						int next_qlty_size=targetBook.get(i+1).getQlty_size();
						if (next_chem_size==0) {
							next_chem_size=1;
						}
						if (next_qlty_size==0) {
							next_qlty_size=1;
						}
						if (next_chem_size+count_chem>max_chem||next_qlty_size+count_qlty>max_qlty) {
							//关闭流
							//book_num = path + "第[" + ord.getCret_no() + "]" + page.toString() + "号质保书.xls";
							//OutputStream os = new FileOutputStream(book_num);
							if (1==page) {
								targetWb=wb;
								targetSheet=targetWb.getSheetAt(0);
							}else {
								MergeHSSFSheet copySheetUtil=new MergeHSSFSheet();
								copySheetUtil.mergeHSSFSheet(targetWb, targetSheet, sheet, true, 1);
								//targetWb.setPrintArea(0, targetSheet.getRow(0).getFirstCellNum(), targetSheet.getRow(0).getLastCellNum()-1, targetSheet.getFirstRowNum(), targetSheet.getLastRowNum());
							}

//							wb.write(os);
//							os.flush();
//							os.close();
							// 填充EXCEL的成分材质
//							tarRoot.add(
//									"/download?filename=" + "第[" + ord.getCret_no() + "]" + page.toString() + "号质保书.xls");

							//统计置为0
							count_chem = 0;
						    count_qlty = 0;
						    page++;
						    //行复原
						    cic_row_val = model.getCic_row_val() == null ? null
									: StringFormat.getNumber(model.getCic_row_val());
				
						    pic_row_val = model.getPic_row_val() == null ? null
									: StringFormat.getNumber(model.getPic_row_val());
						    seq=1;
						    is = new ByteArrayInputStream(model.getTmpl_cert());
							wb = new HSSFWorkbook(is);
							sheet = wb.getSheetAt(0);
							//填充配置信息
							addCePicture(sheet,model,ord,path,wb);
							addErPicture(sheet,model,path,wb,ord.getCret_no());
							addPicture(sheet,model,path,wb);
							addHead(head, sheet, ord,data);
							addChemTitle(cic_col_title, cic_row_title, pic_col_item_l, cicTitle, sameRow, sheet);
							addQltyTitle(model, picTitle, sameRow, sheet);

						}else{
							seq+=1;
							//材质成分行到下一个卷的开始行
							cic_row_val+=chem_size;
							pic_row_val+=qlty_size;
						}
						
						continue;
						
						
						
					}else if (i == targetBook.size()-1) {
						//关闭流
//						book_num = path + "第[" + ord.getCret_no() + "]" + page.toString() + "号质保书.xls";
//						OutputStream os = new FileOutputStream(book_num);
						ByteArrayOutputStream baos = new ByteArrayOutputStream();
						if (1==page) {
							targetWb=wb;
							targetSheet=targetWb.getSheetAt(0);
							targetWb.write(baos);
							baos.flush();
							byte[] bytes = baos.toByteArray();
							ord.setCret_xls(bytes);
							
							ByteArrayInputStream in=new ByteArrayInputStream(bytes);
							if (!getLicense()) {
						           // return;
						        }
							Workbook pdfwb = new Workbook(in);// 原始excel路径
							ByteArrayOutputStream outPdf=new ByteArrayOutputStream();
							pdfwb.save(outPdf, SaveFormat.PDF);
							byte[] pdf=outPdf.toByteArray();
							ord.setCret_pdf(pdf);
							
							this.getMapper(QcmZbsDataOrdMapper.class).update(ord);
							baos.close();
						}else {
							//拼接excel
							MergeHSSFSheet copySheetUtil=new MergeHSSFSheet();
							copySheetUtil.mergeHSSFSheet(targetWb, targetSheet, sheet, true, 1);
						    //targetWb.setPrintArea(0, targetSheet.getRow(0).getFirstCellNum(), targetSheet.getRow(0).getLastCellNum()-1, targetSheet.getFirstRowNum(), targetSheet.getLastRowNum());
						    targetWb.write(baos);
							baos.flush();
							byte[] bytes = baos.toByteArray();
							ord.setCret_xls(bytes);
							ByteArrayInputStream in=new ByteArrayInputStream(bytes);
							if (!getLicense()) {
						           // return;
						        }
							Workbook pdfwb = new Workbook(in);// 原始excel路径
							ByteArrayOutputStream outPdf=new ByteArrayOutputStream();
							pdfwb.save(outPdf, SaveFormat.PDF);
							byte[] pdf=outPdf.toByteArray();
							ord.setCret_pdf(pdf);
							this.getMapper(QcmZbsDataOrdMapper.class).update(ord);
							baos.close();
						}
						//获取xls转化为pdf
						

//						wb.write(os);
//						os.flush();
//						os.close();
//						// 填充EXCEL的成分材质
//						tarRoot.add(
//								"/download?filename=" + "第[" + ord.getCret_no() + "]" + page.toString() + "号质保书.xls");

					}else{
						rm.setState("500");
						rm.setMsgInfo("生成质保书失败,质保书编号为["+ord.getCret_no()+"]的成分或材质实绩不适应模板["+model.getTmpl_desc()+"]");
						return rm;
					}
					
					
					

				}

			}
			//rm.setData(tarRoot);
			rm.setState("200");
			rm.setMsgInfo("质保书生成成功");

		} catch (Exception e) {
			e.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("生成质保书失败！"+e.getMessage());
			return rm;
		}

		return rm;

	}

	/**
	 * 添加CE
	 * @param sheet
	 * @param model
	 * @param ord
	 * @param path
	 * @param wb
	 * @throws Exception 
	 */
	private void addCePicture(HSSFSheet sheet, QcmZbsTmpl model, QcmZbsDataOrd ord, String path, HSSFWorkbook wb) throws Exception {
		// 获取表头配置项 // 获取表头配置文件
		if (ord.getCe_no()==null || "".equals(ord.getCe_no())) {
			return;
		}
		if (ord.getOrg_code()==null ||"".equals(ord.getOrg_code())||"409601".equals(ord.getOrg_code())) {
			return;
		}
		//查询认证机构配置的认证师
		List<QcmZbsOrg> orgs=this.getMapper(QcmZbsOrgMapper.class).findWithCondition(null, "ORG_CODE='"+ord.getOrg_code()+"' and VALIDFLAG='1'", "SEQ", 0, 0);
		if (orgs==null || orgs.size()<1) {
			return;	
		}
		QcmZbsOrg org=orgs.get(0);
		List<QcmZbsTmplItem> ces = this.getMapper(QcmZbsTmplItemMapper.class).findWithCondition(null,
				"TMPL_NO='" + model.getTmpl_no() + "' and DIV_TYPE='2'", null, 0, 0);
		
		if (ces!=null && ces.size()>0 ) {
			for (QcmZbsTmplItem ce : ces) {
				String name=null;
				if (ce.getItem_code()!=null && !"".equals(ce.getItem_code())) {
					name=ce.getItem_code().toLowerCase();
				}
				String value=null;
				if (name!=null && "".equals(name)) {
					value= (String) getFieldValueByName(name, org);
				}
				Integer item_str_row=StringFormat.getNumber(ce.getItem_str_row());
				Integer item_str_col_e=StringFormat.getNumber(ce.getItem_str_col_e());
				if (item_str_row!=null && item_str_col_e!=null) {
					HSSFRow row2 = sheet.getRow(StringFormat.getNumber(ce.getItem_str_row()));
					HSSFCell cel2 = row2.getCell(StringFormat.getNumber(ce.getItem_str_col_e()));
					if (cel2!=null) {
						cel2.setCellValue(value);
					}
				}
			    
				
				
			}
		}
		
	}

	/**
	 * 添加二维码到excel
	 * @param sheet
	 * @param model
	 * @param path
	 * @param wb
	 * @param cret_no 
	 * @throws Exception 
	 */
	private void addErPicture(HSSFSheet sheet, QcmZbsTmpl model, String path, HSSFWorkbook wb, String cret_no) throws Exception {
		//获取二维码填充行列
		int e_row_str = model.getE_row_str()==null?0:StringFormat.getNumber(model.getE_row_str());
		int e_col_str=model.getE_col_str()==null?0:StringFormat.getNumber(model.getE_col_str());
		if (e_col_str!=0 && e_row_str!=0) {
			String er_path=path+cret_no+"二维码.png";
			BufferedImage bufferImg =null; 
			ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); 
			bufferImg = ImageIO.read(new File(er_path));
			ImageIO.write(bufferImg,"png",byteArrayOut);
			HSSFPatriarch patriarch = sheet.createDrawingPatriarch();   
		    HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,512,255,(short) e_col_str,e_row_str,(short)(e_col_str+2),e_row_str+5);
		    patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));   

			
		}
		
		
	}

	private void createErCode(String path, String cret_no) {
		//要生成二维码的链接
		String url="http://172.16.0.76:8004/xingangor.cutDeal/service/QcmZbsDataOrdService/getErCode?id="+cret_no;
		String dir=path;//输出的文件夹
		String name=cret_no+"二维码";//输出的文件名
		try {
			QRCodeUtil.createQRCode(url, dir, name);//调用工具类中的方法进行生成
		} catch (IOException e) {
			e.printStackTrace();
		}

		
	}

	/**
	 * 添加图片信息
	 * @param sheet
	 * @param model
	 * @param path 
	 * @param wb 
	 * @throws Exception 
	 */
	private void addPicture(HSSFSheet sheet, QcmZbsTmpl model, String path, HSSFWorkbook wb) throws Exception {
		//签名图片
		byte[] sin_pic=model.getImg_sig();
		String sin_picture=null;
		if (sin_pic!=null && sin_pic.length>0) {
			sin_picture= new String(sin_pic);
		}
		byte[]  img_ce=model.getImg_ce();
		String ce_picture=null;
		if (img_ce!=null && img_ce.length>0) {
			ce_picture=new String(img_ce);
		}
		//签章图片
		byte[]  img_seal=model.getImg_seal();
		String seal_picture=null;
		if (img_seal!=null && img_seal.length>0) {
			seal_picture=new String(img_seal);
		}
		
		//CE图片
		//签名行列
		int sig_row = model.getSig_row()==null?0:StringFormat.getNumber(model.getSig_row());
		int sig_col=model.getSig_col()==null?0:StringFormat.getNumber(model.getSig_col());
		//CE行列
		int ce_img_row = model.getCe_img_row()==null?0:StringFormat.getNumber(model.getCe_img_row());
		int ce_img_col=model.getCe_img_col()==null?0:StringFormat.getNumber(model.getCe_img_col());
		//签章行列
		int img_3_row = model.getImg_3_row()==null?0:StringFormat.getNumber(model.getImg_3_row());
		int img_3_col=model.getImg_3_col()==null?0:StringFormat.getNumber(model.getImg_3_col());
		String imgFilePath=path + "sig.png";
		String ce_path=path+"ce.png";
		String seal_path=path+"seal.png";
		if (sin_picture!=null && !"".equals(sin_picture)) {
			if (sig_row!=0 && sig_col!=0) {
			DataHelper.GenerateImage(sin_picture, imgFilePath);
				 //先把读进来的图片放到一个ByteArrayOutputStream中，以便产生ByteArray  
			 BufferedImage bufferImg =null;  
			 ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();  
			 bufferImg = ImageIO.read(new File(imgFilePath));
			 ImageIO.write(bufferImg,"png",byteArrayOut);
			 HSSFPatriarch patriarch = sheet.createDrawingPatriarch();   
		     HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,512,255,(short) sig_col,sig_row,(short)(sig_col+2),sig_row+2);
		     patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));   

			}
			
			
		}
		
		//添加CE
		if (ce_picture!=null && !"".equals(ce_picture)) {
			if (ce_img_col!=0 && ce_img_row!=0) {
				DataHelper.GenerateImage(ce_picture, ce_path);
				BufferedImage bufferImg =null;  
				 ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();  
				 bufferImg = ImageIO.read(new File(ce_path));
				 ImageIO.write(bufferImg,"png",byteArrayOut);
				 HSSFPatriarch patriarch = sheet.createDrawingPatriarch();   
			     HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,512,255,(short) ce_img_col,ce_img_row,(short)(ce_img_col+2),ce_img_row+2);
			     patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));   

			}
		}
		
		
		//添加盖章
		if (seal_picture!=null && !"".equals(seal_picture)) {
			if (img_3_row!=0 && img_3_col!=0) {
				DataHelper.GenerateImage(seal_picture, seal_path);
				BufferedImage bufferImg =null;  
				 ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();  
				 bufferImg = ImageIO.read(new File(seal_path));
				 ImageIO.write(bufferImg,"png",byteArrayOut);
				 HSSFPatriarch patriarch = sheet.createDrawingPatriarch();   
			     HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,512,255,(short) img_3_col,img_3_row,(short)(img_3_col+2),img_3_row+2);
			     patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));   

			}
		}
		
	}

	/**
	 * excel填充成分、材质的卷
	 * 
	 * @param model
	 * @param qcmZbsObject
	 * @param sameRow
	 * @param picTitle 
	 * @param sheet
	 * @param seq 
	 * @throws Exception
	 */
	private void addQltyAndChem(QcmZbsTmpl model, QcmZbsObject qcmZbsObject, String sameRow, List<QcmZbsObjectQlty> picTitle, HSSFSheet sheet,Integer me_row_str,Integer ql_row_str, int seq) throws Exception {
	//物料开始行
	int meter_star=me_row_str;
	
	Integer cic_row_val = model.getCic_row_val()==null?null:StringFormat.getNumber(model.getCic_row_val());
	Integer cic_col_val=model.getCic_col_val()==null?null:StringFormat.getNumber(model.getCic_col_val());
	Integer pic_row_val = model.getPic_row_val()==null?null:StringFormat.getNumber(model.getPic_row_val());
	Integer pic_col_val = model.getPic_row_val()==null?null:StringFormat.getNumber(model.getPic_col_val());
	int max_col=0;	
	if ("1".equals(sameRow)) {
			if (pic_col_val==null ) {
				max_col = sheet.getRow(cic_row_val).getPhysicalNumberOfCells() - 1;
			}else{
				max_col = pic_col_val;
			}
			
	} else {
			max_col = sheet.getRow(cic_row_val).getPhysicalNumberOfCells() - 1;
	}
		
	
	//根据配置信息填充成分物料信息
	TreeMap<String, Object> tree=qcmZbsObject.getHeat_info();
	// 获取物料配置项
	List<QcmZbsTmplItem> chemConfigure = this.getMapper(QcmZbsTmplItemMapper.class).findWithCondition(null,
					"TMPL_NO='" + model.getTmpl_no() + "' and DIV_TYPE='1'", "ITEM_STR_ROW,to_number(ITEM_STR_COL)", 0, 0);
	
	//填充序号meter_star
	HSSFRow row2 = sheet.getRow(meter_star);
	HSSFCell cel2 = row2.getCell(0);
	cel2.setCellValue(seq);
	//填充成分材质信息
	List<QcmZbsSampleNo> smples=qcmZbsObject.getSmples();
	List<QcmZbsChemObject> chemList=qcmZbsObject.getChemList();
	if ("1".equals(sameRow)) {
		
	}else{
		for (QcmZbsChemObject c : chemList) {
			cic_col_val = StringFormat.getNumber(model.getCic_col_val());
			//用于判断成分的长度正好是
			int length = 1;
			// 填充成分值
			for (String ch : c.getChemValue()) {

				row2 = sheet.getRow(me_row_str);
			    cel2 = row2.getCell(cic_col_val);
				if (cic_col_val >= max_col) {
					if (c.getChemValue().length == length) {

					} else {
						me_row_str++;
					}
					cic_col_val = StringFormat.getNumber(model.getCic_col_val());
					cel2.setCellValue(ch);
					length++;
				} else {
					cel2.setCellValue(ch);
					cic_col_val++;
					length++;
				}

			}

			// 行加一
			me_row_str++;

			// 填充试验区分
			HSSFRow row = sheet.getRow(meter_star);
			HSSFCell cel = row.getCell(StringFormat.getNumber(model.getCic_col_val()) - 1);
			cel.setCellValue(c.getAssay_type());
			if (me_row_str - 1 > meter_star) {
				sheet.addMergedRegion(new CellRangeAddress(meter_star, me_row_str - 1,
						StringFormat.getNumber(model.getCic_col_val()) - 1,
						StringFormat.getNumber(model.getCic_col_val()) - 1));

			}
		}
		int coil_col=1;	
		if (chemConfigure!=null && chemConfigure.size()>0) {
			Set<String> se = tree.keySet();
			for (int i = 0; i < chemConfigure.size(); i++) {

				for (String key : se) {
					if (chemConfigure.get(i).getItem_code().equals(key.toUpperCase())) {
						Object value = tree.get(key);
						if ("material_no".equals(key) || "batch_no".equals(key)) {
							if ("1".equals(qcmZbsObject.getIs_batch())) {
								 value = tree.get("batch_no");	
							}else {
								value = tree.get("material_no");
							}
							
						}else {
						     value = tree.get(key);
							
							
						}
						// 获取单元格
						if (chemConfigure.get(i).getItem_str_col_e()!=null) {
							HSSFRow row1 = sheet.getRow(meter_star);
							HSSFCell cell = row1
									.getCell(StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()));
							cell.setCellValue((String) value);
							
							
							//合并行列
							if (i != chemConfigure.size()-1) {

								if ((StringFormat.getNumber(chemConfigure.get(i + 1).getItem_str_col_e())
										- 1) > StringFormat.getNumber((chemConfigure.get(i).getItem_str_col_e()))) {

									if (me_row_str - 1 > meter_star) {
										System.out.println(StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()));
										System.out.println((StringFormat.getNumber(chemConfigure.get(i + 1).getItem_str_col_e())
												- 1));
										sheet.addMergedRegion(new CellRangeAddress(meter_star, me_row_str - 1,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												(StringFormat.getNumber(chemConfigure.get(i + 1).getItem_str_col_e())
														- 1)));

									} else {
										sheet.addMergedRegion(new CellRangeAddress(meter_star, meter_star,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												(StringFormat.getNumber(chemConfigure.get(i + 1).getItem_str_col_e())
														- 1)));

									}

								} else {

									if (me_row_str - 1 > meter_star) {
										sheet.addMergedRegion(new CellRangeAddress(meter_star, me_row_str - 1,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e())));

									}

								}

							
								
							}else{

								if ((StringFormat.getNumber(model.getCic_col_val()) - 1) >= StringFormat
										.getNumber((chemConfigure.get(i).getItem_str_col_e()))) {

									if (me_row_str - 1 > meter_star) {
										sheet.addMergedRegion(new CellRangeAddress(meter_star, me_row_str - 1,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												(StringFormat.getNumber(model.getCic_col_val()) - 2)));

									} else {
										if ((StringFormat.getNumber(model.getCic_col_val()) - 2)>StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e())) {
											sheet.addMergedRegion(new CellRangeAddress(meter_star, meter_star,
													StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
													(StringFormat.getNumber(model.getCic_col_val()) - 2)));

										}
										
									}

								} else {
									if (me_row_str - 1 > meter_star) {
										sheet.addMergedRegion(new CellRangeAddress(meter_star, me_row_str - 1,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e())));

									}

								}

							}
						}
						
					}
			
				
				}
			}
		}
		
		
		
		//合并成分配置
		//序号
		if (me_row_str - 1 > meter_star) {
			sheet.addMergedRegion(new CellRangeAddress(meter_star, me_row_str - 1,0,0));
		}
		
		//填充材质
		//最后填充的总行数，用于合并
		int count_row=0;
		//进入材质卷填充时的行
		int qlty_into_row=ql_row_str;
		//填充材质卷号或者轧批号
		HSSFRow row = sheet.getRow(qlty_into_row);
		HSSFCell cel = row.getCell(coil_col);
		//拿到轧批配置中的卷号
		if ("1".equals(qcmZbsObject.getIs_batch())) {
			cel.setCellValue(qcmZbsObject.getBatch_no());
		}else {
			cel.setCellValue(qcmZbsObject.getMaterial_no());
		}
		cel = row.getCell(0);
		cel.setCellValue(seq);
		
		if (smples!=null && smples.size()>0) {
			for (QcmZbsSampleNo sample : smples) {
				//当前试样号向下占有的行数最大数
				int sample_max_row=0;
				//当前试样号的开始行
				int sample_str_row=ql_row_str;
				row = sheet.getRow(ql_row_str);
				cel = row.getCell(0);
				
				//填充卷号
				
				//填写试样区分
				
				//试样号向下的大项
				List<QcmZbsBigItem> bigItems=sample.getBigItems();
				//拿到材质模板对象
				//遍历材质模板的检验大项的结合
				for (QcmZbsObjectQlty title : picTitle) {
					//当前检验大项的开始行，
					//进入方向温度、尺寸集合时的位置行和列
					int big_row_str=sample_str_row;
					int big_col_str=pic_col_val;
					
					List<QcmZbsDataPic> titleSmallItem=title.getSmallItem();
					for (int i = 0; i < bigItems.size(); i++) {
						int dir_row=big_row_str;
						int dir_col=big_col_str;
						//结束
						if (bigItems.get(i).getPhy_code_l().equals(title.getPhy_code_l())) {
							TreeMap<String, List<QcmZbsDataPic>> dirQltyItems=bigItems.get(i).getDirQltyItems();
							Set<String> set=dirQltyItems.keySet();
							
							if (set.size()>sample_max_row) {
								sample_max_row=set.size();
							}
							
							for (String key : set) {
								//获取方向温度尺寸 这里也有个null
								String[] dir=key.split("_");
								row = sheet.getRow(dir_row);
								cel = row.getCell(pic_col_val);
								List<QcmZbsDataPic> pics=dirQltyItems.get(key);
								for (QcmZbsDataPic small : titleSmallItem) {
									cel = row.getCell(pic_col_val);
									if ("I01".equals(small.getPhy_code_s())) {
										if ("null".equals(dir[0])) {
											cel.setCellValue("");
										}else{
											cel.setCellValue(dir[0]);
										}
										pic_col_val++;
										continue;
									}
									if ("I02".equals(small.getPhy_code_s())) {
										if ("null".equals(dir[1])) {
											cel.setCellValue("");
										}else{
											cel.setCellValue(dir[1]);
										}
										pic_col_val++;
										continue;
									}
									if ("I03".equals(small.getPhy_code_s())) {
										if ("null".equals(dir[2])) {
											if (cel != null) {
												cel.setCellValue("");
											}
										}else{
											if (cel != null) {
												cel.setCellValue(dir[2]);
											}
										}
										pic_col_val++;
										continue;
									} //这里
									String[] values=small.getResults();
									for (int j = 0; j < pics.size(); j++) {
										if (small.getPhy_code_s().equals(pics.get(j).getPhy_code_s())) {
											
											if (values.length==1) {
												if (cel != null) {
													cel.setCellValue(pics.get(j).getVal1());
												}
													
											}else{
												for (String value : values) {
													if ("1".equals(value)) {
														if (cel != null) {
															cel.setCellValue(pics.get(j).getVal1());
														}
														
													}else if ("2".equals(value)) {
														cel = row.getCell(++pic_col_val);
														if (cel != null) {
															cel.setCellValue(pics.get(j).getVal2());
														}
														
													}else if ("3".equals(value)) {
														cel = row.getCell(++pic_col_val);
														if (cel != null) {
															cel.setCellValue(pics.get(j).getVal3());
														}
													}else if ("AVE".equals(value)) {
														cel = row.getCell(++pic_col_val);
														if (cel != null) {
															cel.setCellValue(pics.get(j).getAvg_val());
														}
													}
												}
											}
											pic_col_val++;
											break;
										}
										
										if (j == pics.size()-1) {
											for (String value : values) {
												if ("1".equals(value)) {
													if (cel != null) {
														cel.setCellValue("");
													}
													
													
												}else if ("2".equals(value)) {
													cel = row.getCell(++pic_col_val);
													if (cel != null) {
														cel.setCellValue("");
													}
													
												}else if ("3".equals(value)) {
													cel = row.getCell(++pic_col_val);
													if (cel != null) {
														cel.setCellValue("");
													}
													
												}else if ("AVE".equals(value)) {
													cel = row.getCell(++pic_col_val);
													if (cel != null) {
														cel.setCellValue("");
													}
												}
											}
											pic_col_val++;
										}
										
										
									}
									
								}
								
								
								
								dir_row++;
								pic_col_val = dir_col;
								
							}
							
							break;	
						}
						// 这里为2时有问题：
						if (i == bigItems.size()-1) {
							for (QcmZbsDataPic small : titleSmallItem) {
								String[] values=small.getResults();
								for (String value : values) {
									if ("1".equals(value)) {
										cel.setCellValue("");	
									}else if ("2".equals(value)) {
										cel = row.getCell(++pic_col_val);
										if (cel != null) {
											cel.setCellValue("");
										}	
									}else if ("3".equals(value)) {
										cel = row.getCell(++pic_col_val);
										if (cel != null) {
											cel.setCellValue("");
										}
									}else if ("AVE".equals(value)) {
										cel = row.getCell(++pic_col_val);
										if (cel != null) {
											cel.setCellValue("");
										}
									}
								}
								pic_col_val++;
							}
							}
						
						
						}
					pic_col_val+=titleSmallItem.size();
					}
				
				pic_col_val = model.getPic_row_val()==null?null:StringFormat.getNumber(model.getPic_col_val());
				count_row+=sample_max_row;
				ql_row_str+=sample_max_row;
				
				//填写试样类型
				row = sheet.getRow(sample_str_row);
				//cel = row.getCell(2);
				cel = row.getCell(2);
				cel.setCellValue(sample.getSmple_name());
				if (ql_row_str-1>sample_str_row) {
					sheet.addMergedRegion(new CellRangeAddress(sample_str_row, ql_row_str-1,
							2,
							2));
				}/*else{
					sheet.addMergedRegion(new CellRangeAddress(sample_str_row, sample_str_row,
							coil_col+2,
							coil_col+3));
				}*/
				
					
				
			}
			
		}
		
		
		//合并卷号
		if (qlty_into_row+count_row-1>qlty_into_row) {
			sheet.addMergedRegion(new CellRangeAddress(qlty_into_row, qlty_into_row+count_row-1,
					coil_col,
					coil_col));
			
		}/*else{
			sheet.addMergedRegion(new CellRangeAddress(qlty_into_row, qlty_into_row,
					coil_col,
					coil_col+1));
		}*/
		//合并序号
		if (qlty_into_row+count_row-1>qlty_into_row) {
			sheet.addMergedRegion(new CellRangeAddress(qlty_into_row, qlty_into_row+count_row-1,
					0,
					0));
		}
			
			
		}
		
		
		
		
	}
	
	


	/**
	 * 添加材质表头信息
	 * 
	 * @param model
	 * @param picTitle
	 * @param sameRow
	 * @param sheet
	 * @throws Exception
	 */
	private void addQltyTitle(QcmZbsTmpl model, List<QcmZbsObjectQlty> picTitle, String sameRow, HSSFSheet sheet)
			throws Exception {
		Integer pic_row_item_l = model.getPic_row_item_l() == null ? null
				: StringFormat.getNumber(model.getPic_row_item_l());
		Integer pic_col_item_l = model.getPic_row_val() == null ? null
				: StringFormat.getNumber(model.getPic_col_item_l());
		Integer pic_row_item_s = model.getPic_row_item_l() == null ? null
				: StringFormat.getNumber(model.getPic_row_item_s());
		Integer pic_col_item_s = model.getPic_row_val() == null ? null
				: StringFormat.getNumber(model.getPic_col_item_s());
		Integer pic_row_item_u = model.getPic_row_item_l() == null ? null
				: StringFormat.getNumber(model.getPic_row_item_u());
		Integer getPic_col_item_u = model.getPic_row_val() == null ? null
				: StringFormat.getNumber(model.getPic_col_item_u());

		if (pic_row_item_l == null) {
			return;
		}
		if (pic_col_item_l == null) {
			return;
		}
		if (pic_row_item_s == null) {
			return;
		}
		if (pic_col_item_s == null) {
			return;
		}
		if (picTitle == null || picTitle.size() < 1) {
			return;
		}
		// 获取语种代码
		String lng_code = model.getLng_code();
		// 根据语言代码查询语言
		List<QcmZbsLanguage> laugnagesBig = this.getMapper(QcmZbsLanguageMapper.class).findWithCondition(null,
				"LNG_CODE='" + lng_code + "' and TYPE_CODE='41150203'", null, 0, 0);
		List<QcmZbsLanguage> laugnagesItem = this.getMapper(QcmZbsLanguageMapper.class).findWithCondition(null,
				"LNG_CODE='" + lng_code + "' and TYPE_CODE='41150204'", null, 0, 0);

		int max_col = sheet.getRow(pic_row_item_l).getPhysicalNumberOfCells() - 1;

		for (QcmZbsObjectQlty title : picTitle) {
			// 大项开始列
			int big_str_col = pic_col_item_s;
			HSSFRow row = sheet.getRow(pic_row_item_l);
			HSSFCell cel = row.getCell(big_str_col);
			cel.setCellValue(title.getPhy_name_l());
			// 遍历大项配置
			for (QcmZbsLanguage big : laugnagesBig) {
				if (title.getPhy_code_l().equals(big.getItem_code())) {
					if (big.getChn_abbr() != null && !"".equals(big.getChn_abbr())) {
						cel.setCellValue(big.getChn_abbr());
					}
					row = sheet.getRow(pic_row_item_l + 1);
					cel = row.getCell(big_str_col);
					if (big.getEng_abbr() != null && !"".equals(big.getEng_abbr())) {
						cel.setCellValue(big.getEng_abbr());
					} else {
						cel.setCellValue(big.getVal());
					}
				}
			}
			// 拿到大项向下的项目
			List<QcmZbsDataPic> smallItems = title.getSmallItem();
			for (QcmZbsDataPic small : smallItems) {
				int itemStr = pic_col_item_s;
				row = sheet.getRow(pic_row_item_s);
				cel = row.getCell(pic_col_item_s);
				cel.setCellValue(small.getPhy_name_s());

				for (QcmZbsLanguage sm : laugnagesItem) {
					if (small.getPhy_code_s().equals(sm.getItem_code())) {
						if (sm.getChn_abbr() != null && !"".equals(sm.getChn_abbr())) {
							cel.setCellValue(sm.getChn_abbr());
						}
						row = sheet.getRow(pic_row_item_s + 1);
						cel = row.getCell(pic_col_item_s);
						if (sm.getEng_abbr() != null && !"".equals(sm.getEng_abbr())) {
							cel.setCellValue(sm.getEng_abbr());
						} else {
							cel.setCellValue(sm.getVal());
						}
						row = sheet.getRow(pic_row_item_s + 2);
						cel = row.getCell(pic_col_item_s);
						cel.setCellValue(sm.getItem_type());
					}

				}

				// 填写值单位
				if (pic_row_item_u != null) {
					row = sheet.getRow(pic_row_item_u);
					cel = row.getCell(itemStr);
					cel.setCellValue(small.getPhy_unit());
				}

				// 判断有几个值
				String[] values = small.getResults();
				if (values.length == 1) {
					pic_col_item_s++;
				} else {
					for (String value : values) {
						row = sheet.getRow(pic_row_item_s + 2);
						cel = row.getCell(pic_col_item_s++);
						cel.setCellValue(value);
					}
				}

				// 合并项目单元格
				if (pic_col_item_s - 1 > itemStr) {
					sheet.addMergedRegion(
							new CellRangeAddress(pic_row_item_s, pic_row_item_s, itemStr, pic_col_item_s - 1));

				}
				if (pic_col_item_s - 1 > itemStr) {
					sheet.addMergedRegion(
							new CellRangeAddress(pic_row_item_s + 1, pic_row_item_s + 1, itemStr, pic_col_item_s - 1));

				}

				// 合并值单位
				if (pic_col_item_s - 1 > itemStr) {
					sheet.addMergedRegion(
							new CellRangeAddress(pic_row_item_u, pic_row_item_u, itemStr, pic_col_item_s - 1));

				}

			}
			// 合并大项单元格
			if (pic_col_item_s - 1 > big_str_col) {
				sheet.addMergedRegion(
						new CellRangeAddress(pic_row_item_l, pic_row_item_l, big_str_col, pic_col_item_s - 1));

			}

			// 合并大项英文名
			if (pic_col_item_s - 1 > big_str_col) {
				sheet.addMergedRegion(
						new CellRangeAddress(pic_row_item_l + 1, pic_row_item_l + 1, big_str_col, pic_col_item_s - 1));

			}

		}

	}

	/**
	 * 
	 * @param cic_col_title
	 * @param cic_row_title
	 * @param cic_col_val
	 * @param cicTitle
	 * @param sameRow
	 * @param sheet
	 */
	private void addChemTitle(Integer cic_col_title, Integer cic_row_title, Integer pic_col_item_l,
			List<QcmZbsDataCic> cicTitle, String sameRow, HSSFSheet sheet) {
		// 获取成分可填充的最大列数
		int max_col = 0;
		int ran_ql_col_title = cic_col_title;
		// 判断配置行列是否为空
		if (cic_col_title == null) {
			return;
		}
		if (cic_row_title == null) {
			return;
		}

		if (cicTitle == null || cicTitle.size() < 1) {
			return;
		}

		if ("1".equals(sameRow)) {
			if (pic_col_item_l == null) {
				return;
			}
			max_col = pic_col_item_l;
		} else {
			max_col = sheet.getRow(cic_row_title).getPhysicalNumberOfCells() - 1;
		}

		for (QcmZbsDataCic c : cicTitle) {
			HSSFRow row = sheet.getRow(cic_row_title);
			HSSFCell cel = row.getCell(ran_ql_col_title);
			if (ran_ql_col_title >= max_col) {
				cic_row_title++;
				cel = row.getCell(ran_ql_col_title);
				ran_ql_col_title = cic_col_title;
				cel.setCellValue(c.getChem_name());
			} else {
				if (c.getChem_name() != null && !"".equals(c.getChem_name())) {
					cel.setCellValue(c.getChem_name());
					ran_ql_col_title++;
				} else {
					cel.setCellValue("");
					ran_ql_col_title++;
				}

			}
		}

	}

	/**
	 * 质保书封装对象
	 * 
	 * @param ord
	 * @param targetBook
	 * @param cicTitle
	 * @return
	 */
	private void createZbsObject(QcmZbsDataOrd ord, List<QcmZbsObject> targetBook, List<QcmZbsDataCic> cicTitle) {
		// 拿到当前质保书向下所有的卷
		List<QcmZbsDataMat> msts = this.getMapper(QcmZbsDataMatMapper.class).findWithCondition(null,
				"CRET_NO='" + ord.getCret_no() + "'", "MATERIAL_NO", 0, 0);

		for (QcmZbsDataMat mat : msts) {
			QcmZbsObject object = new QcmZbsObject();
			object.setBatch_no(mat.getBatch_no());
			object.setMaterial_no(mat.getMaterial_no());
			object.setIs_batch(ord.getIs_batch());
			int chem_size = 0;

			List<QcmZbsChemObject> tarChem = new ArrayList<QcmZbsChemObject>();

			// 获取mat对象的所有属性名、以及值
			Field[] field = mat.getClass().getDeclaredFields();
			TreeMap<String, Object> heat_info = new TreeMap<String, Object>();
			for (Field f : field) {
				heat_info.put(f.getName(), getFieldValueByName(f.getName(), mat));
			}

			// 获取成品成分信息
			List<QcmZbsDataCic> chemP = this.getMapper(QcmZbsDataCicMapper.class).findWithCondition(null, "CRET_NO='"
					+ ord.getCret_no() + "' and MATERIAL_NO='" + mat.getMaterial_no() + "' and ASSAY_TYPE='P'",
					"MATERIAL_NO,ASSAY_TYPE,CHEM_CODE", 0, 0);

			List<QcmZbsDataCic> chemL = this.getMapper(QcmZbsDataCicMapper.class).findWithCondition(null, "CRET_NO='"
					+ ord.getCret_no() + "' and MATERIAL_NO='" + mat.getMaterial_no() + "' and ASSAY_TYPE='L'",
					"MATERIAL_NO,ASSAY_TYPE,CHEM_CODE", 0, 0);

			QcmZbsChemObject ch = null;
			// 成品成分
			if (chemP != null && chemP.size() > 0) {
				ch = new QcmZbsChemObject();
				
				int length = chemP.size() >= cicTitle.size() ? chemP.size() : cicTitle.size();
				
				String[] chemValue = new String[length];
				int a = 0;
				for (QcmZbsDataCic ti : cicTitle) {
					for (int i = 0; i < chemP.size(); i++) {

						if (ti.getChem_code().equals(chemP.get(i).getChem_code())) {
							chemValue[a++] = chemP.get(i).getChem_value();
							break;
						}

						if (i == chemP.size() - 1) {
							chemValue[a++] = null;
						}
					}

				}

				ch.setChemValue(chemValue);
				ch.setHeat_info(heat_info);
				ch.setAssay_type("P");
				tarChem.add(ch);
				chem_size++;
			}
			// 熔炼成分：原因a自增超过限度，干脆谁长就取水的size() 2030508刘侣
			if (chemL != null && chemL.size() > 0) {
				ch = new QcmZbsChemObject();
				
				int length = chemL.size() >= cicTitle.size() ? chemL.size() : cicTitle.size();
				
				String[] chemValue = new String[length];
				int a = 0;
				for (QcmZbsDataCic ti : cicTitle) {

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

						if (ti.getChem_code().equals(chemL.get(i).getChem_code())) {
							chemValue[a++] = chemL.get(i).getChem_value();
							break;
						}

						if (i == chemL.size() - 1) {
							chemValue[a++] = null;
						}
					}

				}
				// end

				ch.setChemValue(chemValue);
				ch.setHeat_info(heat_info);
				ch.setAssay_type("L");
				tarChem.add(ch);
				chem_size++;
			}
			// 成分项目个数
			object.setChem_size(chem_size);
			// 把成分添加到
			object.setChemList(tarChem);
			// 封装材质项目值
			List<QcmZbsDataPic> pics = this.getMapper(QcmZbsDataPicMapper.class).findWithCondition(null,
					"CRET_NO='" + mat.getCret_no() + "' and MATERIAL_NO='" + mat.getMaterial_no() + "'", "SMP_NO,SEQ",
					0, 0);

			// 拿到所有的试样组
			TreeSet<String> sample_nos = new TreeSet<String>();
			for (QcmZbsDataPic pic : pics) {
				sample_nos.add(pic.getSmp_no());
			}
			// 遍历试样组，查询试样组向下所有的项目
			// 材质所占有的行数
			int sum_size = 0;

			// 保存目标的集合
			List<QcmZbsSampleNo> smple_nos = new ArrayList<QcmZbsSampleNo>();
			for (String sample_no : sample_nos) {
				QcmZbsSampleNo obj_sam = new QcmZbsSampleNo();
				obj_sam.setSmple_no(sample_no);
				// 查询试样类型
				QcmJhySampleConsignM qm = this.getMapper(QcmJhySampleConsignMMapper.class).findById(sample_no);
				if (qm!=null && qm.getSmp_type_name()!=null) {
					obj_sam.setSmple_name(qm.getSmp_type_name());
				}else{
					obj_sam.setSmple_name("常规");
				}
				

				// 拿到该试样号向下的检验项目
				List<QcmZbsDataPic> samPics = new ArrayList<QcmZbsDataPic>();
				for (QcmZbsDataPic pic : pics) {
					if (sample_no.equals(pic.getSmp_no())) {
						samPics.add(pic);
					}
				}
				// 当前试样号所占行数
				int size = 1;
				// 默认为一个方向
				if (samPics != null && samPics.size() > 0) {
					// 拿到试样号向下的检验大项的集合
					TreeSet<String> onlyItem = new TreeSet<String>();
					for (QcmZbsDataPic sam : samPics) {
						onlyItem.add(sam.getPhy_code_l());
					}
					// 遍历项目，查询该大项下是否具有多个方向
					List<QcmZbsBigItem> bigItems = new ArrayList<QcmZbsBigItem>();
					for (String item : onlyItem) {
						// 大项对象
						QcmZbsBigItem big = new QcmZbsBigItem();
						// 方向、温度、尺寸对应的项目
						TreeSet<String> num = new TreeSet<String>();
						// 试样号向下的检验大项向下的项目集合
						List<QcmZbsDataPic> bigItem = new ArrayList<QcmZbsDataPic>();
						for (QcmZbsDataPic pic : samPics) {
							if (item.equals(pic.getPhy_code_l())) {
								bigItem.add(pic);
							}
						}
						// 设定大项名称代码
						big.setPhy_code_l(item);
						if (bigItem != null && bigItem.size() > 0) {
							big.setPhy_name_l(bigItem.get(0).getPhy_name_l());
						}
						// 获取方向、温度、尺寸的并级
						for (QcmZbsDataPic i : bigItem) {
							String bing_ji = DataHelper.getDirValue(i.getItem_name_d(), i.getItem_name_s(),
									i.getItem_name_t());
							num.add(bing_ji);
						}
						TreeMap<String, List<QcmZbsDataPic>> dirQltyItems = new TreeMap<String, List<QcmZbsDataPic>>();
						for (String ds : num) {
							List<QcmZbsDataPic> p = new ArrayList<QcmZbsDataPic>();
							for (QcmZbsDataPic b : bigItem) {
								String bing_ji = DataHelper.getDirValue(b.getItem_name_d(), b.getItem_name_s(),
										b.getItem_name_t());
								if (ds.equals(bing_ji)) {
									p.add(b);
								}
							}
							dirQltyItems.put(ds, p);
						}
						big.setDirQltyItems(dirQltyItems);

						// 判断大项向下是否存在多个方向
						if (num != null && num.size() > 0) {
							if (num.size() > size) {
								size = num.size();
							}
						}

						bigItems.add(big);

					}
					sum_size += size;
					obj_sam.setBigItems(bigItems);
					smple_nos.add(obj_sam);
				}

				object.setQlty_size(sum_size);
				object.setSmples(smple_nos);

			}
			object.setHeat_info(heat_info);
			targetBook.add(object);

		}

	}

	/**
	 * 材质项目封装对象
	 * 
	 * @param ord
	 * @param cicTitle
	 * @throws Exception
	 */
	private void createQltyTitle(QcmZbsDataOrd ord, List<QcmZbsObjectQlty> picTitle) throws Exception {
		// 获取检验大项代码集合
		TreeSet<String> bigItem = new TreeSet<>();
		// 查询当前质保书下所有的材质项目
		List<QcmZbsDataPic> picR = this.getMapper(QcmZbsDataPicMapper.class).findWithCondition(null,
				"CRET_NO='" + ord.getCret_no() + "'", "SMP_NO,SEQ", 0, 0);

		for (QcmZbsDataPic p : picR) {
			bigItem.add(p.getPhy_code_l());
		}

		// 目标材质表头项目集合
		List<QcmZbsObjectQlty> tarItem = new ArrayList<QcmZbsObjectQlty>();
		QcmZbsDataPic dirItem = null;
		QcmZbsDataPic temItem = null;
		QcmZbsDataPic sizeItem = null;
		for (String item : bigItem) {
			// 承装当前检验大项及向下项目的对象
			QcmZbsObjectQlty tarObject = new QcmZbsObjectQlty();
			// 承装当前检验大项向下为一个材质检验项目
			List<QcmZbsDataPic> tarOnlyItem = new ArrayList<QcmZbsDataPic>();

			// 获取当前检验大项向下所有的检验项目
			List<QcmZbsDataPic> bigAllItems = new ArrayList<>();
			for (QcmZbsDataPic qc : picR) {
				if (item.equals(qc.getPhy_code_l())) {
					bigAllItems.add(qc);
				}
			}
			// 去重获取唯一项目
			TreeSet<String> onlySmallItem = new TreeSet<>();
			for (QcmZbsDataPic qc : bigAllItems) {
				onlySmallItem.add(qc.getPhy_code_s());
			}

			for (String s : onlySmallItem) {

				List<QcmZbsDataPic> smallAllItems = new ArrayList<>();
				for (QcmZbsDataPic qc : bigAllItems) {
					if (s.equals(qc.getPhy_code_s())) {
						smallAllItems.add(qc);
					}
				}
				// 方向、温度、尺寸等项目
				for (QcmZbsDataPic sm : smallAllItems) {
					// 方向
					if (sm.getItem_code_d() != null && !"".equals(sm.getItem_code_d())) {
						if (dirItem == null) {
							dirItem = new QcmZbsDataPic();
							DataHelper.CopyEntity(sm, dirItem);
							dirItem.setPhy_code_s("I01");
							dirItem.setPhy_name_s("方向");
							dirItem.setItem_code_d(null);
							dirItem.setItem_code_s(null);
							dirItem.setItem_code_t(null);
							dirItem.setItem_name_d(null);
							dirItem.setItem_name_s(null);
							dirItem.setItem_name_t(null);
							dirItem.setPhy_unit(null);
							String[] result = new String[1];
							result[0] = sm.getItem_name_d();
							dirItem.setResults(result);
							tarOnlyItem.add(dirItem);

						}
					}

					// 尺寸
					if (sm.getItem_code_s() != null && !"".equals(sm.getItem_code_s())) {
						if (sizeItem == null) {
							sizeItem = new QcmZbsDataPic();
							DataHelper.CopyEntity(sm, sizeItem);
							sizeItem.setPhy_code_s("I02");
							sizeItem.setPhy_name_s("尺寸");
							sizeItem.setItem_code_d(null);
							sizeItem.setItem_code_s(null);
							sizeItem.setItem_code_t(null);
							sizeItem.setItem_name_d(null);
							sizeItem.setItem_name_s(null);
							sizeItem.setItem_name_t(null);
							sizeItem.setPhy_unit(null);
							String[] result = new String[1];
							result[0] = sm.getItem_name_s();
							sizeItem.setResults(result);
							tarOnlyItem.add(sizeItem);
						}
					}
					// 温度
					if (sm.getItem_code_t() != null && !"".equals(sm.getItem_code_t())) {
						if (temItem == null) {
							temItem = new QcmZbsDataPic();
							DataHelper.CopyEntity(sm, temItem);
							temItem.setPhy_code_s("I03");
							temItem.setPhy_name_s("温度");
							temItem.setItem_code_d(null);
							temItem.setItem_code_s(null);
							temItem.setItem_code_t(null);
							temItem.setItem_name_d(null);
							temItem.setItem_name_s(null);
							temItem.setItem_name_t(null);
							temItem.setPhy_unit("℃");
							String[] result = new String[1];
							result[0] = sm.getItem_name_t();
							temItem.setResults(result);
							tarOnlyItem.add(temItem);
						}
					}

				}
				// 判断当前项目值的个数
				String[] values = null;
				TreeSet<String> val = new TreeSet<String>();
				for (QcmZbsDataPic sm : smallAllItems) {
					if (sm.getVal1() != null && !"".equals(sm.getVal1())) {
						val.add("1");
					}
					if (sm.getVal2() != null && !"".equals(sm.getVal2())) {
						val.add("2");
					}
					if (sm.getVal3() != null && !"".equals(sm.getVal3())) {
						val.add("3");
					}
					if (sm.getAvg_val() != null && !"".equals(sm.getAvg_val())) {
						val.add("4");
					}
				}
				// 判断值的个数
				if (val != null && val.size() > 0) {
					values = new String[val.size()];
					int i = 0;
					if (val.size() == 1) {
						values[i] = null;
					} else {
						for (String v : val) {
							if ("1".equals(v)) {
								values[i] = "1";
							} else if ("2".equals(v)) {
								values[i] = "2";
							} else if ("3".equals(v)) {
								values[i] = "3";
							} else if ("4".equals(v)) {
								values[i] = "AVE";
							}
							i++;

						}

					}

				}
				// 拿出一个小项目
				QcmZbsDataPic onlyItem = new QcmZbsDataPic();
				if (smallAllItems != null && smallAllItems.size() > 0) {
					onlyItem = smallAllItems.get(0);
					onlyItem.setResults(values);
					tarOnlyItem.add(onlyItem);
				}

			}
			tarObject.setPhy_code_l(item);
			tarObject.setPhy_name_l(tarOnlyItem.get(0).getPhy_name_l());
			tarObject.setSmallItem(tarOnlyItem);
			// 方向、温度、尺寸置空
			dirItem = null;
			temItem = null;
			sizeItem = null;
			tarItem.add(tarObject);

		}

		picTitle.addAll(tarItem);
	}

	/**
	 * 成分元素封装对象
	 * 
	 * @param ord
	 * @param cicTitle
	 */
	private void createChemTitle(QcmZbsDataOrd ord, List<QcmZbsDataCic> cicTitle) {
		// 获取成分信息
		TreeMap<String, String> allChem = new TreeMap<String, String>();
		List<QcmZbsDataCic> cics = this.getMapper(QcmZbsDataCicMapper.class).findWithCondition(null,
				"CRET_NO='" + ord.getCret_no() + "'", "MATERIAL_NO,ASSAY_TYPE,CHEM_CODE", 0, 0);

		for (QcmZbsDataCic qcmZbsDataCic : cics) {
			allChem.put(qcmZbsDataCic.getChem_code(), qcmZbsDataCic.getChem_name());
		}

		Set<String> set = allChem.keySet();

		for (String c : set) {
			QcmZbsDataCic cic = new QcmZbsDataCic();
			cic.setChem_code(c);
			cic.setChem_name(allChem.get(c));
			cicTitle.add(cic);
		}

	}

	private boolean dataSure(String[] chem_name, List<QcmZbsObject> targetBook,
			TreeMap<String, List<QcmZbsDataPic>> qltyItems, ResultModel rm) {

		if (chem_name == null || chem_name.length < 1) {
			rm.setMsgInfo("成分元素实绩不能为空！，请确认");
			rm.setState("500");
			return false;
		}
		if (targetBook == null || targetBook.size() < 1) {
			rm.setMsgInfo("未查询质保书对应的成分、材质实绩信息，请确认！");
			rm.setState("500");
			return false;
		}

		if (qltyItems == null || qltyItems.size() < 1) {
			rm.setMsgInfo("未查询质保书对应的材质项目信息，请确认！");
			rm.setState("500");
			return false;
		}

		int chem_size = targetBook.get(0).getChem_size();
		int qlty_size = targetBook.get(0).getQlty_size();
		if (chem_size == 0) {
			rm.setMsgInfo("物料[" + targetBook.get(0).getMaterial_no() + "]成分实绩不存在，请确认！");
			rm.setState("500");
			return false;
		}
		if (qlty_size == 0) {
			rm.setMsgInfo("物料[" + targetBook.get(0).getMaterial_no() + "]材质实绩不存在，请确认！");
			rm.setState("500");
			return false;
		}

		for (int i = 0; i < targetBook.size(); i++) {
			if (targetBook.get(i).getChem_size() == 0) {
				rm.setMsgInfo("物料[" + targetBook.get(i).getMaterial_no() + "]成分实绩不存在，请确认！");
				rm.setState("500");
				return false;
			}

			if (targetBook.get(i).getChem_size() != chem_size) {
				rm.setMsgInfo("物料[" + targetBook.get(i).getMaterial_no() + "]成分实绩不一致，请确认！");
				rm.setState("500");
				return false;
			}

			if (targetBook.get(i).getQlty_size() == 0) {
				rm.setMsgInfo("物料[" + targetBook.get(i).getMaterial_no() + "]材质实绩不存在，请确认！");
				rm.setState("500");
				return false;
			}

			if (targetBook.get(i).getQlty_size() != qlty_size) {
				rm.setMsgInfo("物料[" + targetBook.get(i).getMaterial_no() + "]材质实绩不一致，请确认！");
				rm.setState("500");
				return false;
			}

		}

		return true;
	}

	
	/**
	 * 填充注释
	 * 
	 * @param notes
	 *            注释
	 * @param sheet
	 * @param model
	 * @throws Exception
	 */
	private void addNotes(String notes, HSSFSheet sheet, QcmZbsTmpl model) throws Exception {
		// 注释填充行
		Integer memo_row = StringFormat.getNumber(model.getMemo_row());
		// 注释填充列
		Integer memo_col = StringFormat.getNumber(model.getMemo_col());
		HSSFRow row2 = sheet.getRow(memo_row);
		HSSFCell cel2 = row2.getCell(memo_col);
		cel2.setCellValue(notes);

	}

	private void addQltyItem(QcmZbsTmpl model, TreeMap<String, List<QcmZbsDataPic>> qltyItems, HSSFSheet sheet)
			throws Exception {

		// 获取语种代码
		String lng_code = model.getLng_code();
		// 根据语言代码查询语言
		List<QcmZbsLanguage> laugnagesBig = this.getMapper(QcmZbsLanguageMapper.class).findWithCondition(null,
				"LNG_CODE='" + lng_code + "' and TYPE_CODE='41150203'", null, 0, 0);
		List<QcmZbsLanguage> laugnagesItem = this.getMapper(QcmZbsLanguageMapper.class).findWithCondition(null,
				"LNG_CODE='" + lng_code + "' and TYPE_CODE='41150204'", null, 0, 0);

		// 材质大项开始列
		Integer pic_col_item_l = StringFormat.getNumber(model.getPic_col_item_l());
		// 材质大项开始行
		Integer pic_row_item_l = StringFormat.getNumber(model.getPic_row_item_l());
		// 材质检验项目开始行
		Integer pic_row_item_s = StringFormat.getNumber(model.getPic_row_item_s());
		// 材质检验项目开始列
		Integer pic_col_item_s = StringFormat.getNumber(model.getPic_col_item_s());
		// 值单位开始行
		Integer pic_row_item_u = StringFormat.getNumber(model.getPic_row_item_u());
		// 值单位开始列
		Integer pic_col_item_u = StringFormat.getNumber(model.getPic_col_item_u());
		Set<String> keys = qltyItems.keySet();
		for (String key : keys) {
			List<QcmZbsDataPic> pics = qltyItems.get(key);
			// 大项开始列
			Integer bigStr = pic_col_item_s;
			// 设置单元格，大项中文开始行
			HSSFRow row2 = sheet.getRow(pic_row_item_l);
			HSSFCell cel2 = row2.getCell(bigStr);
			cel2.setCellValue(pics.get(0).getPhy_name_l());
			// 填充外文
			for (QcmZbsLanguage l : laugnagesBig) {
				if (key.equals(l.getItem_code())) {
					row2 = sheet.getRow(pic_row_item_l + 1);
					cel2 = row2.getCell(bigStr);
					cel2.setCellValue(l.getVal());
					break;
				}
			}
			// System.out.println(pics.get(0).getPhy_name_l());

			for (QcmZbsDataPic p : pics) {
				int itemStr = pic_col_item_s;
				HSSFRow row3 = sheet.getRow(pic_row_item_s);
				HSSFCell cel3 = row3.getCell(pic_col_item_s);
				cel3.setCellValue(p.getPhy_name_s());
				if ("I01".equals(p.getPhy_code_s())) {
					// 填值单位
					row3 = sheet.getRow(pic_row_item_u);
					cel3 = row3.getCell(pic_col_item_s);
					cel3.setCellValue(p.getPhy_unit());

					pic_col_item_s++;
					continue;
				}

				if ("I02".equals(p.getPhy_code_s())) {

					// 填值单位
					row3 = sheet.getRow(pic_row_item_u);
					cel3 = row3.getCell(pic_col_item_s);
					cel3.setCellValue(p.getPhy_unit());

					pic_col_item_s++;
					continue;
				}

				// 填充项目对应的英文
				for (QcmZbsLanguage l : laugnagesItem) {
					if (l.getItem_code().equals(p.getPhy_code_s())) {
						row3 = sheet.getRow(pic_row_item_s + 1);
						cel3 = row3.getCell(pic_col_item_s);
						cel3.setCellValue(l.getEng_abbr());

						if (p.getValue() == 1) {

							// 填值单位
							row3 = sheet.getRow(pic_row_item_u);
							cel3 = row3.getCell(pic_col_item_s);
							cel3.setCellValue(p.getPhy_unit());

							row3 = sheet.getRow(pic_row_item_s + 2);
							cel3 = row3.getCell(pic_col_item_s++);
							cel3.setCellValue(l.getItem_type());

						} else {

							row3 = sheet.getRow(pic_row_item_u);
							cel3 = row3.getCell(pic_col_item_s);
							cel3.setCellValue(p.getPhy_unit());

							if (p.getValue() == 2) {

								row3 = sheet.getRow(pic_row_item_s + 2);
								cel3 = row3.getCell(pic_col_item_s++);
								cel3.setCellValue(1);

								row3 = sheet.getRow(pic_row_item_s + 2);
								cel3 = row3.getCell(pic_col_item_s++);
								cel3.setCellValue("AVE");

							}
							if (p.getValue() == 3) {
								row3 = sheet.getRow(pic_row_item_s + 2);
								cel3 = row3.getCell(pic_col_item_s++);
								cel3.setCellValue(1);

								row3 = sheet.getRow(pic_row_item_s + 2);
								cel3 = row3.getCell(pic_col_item_s++);
								cel3.setCellValue(2);

								row3 = sheet.getRow(pic_row_item_s + 2);
								cel3 = row3.getCell(pic_col_item_s++);
								cel3.setCellValue("AVE");
							}
							if (p.getValue() == 4) {

								row3 = sheet.getRow(pic_row_item_s + 2);
								cel3 = row3.getCell(pic_col_item_s++);
								cel3.setCellValue(1);

								row3 = sheet.getRow(pic_row_item_s + 2);
								cel3 = row3.getCell(pic_col_item_s++);
								cel3.setCellValue(2);

								row3 = sheet.getRow(pic_row_item_s + 2);
								cel3 = row3.getCell(pic_col_item_s++);
								cel3.setCellValue(3);

								row3 = sheet.getRow(pic_row_item_s + 2);
								cel3 = row3.getCell(pic_col_item_s++);
								cel3.setCellValue("AVE");

							}

							// 合并项目单元格
							sheet.addMergedRegion(
									new CellRangeAddress(pic_row_item_s, pic_row_item_s, itemStr, pic_col_item_s - 1));

							//
							sheet.addMergedRegion(new CellRangeAddress(pic_row_item_s + 1, pic_row_item_s + 1, itemStr,
									pic_col_item_s - 1));

						}

						break;
					}
				}
				// 合并值单位
				if (pic_col_item_s - 1 > itemStr) {
					sheet.addMergedRegion(
							new CellRangeAddress(pic_row_item_u, pic_row_item_u, itemStr, pic_col_item_s - 1));

				}

			}

			// 合并大项单元格
			if (pic_col_item_s - 1 > bigStr) {
				sheet.addMergedRegion(new CellRangeAddress(pic_row_item_l, pic_row_item_l, bigStr, pic_col_item_s - 1));

			}

			// 合并大项英文名
			if (pic_col_item_s - 1 > bigStr) {
				sheet.addMergedRegion(
						new CellRangeAddress(pic_row_item_l + 1, pic_row_item_l + 1, bigStr, pic_col_item_s - 1));

			}

		}

	}

	/**
	 * 添加成分元素
	 * 
	 * @param val
	 * @throws Exception
	 */
	private void addChemItem(String[] chem_name, QcmZbsTmpl model, HSSFSheet sheet, String val) throws Exception {
		// 获取成分元素开始行
		Integer cic_row_title = StringFormat.getNumber(model.getCic_row_title());
		Integer cic_col_title = StringFormat.getNumber(model.getCic_col_title());
		// 获取表格最大列
		Integer max_col;
		if ("1".equals(val)) {
			max_col = StringFormat.getNumber(model.getPic_col_val());
		} else {
			max_col = sheet.getRow(cic_row_title).getPhysicalNumberOfCells() - 1;
		}
		for (String c : chem_name) {
			HSSFRow row2 = sheet.getRow(cic_row_title);

			HSSFCell cel2 = row2.getCell(cic_col_title);

			if (cic_col_title >= max_col) {
				cic_row_title++;
				cic_col_title = StringFormat.getNumber(model.getCic_col_title());
				cel2.setCellValue(c);
			} else {
				// System.out.println("111" + c);
				if (c != null && !"".equals(c)) {
					cel2.setCellValue(c);
					cic_col_title++;
				} else {
					cel2.setCellValue("");
					cic_col_title++;
				}

			}

		}

	}

	/*
	 * private void addExcel(HSSFSheet sheet, QcmZbsTmpl model, TreeMap<String,
	 * List<QcmZbsDataPic>> qltyItems, QcmZbsObject qcmZbsObject, Integer
	 * me_row_str, Integer ql_row_str, String val) throws Exception { //
	 * 获取成分值开始列 // Integer
	 * cic_col_val=StringFormat.getNumber(model.getCic_col_val()); // Integer
	 * pic_col_val=StringFormat.getNumber(model.getPic_col_val()); Integer
	 * max_col = sheet.getRow(me_row_str).getPhysicalNumberOfCells() - 1;
	 * 
	 * // 填充材质值，当前卷的试样类型，对应的材质集合 Set<String> qs =
	 * qcmZbsObject.getQltyMap().keySet();
	 * 
	 * // 获取成分配置项 List<QcmZbsTmplItem> chemConfigure =
	 * this.getMapper(QcmZbsTmplItemMapper.class).findWithCondition(null,
	 * "TMPL_NO='" + model.getTmpl_no() + "' and DIV_TYPE='1'", null, 0, 0);
	 * 
	 * if ("1".equals(val)) { // 填充成分值 List<QcmZbsChemObject> cic =
	 * qcmZbsObject.getChemList(); // 计算成分、材质哪个所占行数最多 // 每行成分个数 Integer
	 * one_col_chem = ql_row_str - me_row_str; Integer one_row_chem = 0;
	 * 
	 * for (int i = 0; i < 100; i++) {
	 * 
	 * if (i * one_col_chem >= cic.get(0).getChemValue().length) { one_row_chem
	 * = i * cic.size(); break; } } // 获取材质所占行数 Integer one_row_qlty =
	 * qs.size(); if (one_row_chem > one_row_qlty) { // 当前卷的成分开始行 int meter_star
	 * = me_row_str; for (QcmZbsChemObject o : cic) { Integer cic_col_val =
	 * StringFormat.getNumber(model.getCic_col_val()); // 填充成分值 for (String ch :
	 * o.getChemValue()) {
	 * 
	 * HSSFRow row2 = sheet.getRow(me_row_str); HSSFCell cell =
	 * row2.getCell(cic_col_val); if (cic_col_val >=
	 * (StringFormat.getNumber(model.getPic_col_val()) - 1)) { me_row_str++;
	 * cic_col_val = StringFormat.getNumber(model.getCic_col_val()); } else {
	 * cell.setCellValue(ch); cic_col_val++; }
	 * 
	 * }
	 * 
	 * }
	 * 
	 * // 填充材质 // 材质开始行 Integer qlty_str = meter_star; // key是代表试样号smp_no
	 * Set<String> qi = qltyItems.keySet(); for (String key : qs) { Integer
	 * pic_col_val = StringFormat.getNumber(model.getPic_col_val());
	 * List<QcmZbsDataPic> pic = qcmZbsObject.getQltyMap().get(key);
	 * 
	 * // k拿到所有检验大项代码
	 * 
	 * for (String k : qi) { List<QcmZbsDataPic> pi = qltyItems.get(k);
	 * 
	 * for (QcmZbsDataPic p : pi) { String oldItem = p.getPhy_code_l() +
	 * p.getPhy_code_m() + p.getPhy_code_s() + p.getItem_code_d() +
	 * p.getItem_code_s() + p.getItem_code_t(); for (int j = 0; j < pic.size();
	 * j++) { String newItem = pic.get(j).getPhy_code_l() +
	 * pic.get(j).getPhy_code_m() + pic.get(j).getPhy_code_s() +
	 * pic.get(j).getItem_code_d() + pic.get(j).getItem_code_s() +
	 * pic.get(j).getItem_code_t();
	 * 
	 * if (oldItem.equals(newItem)) { // 材质值 HSSFRow row1 =
	 * sheet.getRow(qlty_str); HSSFCell cel1 = row1.getCell(pic_col_val); if
	 * (p.getValue() == 1) { cel1.setCellValue(pic.get(j).getVal1());
	 * pic_col_val++; } else if (p.getValue() == 2) { cel1 =
	 * row1.getCell(pic_col_val++); cel1.setCellValue(pic.get(j).getVal1());
	 * cel1 = row1.getCell(pic_col_val++);
	 * cel1.setCellValue(pic.get(j).getAvg_val()); pic_col_val++; } else if
	 * (p.getValue() == 3) { cel1 = row1.getCell(pic_col_val++);
	 * cel1.setCellValue(pic.get(j).getVal1()); cel1 =
	 * row1.getCell(pic_col_val++); cel1.setCellValue(pic.get(j).getVal2());
	 * cel1 = row1.getCell(pic_col_val++);
	 * cel1.setCellValue(pic.get(j).getAvg_val()); pic_col_val++; } else if
	 * (p.getValue() == 4) { cel1 = row1.getCell(pic_col_val++);
	 * cel1.setCellValue(pic.get(j).getVal1()); cel1 =
	 * row1.getCell(pic_col_val++); cel1.setCellValue(pic.get(j).getVal2());
	 * cel1 = row1.getCell(pic_col_val++);
	 * cel1.setCellValue(pic.get(j).getVal3()); cel1 =
	 * row1.getCell(pic_col_val++); cel1.setCellValue(pic.get(j).getAvg_val());
	 * 
	 * }
	 * 
	 * break; }
	 * 
	 * if (j == pic.size() - 1) { // 材质值 HSSFRow row1 = sheet.getRow(qlty_str);
	 * HSSFCell cel1 = row1.getCell(pic_col_val); cel1.setCellValue("");
	 * pic_col_val++; break; }
	 * 
	 * }
	 * 
	 * }
	 * 
	 * }
	 * 
	 * 
	 * for (QcmZbsDataPic p : pic) { //材质值 HSSFRow row1 =
	 * sheet.getRow(pic_row_val); HSSFCell cel1 = row1.getCell(pic_col_val);
	 * cel1.setCellValue(p.getVal1()); pic_col_val++; }
	 * 
	 * // 卷号 HSSFRow row2 = sheet.getRow(ql_row_str); HSSFCell cel2 =
	 * row2.getCell(1); cel2.setCellValue(qcmZbsObject.getMaterial_no()); //
	 * 试样类型 QcmJhySampleConsignM qm =
	 * this.getMapper(QcmJhySampleConsignMMapper.class).findById(key); row2 =
	 * sheet.getRow(qlty_str); cel2 = row2.getCell(2);
	 * 
	 * if (qm != null) { cel2.setCellValue(qm.getSmp_type_name()); } else {
	 * cel2.setCellValue(""); }
	 * 
	 * qlty_str++;
	 * 
	 * }
	 * 
	 * // 填入物料信息 // 填充成分配置 TreeMap<String, Object> tree =
	 * cic.get(0).getHeat_info(); Set<String> se = tree.keySet(); for
	 * (QcmZbsTmplItem c : chemConfigure) {
	 * 
	 * for (String key : se) { if (c.getItem_code().equals(key.toUpperCase())) {
	 * Object value = tree.get(key); // 获取单元格 HSSFRow row1 =
	 * sheet.getRow(meter_star); HSSFCell cell =
	 * row1.getCell(StringFormat.getNumber(c.getItem_str_col_e()));
	 * cell.setCellValue((String) value);
	 * 
	 * }
	 * 
	 * }
	 * 
	 * // 合并列
	 * 
	 * }
	 * 
	 * } else {
	 * 
	 * Integer star_row = me_row_str;
	 * 
	 * // 填充材质 Set<String> qi = qltyItems.keySet(); for (String key : qs) {
	 * Integer pic_col_val = StringFormat.getNumber(model.getPic_col_val());
	 * List<QcmZbsDataPic> pic = qcmZbsObject.getQltyMap().get(key);
	 * 
	 * // k拿到所有检验大项代码
	 * 
	 * for (String k : qi) { List<QcmZbsDataPic> pi = qltyItems.get(k);
	 * 
	 * for (QcmZbsDataPic p : pi) { String oldItem = p.getPhy_code_l() +
	 * p.getPhy_code_m() + p.getPhy_code_s() + p.getItem_code_d() +
	 * p.getItem_code_s() + p.getItem_code_t(); for (int j = 0; j < pic.size();
	 * j++) { String newItem = pic.get(j).getPhy_code_l() +
	 * pic.get(j).getPhy_code_m() + pic.get(j).getPhy_code_s() +
	 * pic.get(j).getItem_code_d() + pic.get(j).getItem_code_s() +
	 * pic.get(j).getItem_code_t();
	 * 
	 * if (oldItem.equals(newItem)) { // 材质值 HSSFRow row1 =
	 * sheet.getRow(me_row_str); HSSFCell cel1 = row1.getCell(pic_col_val); if
	 * (p.getValue() == 1) { cel1.setCellValue(pic.get(j).getVal1());
	 * pic_col_val++; } else if (p.getValue() == 2) { cel1 =
	 * row1.getCell(pic_col_val++); cel1.setCellValue(pic.get(j).getVal1());
	 * cel1 = row1.getCell(pic_col_val++);
	 * cel1.setCellValue(pic.get(j).getAvg_val()); pic_col_val++; } else if
	 * (p.getValue() == 3) { cel1 = row1.getCell(pic_col_val++);
	 * cel1.setCellValue(pic.get(j).getVal1()); cel1 =
	 * row1.getCell(pic_col_val++); cel1.setCellValue(pic.get(j).getVal2());
	 * cel1 = row1.getCell(pic_col_val++);
	 * cel1.setCellValue(pic.get(j).getAvg_val()); pic_col_val++; } else if
	 * (p.getValue() == 4) { cel1 = row1.getCell(pic_col_val++);
	 * cel1.setCellValue(pic.get(j).getVal1()); cel1 =
	 * row1.getCell(pic_col_val++); cel1.setCellValue(pic.get(j).getVal2());
	 * cel1 = row1.getCell(pic_col_val++);
	 * cel1.setCellValue(pic.get(j).getVal3()); cel1 =
	 * row1.getCell(pic_col_val++); cel1.setCellValue(pic.get(j).getAvg_val());
	 * 
	 * }
	 * 
	 * break; }
	 * 
	 * if (j == pic.size() - 1) { // 材质值 HSSFRow row1 =
	 * sheet.getRow(me_row_str); HSSFCell cel1 = row1.getCell(pic_col_val);
	 * cel1.setCellValue(""); pic_col_val++; break; }
	 * 
	 * }
	 * 
	 * }
	 * 
	 * }
	 * 
	 * 
	 * for (QcmZbsDataPic p : pic) { //材质值 HSSFRow row1 =
	 * sheet.getRow(pic_row_val); HSSFCell cel1 = row1.getCell(pic_col_val);
	 * cel1.setCellValue(p.getVal1()); pic_col_val++; }
	 * 
	 * // 卷号 HSSFRow row2 = sheet.getRow(me_row_str); // HSSFCell cel2 =
	 * row2.getCell(1); // cel2.setCellValue(qcmZbsObject.getMaterial_no()); //
	 * 试样类型 QcmJhySampleConsignM qm =
	 * this.getMapper(QcmJhySampleConsignMMapper.class).findById(key); // row2 =
	 * sheet.getRow(c); HSSFCell cel2 =
	 * row2.getCell((StringFormat.getNumber(model.getPic_col_val()) - 1));
	 * 
	 * if (qm != null) { cel2.setCellValue(qm.getSmp_type_name()); } else {
	 * cel2.setCellValue(""); }
	 * 
	 * me_row_str++;
	 * 
	 * }
	 * 
	 * // 填充成分 int chem_star = star_row; for (QcmZbsChemObject o : cic) {
	 * Integer cic_col_val = StringFormat.getNumber(model.getCic_col_val());
	 * 
	 * // 填充成分值 for (String ch : o.getChemValue()) {
	 * 
	 * HSSFRow row2 = sheet.getRow(star_row); HSSFCell cel2 =
	 * row2.getCell(cic_col_val); if (cic_col_val >= max_col) { chem_star++;
	 * cic_col_val = StringFormat.getNumber(model.getCic_col_val()); } else {
	 * cel2.setCellValue(ch); cic_col_val++; }
	 * 
	 * }
	 * 
	 * }
	 * 
	 * // 填充物料 TreeMap<String, Object> tree = cic.get(0).getHeat_info();
	 * Set<String> se = tree.keySet(); for (QcmZbsTmplItem c : chemConfigure) {
	 * 
	 * for (String key : se) { if (c.getItem_code().equals(key.toUpperCase())) {
	 * Object value = tree.get(key); // 获取单元格 HSSFRow row1 =
	 * sheet.getRow(star_row); // System.out.println(c.getItem_str_col());
	 * HSSFCell cell =
	 * row1.getCell(StringFormat.getNumber(c.getItem_str_col_e())); //
	 * System.out.println((String)value); cell.setCellValue((String) value);
	 * 
	 * }
	 * 
	 * }
	 * 
	 * } }
	 * 
	 * } else {
	 * 
	 * List<QcmZbsChemObject> cic = qcmZbsObject.getChemList(); for
	 * (QcmZbsChemObject o : cic) { Integer cic_col_val =
	 * StringFormat.getNumber(model.getCic_col_val()); int meter_star =
	 * me_row_str;
	 * 
	 * // 填充成分值 for (String ch : o.getChemValue()) {
	 * 
	 * HSSFRow row2 = sheet.getRow(me_row_str); HSSFCell cel2 =
	 * row2.getCell(cic_col_val); if (cic_col_val >= max_col) { me_row_str++;
	 * cic_col_val = StringFormat.getNumber(model.getCic_col_val()); } else {
	 * cel2.setCellValue(ch); cic_col_val++; }
	 * 
	 * // 行加一 me_row_str++;
	 * 
	 * }
	 * 
	 * // 填充材质值 // Set<String> qs = qcmZbsObject.getQltyMap().keySet();
	 * 
	 * // 获取所有项目 // key是代表试样号smp_no Set<String> qi = qltyItems.keySet(); for
	 * (String key : qs) { Integer pic_col_val =
	 * StringFormat.getNumber(model.getPic_col_val()); List<QcmZbsDataPic> pic =
	 * qcmZbsObject.getQltyMap().get(key);
	 * 
	 * // k拿到所有检验大项代码
	 * 
	 * for (String k : qi) { List<QcmZbsDataPic> pi = qltyItems.get(k);
	 * 
	 * for (QcmZbsDataPic p : pi) { String oldItem = p.getPhy_code_l() +
	 * p.getPhy_code_m() + p.getPhy_code_s() + p.getItem_code_d() +
	 * p.getItem_code_s() + p.getItem_code_t(); for (int j = 0; j < pic.size();
	 * j++) { String newItem = pic.get(j).getPhy_code_l() +
	 * pic.get(j).getPhy_code_m() + pic.get(j).getPhy_code_s() +
	 * pic.get(j).getItem_code_d() + pic.get(j).getItem_code_s() +
	 * pic.get(j).getItem_code_t();
	 * 
	 * if (oldItem.equals(newItem)) { // 材质值 HSSFRow row1 =
	 * sheet.getRow(ql_row_str); HSSFCell cel1 = row1.getCell(pic_col_val); if
	 * (p.getValue() == 1) { cel1.setCellValue(pic.get(j).getVal1());
	 * pic_col_val++; } else if (p.getValue() == 2) { cel1 =
	 * row1.getCell(pic_col_val++); cel1.setCellValue(pic.get(j).getVal1());
	 * cel1 = row1.getCell(pic_col_val++);
	 * cel1.setCellValue(pic.get(j).getAvg_val()); pic_col_val++; } else if
	 * (p.getValue() == 3) { cel1 = row1.getCell(pic_col_val++);
	 * cel1.setCellValue(pic.get(j).getVal1()); cel1 =
	 * row1.getCell(pic_col_val++); cel1.setCellValue(pic.get(j).getVal2());
	 * cel1 = row1.getCell(pic_col_val++);
	 * cel1.setCellValue(pic.get(j).getAvg_val()); pic_col_val++; } else if
	 * (p.getValue() == 4) { cel1 = row1.getCell(pic_col_val++);
	 * cel1.setCellValue(pic.get(j).getVal1()); cel1 =
	 * row1.getCell(pic_col_val++); cel1.setCellValue(pic.get(j).getVal2());
	 * cel1 = row1.getCell(pic_col_val++);
	 * cel1.setCellValue(pic.get(j).getVal3()); cel1 =
	 * row1.getCell(pic_col_val++); cel1.setCellValue(pic.get(j).getAvg_val());
	 * 
	 * }
	 * 
	 * break; }
	 * 
	 * if (j == pic.size() - 1) { // 材质值 HSSFRow row1 =
	 * sheet.getRow(ql_row_str); HSSFCell cel1 = row1.getCell(pic_col_val);
	 * cel1.setCellValue(""); pic_col_val++; break; }
	 * 
	 * }
	 * 
	 * }
	 * 
	 * }
	 * 
	 * 
	 * for (QcmZbsDataPic p : pic) { //材质值 HSSFRow row1 =
	 * sheet.getRow(pic_row_val); HSSFCell cel1 = row1.getCell(pic_col_val);
	 * cel1.setCellValue(p.getVal1()); pic_col_val++; }
	 * 
	 * // 卷号 HSSFRow row2 = sheet.getRow(ql_row_str); HSSFCell cel2 =
	 * row2.getCell(1); cel2.setCellValue(qcmZbsObject.getMaterial_no()); //
	 * 试样类型 QcmJhySampleConsignM qm =
	 * this.getMapper(QcmJhySampleConsignMMapper.class).findById(key); row2 =
	 * sheet.getRow(ql_row_str); cel2 = row2.getCell(2);
	 * 
	 * if (qm != null) { cel2.setCellValue(qm.getSmp_type_name()); } else {
	 * cel2.setCellValue(""); }
	 * 
	 * ql_row_str++;
	 * 
	 * }
	 * 
	 * }
	 * 
	 * }
	 * 
	 * }
	 */
	/**
	 * 创建质保书头部
	 * 
	 * @param head
	 * @param sheet
	 * @param ord
	 * @throws Exception
	 */
	private void addHead(List<QcmZbsTmplItem> head, HSSFSheet sheet, QcmZbsDataOrd ord,String data) throws Exception {
		// 获取表头配置项,表头填充逻辑
		for (QcmZbsTmplItem he : head) {
			if ((he.getItem_str_row() != null && !"".equals(he.getItem_str_row()))
					&& (he.getItem_str_col_e() != null && !"".equals(he.getItem_str_col_e()))) {
				String value = this.getMapper(QcmZbsDataOrdMapper.class).findValue(he.getItem_code(), ord.getCret_no());
				HSSFRow row2 = sheet.getRow(StringFormat.getNumber(he.getItem_str_row()));
				HSSFCell cel2 = row2.getCell(StringFormat.getNumber(he.getItem_str_col_e()));

				if ("CFNM_DATE".equals(he.getItem_code())) {
					if (value == null || "".equals(value)) {
						cel2.setCellValue("");
					} else {
						cel2.setCellValue(value.substring(0, 16));

					}

				}else if ("PRINT_ID".equals(he.getItem_code())) {
					String userName=this.getSession().getAttribute(HomeService.SESSION_USER_NAME).toString();
					if (userName == null || "".equals(userName)) {
						cel2.setCellValue("");
					} else {
						cel2.setCellValue(userName);

					}
				}else if ("PRINT_DTIME".equals(he.getItem_code())) {
					if (data == null || "".equals(data)) {
						cel2.setCellValue("");
					} else {
						cel2.setCellValue(data);

					}
				}else{
					if (value == null || "".equals(value)) {
						cel2.setCellValue("");
					} else {
						cel2.setCellValue(value);

					}

				}
				
				
			}

		}

	}

	/**
	 * 获取质保书对象
	 * 
	 * @param targetBook
	 * @param mats
	 * @param ord
	 * @param chem_name
	 */
	private void createObject(List<QcmZbsObject> targetBook, List<QcmZbsDataMat> mats, QcmZbsDataOrd ord,
			String[] chem_name) {

		for (QcmZbsDataMat mat : mats) {
			QcmZbsObject obj = new QcmZbsObject();
			List<QcmZbsChemObject> tarChem = new ArrayList<>();
			int chem_size = 0;

			QcmZbsChemObject ch;
			// 获取mat对象的所有属性名、以及值
			Field[] field = mat.getClass().getDeclaredFields();
			TreeMap<String, Object> heat_info = new TreeMap<String, Object>();
			for (Field f : field) {
				heat_info.put(f.getName(), getFieldValueByName(f.getName(), mat));
			}

			// 获取成品成分信息
			List<QcmZbsDataCic> chemP = this.getMapper(QcmZbsDataCicMapper.class).findWithCondition(null, "CRET_NO='"
					+ ord.getCret_no() + "' and MATERIAL_NO='" + mat.getMaterial_no() + "' and ASSAY_TYPE='P'",
					"MATERIAL_NO,ASSAY_TYPE,CHEM_CODE", 0, 0);

			List<QcmZbsDataCic> chemL = this.getMapper(QcmZbsDataCicMapper.class).findWithCondition(null, "CRET_NO='"
					+ ord.getCret_no() + "' and MATERIAL_NO='" + mat.getMaterial_no() + "' and ASSAY_TYPE='L'",
					"MATERIAL_NO,ASSAY_TYPE,CHEM_CODE", 0, 0);

			// 成品成分
			if (chemP != null && chemP.size() > 0) {
				ch = new QcmZbsChemObject();
				String[] chemValue = new String[chem_name.length];
				int a = 0;
				for (String chemName : chem_name) {

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

						if (chemP.get(i).getChem_name().equals(chemName)) {
							chemValue[a++] = chemP.get(i).getChem_value();
							break;
						}

						if (i == chemP.size() - 1) {
							chemValue[a++] = null;
						}
					}

				}

				ch.setChemValue(chemValue);
				ch.setHeat_info(heat_info);
				ch.setAssay_type("P");
				tarChem.add(ch);
				chem_size++;
			}
			// 熔炼成分
			if (chemL != null && chemL.size() > 0) {
				ch = new QcmZbsChemObject();
				String[] chemValue = new String[chem_name.length];
				int a = 0;
				for (String chemName : chem_name) {

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

						if (chemL.get(i).getChem_name().equals(chemName)) {
							chemValue[a++] = chemL.get(i).getChem_value();
							break;
						}

						if (i == chemL.size() - 1) {
							chemValue[a++] = null;
						}
					}

				}

				ch.setChemValue(chemValue);
				ch.setHeat_info(heat_info);
				ch.setAssay_type("L");
				tarChem.add(ch);
				chem_size++;
			}

			// 材质分组
			List<QcmZbsDataPic> pics = this.getMapper(QcmZbsDataPicMapper.class).findWithCondition(null,
					"CRET_NO='" + mat.getCret_no() + "' and MATERIAL_NO='" + mat.getMaterial_no() + "'", "SMP_NO,SEQ",
					0, 0);

			// 封装材质项目值
			List<QcmZbsDataPic> RQ = null;
			TreeMap<String, List<QcmZbsDataPic>> qltyList = new TreeMap<>();
			for (int i = 0; i < pics.size(); i++) {

				if (i == 0) {
					RQ = new ArrayList<>();
					RQ.add(pics.get(i));

					if (i == pics.size() - 1) {
						// 封装温度
						encapTemp(RQ);
						// 封装尺寸
						encapSize(RQ);
						// 封装位置
						// encapPodition(RQ);

						qltyList.put(pics.get(i).getSmp_no(), RQ);
					}
					continue;
				}

				if (!pics.get(i).getSmp_no().equals(pics.get(i - 1).getSmp_no())) {

					encapTemp(RQ);

					encapSize(RQ);

					qltyList.put(pics.get(i - 1).getSmp_no(), RQ);
					RQ = new ArrayList<>();
					RQ.add(pics.get(i));
					continue;
				}

				RQ.add(pics.get(i));
				if (i == pics.size() - 1) {

					encapTemp(RQ);

					encapSize(RQ);

					qltyList.put(pics.get(i).getSmp_no(), RQ);

				}

			}

			// 将成分添加到对象
			obj.setMaterial_no(mat.getMaterial_no());
			obj.setQlty_size(qltyList.size());
			obj.setQltyMap(qltyList);
			obj.setChemList(tarChem);
			obj.setChem_size(chem_size);
			targetBook.add(obj);

		}

	}

	/**
	 * 获取材质表头信息
	 * 
	 * @param qltyItems
	 * @param cret_no
	 * @param mats2
	 */

	private void getQltyItems(TreeMap<String, List<QcmZbsDataPic>> qltyItems, String cret_no,
			List<QcmZbsDataMat> mats) {
		// 根据质保书编号查询物料信息
		mats = this.getMapper(QcmZbsDataMatMapper.class).findWithCondition(null, "CRET_NO='" + cret_no + "'", null, 0,
				0);

		// 获取检验大项代码集合
		TreeSet<String> bigItem = new TreeSet<>();

		for (QcmZbsDataMat mat : mats) {
			// 材质分组
			List<QcmZbsDataPic> pics = this.getMapper(QcmZbsDataPicMapper.class).findWithCondition(null,
					"CRET_NO='" + mat.getCret_no() + "' and MATERIAL_NO='" + mat.getMaterial_no() + "'", "SMP_NO,SEQ",
					0, 0);

			for (QcmZbsDataPic p : pics) {
				bigItem.add(p.getPhy_code_l());
			}
		}

		for (String item : bigItem) {

			List<QcmZbsDataPic> picT = null;

			for (QcmZbsDataMat mat : mats) {
				// 材质分组
				picT = new ArrayList<>();
				List<QcmZbsDataPic> picR = this.getMapper(QcmZbsDataPicMapper.class).findWithCondition(null,
						"CRET_NO='" + mat.getCret_no() + "' and MATERIAL_NO='" + mat.getMaterial_no()
								+ "' and PHY_CODE_L='" + item + "'",
						"SMP_NO,SEQ", 0, 0);

				for (int i = 0; i < picR.size(); i++) {
					int value = 0;
					String items = picR.get(i).getPhy_code_l() + picR.get(i).getPhy_code_m()
							+ picR.get(i).getPhy_code_s() + picR.get(i).getItem_code_d() + picR.get(i).getItem_code_s()
							+ picR.get(i).getItem_code_t();

					if (i == 0) {
						if (picR.get(i).getVal1() != null && !"".equals(picR.get(i).getVal1())) {
							value++;
						}
						if (picR.get(i).getVal2() != null && !"".equals(picR.get(i).getVal2())) {
							value++;
						}
						if (picR.get(i).getVal3() != null && !"".equals(picR.get(i).getVal3())) {
							value++;
						}
						if (picR.get(i).getAvg_val() != null && !"".equals(picR.get(i).getAvg_val())) {
							value++;
						}
						picR.get(i).setValue(value);
						picT.add(picR.get(i));
						continue;
					}

					for (int j = 0; j < picT.size(); j++) {
						String newItems = picT.get(j).getPhy_code_l() + picT.get(j).getPhy_code_m()
								+ picT.get(j).getPhy_code_s() + picT.get(j).getItem_code_d()
								+ picT.get(j).getItem_code_s() + picT.get(j).getItem_code_t();

						if (items.equals(newItems)) {
							if (picR.get(i).getVal1() != null && !"".equals(picR.get(i).getVal1())) {
								value++;
							}
							if (picR.get(i).getVal2() != null && !"".equals(picR.get(i).getVal2())) {
								value++;
							}
							if (picR.get(i).getVal3() != null && !"".equals(picR.get(i).getVal3())) {
								value++;
							}
							if (picR.get(i).getAvg_val() != null && !"".equals(picR.get(i).getAvg_val())) {
								value++;
							}
							if (value > picT.get(j).getValue()) {
								picT.get(j).setValue(value);
							}
							break;
						}

						if (j == picT.size() - 1) {
							if (picR.get(i).getVal1() != null && !"".equals(picR.get(i).getVal1())) {
								value++;
							}
							if (picR.get(i).getVal2() != null && !"".equals(picR.get(i).getVal2())) {
								value++;
							}
							if (picR.get(i).getVal3() != null && !"".equals(picR.get(i).getVal3())) {
								value++;
							}
							if (picR.get(i).getAvg_val() != null && !"".equals(picR.get(i).getAvg_val())) {
								value++;
							}
							picR.get(i).setValue(value);
							picT.add(picR.get(i));
							break;
						}

					}

				}

			}
			List<QcmZbsDataPic> T = new ArrayList<>();
			// 获取当前检验大项的检验项目的温度
			for (QcmZbsDataPic t : picT) {

				if (t.getItem_code_t() != null && !"".equals(t.getItem_code_t())) {
					QcmZbsDataPic r = new QcmZbsDataPic();
					r.setPhy_code_l(t.getPhy_code_l());
					r.setPhy_name_l(t.getPhy_name_l());
					r.setPhy_code_s("I01");
					r.setPhy_name_s("温度 T");
					r.setPhy_unit("℃");
					r.setValue(1);
					T.add(r);
					break;

				}

			}

			// 获取当前检验大项的检验项目的尺寸
			for (QcmZbsDataPic t : picT) {

				if (t.getItem_code_s() != null && !"".equals(t.getItem_code_s())) {
					QcmZbsDataPic r = new QcmZbsDataPic();
					r.setPhy_code_l(t.getPhy_code_l());
					r.setPhy_name_l(t.getPhy_name_l());
					r.setPhy_code_s("I02");
					r.setPhy_name_s("尺寸");
					r.setPhy_unit("mm");
					r.setValue(1);
					T.add(r);
					break;

				}

			}
			// 获取所有的小项

			T.addAll(picT);

			qltyItems.put(item, T);

		}

	}

	/**
	 * 获取成分元素数组、获取成分代码map集合
	 * 
	 * @param allChem
	 * @param chem_name
	 * @param cret_no
	 */
	private String[] getAllChemName(TreeMap<String, String> allChem, String[] chem_name, String cret_no) {
		// 获取成分信息
		List<QcmZbsDataCic> cics = this.getMapper(QcmZbsDataCicMapper.class).findWithCondition(null,
				"CRET_NO='" + cret_no + "'", "MATERIAL_NO,ASSAY_TYPE,CHEM_CODE", 0, 0);

		for (QcmZbsDataCic qcmZbsDataCic : cics) {
			allChem.put(qcmZbsDataCic.getChem_code(), qcmZbsDataCic.getChem_name());
		}

		// 判断模板元素项是否满足
		chem_name = new String[allChem.size()];
		Set<String> set = allChem.keySet();
		int count = 0;
		for (String c : set) {
			chem_name[count++] = allChem.get(c);
		}

		return chem_name;
	}

	private Integer[] addExcel(HSSFSheet sheet, QcmZbsTmpl model, TreeMap<String, List<QcmZbsDataPic>> qltyItems,
			QcmZbsObject qcmZbsObject, Integer me_row_str, Integer ql_row_str, Integer chem_seq, Integer qlty_seq,
			String val) throws Exception {
		// 获取成分值开始列
		/*
		 * Integer cic_col_val=StringFormat.getNumber(model.getCic_col_val());
		 * Integer pic_col_val=StringFormat.getNumber(model.getPic_col_val());
		 */
		Integer max_col = null;

		if ("1".equals(val)) {
			max_col = StringFormat.getNumber(model.getPic_col_val()) - 2;
		} else {
			max_col = sheet.getRow(me_row_str).getPhysicalNumberOfCells() - 1;
		}
		// 填充材质值，当前卷的试样类型，对应的材质集合
		Set<String> qs = qcmZbsObject.getQltyMap().keySet();

		// 获取物料配置项
		List<QcmZbsTmplItem> chemConfigure = this.getMapper(QcmZbsTmplItemMapper.class).findWithCondition(null,
				"TMPL_NO='" + model.getTmpl_no() + "' and DIV_TYPE='1'", "ITEM_STR_ROW,to_number(ITEM_STR_COL)", 0, 0);

		if ("1".equals(val)) {
			// 填充成分值
			List<QcmZbsChemObject> cic = qcmZbsObject.getChemList();
			// 计算成分、材质哪个所占行数最多
			// 每行成分个数
			Integer one_col_chem = StringFormat.getNumber(model.getPic_col_val()) - 1
					- StringFormat.getNumber(model.getCic_col_val());
			Integer one_row_chem = 0;

			for (int i = 0; i < 100; i++) {

				if (i * one_col_chem >= cic.get(0).getChemValue().length) {
					one_row_chem = i * cic.size();
					break;
				}
			}
			// 获取材质所占行数
			Integer one_row_qlty = qs.size();
			// 当成分填充行数大于材质填充行数
			if (one_row_chem > one_row_qlty) {
				// 当前卷的成分开始行
				int meter_star = me_row_str;
				for (QcmZbsChemObject o : cic) {
					Integer cic_col_val = StringFormat.getNumber(model.getCic_col_val());
					// 填充成分值
					for (String ch : o.getChemValue()) {

						HSSFRow row2 = sheet.getRow(me_row_str);
						HSSFCell cel2 = row2.getCell(cic_col_val);
						if (cic_col_val >= (StringFormat.getNumber(model.getPic_col_val()) - 2)) {
							me_row_str++;
							cic_col_val = StringFormat.getNumber(model.getCic_col_val());
							cel2.setCellValue(ch);
						} else {
							cel2.setCellValue(ch);
							cic_col_val++;
						}

					}

				}
				// 下一个卷的开始行，
				me_row_str++;
				ql_row_str = me_row_str;

				// 填充材质
				// 材质开始行
				Integer qlty_str = meter_star;
				// key是代表试样号smp_no
				Set<String> qi = qltyItems.keySet();
				for (String key : qs) {
					Integer pic_col_val = StringFormat.getNumber(model.getPic_col_val());
					List<QcmZbsDataPic> pic = qcmZbsObject.getQltyMap().get(key);

					// k拿到所有检验大项代码

					for (String k : qi) {
						List<QcmZbsDataPic> pi = qltyItems.get(k);

						for (QcmZbsDataPic p : pi) {
							String oldItem = p.getPhy_code_l() + p.getPhy_code_m() + p.getPhy_code_s()
									+ p.getItem_code_d() + p.getItem_code_s() + p.getItem_code_t();
							for (int j = 0; j < pic.size(); j++) {
								String newItem = pic.get(j).getPhy_code_l() + pic.get(j).getPhy_code_m()
										+ pic.get(j).getPhy_code_s() + pic.get(j).getItem_code_d()
										+ pic.get(j).getItem_code_s() + pic.get(j).getItem_code_t();

								if (oldItem.equals(newItem)) {
									// 材质值
									HSSFRow row1 = sheet.getRow(qlty_str);
									HSSFCell cel1 = row1.getCell(pic_col_val);
									if (p.getValue() == 1) {
										cel1.setCellValue(pic.get(j).getVal1());
										pic_col_val++;
									} else if (p.getValue() == 2) {
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getVal1());
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getAvg_val());
										pic_col_val++;
									} else if (p.getValue() == 3) {
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getVal1());
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getVal2());
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getAvg_val());
										pic_col_val++;
									} else if (p.getValue() == 4) {
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getVal1());
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getVal2());
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getVal3());
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getAvg_val());

									}

									break;
								}

								if (j == pic.size() - 1) {
									// 材质值
									HSSFRow row1 = sheet.getRow(qlty_str);
									HSSFCell cel1 = row1.getCell(pic_col_val);
									cel1.setCellValue("");
									pic_col_val++;
									break;
								}

							}

						}

					}

					// 试样类型
					HSSFRow row2 = sheet.getRow(qlty_str);
					HSSFCell cel2 = row2.getCell(StringFormat.getNumber(model.getPic_col_val()) - 1);
					// cel2.setCellValue(qcmZbsObject.getMaterial_no());
					// 试样类型
					QcmJhySampleConsignM qm = this.getMapper(QcmJhySampleConsignMMapper.class).findById(key);

					if (qm != null) {
						cel2.setCellValue(qm.getSmp_type_name());
					} else {
						cel2.setCellValue("");
					}

					qlty_str++;

				}

				// 填入物料信息
				// 填充成分配置
				TreeMap<String, Object> tree = cic.get(0).getHeat_info();
				Set<String> se = tree.keySet();

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

					for (String key : se) {
						if (chemConfigure.get(i).getItem_code().equals(key.toUpperCase())) {
							Object value = tree.get(key);
							// 获取单元格
							HSSFRow row1 = sheet.getRow(meter_star);
							HSSFCell cell = row1
									.getCell(StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()));
							cell.setCellValue((String) value);

							// 合并行
							if (i != chemConfigure.size() - 1) {
								if ((StringFormat.getNumber(chemConfigure.get(i + 1).getItem_str_col_e())
										- 1) > StringFormat.getNumber((chemConfigure.get(i).getItem_str_col_e()))) {

									if (me_row_str - 1 > meter_star) {
										sheet.addMergedRegion(new CellRangeAddress(meter_star, me_row_str - 1,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												(StringFormat.getNumber(chemConfigure.get(i + 1).getItem_str_col_e())
														- 1)));

									} else {
										sheet.addMergedRegion(new CellRangeAddress(meter_star, meter_star,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												(StringFormat.getNumber(chemConfigure.get(i + 1).getItem_str_col_e())
														- 1)));
									}

								} else {

									if (me_row_str - 1 > meter_star) {
										sheet.addMergedRegion(new CellRangeAddress(meter_star, me_row_str - 1,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e())));

									}

								}

							} else {

								if ((StringFormat.getNumber(model.getCic_col_val()) - 1) >= StringFormat
										.getNumber((chemConfigure.get(i).getItem_str_col_e()))) {

									if (me_row_str - 1 > meter_star) {
										sheet.addMergedRegion(new CellRangeAddress(meter_star, me_row_str - 1,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												(StringFormat.getNumber(model.getCic_col_val()) - 1)));

									}

								} else {
									if (me_row_str - 1 > meter_star) {
										sheet.addMergedRegion(new CellRangeAddress(meter_star, me_row_str - 1,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e())));

									}

								}

							} //

						}

					}

				}

			} else {

				Integer star_row = me_row_str;

				// 填充材质
				Set<String> qi = qltyItems.keySet();
				for (String key : qs) {
					Integer pic_col_val = StringFormat.getNumber(model.getPic_col_val());
					List<QcmZbsDataPic> pic = qcmZbsObject.getQltyMap().get(key);

					// k拿到所有检验大项代码

					for (String k : qi) {
						List<QcmZbsDataPic> pi = qltyItems.get(k);

						for (QcmZbsDataPic p : pi) {
							String oldItem = p.getPhy_code_l() + p.getPhy_code_m() + p.getPhy_code_s()
									+ p.getItem_code_d() + p.getItem_code_s() + p.getItem_code_t();
							for (int j = 0; j < pic.size(); j++) {
								String newItem = pic.get(j).getPhy_code_l() + pic.get(j).getPhy_code_m()
										+ pic.get(j).getPhy_code_s() + pic.get(j).getItem_code_d()
										+ pic.get(j).getItem_code_s() + pic.get(j).getItem_code_t();

								if (oldItem.equals(newItem)) {
									// 材质值
									HSSFRow row1 = sheet.getRow(me_row_str);
									HSSFCell cel1 = row1.getCell(pic_col_val);
									if (p.getValue() == 1) {
										cel1.setCellValue(pic.get(j).getVal1());
										pic_col_val++;
									} else if (p.getValue() == 2) {
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getVal1());
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getAvg_val());
										pic_col_val++;
									} else if (p.getValue() == 3) {
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getVal1());
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getVal2());
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getAvg_val());
										pic_col_val++;
									} else if (p.getValue() == 4) {
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getVal1());
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getVal2());
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getVal3());
										cel1 = row1.getCell(pic_col_val++);
										cel1.setCellValue(pic.get(j).getAvg_val());

									}

									break;
								}

								if (j == pic.size() - 1) {
									// 材质值
									HSSFRow row1 = sheet.getRow(me_row_str);
									HSSFCell cel1 = row1.getCell(pic_col_val);
									cel1.setCellValue("");
									pic_col_val++;
									break;
								}

							}

						}

					}

					// 卷号
					HSSFRow row2 = sheet.getRow(me_row_str);
					// 试样类型
					QcmJhySampleConsignM qm = this.getMapper(QcmJhySampleConsignMMapper.class).findById(key);
					HSSFCell cel2 = row2.getCell((StringFormat.getNumber(model.getPic_col_val()) - 1));
					if (qm != null) {
						cel2.setCellValue(qm.getSmp_type_name());
					} else {
						cel2.setCellValue("");
					}

					me_row_str++;

				}

				// 下一个卷的开始行
				// me_row_str++;
				ql_row_str = me_row_str;

				// 填充成分
				int chem_star = star_row;
				for (QcmZbsChemObject o : cic) {
					Integer cic_col_val = StringFormat.getNumber(model.getCic_col_val());

					// 填充成分值
					for (String ch : o.getChemValue()) {

						HSSFRow row2 = sheet.getRow(chem_star);
						HSSFCell cel2 = row2.getCell(cic_col_val);
						if (cic_col_val >= max_col) {
							chem_star++;
							cic_col_val = StringFormat.getNumber(model.getCic_col_val());
							cel2.setCellValue(ch);
						} else {
							cel2.setCellValue(ch);
							cic_col_val++;
						}

					}

				}

				// 填充物料
				TreeMap<String, Object> tree = cic.get(0).getHeat_info();
				Set<String> se = tree.keySet();

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

					for (String key : se) {
						if (chemConfigure.get(i).getItem_code().equals(key.toUpperCase())) {
							Object value = tree.get(key);
							// 获取单元格
							HSSFRow row1 = sheet.getRow(star_row);
							HSSFCell cell = row1
									.getCell(StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()));
							cell.setCellValue((String) value);

							// 合并行
							if (i != chemConfigure.size() - 1) {
								if ((StringFormat.getNumber(chemConfigure.get(i + 1).getItem_str_col_e())
										- 1) > StringFormat.getNumber((chemConfigure.get(i).getItem_str_col_e()))) {

									if (me_row_str - 1 > star_row) {
										sheet.addMergedRegion(new CellRangeAddress(star_row, me_row_str - 1,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												(StringFormat.getNumber(chemConfigure.get(i + 1).getItem_str_col_e())
														- 1)));

									} else {
										sheet.addMergedRegion(new CellRangeAddress(star_row, star_row,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												(StringFormat.getNumber(chemConfigure.get(i + 1).getItem_str_col_e())
														- 1)));
									}

								} else {

									if (me_row_str - 1 > star_row) {
										sheet.addMergedRegion(new CellRangeAddress(star_row, me_row_str - 1,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e())));

									}

								}

							} else {

								if ((StringFormat.getNumber(model.getCic_col_val()) - 1) >= StringFormat
										.getNumber((chemConfigure.get(i).getItem_str_col_e()))) {

									if (me_row_str - 1 > star_row) {
										sheet.addMergedRegion(new CellRangeAddress(star_row, me_row_str - 1,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												(StringFormat.getNumber(model.getCic_col_val()) - 1)));

									}

								} else {
									if (me_row_str - 1 > star_row) {
										sheet.addMergedRegion(new CellRangeAddress(star_row, me_row_str - 1,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e())));

									}

								}

							}

						}

					}

				}
			}

			Integer[] row = new Integer[2];
			row[0] = me_row_str;
			row[1] = ql_row_str;
			return row;

		} else {

			int meter_star = me_row_str;
			List<QcmZbsChemObject> cic = qcmZbsObject.getChemList();
			for (QcmZbsChemObject o : cic) {
				Integer cic_col_val = StringFormat.getNumber(model.getCic_col_val());

				int length = 1;
				// 填充成分值
				for (String ch : o.getChemValue()) {

					HSSFRow row2 = sheet.getRow(me_row_str);
					HSSFCell cel2 = row2.getCell(cic_col_val);
					if (cic_col_val >= max_col) {
						if (o.getChemValue().length == length) {

						} else {
							me_row_str++;
						}
						cic_col_val = StringFormat.getNumber(model.getCic_col_val());
						cel2.setCellValue(ch);
						length++;
					} else {
						cel2.setCellValue(ch);
						cic_col_val++;
						length++;
					}

				}

				// 行加一
				me_row_str++;

				// 填充试验区分
				HSSFRow row0 = sheet.getRow(meter_star);
				HSSFCell cel0 = row0.getCell(StringFormat.getNumber(model.getCic_col_val()) - 1);
				cel0.setCellValue(o.getAssay_type());
				if (me_row_str - 1 > meter_star) {
					sheet.addMergedRegion(new CellRangeAddress(meter_star, me_row_str - 1,
							StringFormat.getNumber(model.getCic_col_val()) - 1,
							StringFormat.getNumber(model.getCic_col_val()) - 1));

				}

				// 填充成分配置
				TreeMap<String, Object> tree = o.getHeat_info();
				Set<String> se = tree.keySet();
				for (int i = 0; i < chemConfigure.size(); i++) {

					for (String key : se) {
						if (chemConfigure.get(i).getItem_code().equals(key.toUpperCase())) {
							Object value = tree.get(key);
							// 获取单元格
							HSSFRow row1 = sheet.getRow(meter_star);
							HSSFCell cell = row1
									.getCell(StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()));
							cell.setCellValue((String) value);

							// 合并行
							if (i != chemConfigure.size() - 1) {
								if ((StringFormat.getNumber(chemConfigure.get(i + 1).getItem_str_col_e())
										- 1) > StringFormat.getNumber((chemConfigure.get(i).getItem_str_col_e()))) {

									if (me_row_str - 1 > meter_star) {
										sheet.addMergedRegion(new CellRangeAddress(meter_star, me_row_str - 1,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												(StringFormat.getNumber(chemConfigure.get(i + 1).getItem_str_col_e())
														- 1)));

									} else {
										sheet.addMergedRegion(new CellRangeAddress(meter_star, meter_star,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												(StringFormat.getNumber(chemConfigure.get(i + 1).getItem_str_col_e())
														- 1)));

									}

								} else {

									if (me_row_str - 1 > meter_star) {
										System.out.println(chemConfigure.get(i).getItem_str_col_e());
										System.out.println(meter_star);
										sheet.addMergedRegion(new CellRangeAddress(meter_star, me_row_str - 1,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e())));

									}

								}

							} else {

								if ((StringFormat.getNumber(model.getCic_col_val()) - 1) >= StringFormat
										.getNumber((chemConfigure.get(i).getItem_str_col_e()))) {

									if (me_row_str - 1 > meter_star) {
										sheet.addMergedRegion(new CellRangeAddress(meter_star, me_row_str - 1,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												(StringFormat.getNumber(model.getCic_col_val()) - 2)));

									} else {
										sheet.addMergedRegion(new CellRangeAddress(meter_star, meter_star,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												(StringFormat.getNumber(model.getCic_col_val()) - 2)));

									}

								} else {
									if (me_row_str - 1 > meter_star) {
										sheet.addMergedRegion(new CellRangeAddress(meter_star, me_row_str - 1,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e())));

									}

								}

							}

						}

					}

				}
				// 填充序号
				HSSFRow row1 = sheet.getRow(meter_star);
				HSSFCell cell = row1.getCell(0);
				cell.setCellValue(chem_seq++);

				// 合并单元格
				if (me_row_str - 1 > meter_star) {
					sheet.addMergedRegion(new CellRangeAddress(meter_star, me_row_str - 1, 0, 0));
				}

			}

			// key是代表试样号smp_no
			Set<String> qi = qltyItems.keySet();
			for (String key : qs) {
				Integer pic_col_val = StringFormat.getNumber(model.getPic_col_val());
				List<QcmZbsDataPic> pic = qcmZbsObject.getQltyMap().get(key);

				// k拿到所有检验大项代码

				for (String k : qi) {
					List<QcmZbsDataPic> pi = qltyItems.get(k);

					for (QcmZbsDataPic p : pi) {
						String oldItem = p.getPhy_code_l() + p.getPhy_code_m() + p.getPhy_code_s() + p.getItem_code_d()
								+ p.getItem_code_s() + p.getItem_code_t();
						for (int j = 0; j < pic.size(); j++) {
							String newItem = pic.get(j).getPhy_code_l() + pic.get(j).getPhy_code_m()
									+ pic.get(j).getPhy_code_s() + pic.get(j).getItem_code_d()
									+ pic.get(j).getItem_code_s() + pic.get(j).getItem_code_t();

							if (oldItem.equals(newItem)) {
								// 材质值
								HSSFRow row1 = sheet.getRow(ql_row_str);
								HSSFCell cel1 = row1.getCell(pic_col_val);
								if (p.getValue() == 1) {
									cel1.setCellValue(pic.get(j).getVal1());
									pic_col_val++;
								} else if (p.getValue() == 2) {
									cel1 = row1.getCell(pic_col_val++);
									cel1.setCellValue(pic.get(j).getVal1());
									cel1 = row1.getCell(pic_col_val++);
									cel1.setCellValue(pic.get(j).getAvg_val());
									pic_col_val++;
								} else if (p.getValue() == 3) {
									cel1 = row1.getCell(pic_col_val++);
									cel1.setCellValue(pic.get(j).getVal1());
									cel1 = row1.getCell(pic_col_val++);
									cel1.setCellValue(pic.get(j).getVal2());
									cel1 = row1.getCell(pic_col_val++);
									cel1.setCellValue(pic.get(j).getAvg_val());
									pic_col_val++;
								} else if (p.getValue() == 4) {
									cel1 = row1.getCell(pic_col_val++);
									cel1.setCellValue(pic.get(j).getVal1());
									cel1 = row1.getCell(pic_col_val++);
									cel1.setCellValue(pic.get(j).getVal2());
									cel1 = row1.getCell(pic_col_val++);
									cel1.setCellValue(pic.get(j).getVal3());
									cel1 = row1.getCell(pic_col_val++);
									cel1.setCellValue(pic.get(j).getAvg_val());

								}

								break;
							}

							if (j == pic.size() - 1) {
								// 材质值
								HSSFRow row1 = sheet.getRow(ql_row_str);
								HSSFCell cel1 = row1.getCell(pic_col_val);
								cel1.setCellValue("");
								pic_col_val++;
								break;
							}

						}

					}

				}

				/*
				 * for (QcmZbsDataPic p : pic) { //材质值 HSSFRow row1 =
				 * sheet.getRow(pic_row_val); HSSFCell cel1 =
				 * row1.getCell(pic_col_val); cel1.setCellValue(p.getVal1());
				 * pic_col_val++; }
				 */
				// 卷号
				HSSFRow row2 = sheet.getRow(ql_row_str);
				HSSFCell cel2 = row2.getCell(1);
				cel2.setCellValue(qcmZbsObject.getMaterial_no());
				// 试样类型
				QcmJhySampleConsignM qm = this.getMapper(QcmJhySampleConsignMMapper.class).findById(key);
				row2 = sheet.getRow(ql_row_str);
				cel2 = row2.getCell(2);

				if (qm != null) {
					cel2.setCellValue(qm.getSmp_type_name());
				} else {
					cel2.setCellValue("");
				}

				// 填充序号
				HSSFRow row1 = sheet.getRow(ql_row_str);
				HSSFCell cell = row1.getCell(0);
				cell.setCellValue(qlty_seq++);

				ql_row_str++;

			}

			Integer[] row = new Integer[4];
			row[0] = me_row_str;
			row[1] = ql_row_str;
			row[2] = chem_seq;
			row[3] = qlty_seq;
			return row;
		}

	}

	/**
	 * 根据design_key查询是否是附加要求订单
	 */

	public String getAddRequire(String design_key) {

		String str = null;

		if (design_key == null || "".equals(design_key)) {
			return "0";
		} else {
			int count = this.getMapper(QcmZbsBillMapper.class).findCount(design_key);
			if (count >= 1) {
				return "1";
			} else {
				return "0";
			}
		}

	}

	public ResultModel printBookTarget(CommonPage<QcmZbsDataOrd> sum) throws Exception {

		ResultModel rm = new ResultModel();

		try {

			// 获取订单信息
			QcmZbsDataOrd ord = sum.getObject();
			if (ord == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入需要打印的质保书");
				return rm;
			}
			// 获取模板信息
			String model_no = ord.getTmpl_no();
			if (model_no == null || "".equals(model_no)) {
				rm.setState("500");
				rm.setMsgInfo("请传入模板编号");
				return rm;
			}

			// 数据封装
			// 封装成分元素
			// 提取所有元素，保存元素代码和名称的顺序map
			TreeMap<String, String> allChem = new TreeMap<String, String>();
			// 注释项
			String notes = null;
			// 备注序号
			int re_req = 1;
			// 保存元素名数组
			String[] chem_name = null;
			chem_name = getAllChemName(allChem, chem_name, ord.getCret_no());

			// 模板配置数据
			QcmZbsTmpl model = this.getMapper(QcmZbsTmplMapper.class).findById(model_no);
			// 保存材质检验大项，以及向下的检验项目的set
			TreeMap<String, List<QcmZbsDataPic>> qltyItems = new TreeMap<>();
			// 获取材质表头信息
			List<QcmZbsDataMat> mats = new ArrayList<QcmZbsDataMat>();
			mats = this.getMapper(QcmZbsDataMatMapper.class).findWithCondition(null,
					"CRET_NO='" + ord.getCret_no() + "'", null, 0, 0);

			getQltyItems(qltyItems, ord.getCret_no(), mats);

			// 封装成分、材质数据
			// 质保书保存对象
			List<QcmZbsObject> targetBook = new ArrayList<>();
			createObject(targetBook, mats, ord, chem_name);

			// 填充成分、材质实绩
			int cic_row_val = StringFormat.getNumber(model.getCic_row_val());
			int pic_row_val = StringFormat.getNumber(model.getPic_row_val());
			// 获取最大批次号
			int max_num = StringFormat.getNumber(model.getMax_num());
			// 用于计数的成分和材质个数
			int count_chem = 0;
			int count_qlty = 0;

			InputStream is = new ByteArrayInputStream(model.getTmpl_cert());
			HSSFWorkbook wb = new HSSFWorkbook(is);
			// 创建HSSFSheet对象
			HSSFSheet sheet = wb.getSheetAt(0);

			// 如果成分、材质处于同一行
			if (cic_row_val == pic_row_val) {

			} else {
				// 获取成分可以放的最大行数
				int max_chem = pic_row_val - cic_row_val;
				// 成分元素填充所占行数
				int chem_num = chem_name.length - sheet.getRow(0).getPhysicalNumberOfCells() - 1;
				int chem_row = 0;
				if (chem_name.length % chem_num == 0) {
					chem_row = chem_name.length / chem_num;
				} else {
					chem_row = chem_name.length / chem_num + 1;
				}
				// 成分可以填充的最大卷数
				if (max_chem % chem_row == 0) {
					max_chem = max_chem / chem_row;
				} else {
					max_chem = max_chem / chem_row + 1;
				}

				// 填充质保书分页逻辑
				for (int i = 0; i < targetBook.size(); i++) {
					count_chem = count_chem + targetBook.get(i).getChem_size();
					count_qlty = count_qlty + targetBook.get(i).getQlty_size();

					List<QcmZbsChemObject> chemList = targetBook.get(i).getChemList();
					TreeMap<String, List<QcmZbsDataPic>> qltyMap = targetBook.get(i).getQltyMap();

					if (i == 0) {

						if (count_chem <= max_chem && count_qlty <= max_num) {
							// 填充数据

							if (i == targetBook.size() - 1) {
								// 关闭流、刷新
							} else {
								if (count_chem + targetBook.get(i + 1).getChem_size() + 1 > max_chem
										&& count_qlty + targetBook.get(i + 1).getQlty_size() > max_num) {
									// 关闭流、刷新
									count_chem = 0;
									count_qlty = 0;
								} else {

									continue;
								}
							}

						} else if (count_chem <= max_chem && count_qlty > max_num) {

							int page = 0;
							if (count_qlty % max_num == 0) {
								page = count_qlty / max_num;
							} else {
								page = count_qlty / max_num + 1;
							}

							for (int j = 0; j < page; j++) {

							}

						} else if (count_chem > max_chem && count_qlty <= max_num) {

						} else if (count_chem > max_chem && count_qlty > max_num) {

						}

						continue;
					}

					if (i != targetBook.size() - 1) {

						if (count_chem <= max_chem && count_qlty <= max_num) {
							// 填充数据
							if (count_chem + targetBook.get(i + 1).getChem_size() + 1 > max_chem
									&& count_qlty + targetBook.get(i + 1).getQlty_size() > max_num) {
								// 关闭流、刷新
								count_chem = 0;
								count_qlty = 0;
							} else {

								continue;
							}

						} else if (count_chem <= max_chem && count_qlty > max_num) {

						} else if (count_chem > max_chem && count_qlty <= max_num) {

						} else if (count_chem > max_chem && count_qlty > max_num) {

						}

					}

					if (i == targetBook.size() - 1) {

						if (count_chem <= max_chem && count_qlty <= max_num) {
							// 填充数据、关闭流

						} else if (count_chem <= max_chem && count_qlty > max_num) {

						} else if (count_chem > max_chem && count_qlty <= max_num) {

						} else if (count_chem > max_chem && count_qlty > max_num) {

						}

					}

				}

			}

		} catch (Exception e) {

		}

		return rm;

	}

	private Integer[] addExcel2(HSSFSheet sheet, QcmZbsTmpl model, TreeMap<String, List<QcmZbsDataPic>> qltyItems,
			QcmZbsObject qcmZbsObject, Integer me_row_str, Integer ql_row_str, Integer chem_seq, Integer qlty_seq,
			String val) throws Exception {

		Integer max_col = null;

		if ("1".equals(val)) {
			max_col = StringFormat.getNumber(model.getPic_col_val()) - 2;
		} else {
			max_col = sheet.getRow(me_row_str).getPhysicalNumberOfCells() - 1;
		}

		// 获取物料配置项
		List<QcmZbsTmplItem> chemConfigure = this.getMapper(QcmZbsTmplItemMapper.class).findWithCondition(null,
				"TMPL_NO='" + model.getTmpl_no() + "' and DIV_TYPE='1'", "ITEM_STR_ROW,to_number(ITEM_STR_COL)", 0, 0);

		// 获取成分配置中的卷号的填充列
		int coil_col = 1;

		if ("1".equals(val)) {
		} else {

			// 获取物料开始行
			int meter_star = me_row_str;
			List<QcmZbsChemObject> cic = qcmZbsObject.getChemList();
			for (QcmZbsChemObject o : cic) {
				Integer cic_col_val = StringFormat.getNumber(model.getCic_col_val());

				int length = 1;
				// 填充成分值
				for (String ch : o.getChemValue()) {

					HSSFRow row2 = sheet.getRow(me_row_str);
					HSSFCell cel2 = row2.getCell(cic_col_val);
					if (cic_col_val >= max_col) {
						if (o.getChemValue().length == length) {

						} else {
							me_row_str++;
						}
						cic_col_val = StringFormat.getNumber(model.getCic_col_val());
						cel2.setCellValue(ch);
						length++;
					} else {
						cel2.setCellValue(ch);
						cic_col_val++;
						length++;
					}

				}

				// 行加一
				me_row_str++;

				// 填充试验区分
				HSSFRow row0 = sheet.getRow(meter_star);
				HSSFCell cel0 = row0.getCell(StringFormat.getNumber(model.getCic_col_val()) - 1);
				cel0.setCellValue(o.getAssay_type());
				if (me_row_str - 1 > meter_star) {
					sheet.addMergedRegion(new CellRangeAddress(meter_star, me_row_str - 1,
							StringFormat.getNumber(model.getCic_col_val()) - 1,
							StringFormat.getNumber(model.getCic_col_val()) - 1));

				}

				// 填充成分配置
				TreeMap<String, Object> tree = o.getHeat_info();
				Set<String> se = tree.keySet();
				for (int i = 0; i < chemConfigure.size(); i++) {
					// 如果为当前配置是卷号或者轧批号
					if ("BATCH_NO".equals(chemConfigure.get(i).getItem_code())
							|| "MATERIAL_NO".equals(chemConfigure.get(i).getItem_code())) {
						coil_col = StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e());
					}

					for (String key : se) {
						if (chemConfigure.get(i).getItem_code().equals(key.toUpperCase())) {
							Object value = tree.get(key);
							// 获取单元格
							HSSFRow row1 = sheet.getRow(meter_star);
							HSSFCell cell = row1
									.getCell(StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()));
							cell.setCellValue((String) value);

							// 合并行
							if (i != chemConfigure.size() - 1) {
								if ((StringFormat.getNumber(chemConfigure.get(i + 1).getItem_str_col_e())
										- 1) > StringFormat.getNumber((chemConfigure.get(i).getItem_str_col_e()))) {

									if (me_row_str - 1 > meter_star) {
										sheet.addMergedRegion(new CellRangeAddress(meter_star, me_row_str - 1,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												(StringFormat.getNumber(chemConfigure.get(i + 1).getItem_str_col_e())
														- 1)));

									} else {
										sheet.addMergedRegion(new CellRangeAddress(meter_star, meter_star,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												(StringFormat.getNumber(chemConfigure.get(i + 1).getItem_str_col_e())
														- 1)));

									}

								} else {

									if (me_row_str - 1 > meter_star) {
										System.out.println(chemConfigure.get(i).getItem_str_col_e());
										System.out.println(meter_star);
										sheet.addMergedRegion(new CellRangeAddress(meter_star, me_row_str - 1,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e())));

									}

								}

							} else {

								if ((StringFormat.getNumber(model.getCic_col_val()) - 1) >= StringFormat
										.getNumber((chemConfigure.get(i).getItem_str_col_e()))) {

									if (me_row_str - 1 > meter_star) {
										sheet.addMergedRegion(new CellRangeAddress(meter_star, me_row_str - 1,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												(StringFormat.getNumber(model.getCic_col_val()) - 2)));

									} else {
										sheet.addMergedRegion(new CellRangeAddress(meter_star, meter_star,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												(StringFormat.getNumber(model.getCic_col_val()) - 2)));

									}

								} else {
									if (me_row_str - 1 > meter_star) {
										sheet.addMergedRegion(new CellRangeAddress(meter_star, me_row_str - 1,
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e()),
												StringFormat.getNumber(chemConfigure.get(i).getItem_str_col_e())));

									}

								}

							}

						}

					}

				}
				// 填充序号
				HSSFRow row1 = sheet.getRow(meter_star);
				HSSFCell cell = row1.getCell(0);
				cell.setCellValue(chem_seq++);

				// 合并单元格
				if (me_row_str - 1 > meter_star) {
					sheet.addMergedRegion(new CellRangeAddress(meter_star, me_row_str - 1, 0, 0));
				}

			}

			// 获取材质值填充开始行列
			int max_row_qlty = 0;
			Integer pic_row_val = ql_row_str;
			Integer pic_col_val = StringFormat.getNumber(model.getPic_col_val());
			// 填充材质
			List<QcmZbsSampleNo> sample_nos = qcmZbsObject.getSmples();
			// 获取表头信息
			Set<String> qltyHead = qltyItems.keySet();
			// excel表格
			HSSFRow row = sheet.getRow(ql_row_str);
			HSSFCell cel = row.getCell(pic_col_val);
			for (QcmZbsSampleNo smp : sample_nos) {
				// 获取试样号向下的检验大项
				List<QcmZbsBigItem> bigItems = smp.getBigItems();
				for (String key : qltyHead) {
					// 表头检验大项向下的项目
					List<QcmZbsDataPic> titleItem = qltyItems.get(key);
					for (int i = 0; i < bigItems.size(); i++) {
						if (key.equals(bigItems.get(i).getPhy_code_l())) {
							// 拿到检验大项向下的方向、温度、尺寸
							TreeMap<String, List<QcmZbsDataPic>> ds = bigItems.get(i).getDirQltyItems();
							Set<String> set = ds.keySet();
							// 遍历方向温度尺寸
							for (String se : set) {
								row = sheet.getRow(ql_row_str);

								List<QcmZbsDataPic> dirItems = ds.get(se);
								String[] dir = se.split("_");
								// 根据材质表头信息填充项目

								for (int j = 0; j < titleItem.size(); j++) {
									cel = row.getCell(pic_col_val);
									if ("I01".equals(titleItem.get(j).getPhy_code_s())) {
										if ("null".equals(dir[0])) {
											cel.setCellValue("");
										} else {
											cel.setCellValue(dir[0]);
										}
										pic_col_val++;
										continue;
									} else if ("I02".equals(titleItem.get(j).getPhy_code_s())) {
										if ("null".equals(dir[1])) {
											cel.setCellValue("");
										} else {
											cel.setCellValue(dir[1]);
										}
										pic_col_val++;
										continue;
									} else if ("I03".equals(titleItem.get(j).getPhy_code_s())) {
										if ("null".equals(dir[2])) {
											cel.setCellValue("");
										} else {
											cel.setCellValue(dir[2]);
										}
										pic_col_val++;
										continue;
									} else {
										String[] values = titleItem.get(j).getResults();
										for (int k = 0; k < dirItems.size(); k++) {
											if (titleItem.get(j).getPhy_code_s()
													.equals(dirItems.get(k).getPhy_code_s())) {
												// 判断是否为冲击试验
												double math = 1.0;
												String peiZhi_Code = this.getBean(ComBaseConfigServiceImpl.class)
														.getConfig("ZBS0011");
												if ("1".equals(peiZhi_Code)) {
													if ("R0004".equals(dirItems.get(k).getPhy_code_l())) {
														if (dirItems.get(k).getItem_code_s() != null
																&& !"".equals(dirItems.get(k).getItem_code_s())) {
															ComBasePhyItem items = this
																	.getMapper(ComBasePhyItemMapper.class)
																	.findById(dirItems.get(k).getItem_code_s());
															if (items != null && items.getItem_name() != null
																	&& !"".equals(items.getItem_name())) {
																String[] size = items.getItem_name().split("\\*");
																if (size[1] != null && !"".equals(size[1])) {
																	math = Double.valueOf(size[1]);
																	math = math / 10;

																}
															}

														}

													}

												}
												// 格式化值
												DecimalFormat deFormat = new DecimalFormat("0.00");
												for (String v : values) {

													if (math != 1.0) {

														if ("1".equals(v)) {
															if (dirItems.get(k).getVal1() != null
																	&& !"".equals(dirItems.get(k).getVal1())) {
																double value = Double.valueOf(dirItems.get(k).getVal1())
																		/ math;
																cel.setCellValue(deFormat.format(value));
															} else {
																cel.setCellValue(dirItems.get(k).getVal1());
															}

															pic_col_val++;
														}
														if ("2".equals(v)) {
															if (dirItems.get(k).getVal2() != null
																	&& !"".equals(dirItems.get(k).getVal2())) {
																double value = Double.valueOf(dirItems.get(k).getVal2())
																		/ math;
																cel.setCellValue(deFormat.format(value));
															} else {
																cel.setCellValue(dirItems.get(k).getVal2());
															}
															pic_col_val++;
														}
														if ("3".equals(v)) {
															if (dirItems.get(k).getVal3() != null
																	&& !"".equals(dirItems.get(k).getVal3())) {
																double value = Double.valueOf(dirItems.get(k).getVal3())
																		/ math;
																cel.setCellValue(deFormat.format(value));
															} else {
																cel.setCellValue(dirItems.get(k).getVal3());
															}
															pic_col_val++;
														}
														if ("4".equals(v)) {

															if (dirItems.get(k).getAvg_val() != null
																	&& !"".equals(dirItems.get(k).getAvg_val())) {
																double value = Double
																		.valueOf(dirItems.get(k).getAvg_val()) / math;
																cel.setCellValue(deFormat.format(value));
															} else {
																cel.setCellValue(dirItems.get(k).getAvg_val());
															}
															pic_col_val++;
														}

													} else {
														if ("1".equals(v)) {
															cel.setCellValue(dirItems.get(k).getVal1());
															pic_col_val++;
														}
														if ("2".equals(v)) {
															cel.setCellValue(dirItems.get(k).getVal2());
															pic_col_val++;
														}
														if ("3".equals(v)) {
															cel.setCellValue(dirItems.get(k).getVal3());
															pic_col_val++;
														}
														if ("4".equals(v)) {
															cel.setCellValue(dirItems.get(k).getAvg_val());
															pic_col_val++;
														}
													}

												}

												break;
											}

											if (k == dirItems.size() - 1) {
												for (String v : values) {
													if ("1".equals(v)) {
														cel.setCellValue("");
														pic_col_val++;
													}
													if ("2".equals(v)) {
														cel.setCellValue("");
														pic_col_val++;
													}
													if ("3".equals(v)) {
														cel.setCellValue("");
														pic_col_val++;
													}
													if ("4".equals(v)) {
														cel.setCellValue("");
														pic_col_val++;
													}

												}

											}

										}

									}
									// 行加1
									ql_row_str++;
									pic_col_val = StringFormat.getNumber(model.getPic_col_val());
								}
							}

							if (i == bigItems.size() - 1) {
								// 直接根据材质表头信息填充空数据
								for (QcmZbsDataPic item : titleItem) {
									String[] values = item.getResults();
									for (String v : values) {
										if ("1".equals(v)) {
											cel.setCellValue("");
											pic_col_val++;
										}
										if ("2".equals(v)) {
											cel.setCellValue("");
											pic_col_val++;
										}
										if ("3".equals(v)) {
											cel.setCellValue("");
											pic_col_val++;
										}
										if ("4".equals(v)) {
											cel.setCellValue("");
											pic_col_val++;
										}

									}
								}

							}
						}

					}

				}

				Integer[] rm = new Integer[4];
				rm[0] = me_row_str;
				rm[1] = ql_row_str;
				rm[2] = chem_seq;
				rm[3] = qlty_seq;
				// 填充材质序号、卷号以及试样类型等，并且合并单元格
				// 填充序号
				row = sheet.getRow(ql_row_str);
				cel = row.getCell(0);
				cel.setCellValue(qlty_seq);
				// 合并行列

				// 填充
				cel = row.getCell(coil_col);
				// 卷号
				HSSFRow row2 = sheet.getRow(ql_row_str);
				HSSFCell cel2 = row2.getCell(1);
				if ("1".equals(qcmZbsObject.getIs_batch())) {
					cel.setCellValue(qcmZbsObject.getBatch_no());
				} else {
					cel.setCellValue(qcmZbsObject.getMaterial_no());
				}

				return rm;

			}

		}
		return null;

	}
	
	
	public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is = QcmZbsBillServiceImpl.class.getClassLoader().getResourceAsStream("\\license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
	

}
