/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.workexample.web;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.IdGen;
import com.jeeplus.common.utils.MyBeanUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.act.entity.Act;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.exampleeconomics.entity.ExampleEconomics;
import com.jeeplus.modules.exampleeconomics.service.ExampleEconomicsService;
import com.jeeplus.modules.exampleexpend.entity.ExampleExpend;
import com.jeeplus.modules.exampleexpend.service.ExampleExpendService;
import com.jeeplus.modules.exampleproject.entity.ExampleProject;
import com.jeeplus.modules.exampleproject.service.ExampleProjectService;
import com.jeeplus.modules.examplesingle.entity.ExampleSingle;
import com.jeeplus.modules.examplesingle.service.ExampleSingleService;
import com.jeeplus.modules.sys.dao.AreaDao;
import com.jeeplus.modules.sys.dao.MainDictDetailDao;
import com.jeeplus.modules.sys.entity.Area;
import com.jeeplus.modules.sys.entity.MainDictDetail;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.DictUtils;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workexample.dao.WorkExampleDao;
import com.jeeplus.modules.workexample.entity.ExcelList;
import com.jeeplus.modules.workexample.entity.WorkExample;
import com.jeeplus.modules.workexample.entity.WorkExampleExcel;
import com.jeeplus.modules.workexample.entity.WorkExampleIndices;
import com.jeeplus.modules.workexample.service.WorkExampleIndicesService;
import com.jeeplus.modules.workexample.service.WorkExampleService;
import com.jeeplus.modules.workexampleimplog.dao.WorkExampleImplogDao;
import com.jeeplus.modules.workexampleimplog.entity.WorkExampleImplog;
import com.jeeplus.modules.workmonthlyplan.entity.WorkMonthlyPlan;
import com.jeeplus.modules.workmonthlyplan.entity.WorkMonthlyPlanThis;
import com.jeeplus.modules.workreceiptsregister.entity.ResponseEntity;
import com.jeeplus.modules.workstaff.entity.WorkStaffBasicInfo;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * 案例Controller
 * @author ssrh
 * @version 2018-08-27
 */
@Controller
@RequestMapping(value = "${adminPath}/workexample/workExample")
public class ImportExampleController extends BaseController {
	@Autowired
	private WorkExampleService workExampleService;
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private ActTaskService actTaskService;
	@Autowired
	private ExampleSingleService exampleSingleService;
	@Autowired
	private WorkExampleDao workExampleDao;
	@Autowired
	private WorkExampleIndicesService workExampleIndicesService;
	@Autowired
	private ExampleProjectService exampleProjectService;
	@Autowired
	private ExampleExpendService exampleExpendService;
	@Autowired
	private ExampleEconomicsService exampleEconomicsService;
	@Autowired
	private MainDictDetailDao mainDictDetailDao;
	@Autowired
	private AreaDao areaDao;
    @Autowired
    private WorkExampleImplogDao workExampleImplogDao;

