package com.engine.kq.cmd.group;

import com.api.browser.bean.SearchConditionItem;
import com.api.browser.bean.SearchConditionOption;
import com.api.hrm.bean.HrmFieldBean;
import com.api.hrm.util.HrmFieldSearchConditionComInfo;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

import java.util.*;
import java.util.Map.Entry;

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

	public GetGroupSearchConditionCmd(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<Map<String, Object>> grouplist = new ArrayList<Map<String, Object>>();
		Map<String, Object> groupitem = null;
		List<Object> itemlist = null;
		try{
			//必要的权限判断
			if(!HrmUserVarify.checkUserRight("HrmKQGroup:Add",user)) {
		  	retmap.put("status", "-1");
		  	retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
		  	return retmap;
		  }
			
			Map<String, List<HrmFieldBean>> fieldGroups= new LinkedHashMap<String, List<HrmFieldBean>>();
			List<HrmFieldBean> lsField = new ArrayList<HrmFieldBean>();
			HrmFieldBean hrmFieldBean = null;
			List<SearchConditionOption> options = null;

			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("groupname");
			hrmFieldBean.setFieldlabel("388700");
			hrmFieldBean.setFieldhtmltype("1");
			hrmFieldBean.setType("1");
			lsField.add(hrmFieldBean);

			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("kqtype");
			hrmFieldBean.setFieldlabel("388704");
			hrmFieldBean.setFieldhtmltype("5");
			hrmFieldBean.setType("1");

			options = new ArrayList<SearchConditionOption>();
			options.add(new SearchConditionOption("",SystemEnv.getHtmlLabelName(332, user.getLanguage()),true));
			options.add(new SearchConditionOption("1",SystemEnv.getHtmlLabelName(388705, user.getLanguage())));
			options.add(new SearchConditionOption("2",SystemEnv.getHtmlLabelName(388706, user.getLanguage())));
			options.add(new SearchConditionOption("3",SystemEnv.getHtmlLabelName(500387, user.getLanguage())));
			hrmFieldBean.setSelectOption(options);
			lsField.add(hrmFieldBean);

			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("resourceId");
			hrmFieldBean.setFieldlabel("388736");
			hrmFieldBean.setFieldhtmltype("3");
			hrmFieldBean.setType("1");
			lsField.add(hrmFieldBean);
			fieldGroups.put("1361", lsField);

			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("kqDate");
			hrmFieldBean.setFieldlabel("33456");
			hrmFieldBean.setFieldhtmltype("3");
			hrmFieldBean.setType("2");
			hrmFieldBean.setIsFormField(true);
			lsField.add(hrmFieldBean);
			fieldGroups.put("1361", lsField);

			HrmFieldSearchConditionComInfo hrmFieldSearchConditionComInfo = new HrmFieldSearchConditionComInfo();
			SearchConditionItem searchConditionItem = null;
			Iterator<Entry<String, List<HrmFieldBean>>> iter = fieldGroups.entrySet().iterator();
			while (iter.hasNext()) {
				Entry<String, List<HrmFieldBean>> entry = iter.next();
				String grouplabel = entry.getKey();
				List<HrmFieldBean> fields = entry.getValue();
				groupitem = new HashMap<String, Object>();
				groupitem.put("title", SystemEnv.getHtmlLabelNames(grouplabel, user.getLanguage()));
				groupitem.put("defaultshow", true);
				
				itemlist = new ArrayList<Object>();
				for (int j = 0; j < fields.size(); j++) {
					hrmFieldBean = fields.get(j);
					searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
					itemlist.add(searchConditionItem);
				}
				groupitem.put("items", itemlist);
				grouplist.add(groupitem);
			}
			
			retmap.put("status", "1");
			retmap.put("conditions", grouplist);
		}catch (Exception e) {
			retmap.put("status", "-1");
			retmap.put("message",  SystemEnv.getHtmlLabelName(382661,user.getLanguage()));
			writeLog(e);
		}
		return retmap;
	}

	@Override
	public BizLogContext getLogContext() {
		return null;
	}

}
