/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-05-31
 * 描述            ：横剪主实绩
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Mes.Lengthwise.Plan.impl;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import market.Com.pb.DoubleUtil;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Mes.Lengthwise.Performance.vo.MesPrdDetailReport;
import market.Mes.Lengthwise.Performance.vo.MesSteelCodeTemp;
import market.Mes.Lengthwise.Plan.mapper.MesCrCrossCuttingMMapper;
import market.Mes.Lengthwise.Plan.service.MesCrCrossCuttingMService;
import market.Mes.Lengthwise.Plan.vo.MesCrCrossCuttingM;
import market.Mes.util.DateUtil;

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

@RemoteService(path="MesCrCrossCuttingMService")
public class MesCrCrossCuttingMServiceImpl extends BaseServiceImpl<MesCrCrossCuttingM,Object> implements MesCrCrossCuttingMService {

	
	/**
	 * 3mm机组生产明细报表
	 *  object里面放一些查询字段：比如合同号、订单号、原料卷号等等
	 * memo1:机台代码
	 * startTime:计划下发时间开始
	 * endTime:计划下发时间结束
	 * startTime2:交工时间开始
	 * endTime2:交工时间结束
	 * memo2:生产时间开始
	 * meme3:生产时间结束
	 * memo4:交货时间开始
	 * memo5:交货时间结束
	 * */
	public ResultModel doPrdDetailWeb_3mm(CommonPage<MesPrdDetailReport> oci){
		ResultModel rm=new ResultModel();
		try{
			rm.setData(this.getMapper(MesCrCrossCuttingMMapper.class).doPrdDetailWeb_3mm(oci.getObject(),oci.getMemo1(),oci.getStartTime(),oci.getEndTime(),
					oci.getStartTime2(),oci.getEndTime2(),oci.getMemo2(),oci.getMemo3(),oci.getMemo4(),oci.getMemo5()));
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
		}
		return rm;
	}
	
	
	
	/**
	 * 打印计划单
	 * @param oci
	 * @return
	 */
	public ResultModel doQueryPrint(CommonPage<String[]> oci){
		ResultModel rm=new ResultModel();
		try{
			String [] strs=oci.getObject();
			rm.setState("200");
			List<HashMap> lhm=this.getMapper(MesCrCrossCuttingMMapper.class).doQueryPrint(strs);
			if(lhm==null||lhm.size()==0){
				rm.setMsgInfo("未找到数据");
				return rm;
			}
			
			String customNo = (String) lhm.get(0).get("CUSTOMER_NO");
			for(HashMap hm:lhm){
				if(!customNo.equals((String) hm.get("CUSTOMER_NO"))){
					rm.setState("500");
					rm.setMsgInfo("卷对应的客户不同,请核实后重新选择!");
					return rm;
				}
				List<HashMap> lhmChild=this.getMapper(MesCrCrossCuttingMMapper.class).doQueryPrintChild(hm.get("FINISH_COILNO").toString().trim());
				if(lhmChild!=null&&lhmChild.size()>0){
					hm.put("STACK_POSITION", lhmChild.get(0).get("BUTTRESSNAME"));
				}
			}
			
			rm.setData(lhm);
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo(ex.getMessage().trim());
		}
		return rm;
	}
	
	
	
	/**
	 * 开平产量报表
	 * 根据班次统计报表
	 * */
	public ResultModel doProductionWrkReportWeb(CommonPage<MesCrCrossCuttingM> oci){
		ResultModel rm=new ResultModel();
		try{
			rm.setData(this.getMapper(MesCrCrossCuttingMMapper.class).doProductionWrkReport(oci.getObject()));
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
		}
		return rm;
	}