	@RequestMapping(value = "importDialogre")
	public String form(WorkExampleExcel workExampleExcel, Model model) {
		model.addAttribute("workExampleExcel", workExampleExcel);
		return "modules/workexample/workExampleExcel";
	}
	/**
	 * 导入Excel数据
	 */
	@RequiresPermissions("workexample:workExample:import")
    @RequestMapping(value = "import")
	//@ResponseBody
    public String form(WorkExampleExcel workExampleExcel,@RequestParam(value = "file",required = false) MultipartFile file, RedirectAttributes redirectAttributes) {
		try {
			int successNum = 0;
			int failureNum = 0;
			List<ExcelList> excelLists=workExampleExcel.getExcelList();
			StringBuilder failureMsg = new StringBuilder();
			//工程概况
			ImportExcel ei = new ImportExcel(file, 1, 0);
			WorkExample workExample=new WorkExample();
			String str=this.workExampleImport(ei,workExample);
			if(!"".equals(str)){
				this.getLogEntity(workExample.getId(),
						"",
						"",
						str);
				addMessage(redirectAttributes, "导入失败:"+str);
				return "redirect:"+Global.getAdminPath()+"/workexample/workExample/?repage";
			}
			//单项工程造价指标
			String sheetName="";
			for(int i=1;i<excelLists.size();i++){
				ImportExcel exampleSingleEi = new ImportExcel(file, 1, i);
				WorkExampleIndices workExampleIndices=new WorkExampleIndices();
				workExampleIndices.setCompanyId(UserUtils.getSelectCompany().getId());
				sheetName=exampleSingleEi.getHeaderStr();
				workExampleIndices.setName(sheetName);
				//List<WorkExampleIndices> exampleIndicesList = workExampleIndicesService.findByName(workExampleIndices);//指标
				WorkExampleIndices workExampleIndices1=workExampleIndicesService.get(excelLists.get(i).getId());
                Integer count = workExampleIndicesService.exampleIndicesCount(workExample.getId(),workExampleIndices1.getId());
                if (count == null || count==0){
                    workExampleIndicesService.saveExampleIndices(workExample.getId(),workExampleIndices1.getId());
                }
                Integer countp = workExampleIndicesService.exampleIndicesCount(workExample.getId(),workExampleIndices1.getParent().getId());
                if (countp == null || countp==0){
                    workExampleIndicesService.saveExampleIndices(workExample.getId(),workExampleIndices1.getParent().getId());
                }
                WorkExampleIndices workExampleIndicesp=workExampleIndicesService.get(workExampleIndices1.getParent().getId());
                if(workExampleIndicesp!=null){
                    Integer countpp = workExampleIndicesService.exampleIndicesCount(workExample.getId(),workExampleIndicesp.getParent().getId());
                    if (countpp == null || countpp==0){
                        workExampleIndicesService.saveExampleIndices(workExample.getId(),workExampleIndicesp.getParent().getId());
                    }
                }

					if("1".equals(workExampleIndices1.getType())){
						List<ExampleSingle> exampleSingList = exampleSingleEi.getDataList(ExampleSingle.class);
						for(ExampleSingle exampleSingle : exampleSingList){
							if(exampleSingle.getName()!=null&&!"".equals(exampleSingle.getName())){
								workExampleIndices.setName(exampleSingle.getName());
								workExampleIndices.setParent(workExampleIndices1);
								List<WorkExampleIndices> indicesList = workExampleIndicesService.find(workExampleIndices);//项
								if(indicesList!=null&&indicesList.size()==1){
									try{
										exampleSingle.setExampleId(workExample.getId());
										exampleSingle.setIndexId(workExampleIndices1.getId());
										exampleSingle.setExampleIndicesId(indicesList.get(0).getId());
										exampleSingle.setUnit(indicesList.get(0).getItemUnit());
										exampleSingle.setDigits(indicesList.get(0).getDecimalCount());
										if((exampleSingle.getMoney()==null||"".equals(exampleSingle.getMoney()))
												&&(exampleSingle.getCost()==null||"".equals(exampleSingle.getCost()))
												&&(exampleSingle.getProportion()==null||"".equals(exampleSingle.getProportion()))){
											String m=towFormat("0");
											String c=towFormat("0");
											String p=fourFormat("0");
											exampleSingle.setMoney(m);
											exampleSingle.setCost(c);
											exampleSingle.setProportion(p);
											ExampleSingle single=exampleSingleService.findSingle(exampleSingle);
											if(single!=null){
												exampleSingleService.save(single);//修改
											}else{
												exampleSingleService.save(exampleSingle);//新增
											}
										}else{
											if(isDouble(exampleSingle.getMoney())
													&&isDouble(exampleSingle.getCost())
													&&isDouble(exampleSingle.getProportion())){
												ExampleSingle single=exampleSingleService.findSingle(exampleSingle);
												if(single!=null){
													exampleSingleService.save(single);//修改
												}else{
													String m=towFormat(exampleSingle.getMoney());
													String c=towFormat(exampleSingle.getCost());
													String p=fourFormat(exampleSingle.getProportion());
													exampleSingle.setMoney(m);
													exampleSingle.setCost(c);
													exampleSingle.setProportion(p);
													exampleSingleService.save(exampleSingle);//新增
												}
											}else{
												this.getLogEntity(workExample.getId(),
														workExampleIndices1.getId(),
														indicesList.get(0).getId(),
														"【"+exampleSingle.getName()+"】数据异常或填写格式不正确");
											}
										}

									}catch(ConstraintViolationException ex){

									}catch (Exception ex) {

									}
								}else{
									this.getLogEntity(workExample.getId(),
											workExampleIndices1.getId(),
											"",
											"【"+exampleSingle.getName()+"】在指标项字典中不存在，或数据异常");
								}
							}

						}
					}else if("4".equals(workExampleIndices1.getType())){//workExampleIndices1.getName().contains("经济指标")
					List<ExampleEconomics> economicsList = exampleSingleEi.getDataList(ExampleEconomics.class);
					for(ExampleEconomics exampleEconomics : economicsList){
						if(exampleEconomics.getName()!=null&&!"".equals(exampleEconomics.getName())){
							workExampleIndices.setName(exampleEconomics.getName());
							workExampleIndices.setParent(workExampleIndices1);
							List<WorkExampleIndices> indicesList = workExampleIndicesService.find(workExampleIndices);//项
							if(indicesList!=null&&indicesList.size()==1){
								try{
									exampleEconomics.setExampleId(workExample.getId());
									exampleEconomics.setIndexId(workExampleIndices1.getId());
									exampleEconomics.setExampleindicesId(indicesList.get(0).getId());
									exampleEconomics.setUnit(indicesList.get(0).getItemUnit());
									exampleEconomics.setDigits(indicesList.get(0).getDecimalCount());
									if((exampleEconomics.getMoney()==null||"".equals(exampleEconomics.getMoney()))
											&&(exampleEconomics.getCost()==null||"".equals(exampleEconomics.getCost()))
											&&(exampleEconomics.getProportion()==null||"".equals(exampleEconomics.getProportion()))){
										String m=towFormat("0");
										String c=towFormat("0");
										String p=fourFormat("0");
										exampleEconomics.setMoney(m);
										exampleEconomics.setCost(c);
										exampleEconomics.setProportion(p);
										ExampleEconomics economics=exampleEconomicsService.findEconomics(exampleEconomics);
										if(economics!=null){
											exampleEconomicsService.save(economics);//修改
										}else{
											exampleEconomicsService.save(exampleEconomics);//新增
										}
									}else{
										if(isDouble(exampleEconomics.getMoney())
												&&isDouble(exampleEconomics.getCost())
												&&isDouble(exampleEconomics.getProportion())){
											ExampleEconomics economics=exampleEconomicsService.findEconomics(exampleEconomics);
											if(economics!=null){
												exampleEconomicsService.save(economics);//修改
											}else{
												String m=towFormat(exampleEconomics.getMoney());
												String c=towFormat(exampleEconomics.getCost());
												String p=fourFormat(exampleEconomics.getProportion());
												exampleEconomics.setMoney(m);//保留两位
												exampleEconomics.setCost(c);
												exampleEconomics.setProportion(p);//保留四位
												exampleEconomicsService.save(exampleEconomics);//新增
											}
										}else{
											this.getLogEntity(workExample.getId(),
													workExampleIndices1.getId(),
													indicesList.get(0).getId(),
													"【"+exampleEconomics.getName()+"】数据异常或填写格式不正确");
										}
									}
								}catch(ConstraintViolationException ex){

								}catch (Exception ex) {

								}
							}else{//增加日志
								this.getLogEntity(workExample.getId(),
										workExampleIndices1.getId(),
										"",
										"【"+exampleEconomics.getName()+"】在指标项字典中不存在，或数据异常");
							}
						}

					}
				}else if("5".equals(workExampleIndices1.getType())){//workExampleIndices1.getName().contains("主要工程量指标")
					List<ExampleProject> projectList = exampleSingleEi.getDataList(ExampleProject.class);
					for(ExampleProject exampleProject : projectList){
						if(exampleProject.getName()!=null&&!"".equals(exampleProject.getName())){
							workExampleIndices.setName(exampleProject.getName());
							workExampleIndices.setParent(workExampleIndices1);
							List<WorkExampleIndices> indicesList = workExampleIndicesService.find(workExampleIndices);//项
							if(indicesList!=null&&indicesList.size()==1){
								try{
									exampleProject.setExampleId(workExample.getId());
									exampleProject.setIndexId(workExampleIndices1.getId());
									exampleProject.setExampleindicesId(indicesList.get(0).getId());
									exampleProject.setDigits(indicesList.get(0).getDecimalCount());
									if((exampleProject.getQuota()==null||"".equals(exampleProject.getQuota()))
											&&(exampleProject.getCount()==null||"".equals(exampleProject.getCount()))){
										String m=towFormat("0");
										String p=fourFormat("0");
										exampleProject.setCount(m);
										exampleProject.setQuota(p);
										exampleProject.setUnit(DictUtils.getMainDictLabel(indicesList.get(0).getItemUnit(),"unit_type",""));
										ExampleProject project=exampleProjectService.findProject(exampleProject);
										if(project!=null){
											exampleProjectService.save(project);//修改
										}else{
											exampleProjectService.save(exampleProject);//新增
										}
									}else{
										if(isDouble(exampleProject.getCount())
												&&isDouble(exampleProject.getQuota())){
											exampleProject.setUnit(DictUtils.getMainDictLabel(indicesList.get(0).getItemUnit(),"unit_type",""));
											ExampleProject project=exampleProjectService.findProject(exampleProject);
											if(project!=null){
												exampleProjectService.save(project);//修改
											}else{
												String m=towFormat(exampleProject.getCount());
												String p=fourFormat(exampleProject.getQuota());
												exampleProject.setCount(m);
												exampleProject.setQuota(p);
												exampleProjectService.save(exampleProject);//新增
											}
										}else{
											this.getLogEntity(workExample.getId(),
													workExampleIndices1.getId(),
													indicesList.get(0).getId(),
													"【"+exampleProject.getName()+"】数据异常或填写格式不正确");
										}
									}

								}catch(ConstraintViolationException ex){

								}catch (Exception ex) {

								}
							}else{
								this.getLogEntity(workExample.getId(),
										workExampleIndices1.getId(),
										"",
										"【"+exampleProject.getName()+"】在指标项字典中不存在，或数据异常");
							}
						}
						}

				}else if("6".equals(workExampleIndices1.getType())){//workExampleIndices1.getName().contains("主要工料价格及消耗量指标")
					List<ExampleExpend> exampleExpendList = exampleSingleEi.getDataList(ExampleExpend.class);
					for(ExampleExpend exampleExpend : exampleExpendList){
						if(exampleExpend.getName()!=null&&!"".equals(exampleExpend.getName())){
							workExampleIndices.setName(exampleExpend.getName());
							workExampleIndices.setParent(workExampleIndices1);
							List<WorkExampleIndices> indicesList = workExampleIndicesService.find(workExampleIndices);//项
							if(indicesList!=null&&indicesList.size()==1){
								try{
									exampleExpend.setExampleId(workExample.getId());
									exampleExpend.setIndexId(workExampleIndices1.getId());
									exampleExpend.setExampleindicesId(indicesList.get(0).getId());
									exampleExpend.setDigits(indicesList.get(0).getDecimalCount());
									if((exampleExpend.getCount()==null||"".equals(exampleExpend.getCount()))
											&&(exampleExpend.getPrice()==null||"".equals(exampleExpend.getPrice()))
											&&(exampleExpend.getQuota()==null||"".equals(exampleExpend.getQuota()))){
										String m=towFormat("0");
										String c=towFormat("0");
										String p=fourFormat("0");
										exampleExpend.setCount(m);
										exampleExpend.setPrice(c);
										exampleExpend.setQuota(p);
										exampleExpend.setUnit(DictUtils.getMainDictLabel(indicesList.get(0).getItemUnit(),"unit_type",""));

										ExampleExpend expend=exampleExpendService.findExpend(exampleExpend);
										if(expend!=null){
											exampleExpendService.save(expend);//修改
										}else{
											exampleExpendService.save(exampleExpend);//新增
										}
									}else{
										if(isDouble(exampleExpend.getCount())
												&&isDouble(exampleExpend.getPrice())
												&&isDouble(exampleExpend.getQuota())){
											exampleExpend.setUnit(DictUtils.getMainDictLabel(indicesList.get(0).getItemUnit(),"unit_type",""));
											ExampleExpend expend=exampleExpendService.findExpend(exampleExpend);
											if(expend!=null){
												exampleExpendService.save(expend);//修改
											}else{
												String m=towFormat(exampleExpend.getCount());
												String c=towFormat(exampleExpend.getPrice());
												String p=fourFormat(exampleExpend.getQuota());
												exampleExpend.setCount(m);
												exampleExpend.setPrice(c);
												exampleExpend.setQuota(p);
												exampleExpendService.save(exampleExpend);//新增
											}
										}else{
											this.getLogEntity(workExample.getId(),
													workExampleIndices1.getId(),
													indicesList.get(0).getId(),
													"【"+exampleExpend.getName()+"】数据异常或填写格式不正确");
										}
									}

								}catch(ConstraintViolationException ex){

								}catch (Exception ex) {

								}

							}else{
								this.getLogEntity(workExample.getId(),
										workExampleIndices1.getId(),
										"",
										"【"+exampleExpend.getName()+"】在指标项字典中不存在，或数据异常");
							}
						}
						}

				}

			}
			//List<WorkExampleIndices> workExampleIndicesList = workExampleIndicesService.findByParentId(id);
			if (failureNum>0){
				failureMsg.insert(0, "，失败 "+failureNum+" 条案例记录。");
			}
			addMessage(redirectAttributes, "已成功导入案例记录"+failureMsg);
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入案例失败！");
		}
		return "redirect:"+Global.getAdminPath()+"/workexample/workExample/?repage";
    }


