/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-02-24
 * 描述            ：质保书模板配置表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Qms.qcm.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.common.ExcelHelper;
import market.Qms.qcm.mapper.QcmZbsTmplItemMapper;
import market.Qms.qcm.mapper.QcmZbsTmplMapper;
import market.Qms.qcm.service.QcmZbsTmplService;
import market.Qms.qcm.vo.QcmZbsDataOrd;
import market.Qms.qcm.vo.QcmZbsTmpl;
import market.Qms.qcm.vo.QcmZbsTmplItem;

import org.apache.commons.fileupload.FileItem;
import org.springframework.transaction.annotation.Transactional;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path = "QcmZbsTmplService")
public class QcmZbsTmplServiceImpl extends BaseServiceImpl<QcmZbsTmpl, Object> implements QcmZbsTmplService {

	public void doImport3() throws Exception {

		InputStream in = new FileInputStream(new File("E:\\通用普通模板1.xls"));
		byte[] data = toByteArray(in);
		QcmZbsTmpl tg = new QcmZbsTmpl();
		tg.setTmpl_no("B0001");
		// tg.set
		tg.setTmpl_cert(data);
		this.getMapper(QcmZbsTmplMapper.class).update(tg);

	}
	
	
	public byte[] toByteArray(InputStream in) throws IOException {

		ByteArrayOutputStream out = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024 * 4];
		int n = 0;
		while ((n = in.read(buffer)) != -1) {
			out.write(buffer, 0, n);
		}
		return out.toByteArray();
	}
	
	/**
	 * 导入excel
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doImport2() throws Exception {

		ResultModel resultModel = new ResultModel();
		synchronized (this) {
			
			String userName = this.getSession().getAttribute(HomeService.SESSION_USER_NAME).toString();
			String rm = "";
			String contract_pk = null;
			// 获取文件输入流
			// Workbook wb = null;
			InputStream in = null;
			URL url = this.getClass().getClassLoader().getResource("/");
			// 替换路径中的空格
			String pathFile = url.getPath().replaceAll("%20", " ");
			File file = new File(pathFile);

			try {

				List<FileItem> items = ExcelHelper.getUpload(QcmZbsTmplServiceImpl.this, file, getRequest());
				QcmZbsTmpl tg = new QcmZbsTmpl();
				// 遍历 items:
				String fileName = null;
				for (FileItem item : items) {
					// 若是文件域则读取文件流
					if (item.isFormField()) {
						// contract_pk=item.getString("utf-8");
						String filedName = item.getFieldName();// 普通输入项数据的名
						// 解决普通输入项的数据的中文乱码问题
						String filedValue = item.getString("UTF-8");// 普通输入项的值
						//System.out.println("普通字段:" + filedName + "==" + filedValue);
						if ("tmpl_desc".equals(filedName)) {
							tg.setTmpl_desc(filedValue);

						} else if ("prod_type_code_l".equals(filedName)) {
							tg.setProd_type_code_l(filedValue);

						} else if ("prod_type_name_l".equals(filedName)) {

							tg.setProd_type_name_l(filedValue);
						}else if("prod_code".equals(filedName)) {
							tg.setProd_code(filedValue);
						}else if ("prod_name".equals(filedName)) {
							tg.setProd_name(filedValue);
						}else if ("steel_code".equals(filedName)) {
							tg.setSteel_code(filedValue);
						}else if ("steel_name".equals(filedName)) {
							tg.setSteel_name(filedValue);
						}else if ("std_code".equals(filedName)) {
							tg.setStd_code(filedValue);
						}else if ("std_name".equals(filedName)) {
							tg.setStd_name(filedValue);
						}else if ("customer_no".equals(filedName)) {
							tg.setCustomer_no(filedValue);
						}else if ("customer_name".equals(filedName)) {
							tg.setCustomer_name(filedValue);
						}
						else if ("tmpl_type".equals(filedName)) {

							tg.setTmpl_desc(filedValue);
						} else if ("ext_name".equals(filedName)) {

							tg.setExt_name(filedValue);
						} else if ("head_str_row".equals(filedName)) {

							tg.setHead_str_row(filedValue);
						} else if ("head_end_row".equals(filedName)) {

							tg.setHead_end_row(filedValue);
						} else if ("max_num".equals(filedName)) {

							tg.setMax_num(filedValue);
						} else if ("row_batch".equals(filedName)) {

							tg.setRow_batch(filedValue);
						} else if ("col_batch".equals(filedName)) {
							tg.setCol_batch(filedValue);
						}
						else if ("cic_row_title".equals(filedName)) {
							tg.setCic_row_title(filedValue);
						} else if ("cic_col_title".equals(filedName)) {

							tg.setCic_col_title(filedValue);
						} else if ("cic_row_val".equals(filedName)) {

							tg.setCic_row_val(filedValue);
						} else if ("cic_col_val".equals(filedName)) {

							tg.setCic_col_val(filedValue);

						} else if ("pic_row_title".equals(filedName)) {
							tg.setPic_row_title(filedValue);
						}else if ("pic_col_title".equals(filedName)) {
							tg.setPic_col_title(filedValue);
						}else if ("pic_row_val".equals(filedName)) {

							tg.setPic_row_val(filedValue);
						} else if ("pic_col_val".equals(filedName)) {

							tg.setPic_col_val(filedValue);
						}else if ("pic_row_item_s".equals(filedName)) {
							tg.setPic_row_item_s(filedValue);
						}
						else if ("pic_col_item_l".equals(filedName)) {

							tg.setPic_col_item_l(filedValue);
						} else if ("pic_row_item_l".equals(filedName)) {

							tg.setPic_row_item_l(filedValue);
						} else if ("pic_col_item_s".equals(filedName)) {

							tg.setPic_col_item_s(filedValue);
						} else if ("pic_row_item_u".equals(filedName)) {
							tg.setPic_row_item_u(filedValue);
						}else if ("pic_col_item_u".equals(filedName)) {
							tg.setPic_col_item_u(filedValue);
						}else if ("img_1_col".equals(filedName)) {

							tg.setImg_1_col(filedValue);
						} else if ("img_1_row".equals(filedName)) {

							tg.setImg_1_row(filedValue);
						} else if ("img_2_col".equals(filedName)) {

							tg.setImg_2_col(filedValue);
						} else if ("img_2_row".equals(filedName)) {

							tg.setImg_2_row(filedValue);
						} else if ("img_3_col".equals(filedName)) {

							tg.setImg_3_col(filedValue);
						} else if ("img_3_row".equals(filedName)) {

							tg.setImg_3_row(filedValue);
						} else if ("sig_row".equals(filedName)) {

							tg.setSig_row(filedValue);
						} else if ("sig_col".equals(filedName)) {

							tg.setSig_col(filedValue);
						}else if ("img_seal".equals(filedName)) {

							tg.setImg_seal(filedValue.getBytes());
						}else if ("img_sig".equals(filedName)) {

							tg.setImg_sig(filedValue.getBytes());
						}else if ("e_name".equals(filedName)) {
							tg.setE_name(filedValue);
						}else if ("e_row_str".equals(filedName)) {
							tg.setE_col_str(filedValue);
						}else if ("memo_row".equals(filedName)) {
							tg.setMemo_row(filedValue);
						}else if ("memo_col".equals(filedName)) {
							tg.setMemo_col(filedValue);
						}else if ("remark_row".equals(filedName)) {
							tg.setRemark_row(filedValue);
						}else if ("remark_col".equals(filedName)) {
							tg.setRemark_col(filedValue);
						}else if ("fill_id_col".equals(filedName)) {
							tg.setFill_id_col(filedValue);
						}else if ("fill_id_row".equals(filedName)) {
							tg.setFill_id_row(filedValue);
						}else if ("memo".equals(filedName)) {
							tg.setMemo(filedValue);
						}else if ("e_row_str".equals(filedName)) {
							tg.setE_row_str(filedValue);
						}else if ("e_col_str".equals(filedName)) {
							tg.setE_col_str(filedValue);
						}else if ("e_row_end".equals(filedName)) {
							tg.setE_row_end(filedValue);
						}else if ("e_col_end".equals(filedName)) {
							tg.setE_col_end(filedValue);
						}else if ("max_item".equals(filedName)) {
							tg.setMax_item(filedValue);
						}else if ("img_3_col".equals(filedName)) {
							tg.setImg_3_col(filedValue);
						}else if ("img_3_row".equals(filedName)) {
							tg.setImg_3_row(filedValue);
						}else if ("max_item".equals(filedName)) {
							tg.setMax_item(filedValue);
						}

					} else {
						//System.out.println("流开始");
						in = item.getInputStream();
						byte[] data = toByteArray(in);
						// System.out.println(data.length);
						tg.setTmpl_cert(data);
						// 获取文件名
						fileName = item.getName();
						// 解决fileName兼容性问题
						int lastindex = fileName.lastIndexOf("\\");
						fileName = fileName.substring(lastindex + 1);
						int lastNameIndex = fileName.lastIndexOf(".");
						String lastName = fileName.substring(lastNameIndex + 1);
						System.out.println(lastName);
						if (!"xls".equals(lastName) && !"xlsx".equals(lastName)) {
							resultModel.setState("500");
							resultModel.setMsgInfo("请传入excel文件");
							;
							return resultModel;
						}
						tg.setIs_tmpl("1");
						tg.setExt_name("." + lastName);
					}
				}

				int count = this.getBean(QcmJhySampleConsignDItemServiceImpl.class).GetMaxSerialNo("QCM_ZBS_TMPL",
						"TMPL_NO", "B");
				String tmpl_no = "B" + String.format("%05d", count + 1);
				tg.setTmpl_no(tmpl_no);
				tg.setExt_name(userName);
				tg.setCreate_time(new Date());
				tg.setValidflag("1");
				this.getMapper(QcmZbsTmplMapper.class).insert(tg);
				
				//自动新增表头项与物料信息 :0表头 1物料
				ResultModel r1=this.getBean(QcmZbsTmplItemServiceImpl.class).findTitle("1"); 
			    List<QcmZbsDataOrd> ords1= (List<QcmZbsDataOrd>) r1.getData();	
			    ResultModel r0=this.getBean(QcmZbsTmplItemServiceImpl.class).findTitle("0"); 
			    List<QcmZbsDataOrd> ords0= (List<QcmZbsDataOrd>) r0.getData();
			    
			 	for (QcmZbsDataOrd o0 : ords0) {
			 		QcmZbsTmplItem item=new QcmZbsTmplItem();
			 		item.setDiv_type("0");
			 		item.setTmpl_no(tmpl_no);
			 		item.setItem_code(o0.getNames());
			 		item.setCreate_name(userName);
			 		item.setCreate_time(new Date());
			 		item.setItem_name(o0.getNotes());
			 		this.getMapper(QcmZbsTmplItemMapper.class).insert(item);
					
				}
			 	
			 	for (QcmZbsDataOrd o1 : ords1) {
			 		QcmZbsTmplItem item=new QcmZbsTmplItem();
			 		item.setDiv_type("1");
			 		item.setTmpl_no(tmpl_no);
			 		item.setCreate_name(userName);
			 		item.setCreate_time(new Date());
			 		item.setItem_code(o1.getNames());
			 		item.setItem_name(o1.getNotes());
			 		this.getMapper(QcmZbsTmplItemMapper.class).insert(item);
					
				}
				
				
				resultModel.setData("200");
				resultModel.setMsgInfo("模板数据添加成功");

			} catch (Exception e) {
				resultModel.setData("500");
				//e.printStackTrace();
				resultModel.setMsgInfo("行列只能输入两个字符，且不能为空，请确认！");
				return resultModel;
			}
		}

		return resultModel;

	}
	
	
	/**
	 * 根据模板名称查询模板信息
	 */
	public ResultModel findByTmplDesc(CommonPage<QcmZbsTmpl> oci) {

		ResultModel rm = new ResultModel();
		try {
			QcmZbsTmpl tmpl = oci.getObject();
			if (tmpl == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入参数");
				return rm;
			}
			String condition = "1=1 ";
			if (tmpl.getTmpl_desc() != null && !"".equals(tmpl.getTmpl_desc())) {
				condition += " and TMPL_DESC='" + tmpl.getTmpl_desc() + "'";
			}

			if (tmpl.getValidflag() != null && !"".equals(tmpl.getValidflag())) {
				condition += " and VALIDFLAG='" + tmpl.getValidflag() + "'";
			}
			List<QcmZbsTmpl> tmpls = this.getMapper(QcmZbsTmplMapper.class).findWithCondition(null, condition, "TMPL_NO", 0,
					0);
			rm.setState("200");
			rm.setData(tmpls);

		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败");
			return rm;
		}
		return rm;
	}
	
	
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel AbolishmentAndRestoration(CommonPage<QcmZbsTmpl> oci) throws Exception {

		ResultModel rm = new ResultModel();
		try {
			String userName = this.getSession().getAttribute(HomeService.SESSION_USER_NAME).toString();
			
			if (oci == null) {
				rm.setMsgInfo("请传入参数");
				rm.setState("500");
				return rm;
			}

			QcmZbsTmpl tmpl = oci.getObject();
			if (tmpl == null) {
				rm.setMsgInfo("请传入模板对象");
				rm.setState("500");
				return rm;
			}

			if (tmpl.getTmpl_no() == null || "".equals(tmpl.getTmpl_no())) {
				rm.setMsgInfo("请传入模板号");
				rm.setState("500");
				return rm;
			}

			if ("1".equals(oci.getObject().getValidflag())) {
				//this.getMapper(QcmZbsTmplMapper.class).updateTime(tmpl.getTmpl_no());
				this.getMapper(QcmZbsTmplMapper.class).update(tmpl);
				rm.setMsgInfo("恢复成功");
				rm.setState("200");
			}

			if ("0".equals(oci.getObject().getValidflag())) {
				oci.getObject().setDelete_name(userName);
				oci.getObject().setDelete_time(new Date());
				this.getMapper(QcmZbsTmplMapper.class).update(tmpl);
				rm.setMsgInfo("作废成功");
				rm.setState("200");
			}

		} catch (Exception e) {
			throw new Exception("作废或恢复失败！");
		}
		return rm;

	}
	
	
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doUpdate() throws Exception {

		ResultModel resultModel = new ResultModel();
		synchronized (this) {
			
			String userName = this.getSession().getAttribute(HomeService.SESSION_USER_NAME).toString();
			String rm = "";
			String contract_pk = null;
			// 获取文件输入流
			// Workbook wb = null;
			InputStream in = null;
			URL url = this.getClass().getClassLoader().getResource("/");
			// 替换路径中的空格
			String pathFile = url.getPath().replaceAll("%20", " ");
			File file = new File(pathFile);

			try {

				List<FileItem> items = ExcelHelper.getUpload(QcmZbsTmplServiceImpl.this, file, getRequest());
				QcmZbsTmpl tg = new QcmZbsTmpl();
				// 遍历 items:
				String fileName = null;
				for (FileItem item : items) {
					// 若是文件域则读取文件流
					if (item.isFormField()) {
						// contract_pk=item.getString("utf-8");
						String filedName = item.getFieldName();// 普通输入项数据的名
						// 解决普通输入项的数据的中文乱码问题
						String filedValue = item.getString("UTF-8");// 普通输入项的值
						//System.out.println("普通字段:" + filedName + "==" + filedValue);
						if ("tmpl_no".equals(filedName)) {
							tg.setTmpl_no(filedValue);

						}
						if ("tmpl_desc".equals(filedName)) {
							tg.setTmpl_desc(filedValue);

						} else if ("prod_type_code_l".equals(filedName)) {
							tg.setProd_type_code_l(filedValue);

						} else if ("prod_type_name_l".equals(filedName)) {

							tg.setProd_type_name_l(filedValue);
						}else if("prod_code".equals(filedName)) {
							tg.setProd_code(filedValue);
						}else if ("prod_name".equals(filedName)) {
							tg.setProd_name(filedValue);
						}else if ("steel_code".equals(filedName)) {
							tg.setSteel_code(filedValue);
						}else if ("steel_name".equals(filedName)) {
							tg.setSteel_name(filedValue);
						}else if ("std_code".equals(filedName)) {
							tg.setStd_code(filedValue);
						}else if ("std_name".equals(filedName)) {
							tg.setStd_name(filedValue);
						}else if ("customer_no".equals(filedName)) {
							tg.setCustomer_no(filedValue);
						}else if ("customer_name".equals(filedName)) {
							tg.setCustomer_name(filedValue);
						}
						else if ("tmpl_type".equals(filedName)) {

							tg.setTmpl_desc(filedValue);
						} else if ("ext_name".equals(filedName)) {

							tg.setExt_name(filedValue);
						} else if ("head_str_row".equals(filedName)) {

							tg.setHead_str_row(filedValue);
						} else if ("head_end_row".equals(filedName)) {

							tg.setHead_end_row(filedValue);
						} else if ("max_num".equals(filedName)) {

							tg.setMax_num(filedValue);
						} else if ("row_batch".equals(filedName)) {

							tg.setRow_batch(filedValue);
						} else if ("col_batch".equals(filedName)) {
							tg.setCol_batch(filedValue);
						}
						else if ("cic_row_title".equals(filedName)) {
							tg.setCic_row_title(filedValue);
						} else if ("cic_col_title".equals(filedName)) {

							tg.setCic_col_title(filedValue);
						} else if ("cic_row_val".equals(filedName)) {

							tg.setCic_row_val(filedValue);
						} else if ("cic_col_val".equals(filedName)) {

							tg.setCic_col_val(filedValue);

						} else if ("pic_row_title".equals(filedName)) {
							tg.setPic_row_title(filedValue);
						}else if ("pic_col_title".equals(filedName)) {
							tg.setPic_col_title(filedValue);
						}else if ("pic_row_val".equals(filedName)) {

							tg.setPic_row_val(filedValue);
						} else if ("pic_col_val".equals(filedName)) {

							tg.setPic_col_val(filedValue);
						}else if ("pic_row_item_s".equals(filedName)) {
							tg.setPic_row_item_s(filedValue);
						}
						else if ("pic_col_item_l".equals(filedName)) {

							tg.setPic_col_item_l(filedValue);
						} else if ("pic_row_item_l".equals(filedName)) {

							tg.setPic_row_item_l(filedValue);
						} else if ("pic_col_item_s".equals(filedName)) {

							tg.setPic_col_item_s(filedValue);
						} else if ("pic_row_item_u".equals(filedName)) {
							tg.setPic_row_item_u(filedValue);
						}else if ("pic_col_item_u".equals(filedName)) {
							tg.setPic_col_item_u(filedValue);
						}else if ("img_1_col".equals(filedName)) {

							tg.setImg_1_col(filedValue);
						} else if ("img_1_row".equals(filedName)) {

							tg.setImg_1_row(filedValue);
						} else if ("img_2_col".equals(filedName)) {

							tg.setImg_2_col(filedValue);
						} else if ("img_2_row".equals(filedName)) {

							tg.setImg_2_row(filedValue);
						} else if ("img_3_col".equals(filedName)) {

							tg.setImg_3_col(filedValue);
						} else if ("img_3_row".equals(filedName)) {

							tg.setImg_3_row(filedValue);
						} else if ("sig_row".equals(filedName)) {

							tg.setSig_row(filedValue);
						} else if ("sig_col".equals(filedName)) {

							tg.setSig_col(filedValue);
						}else if ("img_seal".equals(filedName)) {

							tg.setImg_seal(filedValue.getBytes());
						}else if ("img_sig".equals(filedName)) {

							tg.setImg_sig(filedValue.getBytes());
						}else if ("e_name".equals(filedName)) {
							tg.setE_name(filedValue);
						}else if ("e_row_str".equals(filedName)) {
							tg.setE_col_str(filedValue);
						}else if ("memo_row".equals(filedName)) {
							tg.setMemo_row(filedValue);
						}else if ("memo_col".equals(filedName)) {
							tg.setMemo_col(filedValue);
						}else if ("remark_row".equals(filedName)) {
							tg.setRemark_row(filedValue);
						}else if ("remark_col".equals(filedName)) {
							tg.setRemark_col(filedValue);
						}else if ("fill_id_col".equals(filedName)) {
							tg.setFill_id_col(filedValue);
						}else if ("fill_id_row".equals(filedName)) {
							tg.setFill_id_row(filedValue);
						}else if ("memo".equals(filedName)) {
							tg.setMemo(filedValue);
						}else if ("e_row_str".equals(filedName)) {
							tg.setE_row_str(filedValue);
						}else if ("e_col_str".equals(filedName)) {
							tg.setE_col_str(filedValue);
						}else if ("e_row_end".equals(filedName)) {
							tg.setE_row_end(filedValue);
						}else if ("e_col_end".equals(filedName)) {
							tg.setE_col_end(filedValue);
						}else if ("max_item".equals(filedName)) {
							tg.setMax_item(filedValue);
						}else if ("img_3_col".equals(filedName)) {
							tg.setImg_3_col(filedValue);
						}else if ("img_3_row".equals(filedName)) {
							tg.setImg_3_row(filedValue);
						}

					} else {
						//System.out.println("流开始");
						in = item.getInputStream();
						byte[] data = toByteArray(in);
						// System.out.println(data.length);
						tg.setTmpl_cert(data);
						// 获取文件名
						fileName = item.getName();
						// 解决fileName兼容性问题
						int lastindex = fileName.lastIndexOf("\\");
						fileName = fileName.substring(lastindex + 1);
						int lastNameIndex = fileName.lastIndexOf(".");
						String lastName = fileName.substring(lastNameIndex + 1);
						System.out.println(lastName);
						if (!"xls".equals(lastName) && !"xlsx".equals(lastName)) {
							resultModel.setState("500");
							resultModel.setMsgInfo("请传入excel文件");
							;
							return resultModel;
						}
						tg.setIs_tmpl("1");
						tg.setExt_name("." + lastName);
					}
				}

				/*int count = this.getBean(QcmJhySampleConsignDItemServiceImpl.class).GetMaxSerialNo("QCM_ZBS_TMPL",
						"TMPL_NO", "B");
				String tmpl_no = "B" + String.format("%05d", count + 1);*/
				if (tg.getTmpl_no()==null || "".equals(tg.getTmpl_no())) {
					resultModel.setState("500");
					resultModel.setMsgInfo("请传入需要修改的模板号");
				
					return resultModel;
				}
				if (tg.getTmpl_desc()==null || "".equals(tg.getTmpl_desc())) {
					resultModel.setState("500");
					resultModel.setMsgInfo("模板名称不能为空");
				
					return resultModel;
				}	
				List<QcmZbsTmpl> temls=this.getMapper(QcmZbsTmplMapper.class).findWithCondition(null, "TMPL_DESC='"+tg.getTmpl_desc()+"'", null, 0, 0);
				if (temls!=null  && temls.size()>0) {
					if (!tg.getTmpl_desc().equals(temls.get(0).getTmpl_desc())) {
						resultModel.setState("500");
						resultModel.setMsgInfo("模板名称已存在，请确认");
					
						return resultModel;
					}
				}
				tg.setUpdate_name(userName);
				tg.setUpdate_time(new Date());
				tg.setValidflag("1");
				this.getMapper(QcmZbsTmplMapper.class).update(tg);
				resultModel.setData("200");
				resultModel.setMsgInfo("模板数据修改成功");

			} catch (Exception e) {
				resultModel.setData("500");
				//e.printStackTrace();
				resultModel.setMsgInfo("行列只能输入两个字符，且不能为空，请确认！");
				return resultModel;
			}
		}

		return resultModel;

	}
	
	
	public ResultModel downloadTmpl(String tmpl_no) throws Exception {
		ResultModel rm=new ResultModel();
		// 获取excel对象
		// 模板配置数据
		QcmZbsTmpl model = this.getMapper(QcmZbsTmplMapper.class).findById(tmpl_no);
		String path = this.getRequest().getServletContext().getRealPath("/") + "excel" + File.separator; // 临时保存路径
		File dir = new File(path);
		if (!dir.exists()) {
			new File(path).mkdir();
		}
		// 生成质保书的保存路径
		List<String> tarRoot = new ArrayList<String>();
		if (model!=null && model.getTmpl_cert()!=null ) {
			byte[] by=model.getTmpl_cert();
			if (by==null || by.length<1) {
				rm.setState("500");
				rm.setMsgInfo("质保书模板["+model.getTmpl_desc()+"]无可下载数据，请确认");
				return rm;
			}
			String book_num = path + "质保书模板[" + model.getTmpl_desc() + "].xls";
			FileOutputStream out=new FileOutputStream(book_num);
			out.write(by);
			out.flush();
			out.close();
			
			// 填充EXCEL的成分材质
			tarRoot.add(
					"/download?filename="+"质保书模板[" + model.getTmpl_desc() + "].xls");

		}
		rm.setData(tarRoot);
		rm.setState("200");	
		return rm;
	}
	
	
	
	

}