	/**
	 * 各牌号开平产量报表
	 * */
	public ResultModel doProductionSteelReportWeb(CommonPage<MesCrCrossCuttingM> oci){
		ResultModel rm=new ResultModel();
		try{
			rm.setData(this.getMapper(MesCrCrossCuttingMMapper.class).doProductionSteelReport(oci.getObject()));
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 开平综合产量报表
	 * @param oci
	 * [{"object":{"wrk_date":"2018-03","machine_code":"M0003"}}]格式
	 * @return
	 */
	public ResultModel doProductionMultReportWeb(CommonPage<MesCrCrossCuttingM> oci){
		ResultModel rm=new ResultModel();
		try {
			rm.setData(this.getMapper(MesCrCrossCuttingMMapper.class).doProductionMultReport(oci.getObject()));
			rm.setState("200");
		} catch (Exception ex) {
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 开平产量报表   导出Excel
	 * 根据班次统计报表
	 * */
	public ResultModel doProductionWrkReportWeb_Excel(CommonPage<MesCrCrossCuttingM> oci){
		ResultModel rm=new ResultModel();
		try{
			if(oci==null || "".equals(oci.getObject().getWrk_date())){
				rm.setState("500");
				rm.setMsgInfo("请输入参数!");
				return rm;
			}
			List<HashMap> lhap = this.getMapper(MesCrCrossCuttingMMapper.class).doProductionWrkReport(oci.getObject());
			// 1，创建一个webbook，对应一个Excel文件
			WritableWorkbook workbook;
			// 2,设置用户下载路径
			String path = this.getRequest().getServletContext().getRealPath("/")+ "excel" + File.separator;  //临时保存路径
			File dir = new File(path);
			if(!dir.exists()) {
				new File(path).mkdir();
			}
			generateExcelProduWrk3(rm, lhap, path,oci.getObject().getWrk_date(),oci.getObject().getMachine_code());
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("导出失败!"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 开平产量报表   导出Excel
	 * @param rm
	 * @param lhap
	 * @param path
	 * @param wrk_date 
	 * @throws Exception 
	 */
	private void generateExcelProduWrk3(ResultModel rm, List<HashMap> lhap,
			String path, String wrk_date,String machine_code) throws Exception {
		String machine_name="";
//		if("M0001".equals(machine_code)){
//			machine_name="4mm";
//		}else if("M0002".equals(machine_code)){
//			machine_name="12mm";
//		}
		machine_name="3mm";
		String curDate = DateUtil.formatDate(new Date(), "yyMMdd");
		String str=wrk_date.substring(0, 4)+"年"+wrk_date.substring(5, 7)+"月";
		String filename = str +machine_name+ "开平产量报表_"+curDate;
    	WritableWorkbook workbook;
    	String file=filename+".xls";  //文件名
    	//3. 操作EXCEL文件
    	workbook = Workbook.createWorkbook(new File(path+file));
    	WritableSheet sheet = workbook.createSheet(str +machine_name+"开平产量报表", 1);
    	//行高
    	sheet.getSettings().setDefaultRowHeight(300);
		//字体  参数可自行配置
		WritableFont font1 = new WritableFont(WritableFont.ARIAL,11,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE);  
		WritableCellFormat cellFormat1 = new WritableCellFormat(font1); 
		cellFormat1.setAlignment(Alignment.CENTRE);
		cellFormat1.setVerticalAlignment(VerticalAlignment.CENTRE);
		//表头
		/*
		 * //sheet.mergeCells(int m,int n,int p,int q);
           //作用是从(m,n)到(p,q)的单元格全部合并[我的理解：m列、n行和p列、q行合并]
          sheet.addCell(new Label(i, 0, tittles[i],wcf));//i列，1行，title内容，wcf样式
		 */
		sheet.mergeCells(0, 0, 0, 2);//设置第一列、第一行和 第一列、第三行合并
		sheet.addCell(new Label(0,0,"日期",cellFormat1));
		
		sheet.mergeCells(1, 0, 6, 0);
		sheet.addCell(new Label(1,0,"常白班",cellFormat1));
		sheet.mergeCells(1,1,3,1);
		sheet.addCell(new Label(1,1,"当日",cellFormat1));
		sheet.mergeCells(4,1,6,1);
		sheet.addCell(new Label(4,1,"月累",cellFormat1));
		
		sheet.addCell(new Label(1,2,"投入",cellFormat1));
		sheet.addCell(new Label(2,2,"产出",cellFormat1));
		sheet.addCell(new Label(3,2,"成材率(%)",cellFormat1));
		sheet.addCell(new Label(4,2,"投入",cellFormat1));
		sheet.addCell(new Label(5,2,"产出",cellFormat1));
		sheet.addCell(new Label(6,2,"成材率(%)",cellFormat1));
		
		sheet.mergeCells(7, 0, 12, 0);
		sheet.addCell(new Label(7,0,"甲班",cellFormat1));
		sheet.mergeCells(7,1,9,1);
		sheet.addCell(new Label(7,1,"当日",cellFormat1));
		sheet.mergeCells(10,1,12,1);
		sheet.addCell(new Label(10,1,"月累",cellFormat1));
		
		sheet.addCell(new Label(7,2,"投入",cellFormat1));
		sheet.addCell(new Label(8,2,"产出",cellFormat1));
		sheet.addCell(new Label(9,2,"成材率(%)",cellFormat1));
		sheet.addCell(new Label(10,2,"投入",cellFormat1));
		sheet.addCell(new Label(11,2,"产出",cellFormat1));
		sheet.addCell(new Label(12,2,"成材率(%)",cellFormat1));
		
		
		sheet.mergeCells(13, 0, 18, 0);
		sheet.addCell(new Label(13,0,"乙班",cellFormat1));
		sheet.mergeCells(13,1,15,1);
		sheet.addCell(new Label(13,1,"当日",cellFormat1));
		sheet.mergeCells(16,1,18,1);
		sheet.addCell(new Label(16,1,"月累",cellFormat1));
		
		sheet.addCell(new Label(13,2,"投入",cellFormat1));
		sheet.addCell(new Label(14,2,"产出",cellFormat1));
		sheet.addCell(new Label(15,2,"成材率(%)",cellFormat1));
		sheet.addCell(new Label(16,2,"投入",cellFormat1));
		sheet.addCell(new Label(17,2,"产出",cellFormat1));
		sheet.addCell(new Label(18,2,"成材率(%)",cellFormat1));
		
		sheet.mergeCells(19, 0, 24, 0);
		sheet.addCell(new Label(19,0,"丙班",cellFormat1));
		sheet.mergeCells(19,1,21,1);
		sheet.addCell(new Label(19,1,"当日",cellFormat1));
		sheet.mergeCells(22,1,24,1);
		sheet.addCell(new Label(22,1,"月累",cellFormat1));
		
		sheet.addCell(new Label(19,2,"投入",cellFormat1));
		sheet.addCell(new Label(20,2,"产出",cellFormat1));
		sheet.addCell(new Label(21,2,"成材率(%)",cellFormat1));
		sheet.addCell(new Label(22,2,"投入",cellFormat1));
		sheet.addCell(new Label(23,2,"产出",cellFormat1));
		sheet.addCell(new Label(24,2,"成材率(%)",cellFormat1));
		
		sheet.mergeCells(25, 0, 30, 0);
		sheet.addCell(new Label(25,0,"丁班",cellFormat1));
		sheet.mergeCells(25,1,27,1);
		sheet.addCell(new Label(25,1,"当日",cellFormat1));
		sheet.mergeCells(28,1,30,1);
		sheet.addCell(new Label(28,1,"月累",cellFormat1));
		
		sheet.addCell(new Label(25,2,"投入",cellFormat1));
		sheet.addCell(new Label(26,2,"产出",cellFormat1));
		sheet.addCell(new Label(27,2,"成材率(%)",cellFormat1));
		sheet.addCell(new Label(28,2,"投入",cellFormat1));
		sheet.addCell(new Label(29,2,"产出",cellFormat1));
		sheet.addCell(new Label(30,2,"成材率(%)",cellFormat1));
		
		
		
		sheet.mergeCells(31, 0, 39, 0);
		sheet.addCell(new Label(31,0,"合计",cellFormat1));
		sheet.mergeCells(31,1,33,1);
		sheet.addCell(new Label(31,1,"当日",cellFormat1));
		sheet.mergeCells(34,1,36,1);
		sheet.addCell(new Label(34,1,"月累",cellFormat1));
		sheet.mergeCells(37,1,39,1);
		sheet.addCell(new Label(37,1,"年累",cellFormat1));
		
		
		sheet.addCell(new Label(31,2,"投入",cellFormat1));
		sheet.addCell(new Label(32,2,"产出",cellFormat1));
		sheet.addCell(new Label(33,2,"成材率(%)",cellFormat1));
		sheet.addCell(new Label(34,2,"投入",cellFormat1));
		sheet.addCell(new Label(35,2,"产出",cellFormat1));
		sheet.addCell(new Label(36,2,"成材率(%)",cellFormat1));
		sheet.addCell(new Label(37,2,"投入",cellFormat1));
		sheet.addCell(new Label(38,2,"产出",cellFormat1));
		sheet.addCell(new Label(39,2,"成材率(%)",cellFormat1));
		
		
		WritableFont font = new WritableFont(WritableFont.ARIAL,11,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE);  
		WritableCellFormat cellFormat = new WritableCellFormat(font); 
		cellFormat.setAlignment(Alignment.CENTRE);
		cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
		int i=1;
		for(HashMap hm:lhap){
			//日期
			if(hm.get("WRK_DATE_2")!=null){
				sheet.addCell(new Label(0,i+2,""+hm.get("WRK_DATE_2"),cellFormat));
			}
			//常白班 -当日-投入
			if(hm.get("DAILY_0_IN")!=null){
				sheet.addCell(new Label(1,i+2,""+hm.get("DAILY_0_IN"),cellFormat));
			}
			//常白班 -当日-产出
			if(hm.get("DAILY_0")!=null){
				sheet.addCell(new Label(2,i+2,""+hm.get("DAILY_0"),cellFormat));
			}
			if(hm.get("RATE_DAILY_0")!=null){
				sheet.addCell(new Label(3,i+2,""+hm.get("RATE_DAILY_0"),cellFormat));
			}
			//常白班 -月累-投入
			if(hm.get("MONTH_0_IN")!=null){
				sheet.addCell(new Label(4,i+2,""+hm.get("MONTH_0_IN"),cellFormat));
			}
			//常白班 -月累-产出
			if(hm.get("MONTH_0")!=null){
				sheet.addCell(new Label(5,i+2,""+hm.get("MONTH_0"),cellFormat));
			}
			if(hm.get("RATE_MONTH_0")!=null){
				sheet.addCell(new Label(6,i+2,""+hm.get("RATE_MONTH_0"),cellFormat));
			}
			//甲班 -当日-投入
			if(hm.get("DAILY_1_IN")!=null){
				sheet.addCell(new Label(7,i+2,""+hm.get("DAILY_1_IN"),cellFormat));
			}
			//甲班 -当日-产出
			if(hm.get("DAILY_1")!=null){
				sheet.addCell(new Label(8,i+2,""+hm.get("DAILY_1"),cellFormat));
			}
			if(hm.get("RATE_DAILY_1")!=null){
				sheet.addCell(new Label(9,i+2,""+hm.get("RATE_DAILY_1"),cellFormat));
			}
			//甲班 -月累-投入
			if(hm.get("MONTH_1_IN")!=null){
				sheet.addCell(new Label(10,i+2,""+hm.get("MONTH_1_IN"),cellFormat));
			}
			//甲班 -月累-产出
			if(hm.get("MONTH_1")!=null){
				sheet.addCell(new Label(11,i+2,""+hm.get("MONTH_1"),cellFormat));
			}
			if(hm.get("RATE_MONTH_1")!=null){
				sheet.addCell(new Label(12,i+2,""+hm.get("RATE_MONTH_1"),cellFormat));
			}
			
			
			//乙班 -当日-投入
			if(hm.get("DAILY_2_IN")!=null){
				sheet.addCell(new Label(13,i+2,""+hm.get("DAILY_2_IN"),cellFormat));
			}
			//乙班 -当日-产出
			if(hm.get("DAILY_2")!=null){
				sheet.addCell(new Label(14,i+2,""+hm.get("DAILY_2"),cellFormat));
			}
			if(hm.get("RATE_DAILY_2")!=null){
				sheet.addCell(new Label(15,i+2,""+hm.get("RATE_DAILY_2"),cellFormat));
			}
			//乙班 -月累-投入
			if(hm.get("MONTH_2_IN")!=null){
				sheet.addCell(new Label(16,i+2,""+hm.get("MONTH_2_IN"),cellFormat));
			}
			//乙班 -月累-产出
			if(hm.get("MONTH_2")!=null){
				sheet.addCell(new Label(17,i+2,""+hm.get("MONTH_2"),cellFormat));
			}
			if(hm.get("RATE_MONTH_2")!=null){
				sheet.addCell(new Label(18,i+2,""+hm.get("RATE_MONTH_2"),cellFormat));
			}
			
			//丙班 -当日-投入
			if(hm.get("DAILY_3_IN")!=null){
				sheet.addCell(new Label(19,i+2,""+hm.get("DAILY_3_IN"),cellFormat));
			}
			//丙班 -当日-产出
			if(hm.get("DAILY_3")!=null){
				sheet.addCell(new Label(20,i+2,""+hm.get("DAILY_3"),cellFormat));
			}
			if(hm.get("RATE_DAILY_3")!=null){
				sheet.addCell(new Label(21,i+2,""+hm.get("RATE_DAILY_3"),cellFormat));
			}
			
			//丙班-月累-投入
			if(hm.get("MONTH_3_IN")!=null){
				sheet.addCell(new Label(22,i+2,""+hm.get("MONTH_3_IN"),cellFormat));
			}
			//丙班-月累-产出
			if(hm.get("MONTH_3")!=null){
				sheet.addCell(new Label(23,i+2,""+hm.get("MONTH_3"),cellFormat));
			}
			if(hm.get("RATE_MONTH_3")!=null){
				sheet.addCell(new Label(24,i+2,""+hm.get("RATE_MONTH_3"),cellFormat));
			}
			
			//丁班 -当日-投入
			if(hm.get("DAILY_4_IN")!=null){
				sheet.addCell(new Label(25,i+2,""+hm.get("DAILY_4_IN"),cellFormat));
			}
			//丁班 -当日-产出
			if(hm.get("DAILY_4")!=null){
				sheet.addCell(new Label(26,i+2,""+hm.get("DAILY_4"),cellFormat));
			}
			if(hm.get("RATE_DAILY_4")!=null){
				sheet.addCell(new Label(27,i+2,""+hm.get("RATE_DAILY_4"),cellFormat));
			}
			
			
			//丁班 -月累-投入
			if(hm.get("MONTH_4_IN")!=null){
				sheet.addCell(new Label(28,i+2,""+hm.get("MONTH_4_IN"),cellFormat));
			}
			//丁班 -月累-产出
			if(hm.get("MONTH_4")!=null){
				sheet.addCell(new Label(29,i+2,""+hm.get("MONTH_4"),cellFormat));
			}
			if(hm.get("RATE_MONTH_4")!=null){
				sheet.addCell(new Label(30,i+2,""+hm.get("RATE_MONTH_4"),cellFormat));
			}
			
			//合计-当日-投入
			if(hm.get("DAILY_ALL_IN")!=null){
				sheet.addCell(new Label(31,i+2,""+hm.get("DAILY_ALL_IN"),cellFormat));
			}
			//合计-当日-产出
			if(hm.get("DAILY_ALL")!=null){
				sheet.addCell(new Label(32,i+2,""+hm.get("DAILY_ALL"),cellFormat));
			}
			if(hm.get("RATE_DAILY_ALL")!=null){
				sheet.addCell(new Label(33,i+2,""+hm.get("RATE_DAILY_ALL"),cellFormat));
			}
			
			//合计-月累-投入
			if(hm.get("MONTH_ALL_IN")!=null){
				sheet.addCell(new Label(34,i+2,""+hm.get("MONTH_ALL_IN"),cellFormat));
			}
			//合计-月累-产出
			if(hm.get("MONTH_ALL")!=null){
				sheet.addCell(new Label(35,i+2,""+hm.get("MONTH_ALL"),cellFormat));
			}
			if(hm.get("RATE_MONTH_ALL")!=null){
				sheet.addCell(new Label(36,i+2,""+hm.get("RATE_MONTH_ALL"),cellFormat));
			}
			
			//合计-年累-投入
			if(hm.get("YEAR_ALL_IN")!=null){
				sheet.addCell(new Label(37,i+2,""+hm.get("YEAR_ALL_IN"),cellFormat));
			}
			//合计-年累-产出
			if(hm.get("YEAR_ALL")!=null){
				sheet.addCell(new Label(38,i+2,""+hm.get("YEAR_ALL"),cellFormat));
			}
			if(hm.get("RATE_YEAR_ALL")!=null){
				sheet.addCell(new Label(39,i+2,""+hm.get("RATE_YEAR_ALL"),cellFormat));
			}
			i++;
		}
		sheet.getSettings().setVerticalFreeze(1);  		//首行冻结
		sheet.getSettings().setVerticalFreeze(2);
		sheet.getSettings().setVerticalFreeze(3);
		//关闭
		workbook.write();
		workbook.close();
		rm.setData("/download?filename="+file);
	}
	
	
	/**
	 * 各牌号开平产量报表    导出Excel
	 * */
	public ResultModel doProductionSteelReportWeb_Excel(CommonPage<MesCrCrossCuttingM> oci){
		ResultModel rm=new ResultModel();
		try{
			if(oci==null || "".equals(oci.getObject().getWrk_date())){
				rm.setState("500");
				rm.setMsgInfo("请输入参数!");
				return rm;
			}
			List<HashMap> lhap = this.getMapper(MesCrCrossCuttingMMapper.class).doProductionSteelReport(oci.getObject());
			// 1，创建一个webbook，对应一个Excel文件
			WritableWorkbook workbook;
			// 2,设置用户下载路径
			String path = this.getRequest().getServletContext().getRealPath("/")+ "excel" + File.separator;  //临时保存路径
			File dir = new File(path);
			if(!dir.exists()) {
				new File(path).mkdir();
			}
			generateExcelProduSteel3(rm, lhap, path,oci.getObject().getWrk_date(),oci.getObject().getMachine_code());
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("导出失败!"+ex.getMessage());
		}
		return rm;
	}
	
	private void generateExcelProduSteel3(ResultModel rm, List<HashMap> lhap,
			String path, String wrk_date,String machine_code) throws Exception {
		//数据处理===============================
		//存储各牌号
		List<String> lstr=new ArrayList<>();
		//获取日期，各牌号日，月，年累集合
		Map<String,List<MesSteelCodeTemp>> maptp=new HashMap<String,List<MesSteelCodeTemp>>();
		List<MesSteelCodeTemp> lmsct=new ArrayList<>();
		for(HashMap hm:lhap){
			MesSteelCodeTemp msct=new MesSteelCodeTemp();
			if(!lstr.contains((""+hm.get("STEEL_CODE")))){
				lstr.add(""+hm.get("STEEL_CODE"));
			}
			msct.setSteel_code(""+hm.get("STEEL_CODE"));
			msct.setWrk_date(""+hm.get("WRK_DATE"));
			msct.setStl_daily(""+hm.get("STL_DAILY"));
			msct.setStl_month(""+hm.get("STL_MONTH"));
			msct.setStl_year(""+hm.get("STL_YEAR"));
			msct.setStl_daily_in(""+hm.get("STL_DAILY_IN"));
			msct.setStl_month_in(""+hm.get("STL_MONTH_IN"));
			msct.setStl_year_in(""+hm.get("STL_YEAR_IN"));
			msct.setRate_daily(""+hm.get("RATE_DAILY"));
			msct.setRate_month(""+hm.get("RATE_MONTH"));
			msct.setRate_year(""+hm.get("RATE_YEAR"));
			lmsct.add(msct);
		}
		if(lstr.size()<=0){
			rm.setMsgInfo("无对应的牌号");
			return ;
		}
		//对List集合中对象元素中的wrk_date分组  重要
		for(MesSteelCodeTemp tmExcpNew : lmsct){
			if(maptp.containsKey(tmExcpNew.getWrk_date())){//map中异常批次已存在，将该数据存放到同一个key（key存放的是异常批次）的map中
				maptp.get(tmExcpNew.getWrk_date()).add(tmExcpNew);
			}else{//map中不存在，新建key，用来存放数据
				List<MesSteelCodeTemp> tmpList = new ArrayList<MesSteelCodeTemp>();
				tmpList.add(tmExcpNew);
				maptp.put(tmExcpNew.getWrk_date(), tmpList);
			}
		}
		//====================================
		String machine_name="";
//		if("M0001".equals(machine_code)){
//			machine_name="4mm";
//		}else if("M0002".equals(machine_code)){
//			machine_name="12mm";
//		}
		machine_name="3mm";
		String curDate = DateUtil.formatDate(new Date(), "yyMMdd");
		String str=wrk_date.substring(0, 4)+"年"+wrk_date.substring(5, 7)+"月";
		String filename = str + machine_name+"各牌号开平产量报表_"+curDate;
    	WritableWorkbook workbook;
    	String file=filename+".xls";  //文件名
    	//3. 操作EXCEL文件
    	workbook = Workbook.createWorkbook(new File(path+file));
    	WritableSheet sheet = workbook.createSheet(machine_name+"各牌号开平产量报表", 1);
    	//行高
    	sheet.getSettings().setDefaultRowHeight(300);
		//字体  参数可自行配置
		WritableFont font1 = new WritableFont(WritableFont.ARIAL,11,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE);  
		WritableCellFormat cellFormat1 = new WritableCellFormat(font1); 
		cellFormat1.setAlignment(Alignment.CENTRE);
		cellFormat1.setVerticalAlignment(VerticalAlignment.CENTRE);
		
		//表头
		/*
		 * //sheet.mergeCells(int m,int n,int p,int q);
           //作用是从(m,n)到(p,q)的单元格全部合并[我的理解：m列、n行和p列、q行合并]
          sheet.addCell(new Label(i, 0, tittles[i],wcf));//i列，1行，title内容，wcf样式
		 */
		sheet.mergeCells(0, 0, 0, 2);//设置第一列、第一行和 第一列、第三行合并
		sheet.addCell(new Label(0,0,"日期",cellFormat1));
		//各牌号对应的月累，年累。
		Map<String,MesSteelCodeTemp> mapSteelC=new HashMap<>();
		int i=1;
		for(String steel_code:lstr){
			MesSteelCodeTemp ms=new MesSteelCodeTemp();
			sheet.mergeCells(i, 0, i+8, 0);
			sheet.addCell(new Label(i,0,steel_code,cellFormat1));
			sheet.mergeCells(i, 1, i+2, 0);
			sheet.addCell(new Label(i,1,"当日",cellFormat1));
			sheet.mergeCells(i+3, 1, i+5, 0);
			sheet.addCell(new Label(i+3,1,"月累",cellFormat1));
			sheet.mergeCells(i+6, 1, i+8, 0);
			sheet.addCell(new Label(i+6,1,"年累",cellFormat1));
			sheet.addCell(new Label(i,2,"投入",cellFormat1));
			sheet.addCell(new Label(i+1,2,"产出",cellFormat1));
			sheet.addCell(new Label(i+2,2,"成材率(%)",cellFormat1));
			sheet.addCell(new Label(i+3,2,"投入",cellFormat1));
			sheet.addCell(new Label(i+4,2,"产出",cellFormat1));
			sheet.addCell(new Label(i+5,2,"成材率(%)",cellFormat1));
			sheet.addCell(new Label(i+6,2,"投入",cellFormat1));
			sheet.addCell(new Label(i+7,2,"产出",cellFormat1));
			sheet.addCell(new Label(i+8,2,"成材率(%)",cellFormat1));
			i=i+9;
			mapSteelC.put(steel_code, ms);
		}
		
		sheet.mergeCells(i, 0, i+8, 0);
		sheet.addCell(new Label(i,0,"合计",cellFormat1));
		sheet.mergeCells(i, 1, i+2, 0);
		sheet.addCell(new Label(i,1,"当日",cellFormat1));
		sheet.mergeCells(i+3, 1, i+5, 0);
		sheet.addCell(new Label(i+3,1,"月累",cellFormat1));
		sheet.mergeCells(i+6, 1, i+8, 0);
		sheet.addCell(new Label(i+6,1,"年累",cellFormat1));
		sheet.addCell(new Label(i,2,"投入",cellFormat1));
		sheet.addCell(new Label(i+1,2,"产出",cellFormat1));
		sheet.addCell(new Label(i+2,2,"成材率(%)",cellFormat1));
		sheet.addCell(new Label(i+3,2,"投入",cellFormat1));
		sheet.addCell(new Label(i+4,2,"产出",cellFormat1));
		sheet.addCell(new Label(i+5,2,"成材率(%)",cellFormat1));
		sheet.addCell(new Label(i+6,2,"投入",cellFormat1));
		sheet.addCell(new Label(i+7,2,"产出",cellFormat1));
		sheet.addCell(new Label(i+8,2,"成材率(%)",cellFormat1));
		
		
		WritableFont font = new WritableFont(WritableFont.ARIAL,11,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE);  
		WritableCellFormat cellFormat = new WritableCellFormat(font); 
		cellFormat.setAlignment(Alignment.CENTRE);
		cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
		
		//合计对应的月累，年累。
		Map<String,MesSteelCodeTemp> mapSteelC_Total=new HashMap<>();
		MesSteelCodeTemp mt2=new MesSteelCodeTemp();
		mapSteelC_Total.put("total_mes", mt2);
		double all_day=0;
		double all_month=0;
		double all_year=0;
		double all_day_in=0;
		double all_month_in=0;
		double all_year_in=0;
		//总共列数
		int columnCount=i+8;//从0开始
		//wrk_date的天数
		int monthLastDay = DateUtil.getMonthLastDay(Integer.parseInt(wrk_date.substring(0, 4)), Integer.parseInt(wrk_date.substring(5, 7)));
		//2018-06-形式
		String wrkdate_temp=wrk_date+"-";
		int j=1;
		for(int day=1;day<=monthLastDay;day++){
			all_day=0;
			all_month=0;
			all_year=0;
			all_day_in=0;
			all_month_in=0;
			all_year_in=0;
			//日期
			sheet.addCell(new Label(0,j+2,String.format("%02d", day)+"日",cellFormat));
			i=1;
			
			if(maptp.containsKey(wrkdate_temp+String.format("%02d", day))){
				List<MesSteelCodeTemp> list = maptp.get(wrkdate_temp+String.format("%02d", day));
				
				for(String steel_code:lstr){
					MesSteelCodeTemp mt=new MesSteelCodeTemp();
					mt.setSteel_code(steel_code);
					if(list.contains(mt)){
						mt = list.get(list.indexOf(mt));
						//sheet.mergeCells(i, j+2, i+2, 0);
						//sheet.addCell(new Label(i,j+2,steel_code,cellFormat1));
						sheet.addCell(new Label(i,j+2,"0.0".equals(mt.getStl_daily_in())?"0":mt.getStl_daily_in(),cellFormat));
						sheet.addCell(new Label(i+1,j+2,"0.0".equals(mt.getStl_daily())?"0":mt.getStl_daily(),cellFormat));
						sheet.addCell(new Label(i+2,j+2,"0.0".equals(mt.getRate_daily())?"0":mt.getRate_daily(),cellFormat));
						sheet.addCell(new Label(i+3,j+2,"0.0".equals(mt.getStl_month_in())?"0":mt.getStl_month_in(),cellFormat));
						sheet.addCell(new Label(i+4,j+2,"0.0".equals(mt.getStl_month())?"0":mt.getStl_month(),cellFormat));
						sheet.addCell(new Label(i+5,j+2,"0.0".equals(mt.getRate_month())?"0":mt.getRate_month(),cellFormat));
						sheet.addCell(new Label(i+6,j+2,"0.0".equals(mt.getStl_year_in())?"0":mt.getStl_year_in(),cellFormat));
						sheet.addCell(new Label(i+7,j+2,"0.0".equals(mt.getStl_year())?"0":mt.getStl_year(),cellFormat));
						sheet.addCell(new Label(i+8,j+2,"0.0".equals(mt.getRate_year())?"0":mt.getRate_year(),cellFormat));
						
						all_day=DoubleUtil.add(all_day, Double.parseDouble(mt.getStl_daily()));
						all_day_in=DoubleUtil.add(all_day_in, Double.parseDouble(mt.getStl_daily_in()));
//						all_month=DoubleUtil.add(all_month, Double.parseDouble(mt.getStl_month()));
//						all_year=DoubleUtil.add(all_year, Double.parseDouble(mt.getStl_year()));
						mapSteelC.get(steel_code).setStl_month(mt.getStl_month());
						mapSteelC.get(steel_code).setStl_month_in(mt.getStl_month_in());
						mapSteelC.get(steel_code).setStl_year(mt.getStl_year());
						mapSteelC.get(steel_code).setStl_year_in(mt.getStl_year_in());
					}else{
						//sheet.mergeCells(i, j+2, i+2, 0);
						//sheet.addCell(new Label(i,j+2,steel_code,cellFormat1));
						sheet.addCell(new Label(i,j+2,"0",cellFormat));
						sheet.addCell(new Label(i+1,j+2,"0",cellFormat));
						sheet.addCell(new Label(i+2,j+2,"0",cellFormat));
//						sheet.addCell(new Label(i+1,j+2,"0",cellFormat));
//						sheet.addCell(new Label(i+2,j+2,"0",cellFormat));
						if(mapSteelC.get(steel_code).getStl_month()==null || "".equals(mapSteelC.get(steel_code).getStl_month())){
							sheet.addCell(new Label(i+4,j+2,"0",cellFormat));
						}else{
							sheet.addCell(new Label(i+4,j+2,mapSteelC.get(steel_code).getStl_month(),cellFormat));
						}
						if(mapSteelC.get(steel_code).getStl_year()==null || "".equals(mapSteelC.get(steel_code).getStl_year())){
							sheet.addCell(new Label(i+7,j+2,"0",cellFormat));
						}else{
							sheet.addCell(new Label(i+7,j+2,mapSteelC.get(steel_code).getStl_year(),cellFormat));
						}
						
						if(mapSteelC.get(steel_code).getStl_month_in()==null || "".equals(mapSteelC.get(steel_code).getStl_month_in())){
							sheet.addCell(new Label(i+3,j+2,"0",cellFormat));
							sheet.addCell(new Label(i+5,j+2,"0",cellFormat));
						}else{
							sheet.addCell(new Label(i+3,j+2,mapSteelC.get(steel_code).getStl_month_in(),cellFormat));
							Double stl_month_in=Double.parseDouble(mapSteelC.get(steel_code).getStl_month_in()+"");
							Double stl_month=Double.parseDouble(mapSteelC.get(steel_code).getStl_month()+"");
							if(stl_month_in==0){
								sheet.addCell(new Label(i+5,j+2,"0",cellFormat));
							}else{
								if("0.0".equals((""+DoubleUtil.round((stl_month/stl_month_in)*100, 2)))){
									sheet.addCell(new Label(i+5,j+2,"0",cellFormat));
								}else{
									sheet.addCell(new Label(i+5,j+2,""+DoubleUtil.round((stl_month/stl_month_in)*100, 2),cellFormat));
								}
							}
							
						}
						if(mapSteelC.get(steel_code).getStl_year_in()==null || "".equals(mapSteelC.get(steel_code).getStl_year_in())){
							sheet.addCell(new Label(i+6,j+2,"0",cellFormat));
							sheet.addCell(new Label(i+8,j+2,"0",cellFormat));
						}else{
							sheet.addCell(new Label(i+6,j+2,mapSteelC.get(steel_code).getStl_year_in(),cellFormat));
							Double stl_year_in=Double.parseDouble(mapSteelC.get(steel_code).getStl_year_in()+"");
							Double stl_year=Double.parseDouble(mapSteelC.get(steel_code).getStl_year()+"");
							if(stl_year_in==0){
								sheet.addCell(new Label(i+8,j+2,"0",cellFormat));
							}else{
								if("0.0".equals((""+DoubleUtil.round((stl_year/stl_year_in)*100, 2)))){
									sheet.addCell(new Label(i+8,j+2,"0",cellFormat));
								}else{
									sheet.addCell(new Label(i+8,j+2,""+DoubleUtil.round((stl_year/stl_year_in)*100, 2),cellFormat));
								}
							}
							
						}
						
					}
					i=i+9;
					if(mapSteelC.get(steel_code).getStl_month()!=null && !"".equals(mapSteelC.get(steel_code).getStl_month())){
						all_month=DoubleUtil.add(all_month, Double.parseDouble(mapSteelC.get(steel_code).getStl_month()));
					}
					if(mapSteelC.get(steel_code).getStl_year()!=null && !"".equals(mapSteelC.get(steel_code).getStl_year())){
						all_year=DoubleUtil.add(all_year, Double.parseDouble(mapSteelC.get(steel_code).getStl_year()));
					}
					if(mapSteelC.get(steel_code).getStl_month_in()!=null && !"".equals(mapSteelC.get(steel_code).getStl_month_in())){
						all_month_in=DoubleUtil.add(all_month_in, Double.parseDouble(mapSteelC.get(steel_code).getStl_month_in()));
					}
					if(mapSteelC.get(steel_code).getStl_year_in()!=null && !"".equals(mapSteelC.get(steel_code).getStl_year_in())){
						all_year_in=DoubleUtil.add(all_year_in, Double.parseDouble(mapSteelC.get(steel_code).getStl_year_in()));
					}
					
					
					mapSteelC_Total.get("total_mes").setStl_month(""+all_month);
					mapSteelC_Total.get("total_mes").setStl_year(""+all_year);
					
					mapSteelC_Total.get("total_mes").setStl_month_in(""+all_month_in);
					mapSteelC_Total.get("total_mes").setStl_year_in(""+all_year_in);
				}
			}else{
				for(String steel_code:lstr){
					//sheet.mergeCells(i, j+2, i+2, 0);
					//sheet.addCell(new Label(i,j+2,steel_code,cellFormat1));
					sheet.addCell(new Label(i,j+2,"0",cellFormat));
					sheet.addCell(new Label(i+1,j+2,"0",cellFormat));
					sheet.addCell(new Label(i+2,j+2,"0",cellFormat));
//					sheet.addCell(new Label(i+1,j+2,"0",cellFormat));
//					sheet.addCell(new Label(i+2,j+2,"0",cellFormat));
					if(mapSteelC.get(steel_code).getStl_month()==null || "".equals(mapSteelC.get(steel_code).getStl_month())){
						sheet.addCell(new Label(i+4,j+2,"0",cellFormat));
					}else{
						sheet.addCell(new Label(i+4,j+2,mapSteelC.get(steel_code).getStl_month(),cellFormat));
					}
					if(mapSteelC.get(steel_code).getStl_year()==null || "".equals(mapSteelC.get(steel_code).getStl_year())){
						sheet.addCell(new Label(i+7,j+2,"0",cellFormat));
					}else{
						sheet.addCell(new Label(i+7,j+2,mapSteelC.get(steel_code).getStl_year(),cellFormat));
					}
					
					if(mapSteelC.get(steel_code).getStl_month_in()==null || "".equals(mapSteelC.get(steel_code).getStl_month_in())){
						sheet.addCell(new Label(i+3,j+2,"0",cellFormat));
						sheet.addCell(new Label(i+5,j+2,"0",cellFormat));
					}else{
						sheet.addCell(new Label(i+3,j+2,mapSteelC.get(steel_code).getStl_month_in(),cellFormat));
						Double stl_month_in=Double.parseDouble(mapSteelC.get(steel_code).getStl_month_in()+"");
						Double stl_month=Double.parseDouble(mapSteelC.get(steel_code).getStl_month()+"");
						if(stl_month_in==0){
							sheet.addCell(new Label(i+5,j+2,"0",cellFormat));
						}else{
							if("0.0".equals((""+DoubleUtil.round((stl_month/stl_month_in)*100, 2)))){
								sheet.addCell(new Label(i+5,j+2,"0",cellFormat));
							}else{
								sheet.addCell(new Label(i+5,j+2,""+DoubleUtil.round((stl_month/stl_month_in)*100, 2),cellFormat));
							}
						}
					}
					if(mapSteelC.get(steel_code).getStl_year_in()==null || "".equals(mapSteelC.get(steel_code).getStl_year_in())){
						sheet.addCell(new Label(i+6,j+2,"0",cellFormat));
						sheet.addCell(new Label(i+8,j+2,"0",cellFormat));
					}else{
						sheet.addCell(new Label(i+6,j+2,mapSteelC.get(steel_code).getStl_year_in(),cellFormat));
						Double stl_year_in=Double.parseDouble(mapSteelC.get(steel_code).getStl_year_in()+"");
						Double stl_year=Double.parseDouble(mapSteelC.get(steel_code).getStl_year()+"");
						if(stl_year_in==0){
							sheet.addCell(new Label(i+8,j+2,"0",cellFormat));
						}else{
							if("0.0".equals((""+DoubleUtil.round((stl_year/stl_year_in)*100, 2)))){
								sheet.addCell(new Label(i+8,j+2,"0",cellFormat));
							}else{
								sheet.addCell(new Label(i+8,j+2,""+DoubleUtil.round((stl_year/stl_year_in)*100, 2),cellFormat));
							}
						}
					}
					
					i=i+9;
				}
			}
			//all_day==0?"0":all_day这种形式是为了防止在excel中显示0.0的情况
			
			sheet.addCell(new Label(i,j+2,""+(all_day_in==0?"0":all_day_in),cellFormat));
			sheet.addCell(new Label(i+1,j+2,""+(all_day==0?"0":all_day),cellFormat));
			//日成材率
			if(all_day_in==0){
				sheet.addCell(new Label(i+2,j+2,0+"",cellFormat));
			}else{
				//sheet.addCell(new Label(i+2,j+2,""+DoubleUtil.round((all_day/all_day_in)*100, 2),cellFormat));
				if("0.0".equals((""+DoubleUtil.round((all_day/all_day_in)*100, 2)))){
					sheet.addCell(new Label(i+2,j+2,0+"",cellFormat));
				}else{
					sheet.addCell(new Label(i+2,j+2,""+DoubleUtil.round((all_day/all_day_in)*100, 2),cellFormat));
				}
			}
			sheet.addCell(new Label(i+3,j+2,("0.0".equals(mapSteelC_Total.get("total_mes").getStl_month_in())?"0":mapSteelC_Total.get("total_mes").getStl_month_in()),cellFormat));
			sheet.addCell(new Label(i+4,j+2,("0.0".equals(mapSteelC_Total.get("total_mes").getStl_month())?"0":mapSteelC_Total.get("total_mes").getStl_month()),cellFormat));
			//月成材率
//			if(all_month_in==0){
//				sheet.addCell(new Label(i+5,j+2,0+"",cellFormat));
//			}else{
//				
//				sheet.addCell(new Label(i+5,j+2,""+DoubleUtil.round((all_month/all_month_in)*100, 2),cellFormat));
//			}
			if(mapSteelC_Total.get("total_mes").getStl_month_in()==null){
				mapSteelC_Total.get("total_mes").setStl_month_in("0");
			}
			if(mapSteelC_Total.get("total_mes").getStl_month()==null){
				mapSteelC_Total.get("total_mes").setStl_month("0");
			}
			Double stl_month_in=Double.parseDouble(mapSteelC_Total.get("total_mes").getStl_month_in()+"");
			Double stl_month=Double.parseDouble(mapSteelC_Total.get("total_mes").getStl_month()+"");
			if(stl_month_in==0){
				sheet.addCell(new Label(i+5,j+2,0+"",cellFormat));
			}else{
				//sheet.addCell(new Label(i+5,j+2,""+DoubleUtil.round((stl_month/stl_month_in)*100, 2),cellFormat));
				if("0.0".equals((""+DoubleUtil.round((stl_month/stl_month_in)*100, 2)))){
					sheet.addCell(new Label(i+5,j+2,0+"",cellFormat));
				}else{
					sheet.addCell(new Label(i+5,j+2,""+DoubleUtil.round((stl_month/stl_month_in)*100, 2),cellFormat));
				}
			}
			
			
			
			sheet.addCell(new Label(i+6,j+2,("0.0".equals(mapSteelC_Total.get("total_mes").getStl_year_in())?"0":mapSteelC_Total.get("total_mes").getStl_year_in()),cellFormat));
			sheet.addCell(new Label(i+7,j+2,("0.0".equals(mapSteelC_Total.get("total_mes").getStl_year())?"0":mapSteelC_Total.get("total_mes").getStl_year()),cellFormat));
			//年成材率
//			if(all_year_in==0){
//				sheet.addCell(new Label(i+8,j+2,0+"",cellFormat));
//			}else{
//				
//				sheet.addCell(new Label(i+8,j+2,""+DoubleUtil.round((all_year/all_year_in)*100, 2),cellFormat));
//			}
			if(mapSteelC_Total.get("total_mes").getStl_year_in()==null){
				mapSteelC_Total.get("total_mes").setStl_year_in("0");
			}
			if(mapSteelC_Total.get("total_mes").getStl_year_in()==null){
				mapSteelC_Total.get("total_mes").setStl_year("0");
			}
			Double stl_year_in=Double.parseDouble(mapSteelC_Total.get("total_mes").getStl_year_in()+"");
			Double stl_year=Double.parseDouble(mapSteelC_Total.get("total_mes").getStl_year()+"");
			if(stl_year_in==0){
				sheet.addCell(new Label(i+8,j+2,"0",cellFormat));
			}else{
				//sheet.addCell(new Label(i+8,j+2,""+DoubleUtil.round((stl_year/stl_year_in)*100, 2),cellFormat));
				if("0.0".equals((""+DoubleUtil.round((stl_year/stl_year_in)*100, 2)))){
					sheet.addCell(new Label(i+8,j+2,"0",cellFormat));
				}else{
					sheet.addCell(new Label(i+8,j+2,""+DoubleUtil.round((stl_year/stl_year_in)*100, 2),cellFormat));
				}
			}
			j++;
		}
		
		sheet.getSettings().setVerticalFreeze(1);  		//首行冻结
		sheet.getSettings().setVerticalFreeze(2);
		sheet.getSettings().setVerticalFreeze(3);
		//关闭
		workbook.write();
		workbook.close();
		rm.setData("/download?filename="+file);
	}
	
	

}