	@RequestMapping(value = "excel")
	@ResponseBody
	public ResponseEntity importExcel(MultipartFile file, RedirectAttributes redirectAttributes) {
		List<ExcelList> excelLists=new ArrayList<>();
		ResponseEntity<ExcelList> responseEntity = new ResponseEntity<>();
		try {
            ImportExcel examplecount = new ImportExcel(file, 1, 0);
            int count=examplecount.sheetCirculation();
			String sheetName="";
			for(int i=1;i<count;i++){
				ImportExcel exampleSingleEi = new ImportExcel(file, 1, i);

				WorkExampleIndices workExampleIndices=new WorkExampleIndices();
				workExampleIndices.setCompanyId(UserUtils.getSelectCompany().getId());
				sheetName=exampleSingleEi.getHeaderStr();
				workExampleIndices.setName(sheetName);
				List<WorkExampleIndices> workExampleIndicesList = workExampleIndicesService.findByName(workExampleIndices);
				if(workExampleIndicesList!=null&&workExampleIndicesList.size()==1){
					ExcelList excelList=new ExcelList();
					excelList.setSheetName(sheetName);
					excelList.setName(workExampleIndicesList.get(0).getName());
					excelList.setId(workExampleIndicesList.get(0).getId());
					excelLists.add(excelList);
				}else{
					ExcelList excelList=new ExcelList();
					excelList.setSheetName(sheetName);
					excelLists.add(excelList);
				}

			}
		} catch (Exception e) {

		}
		responseEntity.setData(excelLists);
		return responseEntity;
	}

