package com.gaowj.procdefinition.action;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gaowj.business.ProcdefiChooseDeptBusiness;
import com.gaowj.business.ProcdefiChooseUserBusiness;
import com.gaowj.business.ProcdefinitionBusiness;
import com.gaowj.business.ProcdefinitionInitLinkBusiness;
import com.gaowj.business.action.BasicAction;
import com.gaowj.business.comp.BusinessManager;
import com.gaowj.business.exception.BusinessException;
import com.gaowj.business.util.Page;
import com.gaowj.business.util.QueryParamUtil;
import com.gaowj.business.util.RequestUtils;
import com.gaowj.business.util.Status;

import net.sf.json.JSONObject;

public class dataAction extends BasicAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -5059421324729466816L;
	
	private StringBuffer filename=new StringBuffer();
	private InputStream is;
	
	private JSONObject jsonObject = new JSONObject();
	private int rows = 20;// 每页显示的记录数
	private int page = 1;// 当前第几页
	public JSONObject getJsonObject() {
		return jsonObject;
	}
	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	
	/**
	 * 分页获取记录
	 * @param entity
	 * @throws BusinessException
	 */
	public String list() throws BusinessException {
		ProcdefinitionBusiness business = BusinessManager.getBusiness(ProcdefinitionBusiness.class);
		Map<String, Object> query = RequestUtils.getUpdateFieldMap(request,RequestUtils.UPDATEDATAFIELDPREFIX);
		try {
			query = QueryParamUtil.StringToDate(query);
			Page<Map<String, Object>> pageRows = business.list_procde_finition(pageNo,pageSize, query);
			setPageCount((pageRows.getCount() - 1) / pageSize + 1);
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("rowSet", QueryParamUtil.DateToString(pageRows.getItems()));
			data.put("pageNo", pageNo);
			data.put("pageSize", pageSize);
			data.put("pageCount", pageCount);
			data.put("rows", data.get("rowSet"));
			data.put("total", pageRows.getCount());
			jsonObject = new JSONObject();
			jsonObject.putAll(data);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "json";
	}
	
	public String listAll() throws BusinessException {
		ProcdefinitionBusiness business = BusinessManager.getBusiness(ProcdefinitionBusiness.class);
		Map<String, Object> query = RequestUtils.getUpdateFieldMap(request,RequestUtils.UPDATEDATAFIELDPREFIX);
		try {
			query = QueryParamUtil.StringToDate(query);
			List<Map<String, Object>> listRows = business.list_procde_finition( query);
			setPageCount((listRows.size() - 1) / pageSize + 1);
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("rowSet", QueryParamUtil.DateToString(listRows));
			data.put("pageNo", pageNo);
			data.put("pageSize", pageSize);
			data.put("pageCount", pageCount);
			data.put("rows", data.get("rowSet"));
			data.put("total", listRows.size());
			jsonObject = new JSONObject();
			jsonObject.putAll(data);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "json";
	}
	
	/**
	 * 获取流程定义中的表模板
	 * @return
	 */
	public String findFormModel() throws BusinessException {
		ProcdefinitionBusiness business = BusinessManager.getBusiness(ProcdefinitionBusiness.class);
		Map<String, Object> query = RequestUtils.getUpdateFieldMap(request,RequestUtils.UPDATEDATAFIELDPREFIX);
		jsonObject = new JSONObject();
		if(query.get("PROCDEFINITION_UUID")==null){
			query.put("PROCDEFINITION_UUID","");
		}
		Map<String,Object> model=business.findFormModel(query.get("PROCDEFINITION_UUID").toString());
		jsonObject.put("MODEL", model);
		return "json";
	}
	
	/**
	 * 获取流程图片
	 * @return
	 * @throws BusinessException
	 */
	public String showProcdefinitionImg() throws BusinessException{
		ProcdefinitionBusiness business = BusinessManager.getBusiness(ProcdefinitionBusiness.class);
		is=business.viewPic(request.getParameter("deploymentId"),filename);
		return "download";
		/*ProcdefinitionBusiness business = BusinessManager.getBusiness(ProcdefinitionBusiness.class);
		InputStream is=business.viewPic(request.getParameter("deploymentId"),filename);
		return is;*/
	}
	
	public InputStream getInputStream() throws Exception {
		return is;
	}
	
	public String getFileName() throws UnsupportedEncodingException{
		String name=filename.toString();
		String agent = request.getHeader("User-agent");
		// 如果浏览器是IE浏览器,就得进行编码转换
		if (agent.contains("MSIE")) {
			name = URLEncoder.encode(name, "UTF-8");
		} else {
			name = new String(name.getBytes(), "ISO-8859-1");
		}
		return name;
	}
	
	public String getLinksList() throws BusinessException{
		String DEPLOYMENT_ID=RequestUtils.getString(request, "DEPLOYMENT_ID");
		ProcdefinitionInitLinkBusiness linkBusiness =BusinessManager.getBusiness(ProcdefinitionInitLinkBusiness.class);
		Map<String, Object> query=new HashMap<String, Object>();
		query.put("DEPLOYMENT_ID", DEPLOYMENT_ID);
		Page<Map<String, Object>>  pageRows=linkBusiness.list_procde_finition(pageNo, pageSize, query );
		setPageCount((pageRows.getCount() - 1) / pageSize + 1);
		Map<String, Object> data = new HashMap<String, Object>();
		try {
			data.put("rowSet", QueryParamUtil.DateToString(pageRows.getItems()));
			data.put("pageNo", pageNo);
			data.put("pageSize", pageSize);
			data.put("pageCount", pageCount);
			data.put("rows", data.get("rowSet"));
			data.put("total", pageRows.getCount());
			jsonObject = new JSONObject();
			jsonObject.putAll(data);
			jsonObject.putAll(Status.getStatusSuccessMessage("成功"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			jsonObject.putAll(Status.getStatusSuccessMessage("失败"));
		}
	
		return "json";
	}
	
	public String getChooseUser()throws BusinessException{
		ProcdefiChooseUserBusiness business =BusinessManager.getBusiness(ProcdefiChooseUserBusiness.class);
		Map<String, Object> query=new HashMap<String, Object>();
		query.put("KEY_ID", RequestUtils.getString(request, "key_id"));
		Page<Map<String, Object>>  pageRows=business.list_procdefi_chooseUser(pageNo, pageSize, query );
		setPageCount((pageRows.getCount() - 1) / pageSize + 1);
		Map<String, Object> data = new HashMap<String, Object>();
		try {
			data.put("rowSet", QueryParamUtil.DateToString(pageRows.getItems()));
			data.put("pageNo", pageNo);
			data.put("pageSize", pageSize);
			data.put("pageCount", pageCount);
			data.put("rows", data.get("rowSet"));
			data.put("total", pageRows.getCount());
			jsonObject = new JSONObject();
			jsonObject.putAll(data);
			jsonObject.putAll(Status.getStatusSuccessMessage("成功"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			jsonObject.putAll(Status.getStatusSuccessMessage("失败"));
		}
	
		return "json";
	}
	
	public String getChooseDeparment()throws BusinessException{
		ProcdefiChooseDeptBusiness business =BusinessManager.getBusiness(ProcdefiChooseDeptBusiness.class);
		Map<String, Object> query=new HashMap<String, Object>();
		query.put("KEY_ID", RequestUtils.getString(request, "key_id"));
		Page<Map<String, Object>>  pageRows=business.list_procdefi_chooseDept(pageNo, pageSize, query );
		setPageCount((pageRows.getCount() - 1) / pageSize + 1);
		Map<String, Object> data = new HashMap<String, Object>();
		try {
			data.put("rowSet", QueryParamUtil.DateToString(pageRows.getItems()));
			data.put("pageNo", pageNo);
			data.put("pageSize", pageSize);
			data.put("pageCount", pageCount);
			data.put("rows", data.get("rowSet"));
			data.put("total", pageRows.getCount());
			jsonObject = new JSONObject();
			jsonObject.putAll(data);
			jsonObject.putAll(Status.getStatusSuccessMessage("成功"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			jsonObject.putAll(Status.getStatusSuccessMessage("失败"));
		}
	
		return "json";
	}
	
	public String listShowField() throws BusinessException{
		ProcdefinitionBusiness business = BusinessManager.getBusiness(ProcdefinitionBusiness.class);
		//流程定义id
		String procDefinitionId=RequestUtils.getString(request, "PROC_DEFINITION_ID");
		List<Map<String, Object>>  listField=business.listShowField(procDefinitionId);
		Map<String, Object> data = new HashMap<String, Object>();
		try {
			data.put("rowSet", QueryParamUtil.DateToString(listField));
			data.put("pageNo", pageNo);
			data.put("pageSize", pageSize);
			data.put("pageCount", pageCount);
			data.put("rows", data.get("rowSet"));
			data.put("total", listField.size());
			jsonObject = new JSONObject();
			jsonObject.putAll(data);
			jsonObject.putAll(Status.getStatusSuccessMessage("成功"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			jsonObject.putAll(Status.getStatusSuccessMessage("失败"));
		}
	
		return "json";
	}
}