package com.jw.app.usertest.action;

import java.io.InputStream;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

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.RequestUtils;
import com.jw.app.business.UnitBusiness;
import com.jw.app.business.UserBusiness;
import com.jw.app.business.UserTestBusiness;
import com.jw.app.business.utils.QueryParamUtil;
import com.jw.app.common.EasyuiJsonUtils;
import com.jw.app.common.ZtreeJsonUtils;

import net.sf.json.JSONObject;

public class dataAction extends BasicAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6083279531948261141L;

	private JSONObject jsonObject = new JSONObject();
	private int rows = 20;// 每页显示的记录数
	private int page = 1;// 当前第几页
	private String dataReturn;
	// output属性
	private InputStream inputStream; // 这个名字和struts.xml中对应，不能写错

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public JSONObject getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}

	public String getDataReturn() {
		return dataReturn;
	}

	public void setDataReturn(String dataReturn) {
		this.dataReturn = dataReturn;
	}

	public int getRows() {
		rows = getPageSize();
		return rows;
	}

	public void setRows(int rows) {
		setPageSize(rows);
		this.rows = getPageSize();
	}

	public int getPage() {
		page = getPageNo();
		return page;
	}

	public void setPage(int page) {
		setPageNo(page);
		this.page = getPageNo();
	}

	public String list() throws BusinessException {
		UserTestBusiness business = BusinessManager.getBusiness(UserTestBusiness.class);
		Map<String, Object> query = RequestUtils.getUpdateFieldMap(request,
				RequestUtils.UPDATEDATAFIELDPREFIX);

		// 找出所有子级单位
		if (query.get("UNIT_ID_IS_PARENT") != null) {
			UnitBusiness businessUnit = BusinessManager
					.getBusiness(UnitBusiness.class);
			List<Map<String, Object>> listData = businessUnit.list_unit(null);
			List<String> list = EasyuiJsonUtils.getParentsData(listData,
					(String) query.get("UNIT_ID_IS_PARENT"));
			query.put("UNIT_ID_IS_PARENT", list);
		}
		//
		try {
			query = QueryParamUtil.StringToDate(query);
			query.put("DELETE_FLAG", "0");
			Page<Map<String, Object>> pageRows = business.list_usertest(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 {
		UserTestBusiness business = BusinessManager.getBusiness(UserTestBusiness.class);
		Map<String, Object> query = RequestUtils.getUpdateFieldMap(request,
				RequestUtils.UPDATEDATAFIELDPREFIX);
		// 找出所有子级单位
		if (query.get("UNIT_ID_IS_PARENT") != null) {
			UnitBusiness businessUnit = BusinessManager
					.getBusiness(UnitBusiness.class);
			List<Map<String, Object>> listData = businessUnit.list_unit(null);
			List<String> list = EasyuiJsonUtils.getParentsData(listData,
					(String) query.get("UNIT_ID_IS_PARENT"));
			query.put("UNIT_ID_IS_PARENT", list);
		}
		//
		try {
			query = QueryParamUtil.StringToDate(query);
			query.put("DELETE_FLAG", "0");
			List<Map<String, Object>> listData = business.list_usertest(query);
			int listDataCount = business.listCount_usertest(query);
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("rowSet", QueryParamUtil.DateToString(listData));
			data.put("pageNo", 1);
			data.put("pageSize", listDataCount);
			data.put("pageCount", listDataCount);
			data.put("rows", data.get("rowSet"));
			data.put("total", listDataCount);
			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 listMy() throws BusinessException {

		UserTestBusiness business = BusinessManager.getBusiness(UserTestBusiness.class);
		Map<String, Object> query = RequestUtils.getUpdateFieldMap(request,
				RequestUtils.UPDATEDATAFIELDPREFIX);
		jsonObject = new JSONObject();
		// 找出所有子级单位
		UnitBusiness businessUnit = BusinessManager
				.getBusiness(UnitBusiness.class);
		List<Map<String, Object>> listData = businessUnit.list_unit(null);
		if (query.get("TREE_CLICK") == null
				|| StringUtils.equals((String) query.get("UNIT_ID_IS_PARENT"),
						"A")) {

			listData = ZtreeJsonUtils.getUserGridJsonData(listData);
			List<String> list = new ArrayList<String>();
			for (Map<String, Object> m : listData) {
				list.add((String) m.get("UUID"));
			}
			query.put("UNIT_ID_IS_PARENT", list);
		} else {
			List<String> list = EasyuiJsonUtils.getParentsData(listData,
					(String) query.get("UNIT_ID_IS_PARENT"));
			query.put("UNIT_ID_IS_PARENT", list);
		}
		
		try {
			query = QueryParamUtil.StringToDate(query);
			query.put("DELETE_FLAG", "0");
			Page<Map<String, Object>> pageRows = business.list_usertest(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.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 listtest() throws BusinessException{
		UserTestBusiness business = BusinessManager.getBusiness(UserTestBusiness.class);
		String list_test_unit = business.list_test_unit("dept0051004");
		Map<String, Object> data = new HashMap<String, Object>();
		//data.put("dept0051004", list_test_unit);
		data.put("rowSet", list_test_unit);
        data.put("pageNo", 1);
        data.put("pageSize", pageSize);
        data.put("pageCount", pageCount);
        data.put("rows", data.get("rowSet"));
        data.put("total", 1);
        jsonObject.putAll(data);
		return "json";
	}
	
	public String userDetail() throws BusinessException {
		try {
			Map<String, Object> query = RequestUtils.getUpdateFieldMap(request,
					RequestUtils.UPDATEDATAFIELDPREFIX);
			if(query.get("USER_ID")!=	null){
				UserBusiness business = BusinessManager.getBusiness(UserBusiness.class);
				// 找出相应单位的详情
				List<Map<String, Object>> listData = business.list_user(query);
				jsonObject = new JSONObject();
				listData=QueryParamUtil.DateToString(listData);
				if(listData!=null&&listData.size()>0)
					jsonObject.putAll(listData.get(0));
			}
			else{
				jsonObject = new JSONObject();
				jsonObject.put("errCode", 1);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return "success";
	}
	
	
	public String listMaxOrder() throws BusinessException {
		UserBusiness business = BusinessManager.getBusiness(UserBusiness.class);
		try {
			Map<String, Object> query = RequestUtils.getUpdateFieldMap(request,RequestUtils.UPDATEDATAFIELDPREFIX);
			query = QueryParamUtil.StringToDate(query);
			int listDataCount = business.list_max_order(query);
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("maxorder", listDataCount);
			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 listMyUser() throws BusinessException {

		UserTestBusiness business = BusinessManager.getBusiness(UserTestBusiness.class);
		List<Map<String, Object>> listData = business.selectAllUser();
            Map<String, Object> data = new HashMap<String, Object>();
			data.put("rowSet", listData);
			data.put("pageNo", 1);
			data.put("pageSize", 100);
			data.put("pageCount", 100);
			data.put("rows", data.get("rowSet"));
			data.put("total", 100);
			jsonObject.putAll(data);
		
		return "json";
	}
	

}
