package com.engine.kq.cmd.schedulesignimport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.api.browser.bean.SearchConditionOption;
import com.api.hrm.bean.HrmFieldBean;
import com.api.hrm.util.HrmFieldUtil;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.servicefiles.DataSourceXML;
import weaver.servicefiles.ScheduleXML;
import weaver.systeminfo.SystemEnv;

public class GetImportSetFormCmd extends AbstractCommonCommand<Map<String, Object>> {

	public GetImportSetFormCmd(Map<String, Object> params, User user) {
		this.user = user;
		this.params = params;
	}

	@Override
	public Map<String, Object> execute(CommandContext commandContext) {
		Map<String, Object> retmap = new HashMap<String, Object>();
		List<Object> lsGroup = new ArrayList<Object>();
		Map<String, Object> groupitem = null;
		List<Object> itemlist = null;
		Map<String,Object> item = null;
		RecordSet rs = new RecordSet();
		String sql = "";

		try {
			if (!HrmUserVarify.checkUserRight("HrmResourceEdit:Edit", user)) {
				retmap.put("status", "-1");
				retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
				return retmap;
			}
			
			itemlist = new ArrayList<Object>();
			groupitem = new HashMap<String,Object>();
			groupitem.put("title", SystemEnv.getHtmlLabelName(33537, user.getLanguage()));
			groupitem.put("defaultshow", true);
			boolean oldisinvald = false;
			ScheduleXML ScheduleXML = new ScheduleXML();
			if (ScheduleXML.getPointArrayList().indexOf("KQScheduleSignImport") > -1){
				oldisinvald = true;
			}
			item = new HashMap<String,Object>();
			item.put("isVaild", oldisinvald);
			item.put("tips", SystemEnv.getHtmlLabelName(125962,user.getLanguage()));
			itemlist.add(item);
			item = new HashMap<String,Object>();
			item.put("tips", SystemEnv.getHtmlLabelName(33544,user.getLanguage()));
			itemlist.add(item);
			groupitem.put("items", itemlist);
			lsGroup.add(groupitem);
			
			itemlist = new ArrayList<Object>();
			groupitem = new HashMap<String,Object>();
			groupitem.put("title", SystemEnv.getHtmlLabelName(33538, user.getLanguage()));
			groupitem.put("defaultshow", true);
			List<HrmFieldBean> titles = null;
			Map<String, Object> table = null;
			HrmFieldBean hrmFieldBean = null;
			List<Map<String, Object>> columns = null;
			List<Map<String, Object>> datas = null;
			Map<String, Object> data = null;

			titles = new ArrayList<HrmFieldBean>();
			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("datasourceid");
			hrmFieldBean.setFieldlabel("23963");
			hrmFieldBean.setFieldhtmltype("5");
			hrmFieldBean.setType("1");
			List<SearchConditionOption> options = null;
			DataSourceXML DataSourceXML = new DataSourceXML();
			ArrayList pointArrayList = DataSourceXML.getPointArrayList();
			options = new ArrayList<SearchConditionOption>();
			for(int i=0;pointArrayList!=null&&i<pointArrayList.size();i++){
				options.add(new SearchConditionOption(Util.null2String(pointArrayList.get(i)),Util.null2String(pointArrayList.get(i))));
			}
			hrmFieldBean.setSelectOption(options);
			titles.add(hrmFieldBean);

			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("tablename");
			hrmFieldBean.setFieldlabel("23574");
			hrmFieldBean.setFieldhtmltype("1");
			hrmFieldBean.setType("1");
			hrmFieldBean.setViewAttr(3);
			hrmFieldBean.setMultilang(false);
			titles.add(hrmFieldBean);

			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("loginid");
			hrmFieldBean.setFieldlabel("412");
			hrmFieldBean.setFieldhtmltype("1");
			hrmFieldBean.setType("1");
			hrmFieldBean.setMultilang(false);
			titles.add(hrmFieldBean);

			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("workcode");
			hrmFieldBean.setFieldlabel("27940");
			hrmFieldBean.setFieldhtmltype("1");
			hrmFieldBean.setType("1");
			hrmFieldBean.setMultilang(false);
			titles.add(hrmFieldBean);

			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("lastname");
			hrmFieldBean.setFieldlabel("413");
			hrmFieldBean.setFieldhtmltype("1");
			hrmFieldBean.setType("1");
			hrmFieldBean.setMultilang(false);
			titles.add(hrmFieldBean);

			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("signdate");
			hrmFieldBean.setFieldlabel("33563");
			hrmFieldBean.setFieldhtmltype("1");
			hrmFieldBean.setType("1");
			hrmFieldBean.setViewAttr(3);
			hrmFieldBean.setMultilang(false);
			titles.add(hrmFieldBean);

			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("signtime");
			hrmFieldBean.setFieldlabel("20035");
			hrmFieldBean.setFieldhtmltype("1");
			hrmFieldBean.setType("1");
			hrmFieldBean.setMultilang(false);
			titles.add(hrmFieldBean);

			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("clientaddress");
			hrmFieldBean.setFieldlabel("20045");
			hrmFieldBean.setFieldhtmltype("1");
			hrmFieldBean.setType("1");
			hrmFieldBean.setMultilang(false);
			titles.add(hrmFieldBean);

			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("longitude");
			hrmFieldBean.setFieldlabel("801");
			hrmFieldBean.setFieldhtmltype("1");
			hrmFieldBean.setType("1");
			hrmFieldBean.setMultilang(false);
			titles.add(hrmFieldBean);

			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("latitude");
			hrmFieldBean.setFieldlabel("802");
			hrmFieldBean.setFieldhtmltype("1");
			hrmFieldBean.setType("1");
			hrmFieldBean.setMultilang(false);
			titles.add(hrmFieldBean);

			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("addr");
			hrmFieldBean.setFieldlabel("81524");
			hrmFieldBean.setFieldhtmltype("1");
			hrmFieldBean.setType("1");
			hrmFieldBean.setMultilang(false);
			titles.add(hrmFieldBean);

			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("memo");
			hrmFieldBean.setFieldlabel("454");
			hrmFieldBean.setFieldhtmltype("1");
			hrmFieldBean.setType("1");
			hrmFieldBean.setMultilang(false);
			titles.add(hrmFieldBean);

			table = new HashMap<String, Object>();
			columns = HrmFieldUtil.getHrmDetailTable(titles, null, user);
			table.put("columns", columns);
			
		  datas = new ArrayList<Map<String,Object>>();
			sql = " select datasourceid, tablename, loginid, workcode, lastname, signdate, signtime, clientaddress,longitude,latitude,addr,memo " +
						" from HrmScheduleSignSet " +
						" order by datasourceid desc ";
			rs.executeSql(sql);
			StringBuffer ajaxData = new StringBuffer();
			ajaxData.append("[");
			while (rs.next()) {
				data = new HashMap<String, Object>();
				String[] fieldnames = new String[] { "datasourceid", "tablename", "loginid","workcode", "lastname", "signdate", "signtime", "clientaddress", "longitude", "latitude", "addr", "memo" };
				for (int i = 0; i < fieldnames.length; i++) {
					data.put(fieldnames[i], Util.null2String(rs.getString(fieldnames[i])));
				}
				datas.add(data);
			}
			table.put("datas", datas);
			item = new HashMap<String,Object>();
			item.put("table", table);
			itemlist.add(item);
			groupitem.put("items", itemlist);
			lsGroup.add(groupitem);
			
			itemlist = new ArrayList<Object>();
			groupitem = new HashMap<String,Object>();
			groupitem.put("title", SystemEnv.getHtmlLabelName(10000655,weaver.general.Util.getIntValue(user.getLanguage())));
			groupitem.put("defaultshow", true);
			List<String> lsPromptLabel = new ArrayList<String>(); //提示信息
			lsPromptLabel.add("23963,124881");
			lsPromptLabel.add("23574,124882");
			lsPromptLabel.add("503606");
			lsPromptLabel.add("503607");
			lsPromptLabel.add("503608");
			lsPromptLabel.add("33563,124885");
			lsPromptLabel.add("20035,124886");
			lsPromptLabel.add("20045,124887");

			for(int i=0;i<lsPromptLabel.size();i++){
				item = new HashMap<String,Object>();
				item.put("index", (i+1));
				String value = "";
				String[] label = Util.splitString(lsPromptLabel.get(i), ",");
				value = Util.toScreen(SystemEnv.getHtmlLabelNames(label[0], user.getLanguage()), user.getLanguage());
				if(label.length>1){
					value = Util.toScreen(SystemEnv.getHtmlLabelNames(label[1], user.getLanguage()), user.getLanguage());
				}
				item.put("value",value);
				itemlist.add(item);
			}
			groupitem.put("items", itemlist);
			lsGroup.add(groupitem);
			
//			itemlist = new ArrayList<Object>();
//			groupitem = new HashMap<String,Object>();
//			groupitem.put("title", SystemEnv.getHtmlLabelNames("33587,85", user.getLanguage()));
//			groupitem.put("defaultshow", true);
//			lsPromptLabel = new ArrayList<String>(); //提示信息
//			lsPromptLabel.add("124888");
//			
//			for(int i=0;i<lsPromptLabel.size();i++){
//				item = new HashMap<String,Object>();
//				item.put("index", (i+1));
//				String value = "";
//				String[] label = Util.splitString(lsPromptLabel.get(i), ",");
//				value = Util.toScreen(SystemEnv.getHtmlLabelNames(label[0], user.getLanguage()), user.getLanguage());
//				if(label.length>1){
//					value = Util.toScreen(SystemEnv.getHtmlLabelNames(label[1], user.getLanguage()), user.getLanguage());
//				}
//				item.put("value",value);
//				itemlist.add(item);
//			}
//			groupitem.put("items", itemlist);
//			lsGroup.add(groupitem);
			
			retmap.put("group", lsGroup);
			retmap.put("status", "1");
		} catch (Exception e) {
			retmap.put("status", "-1");
			retmap.put("message", SystemEnv.getHtmlLabelName(382661, user.getLanguage()));
			writeLog(e);
		}
		return retmap;
	}

	@Override
	public BizLogContext getLogContext() {
		// TODO Auto-generated method stub
		return null;
	}

}