    public String workExampleImport(ImportExcel ei,WorkExample example){
		//WorkExample example=new WorkExample();
		try {
			example.preInsert();
			String companyId=UserUtils.getSelectCompany().getId();
			example.setCompanyId(companyId);
			example.setOfficeId(UserUtils.getSelectOffice().getId());
			example.setStatus("1");
			if(ei.getCellValue(ei.getRow(1),1).toString()==null||"".equals(ei.getCellValue(ei.getRow(1),1).toString())){
				return "工程名称不允许为空!";
			}
			if(ei.getCellValue(ei.getRow(1),3).toString()==null||"".equals(ei.getCellValue(ei.getRow(1),3).toString())){
				return "工程类型不允许为空!";
			}
			if(ei.getCellValue(ei.getRow(2),1).toString()==null||"".equals(ei.getCellValue(ei.getRow(2),1).toString())){
				return "工程地点不允许为空!";
			}
			if(ei.getCellValue(ei.getRow(2),3).toString()==null||"".equals(ei.getCellValue(ei.getRow(2),3).toString())){
				return "建设规模不允许为空!";
			}
			example.setName(ei.getCellValue(ei.getRow(1),1).toString());
			String type=ei.getCellValue(ei.getRow(1),3).toString().trim();
			MainDictDetail mainDictDetail=mainDictDetailDao.getId("work_example_type",companyId,type);
			if(mainDictDetail!=null){
				example.setMainDictDetail(mainDictDetail);
			}else{
				return "工程类型在系统业务字典中不存在，请联系管理员!";
			}
			String address=ei.getCellValue(ei.getRow(2),1).toString().trim();
			Area area= areaDao.findByName(address);
			if(area!=null){
				example.setArea(area);
			}else{
				return "工程地点填写不正确!";
			}
			example.setBuildScale(ei.getCellValue(ei.getRow(2),3).toString());
			example.setModus(ei.getCellValue(ei.getRow(3),1).toString());
			example.setBasicType(ei.getCellValue(ei.getRow(3),3).toString());
			example.setScale(ei.getCellValue(ei.getRow(4),1).toString());
			example.setSafeLevel(ei.getCellValue(ei.getRow(4),3).toString());
			example.setStage(ei.getCellValue(ei.getRow(5),1).toString());
			example.setInvestmentType(ei.getCellValue(ei.getRow(5),3).toString());
			example.setValuationType(ei.getCellValue(ei.getRow(6),1).toString());
			example.setValuationAccording(ei.getCellValue(ei.getRow(6),3).toString());
			example.setBiddingPrice(ei.getCellValue(ei.getRow(7),1)==null?"":ei.getCellValue(ei.getRow(7),1).toString());
			example.setTaxModel(ei.getCellValue(ei.getRow(7),3).toString());
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			String date=ei.getCellValue(ei.getRow(8),1).toString();
			if(ei.getCellValue(ei.getRow(8),1)!=null){
				if(isDateString(ei.getCellValue(ei.getRow(8),1).toString(),"yyyy-MM-dd")){
					example.setStartDate(df.parse(ei.getCellValue(ei.getRow(8),1).toString()));
				}
			}
			if(ei.getCellValue(ei.getRow(8),3)!=null){
				if(isDateString(ei.getCellValue(ei.getRow(8),3).toString(),"yyyy-MM-dd")){
					example.setEndDate(df.parse(ei.getCellValue(ei.getRow(8),3).toString()));
				}
			}
			example.setAbovegroundArea(ei.getCellValue(ei.getRow(9),1).toString());
			example.setUndergroundArea(ei.getCellValue(ei.getRow(9),3).toString());
			example.setCivilArea(ei.getCellValue(ei.getRow(10),1).toString());
			example.setGarageArea(ei.getCellValue(ei.getRow(10),3).toString());
			example.setAbovegroundLayer(ei.getCellValue(ei.getRow(11),1).toString());
			example.setUndergroundLayer(ei.getCellValue(ei.getRow(11),3).toString());
			example.setAbovegroundHeight(ei.getCellValue(ei.getRow(12),1).toString());
			example.setUndergroundHeight(ei.getCellValue(ei.getRow(12),3).toString());
			example.setTotalHeight(ei.getCellValue(ei.getRow(13),1).toString());
			example.setEavesHeight(ei.getCellValue(ei.getRow(13),3).toString());
			example.setInsideDecoration(ei.getCellValue(ei.getRow(14),1).toString());
			example.setOutsideDecoration(ei.getCellValue(ei.getRow(14),3).toString());
			example.setSkylightDecoration(ei.getCellValue(ei.getRow(15),1).toString());
			example.setFloorDecoration(ei.getCellValue(ei.getRow(15),3).toString());
			example.setDoor(ei.getCellValue(ei.getRow(16),1).toString());
			example.setWindow(ei.getCellValue(ei.getRow(16),3).toString());
			example.setInstruction(ei.getCellValue(ei.getRow(17),1).toString());
			workExampleDao.insert(example);
		} catch (Exception e) {
            e.printStackTrace();
			return "导入失败：请联系管理员!";
		}
		return "";
	}

