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.bean.WeaRadioGroup;
import com.api.hrm.util.HrmAdvancedSearchUtil;
import com.api.hrm.util.HrmFieldSearchConditionComInfo;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.common.service.HrmCommonService;
import com.engine.common.service.impl.HrmCommonServiceImpl;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.biz.KQLeaveRulesBiz;
import com.engine.kq.biz.KQReportFieldComInfo;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.hrm.resource.ResourceComInfo;
import weaver.systeminfo.SystemEnv;

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

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

    public GetGroupMemberConditionCmd(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> lsCondition = new ArrayList<Object>();
        String[] options = null;
        String[] selectLinkageDatas = null;

        try {
            //必要的权限判断
            if (!HrmUserVarify.checkUserRight("HrmKQGroup:Add", user)) {
                retmap.put("status", "-1");
                retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
                return retmap;
            }

            int uid = user.getUID();
            ResourceComInfo rci = new ResourceComInfo();
            String subcompanyid = rci.getSubCompanyID("" + uid);
            String departmentid = rci.getDepartmentID("" + uid);

            HrmAdvancedSearchUtil hrmAdvancedSearchUtil = new HrmAdvancedSearchUtil();
            HrmCommonService hrmCommonService = new HrmCommonServiceImpl();

            WeaRadioGroup radioGroup = null;
            //数据范围
            if (hrmCommonService.isManager(user.getUID())) {
                options = new String[]{"0,140,true", "1,141,false", "2,124,false", "3,1867,false", "4,15089,false"};
            } else {
                options = new String[]{"0,140,true", "1,141,false", "2,124,false", "3,1867,false"};
            }

            selectLinkageDatas = new String[]{"1,subcompanyId,141,3,194", "2,departmentId,124,3,57", "3,resourceId,1867,3,17", "4,allLevel,389995,4,1"};
            if (user.getUID() != 1) {
                selectLinkageDatas[0] = "1,subcompanyId,141,3,194," + subcompanyid;
                selectLinkageDatas[1] = "2,departmentId,124,3,57," + departmentid;
                selectLinkageDatas[2] = "3,resourceId,1867,3,17," + uid;
                options[0] = "0,140,false";
                options[3] = "3,1867,true";
            }
            radioGroup = hrmAdvancedSearchUtil.getAdvanceCondition("viewScope", "34102", options, selectLinkageDatas, user);
            radioGroup.setLabelcol(3);
            radioGroup.setFieldcol(20);
            lsCondition.add(radioGroup);

            //所属考勤组
            options = new String[]{"0,332,true", "1,514133,false", "2,515133,false"};
            selectLinkageDatas = new String[]{"2,groupId,390221,3,kqgroup"};
            radioGroup = hrmAdvancedSearchUtil.getAdvanceCondition("groupType", "515132", options, selectLinkageDatas, user);
            SearchConditionItem condition = (SearchConditionItem)radioGroup.getSelectLinkageDatas().get("2");
            Map<String, Object> conditionParams = new HashMap<>();
            conditionParams.put("advance", "1");
            Map<String, Object> dataParams = new HashMap<>();
            dataParams.put("advance", "1");
            Map<String, Object> completeParams = new HashMap<>();
            completeParams.put("includeAll", "1");
            condition.getBrowserConditionParam().setConditionDataParams(conditionParams);
            condition.getBrowserConditionParam().setDataParams(dataParams);
            condition.getBrowserConditionParam().setCompleteParams(completeParams);
            condition.getBrowserConditionParam().setQuickSearchName("groupname");
            radioGroup.getSelectLinkageDatas().put("2", condition);
            lsCondition.add(radioGroup);

            retmap.put("status", "1");
            retmap.put("conditions", lsCondition);
        } 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;
    }

}
