/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-08-13
 * 描述            ：采购需求主表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
 ***********************************************************/
package market.Pms.Demand.impl;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

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 jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Pms.Demand.mapper.PurBudgetMMapper;
import market.Pms.Demand.mapper.PurDemandCMapper;
import market.Pms.Demand.mapper.PurDemandMMapper;
import market.Pms.Demand.service.PurDemandMService;
import market.Pms.Demand.vo.PurBudgetM;
import market.Pms.Demand.vo.PurDemandC;
import market.Pms.Demand.vo.PurDemandM;

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.mapper.UserMapper;
import com.hnshituo.core.auth.service.HomeService;
import com.hnshituo.core.auth.vo.User;

@RemoteService(path = "PurDemandMService")
public class PurDemandMServiceImpl extends BaseServiceImpl<PurDemandM, Object>
		implements PurDemandMService {
	/**
	 * 资材备件特殊处理部门获取方法，目前处理方法为获取部门信息分出不同信息查询（限资材备件科），
	 * 平台更新后，可维护此处，以及后查询及新增操作代码（已注释有《资材备件科》）。
	 * */
	public ResultModel doUser(CommonPage<PurDemandM> oci){
		ResultModel rm = new ResultModel();
		String userId = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_ID);
//		String userName = (String) this.getSession().getAttribute(
//				HomeService.SESSION_USER_NAME);
		try {
			User  user=this.getMapper(UserMapper.class).findById(userId);
			
			rm.setData(user.getDept_name());
			rm.setMsgInfo("返回部门名称");
	        rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("查询部门名称失败！");
			throw e;
		}	
		return rm;
		
	}
	/**
	 * 需求信息查询
	 * 
	 * @param oci
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<PurDemandM> oci) {
		ResultModel rm = new ResultModel();
		PurDemandM pdm = oci.getObject();
//		String demand_no = pdm.getDemand_no();
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		String startTime = oci.getStartTime();
		String endTime = oci.getEndTime();
		String userId = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_ID);
//		String userName = (String) this.getSession().getAttribute(
//				HomeService.SESSION_USER_NAME);
		try {

//			if (startTime != null && !"".equals(startTime)) {
//				startTime = sdf.format(oci.getStartTime());
//			}
//			if (endTime != null && !"".equals(endTime)) {
//				endTime = sdf.format(oci.getEndTime());
//			}
			User user2=this.getMapper(UserMapper.class).findById(userId);
				String str="";
				if("资材备件科".equals(user2.getDept_name())||"原料科".equals(user2.getDept_name())){
					str+=user2.getDept_name();
				}                				 
				List<PurDemandM> list = this.getMapper(PurDemandMMapper.class)
						.findPurDemandM(pdm, startTime, endTime,str);
				rm.setData(list);
				rm.setState("200");
			
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败，请联系管理员");
			throw ex;
		}
		return rm;
	}
	/*public ResultModel doQueryWeb(CommonPage<PurDemandM> oci) {
		ResultModel rm = new ResultModel();
		PurDemandM pdm = oci.getObject();
		String demand_no = pdm.getDemand_no();
		String urgency_type_name = pdm.getUrgency_type_name();// 紧急程度
		String validflag = pdm.getValidflag();// 需求状态;
		String status = pdm.getStatus();// 评审状态
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		String startTime = oci.getStartTime();
		String endTime = oci.getEndTime();
		try {
			String sWhere = " 1=1 ";
			if (startTime != null && !"".equals(startTime)) {
				startTime = sdf.format(oci.getStartTime());
				sWhere += " and to_char(bal_year_month, 'yyyy-mm-dd hh24:mi:ss') >='"
						+ startTime + "'";
			}
			if (endTime != null && !"".equals(endTime)) {
				endTime = sdf.format(oci.getEndTime());
				sWhere += " and to_char(bal_year_month, 'yyyy-mm-dd hh24:mi:ss') <= '"
						+ endTime + "'";
			}
			if (demand_no != null && !"".equals(demand_no)) {
				sWhere += "and demand_no like '%" + demand_no + "%'";
			}
			if (urgency_type_name != null && !"".equals(urgency_type_name)) {
				sWhere += "and urgency_type_name = '" + urgency_type_name + "'";
			}
			if (validflag != null && !"".equals(validflag)) {
				sWhere += "and validflag = '" + validflag + "'";
			}
			if (status != null && !"".equals(status)) {
				sWhere += "and status = '" + status + "'";
			}
			List<PurDemandM> list = this.getMapper(PurDemandMMapper.class)
					.findWithCondition(pdm, sWhere, "create_time desc", 0, 0);
			rm.setData(list);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败，请联系管理员");
		}
		return rm;
	}*/

	/**
	 * 需求信息新增
	 * 
	 * @param oci
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doAddWeb(CommonPage<PurDemandM> oci) throws Exception {
		String userId = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		if (oci.getObjectList() == null || oci.getObjectList().size() == 0) {
			rm.setState("500");
			rm.setMsgInfo("请传入需新增的数据！");
			return rm;
		}
		User user2=this.getMapper(UserMapper.class).findById(userId);
		try {
			for (PurDemandM pdm : oci.getObjectList()) {
				String demand_no = "XQ"
						+ new SimpleDateFormat("yyyyMM").format(new Date());
				String maxD = this.getMapper(PurDemandMMapper.class)
						.findMaxDemandNo(demand_no);
				if (maxD != null && maxD.length() > 4) {
					String seq = maxD.substring(maxD.length() - 4);
					int iseq = Integer.parseInt(seq) + 1;
					demand_no += String.format("%04d", iseq);
				} else {
					demand_no += "0001";
				}
				pdm.setDept_code(user2.getDept_id());//设置部门
				pdm.setDept_desc(user2.getDept_name());
				pdm.setDemand_no(demand_no);
				pdm.setValidflag("10");// 默认10草稿
				pdm.setStatus("00"); // 默认00代提报
				pdm.setCreate_userid(userId);
				pdm.setCreate_name(userName);
				pdm.setCreate_time(new Date());
				this.getMapper(PurDemandMMapper.class).insert(pdm);
			}

			rm.setState("200");
			rm.setMsgInfo("新增成功！");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("新增失败，请联系管理员");
			throw ex;
		}
		return rm;
	}

	/**
	 * 需求信息提报
	 * 
	 * @param oci
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel upPurDemandM(CommonPage<PurDemandM> oci)
			throws Exception {
		String userId = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		if (oci.getObjectList() == null || oci.getObjectList().size() == 0) {
			rm.setState("500");
			rm.setMsgInfo("请选择数据！");
			return rm;
		}
		try {
			for (PurDemandM pdm : oci.getObjectList()) {
				PurDemandM pdm2=this.getMapper(PurDemandMMapper.class).findById(pdm.getDemand_no());
				pdm.setRemark(pdm2.getRemark());
				if ("10".equals(pdm2.getValidflag())
						&& ("00".equals(pdm2.getStatus()) || "40".equals(pdm2
								.getStatus()))) {
					// 主表
					pdm2.setStatus("10"); // 暂时提报即审批通过并且生效
					pdm2.setValidflag("20"); // 暂时生效
					pdm2.setUpdate_userid(userId);
					pdm2.setUpdate_name(userName);
					pdm2.setUpdate_time(new Date());
					this.getMapper(PurDemandMMapper.class).update(pdm2);
					// 从表
					PurDemandC pdc = new PurDemandC();
					pdc.setDemand_no(pdm.getDemand_no());
					List<PurDemandC> pdcList = this.getMapper(
							PurDemandCMapper.class).find(pdc, 0, 0);
					if (pdcList.size() > 0) {
						for (PurDemandC pDc : pdcList) {
							pDc.setStatus("10"); // 暂时提报即审批通过并且生效
							pDc.setValidflag("20"); // 暂时生效
							pDc.setUpdate_userid(userId);
							pDc.setUpdate_name(userName);
							pDc.setUpdate_time(new Date());
							this.getMapper(PurDemandCMapper.class).update(pDc);
						}
					}
				} else {
					rm.setState("500");
					rm.setMsgInfo("提报失败，请联系管理员");
					return rm;
				}
			}

			rm.setState("200");
			rm.setMsgInfo("提报成功！");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("提报失败，请联系管理员");
			throw ex;
		}
		return rm;
	}

	/**
	 * 需求信息审批
	 * 
	 * @param oci
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel appPurDemandM(CommonPage<PurDemandM> oci)
			throws Exception {
		String userId = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		if (oci.getObjectList() == null || oci.getObjectList().size() == 0) {
			rm.setState("500");
			rm.setMsgInfo("请选择数据！");
			return rm;
		}
		String memo1=oci.getMemo1();
		if(memo1==null&&"".equals(memo1)){
			rm.setState("500");
			rm.setMsgInfo("请选择操作！");
			return rm;
		}
		try {
			for (PurDemandM pdm2 : oci.getObjectList()) {
				PurDemandM pdm=this.getMapper(PurDemandMMapper.class).findById(pdm2.getDemand_no());
				pdm.setRemark(pdm2.getRemark());
				if ("10".equals(pdm.getStatus()) ) {
					if("1".equals(memo1)){
					// 主表
					pdm.setStatus("50"); //审批通过
					pdm.setUpdate_userid(userId);
					pdm.setUpdate_name(userName);
					pdm.setUpdate_time(new Date());
					this.getMapper(PurDemandMMapper.class).update(pdm);
					// 从表
					PurDemandC pdc = new PurDemandC();
					pdc.setDemand_no(pdm.getDemand_no());
					List<PurDemandC> pdcList = this.getMapper(
							PurDemandCMapper.class).find(pdc, 0, 0);
					if (pdcList.size() > 0) {
						for (PurDemandC pDc : pdcList) {
							if ("10".equals(pDc.getStatus())) {
							pDc.setStatus("50"); // 审批通过
							pDc.setUpdate_userid(userId);
							pDc.setUpdate_name(userName);
							pDc.setUpdate_time(new Date());
							this.getMapper(PurDemandCMapper.class).update(pDc);
							}
						}
					}
					}
					if("2".equals(memo1)){
						// 主表
						pdm.setStatus("40"); //审批退回
						pdm.setUpdate_userid(userId);
						pdm.setUpdate_name(userName);
						pdm.setUpdate_time(new Date());
						this.getMapper(PurDemandMMapper.class).update(pdm);
						// 从表
						PurDemandC pdc = new PurDemandC();
						pdc.setDemand_no(pdm.getDemand_no());
						List<PurDemandC> pdcList = this.getMapper(
								PurDemandCMapper.class).find(pdc, 0, 0);
						if (pdcList.size() > 0) {
							for (PurDemandC pDc : pdcList) {
								pDc.setStatus("40"); // 审批退回
								pDc.setUpdate_userid(userId);
								pDc.setUpdate_name(userName);
								pDc.setUpdate_time(new Date());
								this.getMapper(PurDemandCMapper.class).update(pDc);
							}
						}
						}
					 if("3".equals(memo1)){
						// 主表
						pdm.setStatus("30"); //审批拒绝
						pdm.setValidflag("00");//数据为无效
						pdm.setUpdate_userid(userId);
						pdm.setUpdate_name(userName);
						pdm.setUpdate_time(new Date());
						this.getMapper(PurDemandMMapper.class).update(pdm);
						// 从表
						PurDemandC pdc = new PurDemandC();
						pdc.setDemand_no(pdm.getDemand_no());
						List<PurDemandC> pdcList = this.getMapper(
								PurDemandCMapper.class).find(pdc, 0, 0);
						if (pdcList.size() > 0) {
							for (PurDemandC pDc : pdcList) {
								pDc.setStatus("30"); // 审批拒绝
								pDc.setValidflag("00");//数据为无效
								pDc.setUpdate_userid(userId);
								pDc.setUpdate_name(userName);
								pDc.setUpdate_time(new Date());
								this.getMapper(PurDemandCMapper.class).update(pDc);
							}
						}
						}
				} else {
					rm.setState("500");
					rm.setMsgInfo("数据状态为待审批，请刷新界面！");
					return rm;
				}
			}

			rm.setState("200");
			rm.setMsgInfo("提报成功！");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("提报失败，请联系管理员");
			throw ex;
		}
		return rm;
	}
	/**
	 * 需求信息删除
	 * 
	 * @param oci
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doDelete(CommonPage<PurDemandM> oci) throws Exception {
		ResultModel rm = new ResultModel();
		String userId = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_NAME);
		if (oci.getObjectList() == null || oci.getObjectList().size() == 0) {
			rm.setState("500");
			rm.setMsgInfo("请选择数据！");
			return rm;
		}
		try {
			for (PurDemandM m : oci.getObjectList()) {
				if ("00".equals(m.getValidflag())||"10".equals(m.getValidflag())) { // 状态 无效、草稿
					// 修改主表
					m.setValidflag("00"); // 无效
					m.setDelete_userid(userId);
					m.setDelete_name(userName);
					m.setDelete_time(new Date());
					this.getMapper(PurDemandMMapper.class).delete(m.getDemand_no());
					// 修改从表				
					this.getMapper(PurDemandCMapper.class).deleteByDemandno(m.getDemand_no());
					
				} else {
					rm.setState("500");
					rm.setMsgInfo("操作失败！当前数据不可删除！");
					return rm;
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("操作失败！请联系管理员！" + e.getMessage());
			throw e;
		}

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

	/**
	 * 需求信息提报撤销
	 * 
	 * @param oci
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel disPurDemandM(CommonPage<PurDemandM> oci)
			throws Exception {
		String userId = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		if (oci.getObjectList() == null || oci.getObjectList().size() == 0) {
			rm.setState("500");
			rm.setMsgInfo("请选择数据！");
			return rm;
		}
		try {
			for (PurDemandM pdm : oci.getObjectList()) {
				PurDemandM pdm2=this.getMapper(PurDemandMMapper.class).findById(pdm.getDemand_no());
				if ("20".equals(pdm2.getValidflag())
						&& ("10".equals(pdm2.getStatus()) || "40".equals(pdm2
								.getStatus()))) {
					// 主表
					pdm2.setStatus("00"); //置为待提报
					pdm2.setValidflag("10"); // 置为草稿状态
					pdm2.setUpdate_userid(userId);
					pdm2.setUpdate_name(userName);
					pdm2.setUpdate_time(new Date());
					this.getMapper(PurDemandMMapper.class).update(pdm2);
					// 从表
					PurDemandC pdc = new PurDemandC();
					pdc.setDemand_no(pdm.getDemand_no());
					List<PurDemandC> pdcList = this.getMapper(
							PurDemandCMapper.class).find(pdc, 0, 0);
					if (pdcList.size() > 0) {
						for (PurDemandC pDc : pdcList) {
							pDc.setStatus("00"); 
							pDc.setValidflag("10"); 
							pDc.setUpdate_userid(userId);
							pDc.setUpdate_name(userName);
							pDc.setUpdate_time(new Date());
							this.getMapper(PurDemandCMapper.class).update(pDc);
						}
					}
				} else {
					rm.setState("500");
					rm.setMsgInfo("当前数据不能撤销提报");
					return rm;
				}
			}

			rm.setState("200");
			rm.setMsgInfo("提报成功！");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("提报失败，请联系管理员");
			throw ex;
		}
		return rm;
	}

	/**
	 * 可用预算信息查询
	 * 
	 * @param oci
	 * @return
	 */
	public ResultModel doQueryAmount(CommonPage<PurDemandM> oci) {
		ResultModel rm = new ResultModel();
		PurDemandM pdm = oci.getObject();
		String departname = pdm.getApply_org_name();
		try {
			PurBudgetM list = this.getMapper(PurBudgetMMapper.class)
					.totalAmount(departname);
			double totalAmount=0;
			if(list!=null&&!"".equals(list)){
			totalAmount=list.getM1()+list.getM2()+list.getM3()+list.getM4()+
					list.getM5()+list.getM6()+list.getM7()+list.getM8()
					+list.getM9()+list.getM10()+list.getM11()+list.getM12();
			}		
			rm.setData(totalAmount);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败，请联系管理员");
			throw ex;
		}
		return rm;
	}
	
	/**
	 * 需求信息导出EXCEL
	 * 
	 * @param oci
	 * @return
	 */
	public ResultModel doExcel(CommonPage<PurDemandM> oci) {
		ResultModel rm = new ResultModel();
		PurDemandM pdm = oci.getObject();
//		String demand_no = pdm.getDemand_no();
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		String startTime = oci.getStartTime();
		String endTime = oci.getEndTime();
		try {

//			if (startTime != null && !"".equals(startTime)) {
//				startTime = sdf.format(oci.getStartTime());
//			}
//			if (endTime != null && !"".equals(endTime)) {
//				endTime = sdf.format(oci.getEndTime());
//			}		
			List<PurDemandM> list = this.getMapper(PurDemandMMapper.class)
					.findPurDemandM(pdm, startTime, endTime,null);
			// 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();
			}
			generateExcel(rm, list, path);
			rm.setState("200");
			rm.setMsgInfo("导出成功！");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("导出失败，请联系管理员"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 *
	 * 报表打印
	 * **/
	private void generateExcel(ResultModel rm, List<PurDemandM> list, String path)
			throws IOException, WriteException, RowsExceededException {
		Calendar cal = Calendar.getInstance();
		String year=cal.get(Calendar.YEAR)+"年";
		String month=(cal.get(Calendar.MONTH)+1)+"月";
		String dept=cal.get(Calendar.DATE)+"日";
		String filename = year + month + dept + "需求信息报表";
		WritableWorkbook workbook;
		String file=filename+".xls";  //文件名
		//3. 操作EXCEL文件
		workbook = Workbook.createWorkbook(new File(path+file));
		WritableSheet sheet = workbook.createSheet("需求信息报表", 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(0, 0, 0, 1);
		sheet.addCell(new Label(0,0,"需求号",cellFormat1));

		sheet.mergeCells(1, 0, 1, 1);
		sheet.addCell(new Label(1,0,"需求分类",cellFormat1));

		sheet.mergeCells(2, 0, 2, 1);
		sheet.addCell(new Label(2,0,"需求状态",cellFormat1));

		sheet.mergeCells(3, 0, 3, 1);
		sheet.addCell(new Label(3,0,"审批状态",cellFormat1));

		sheet.mergeCells(4, 0, 4, 1);
		sheet.addCell(new Label(4,0,"预算总金额",cellFormat1));

		sheet.mergeCells(5, 0, 5, 1);
		sheet.addCell(new Label(5,0,"需求年月",cellFormat1));

		sheet.mergeCells(6, 0, 6, 1);
		sheet.addCell(new Label(6,0,"紧急程度",cellFormat1));

		sheet.mergeCells(7, 0, 7, 1);
		sheet.addCell(new Label(7,0,"申请单位",cellFormat1));

		sheet.mergeCells(8, 0, 8, 1);
		sheet.addCell(new Label(8,0,"申请人",cellFormat1));

		sheet.mergeCells(9, 0, 9, 1);
		sheet.addCell(new Label(9,0,"申请时间",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);
		//时间转字符串的格式
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		double totalTheoryWeight = 0d, totalBalance_price=0d,totalAmount = 0d;
		for(int i = 0; i < list.size(); i++) {
			//需求号
			if(list.get(i).getDemand_no() != null && !"".equals(list.get(i).getDemand_no())) {
				sheet.addCell(new Label(0,i+2,list.get(i).getDemand_no(),cellFormat));
			}
			//需求分类
			if(list.get(i).getPru_type_name() != null && !"".equals(list.get(i).getPru_type_name() )) {
				sheet.addCell(new Label(1,i+2,list.get(i).getPru_type_name() ,cellFormat));
			}
			//需求状态
			if(list.get(i).getValidflag() != null && !"".equals(list.get(i).getValidflag() )) {
				if("10".equals(list.get(i).getValidflag())){
				      sheet.addCell(new Label(2,i+2,"草稿" ,cellFormat));
				}else if("20".equals(list.get(i).getValidflag())){
					  sheet.addCell(new Label(2,i+2,"生效" ,cellFormat));
				}
			}
			//审批状态
			if(list.get(i).getStatus() != null && !"".equals(list.get(i).getStatus() )) {
				if("00".equals(list.get(i).getStatus())){
				      sheet.addCell(new Label(3,i+2,"待提报" ,cellFormat));
				}else if("10".equals(list.get(i).getStatus())){
					  sheet.addCell(new Label(3,i+2,"待审批" ,cellFormat));
				}else if("20".equals(list.get(i).getStatus())){
					  sheet.addCell(new Label(3,i+2,"审批中" ,cellFormat));
				}else if("30".equals(list.get(i).getStatus())){
					  sheet.addCell(new Label(3,i+2,"审批拒绝" ,cellFormat));
				}else if("40".equals(list.get(i).getStatus())){
					  sheet.addCell(new Label(3,i+2,"审批退回" ,cellFormat));
				}else if("50".equals(list.get(i).getStatus())){
					  sheet.addCell(new Label(3,i+2,"审批通过" ,cellFormat));
				}
			}
			//预算总金额
			if(list.get(i).getTotalbudget_amt() != null && !"".equals(list.get(i).getTotalbudget_amt() )) {
				sheet.addCell(new Label(4,i+2," "+list.get(i).getTotalbudget_amt(),cellFormat));
			}
			//需求年月
			if(list.get(i).getBal_year_month() != null && !"".equals(list.get(i).getBal_year_month() )) {
				sheet.addCell(new Label(5,i+2," "+list.get(i).getBal_year_month(),cellFormat));
			}
			//紧急程度
			if(list.get(i).getUrgency_type_name() != null && !"".equals(list.get(i).getUrgency_type_name())) {
				sheet.addCell(new Label(6,i+2," "+list.get(i).getUrgency_type_name(),cellFormat));
			}
			//申请单位
			if(list.get(i).getApply_org_name() != null && !"".equals(list.get(i).getApply_org_name())) {
				sheet.addCell(new Label(7,i+2," "+list.get(i).getApply_org_name(),cellFormat));
			}
			//申请人
			if(list.get(i).getApply_org_name() != null && !"".equals(list.get(i).getApply_org_name())) {
				sheet.addCell(new Label(7,i+2," "+list.get(i).getApply_org_name(),cellFormat));
			}
			//申请时间
			if(list.get(i).getCreate_time() != null && !"".equals(list.get(i).getCreate_time() )) {
				sheet.addCell(new Label(7,i+2,sdf.format(list.get(i).getCreate_time()) ,cellFormat));
			}
			
			
		}

		sheet.addCell(new Label(0,list.size()+2,"总计",cellFormat));
		WritableCellFormat cellFormat2 = new WritableCellFormat(font); 
		cellFormat2.setVerticalAlignment(VerticalAlignment.CENTRE);
//		DecimalFormat df=new DecimalFormat("0.00");
		sheet.addCell(new Label(1,list.size()+2,""+list.size(),cellFormat2));//统计总条数
		sheet.getSettings().setVerticalFreeze(1);  		//首行冻结
		sheet.getSettings().setVerticalFreeze(2);
		//关闭
		workbook.write();
		workbook.close();

		rm.setData("/download?filename="+file);
	}

}