	private static boolean isDouble(String str) {
		if (null == str || "".equals(str)) {
			return false;
		}
		Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$");
		return pattern.matcher(str).matches();
	}

	public static String fourFormat(String str){
    Double d= Double.parseDouble(str);
    DecimalFormat df = new DecimalFormat("0.0000");
    String s = df.format(d);
    return s;
	}

    public static String towFormat(String str){
        Double d= Double.parseDouble(str);
        DecimalFormat df = new DecimalFormat("0.00");
        String s = df.format(d);
        return s;
    }
  /*  public static void main(String[] args) {
        System.out.println(format(""));
    }*/
	public static boolean isDateString(String datevalue, String dateFormat) {
		try {
			SimpleDateFormat fmt = new SimpleDateFormat(dateFormat);
			java.util.Date dd = fmt.parse(datevalue);
			if (datevalue.equals(fmt.format(dd))) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}
	}
//添加日志
    public  void getLogEntity(String exampleId, String indexId,String termId,String reason) {
        WorkExampleImplog workExampleImplog=new WorkExampleImplog();
        workExampleImplog.preInsert();
        workExampleImplog.setCompanyId(UserUtils.getSelectCompany().getId());
        workExampleImplog.setOfficeId(UserUtils.getSelectOffice().getId());
        workExampleImplog.setExampleId(exampleId);
        workExampleImplog.setIndexId(indexId);
        workExampleImplog.setTermId(termId);
        workExampleImplog.setReason(reason);
        workExampleImplogDao.insert(workExampleImplog);
    }



