package com.engine.kq.cmd.groupmemeber;

import com.api.browser.bean.SearchConditionItem;
import com.api.browser.bean.SearchConditionOption;
import com.api.browser.util.ConditionFactory;
import com.api.browser.util.ConditionType;
import com.api.hrm.bean.HrmFieldBean;
import com.api.hrm.bean.SelectOption;
import com.api.hrm.bean.WeaRadioGroup;
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.hrm.moduledetach.ManageDetachComInfo;
import weaver.systeminfo.SystemEnv;

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

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

	public GetGroupMemberFormCmd(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>();
		Map<String,Object> groupitem = null;
		List<Object> itemlist = null;
		RecordSet rs = new RecordSet();
		String sql = "";
		try{
			//必要的权限判断
			if(!HrmUserVarify.checkUserRight("HrmKQGroup:Add",user)) {
				retmap.put("status", "-1");
				retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
				return retmap;
			}

			//类型、对象、成员级别
			String[] fields = new String[]{"type,63,5,1","resourceid,106,3,17","subcompanyid,106,3,164","departmentid,106,3,4",
							"jobtitle,106,3,24","jobtitlelevel,28169,5,1","jobtitlesubcompany,19437,3,164","jobtitledepartment,19438,3,4",
							"seclevel,683,1,scope"};

			HrmFieldSearchConditionComInfo hrmFieldSearchConditionComInfo = new HrmFieldSearchConditionComInfo();
			SearchConditionItem searchConditionItem = null;
			HrmFieldBean hrmFieldBean = null;
			itemlist = new ArrayList<Object>();
			groupitem = new HashMap<String,Object>();
			groupitem.put("title", SystemEnv.getHtmlLabelName(1361,weaver.general.Util.getIntValue(user.getLanguage())));
			groupitem.put("defaultshow", true);
			for(int i=0;i<fields.length;i++){
				String[] fieldinfo = fields[i].split(",");
				hrmFieldBean = new HrmFieldBean();
				hrmFieldBean.setFieldname(fieldinfo[0]);
				hrmFieldBean.setFieldlabel(fieldinfo[1]);
				hrmFieldBean.setFieldhtmltype(fieldinfo[2]);
				hrmFieldBean.setType(fieldinfo[3]);
				if(hrmFieldBean.getType().equals("scope")){
					hrmFieldBean.setIsScope(true);
				}
				hrmFieldBean.setIsFormField(true);
				searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
				if(hrmFieldBean.getFieldhtmltype().equals("5")){
					List<SearchConditionOption> statusOptions = new ArrayList<SearchConditionOption>();
					if(hrmFieldBean.getFieldname().equals("type")){
						statusOptions.add(new SearchConditionOption("1",SystemEnv.getHtmlLabelName(179,user.getLanguage()),true));
						statusOptions.add(new SearchConditionOption("2",SystemEnv.getHtmlLabelName(141,user.getLanguage())));
						statusOptions.add(new SearchConditionOption("3",SystemEnv.getHtmlLabelName(124,user.getLanguage())));
						statusOptions.add(new SearchConditionOption("5",SystemEnv.getHtmlLabelName(6086,user.getLanguage())));
						if(!new ManageDetachComInfo().appDetachDisableAll(user)) {
							statusOptions.add(new SearchConditionOption("6", SystemEnv.getHtmlLabelName(1340, user.getLanguage())));
						}
						searchConditionItem.setOptions(statusOptions);
					}else if(hrmFieldBean.getFieldname().equals("jobtitlelevel")){
						statusOptions.add(new SearchConditionOption("1",SystemEnv.getHtmlLabelName(140,user.getLanguage()),true));
						statusOptions.add(new SearchConditionOption("3",SystemEnv.getHtmlLabelName(19438,user.getLanguage())));
						statusOptions.add(new SearchConditionOption("2",SystemEnv.getHtmlLabelName(19437,user.getLanguage())));
						searchConditionItem.setOptions(statusOptions);
					}
				}
				searchConditionItem.setLabelcol(6);
				if(hrmFieldBean.getFieldname().equals("typevalue")||hrmFieldBean.getFieldname().equals("jobtitle")
								||hrmFieldBean.getFieldname().equals("subcompanyid") || hrmFieldBean.getFieldname().equals("departmentid")){
					searchConditionItem.setFieldcol(10);
				}else{
					searchConditionItem.setFieldcol(18);
				}

				if(!hrmFieldBean.getFieldname().equals("type")||!hrmFieldBean.getFieldname().equals("seclevel")){
					searchConditionItem.setViewAttr(3);
				}
				if(fieldinfo[2].equals("3")){
					searchConditionItem.getBrowserConditionParam().setViewAttr(3);
					searchConditionItem.getBrowserConditionParam().setIsSingle(false);
				}
				if(hrmFieldBean.getFieldname().equals("jobtitlesubcompany")||hrmFieldBean.getFieldname().equals("jobtitledepartment")){
					searchConditionItem.getBrowserConditionParam().setIsSingle(true);
				}
				if(hrmFieldBean.getFieldname().equals("seclevel")){
					List<Object> value1 = new ArrayList<Object>();
					value1.add(0);
					value1.add(100);
					searchConditionItem.setValue(value1);
					searchConditionItem.setConditionType(ConditionType.INPUT_INTERVAL);
				}
				itemlist.add(searchConditionItem);
			}
			ConditionFactory conditionFactory = new ConditionFactory(user);
			searchConditionItem = conditionFactory.createCondition(ConditionType.CHECKBOX, "125963","alllevel",true);
			itemlist.add(searchConditionItem);

			//下拉
			List<Object> condition  = new ArrayList<Object>();
			condition.add(itemlist.get(0));

			//人力
			Map<String,Object> conditionMap = new HashMap<String, Object>();
			List<Object> conditionlist  = new ArrayList<Object>();
			List<Object> conditionlist2  = new ArrayList<Object>();
			conditionlist2.add(itemlist.get(1));
			conditionlist.add(conditionlist2);
			conditionMap.put("1",conditionlist);

			//分部
			conditionlist2  = new ArrayList<Object>();
			conditionlist  = new ArrayList<Object>();
			conditionlist2.add(itemlist.get(2));
			//暂不支持包含下级
			//conditionlist2.add(itemlist.get(11));
			conditionlist.add(conditionlist2);

			conditionlist2  = new ArrayList<Object>();
			conditionlist2.add(itemlist.get(8));
			conditionlist.add(conditionlist2);
			conditionMap.put("2",conditionlist);

			//部门
			conditionlist2  = new ArrayList<Object>();
			conditionlist  = new ArrayList<Object>();
			conditionlist2.add(itemlist.get(3));
			//暂不支持包含下级
			//conditionlist2.add(itemlist.get(11));
			conditionlist.add(conditionlist2);

			conditionlist2  = new ArrayList<Object>();
			conditionlist2.add(itemlist.get(8));
			conditionlist.add(conditionlist2);
			conditionMap.put("3",conditionlist);

			//岗位
			conditionlist2  = new ArrayList<Object>();
			conditionlist  = new ArrayList<Object>();
			conditionlist2.add(itemlist.get(4));
			conditionlist.add(conditionlist2);

			conditionlist2  = new ArrayList<Object>();
			conditionlist2.add(itemlist.get(5));

			WeaRadioGroup wrg = new WeaRadioGroup();
			wrg.setLabel(SystemEnv.getHtmlLabelName(28169, user.getLanguage()));
			List<Object> option = new ArrayList<Object>();
			Map<String, Object> selectLinkageDatas = new HashMap<String, Object>();
			selectLinkageDatas.put("2" ,itemlist.get(6));
			selectLinkageDatas.put("3" ,itemlist.get(7));
			wrg.setSelectLinkageDatas(selectLinkageDatas);
			option.add(new SelectOption("1",SystemEnv.getHtmlLabelName(140, user.getLanguage()),true));
			option.add(new SelectOption("2",SystemEnv.getHtmlLabelName(19437, user.getLanguage())));
			option.add(new SelectOption("3",SystemEnv.getHtmlLabelName(19438, user.getLanguage())));

			List<String>  domkey =  new  ArrayList<String>();
			wrg.setOptions(option);
			wrg.setConditionType("SELECT_LINKAGE");
			wrg.setFieldcol(18);
			wrg.setLabelcol(6);
			domkey.add("jobtitlelevel");
			wrg.setDomkey(domkey);

			conditionlist2  = new ArrayList<Object>();
			conditionlist2.add(wrg);
			conditionlist.add(conditionlist2);
			conditionlist2  = new ArrayList<Object>();
			conditionlist2.add(itemlist.get(8));
			conditionlist.add(conditionlist2);
			conditionMap.put("5",conditionlist);

			//所有人
			conditionlist  = new ArrayList<Object>();
			conditionlist2  = new ArrayList<Object>();
			conditionlist2.add(itemlist.get(8));
			conditionlist.add(conditionlist2);
			conditionMap.put("6",conditionlist);

			condition.add(conditionMap);

			retmap.put("conditions", condition);
			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() {
		return null;
	}

}
