package com.gaowj.system.back;

import java.sql.SQLException;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import com.gaowj.business.SystemOpeBusiness;
import com.gaowj.business.action.BasicAction;
import com.gaowj.business.action.BasicActionMethod;
import com.gaowj.business.comp.BusinessManager;
import com.gaowj.business.exception.BusinessException;
import com.gaowj.business.util.Page;
import com.gaowj.business.util.RequestUtils;

public class LoginuserAction extends BasicAction implements BasicActionMethod{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3824623016480456824L;
	private List<Map<String,Object>> depart;
	private Map<String,Object> ajaxdata;

	public List<Map<String, Object>> getDepart() {
		SystemOpeBusiness systemOpeBusiness = BusinessManager.getBusiness(SystemOpeBusiness.class);
		depart = systemOpeBusiness.listDepartmentInfo(null);
		return depart;
	}	

	@Override
	public String grid() throws BusinessException {
		SystemOpeBusiness systemOpeBusiness = BusinessManager.getBusiness(SystemOpeBusiness.class);
		Map<String,Object> query = RequestUtils.getUpdateFieldMap(request, RequestUtils.UPDATEDATAFIELDPREFIX);
		try {
			query = StringToDate(query);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Page<Map<String, Object>> listAll = systemOpeBusiness.listLoginUserInfo(pageNo, pageSize, query);
		setListdata(listAll.getItems());
		setPageCount((listAll.getCount() - 1)/pageSize + 1);
	    return "grid";
	}

	@Override
	public String insert() throws BusinessException {
		Map<String,Object> editdata = new HashMap<String,Object>();
		editdata.put("REGDATE", new Date());
		editdata.put("EMSEX", "M");
		editdata.put("STATE", "1");
		editdata.put("TYPE", "S");
		editdata.put("O_ENABLE", "N");
		editdata.put("W_ENABLE", "N");
		this.setEditdata(editdata);
		return "insert";
	}

	@Override
	public String update() throws BusinessException {
		String ls_myzd_id=RequestUtils.getString(request,"myzd_id");
		SystemOpeBusiness systemOpeBusiness = BusinessManager.getBusiness(SystemOpeBusiness.class);
		if(StringUtils.isNotEmpty(ls_myzd_id)){
			Map<String,Object> query = new HashMap<String,Object>();
			query.put("CODE", ls_myzd_id);
			editdata = systemOpeBusiness.listLoginUserInfo(query).get(0);
		}
	    return "update";
	}

	@Override
	public String save() throws BusinessException {
		String method = RequestUtils.getString(request,"method");
		Map<String,Object> data = RequestUtils.getUpdateFieldMap(request,RequestUtils.UPDATEDATAFIELDPREFIX);
		SystemOpeBusiness systemOpeBusiness = BusinessManager.getBusiness(SystemOpeBusiness.class);
		if(method.equalsIgnoreCase("insert")){
			systemOpeBusiness.insert_user(data);
		}else if(method.equalsIgnoreCase("update")){
			systemOpeBusiness.update_user(data);
			editdata = data;
		}
		return "save";
	}

	@Override
	public String delete() throws BusinessException {
		String ls_myzd_id=RequestUtils.getString(request,"myzd_id");
		SystemOpeBusiness systemOpeBusiness = BusinessManager.getBusiness(SystemOpeBusiness.class);
		Map<String,Object> query = new HashMap<String,Object>();
		query.put("CODE", ls_myzd_id);
		systemOpeBusiness.delete_user(query);
		
		Page<Map<String, Object>> listAll = systemOpeBusiness.listLoginUserInfo(pageNo, pageSize, null);
		setListdata(listAll.getItems());
		setPageCount((listAll.getCount() - 1)/pageSize + 1);
	    return "delete";
	}

	@Override
	public String copy() throws BusinessException {
		String ls_myzd_id=RequestUtils.getString(request,"myzd_id");
		SystemOpeBusiness systemOpeBusiness = BusinessManager.getBusiness(SystemOpeBusiness.class);
		if(StringUtils.isNotEmpty(ls_myzd_id)){
			Map<String,Object> query = new HashMap<String,Object>();
			query.put("CODE", ls_myzd_id);
			editdata = systemOpeBusiness.listLoginUserInfo(query).get(0);
			editdata.remove("CODE");
		}
	    return "copy";
	}
	
	public String getNewCode(){
		String method = RequestUtils.getString(request,"method");
		Map<String,Object> data = RequestUtils.getUpdateFieldMap(request,RequestUtils.UPDATEDATAFIELDPREFIX);
		SystemOpeBusiness systemOpeBusiness = BusinessManager.getBusiness(SystemOpeBusiness.class);
		ajaxdata = new HashMap<String,Object>();
		if(method.equalsIgnoreCase("insert")){
			Map<String,Object> maxcode = systemOpeBusiness.getMaxLoginUserCode(data);
			Map<String,Object> maxemid = systemOpeBusiness.getMaxLoginUserEmid(data);
			ajaxdata.put("NEWCODE", maxcode.get("MAXCODE"));
			ajaxdata.put("NEWEMID", maxemid.get("MAXEMID"));
		}else if(method.equalsIgnoreCase("update")){
			Map<String,Object> maxemid = systemOpeBusiness.getMaxLoginUserEmid(data);
			ajaxdata.put("NEWEMID", maxemid.get("MAXEMID"));
		}
		return "getNewCode";
	}
	
	private  Map<String,Object> StringToDate(Map<String,Object> map) throws SQLException, ParseException{
		Set<String> key = map.keySet();
		for (Iterator it = key.iterator(); it.hasNext();) {
		   String s = (String) it.next();
		   if(map.get(s)!=null && map.get(s).toString().length()>0){
		     if(StringUtils.contains(map.get(s).toString(),"DATE_")){
			   if(map.get(s).toString().split("_").length>=2 && map.get(s).toString().split("_")[1].length()>0){
				 String tempDate = DateFormatUtils.format(new Date(Long.parseLong(map.get(s).toString().split("_")[1])), "yyyy-MM-dd HH:mm:ss");
			     map.put(s,DateUtils.parseDate(tempDate, "yyyy-MM-dd HH:mm:ss"));
			   }
		     } 
		   }
		}
		return map;
	}

	public Map<String, Object> getAjaxdata() {
		return ajaxdata;
	}

	public void setAjaxdata(Map<String, Object> ajaxdata) {
		this.ajaxdata = ajaxdata;
	}
	
	
	
	

}