	/**
	 * 下载导入案例数据模板
	 */
	/*@RequiresPermissions("workexample:workExample:import")*/
	@RequestMapping(value = "import/template")
	public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
		String ids=request.getParameter("ids");
		try {
			String fileName = "案例数据导入模板.xlsx";
			List<WorkExample> list = Lists.newArrayList();
			ExportExcel ex=new ExportExcel(500);
			if(ids!=null&&!"".equals(ids)){
				String [] arr=ids.split(",");
				for(String id:arr){
					WorkExampleIndices workExampleIndices=workExampleIndicesService.get(id);
					List<WorkExampleIndices> indicesList = workExampleIndicesService.findExampleIndices(id);//项
					if("1".equals(workExampleIndices.getType())){//单项工程造价指标
						List<String> headList=Lists.newArrayList();
						headList.add("单位工程名称");
						headList.add("金额(元)");
						headList.add("单位造价(元/m2)");
						headList.add("占造价比例(%)");
						ex.createSheet(workExampleIndices.getName(), indicesList,headList,"1");
					}else if("4".equals(workExampleIndices.getType())){//经济指标
						List<String> headList=Lists.newArrayList();
						headList.add("主要分项工程");
						headList.add("工程造价(元)");
						headList.add("单位造价(元/m2)");
						headList.add("占造价比例(%)");
						ex.createSheet(workExampleIndices.getName(), indicesList,headList,"1");
					}else if("5".equals(workExampleIndices.getType())){//主要工程指标
						List<String> headList=Lists.newArrayList();
						headList.add("工料名称");
						headList.add("单位");
						headList.add("数量");
						headList.add("单位指标(单位/m2)");
						ex.createSheet(workExampleIndices.getName(), indicesList,headList,"2");
					}else if("6".equals(workExampleIndices.getType())){//主要工料价格及消耗量指标
						List<String> headList=Lists.newArrayList();
						headList.add("工料名称");
						headList.add("单位");
						headList.add("数量");
						headList.add("单价(元)");
						headList.add("单位指标(单位/m2)");
						headList.add("备注");
						ex.createSheet(workExampleIndices.getName(), indicesList,headList,"2");
					}
				}
			}

			ex.setDataList(list).write(response, fileName).dispose();
			//new ExportExcel("案例数据", WorkExample.class, 1).setDataList(list).write(response, fileName).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/workexample/workExample/?repage";
	}
}