package com.engine.kq.cmd.group;

import com.api.browser.bean.SearchConditionOption;
import com.cloudstore.dev.api.util.Util_TableMap;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.biz.KQGroupMemberComInfo;
import com.engine.kq.util.PageUidFactory;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.hrm.company.SubCompanyComInfo;
import weaver.hrm.moduledetach.ManageDetachComInfo;
import weaver.systeminfo.SystemEnv;
import weaver.systeminfo.systemright.CheckSubCompanyRight;

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

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

    public GetGroupSearchListCmd(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>();
        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;
            }

            CheckSubCompanyRight checkSubCompanyRight = new CheckSubCompanyRight();
            ManageDetachComInfo manageDetachComInfo = new ManageDetachComInfo();
            boolean hrmdetachable = manageDetachComInfo.isUseHrmManageDetach();//是否开启了人力资源模块的管理分权
            int operatelevel = 2;

            String subcompanyId = Util.null2String(params.get("subcompanyid"));
            if (subcompanyId.equals("0")) subcompanyId = "";
            String resourceId = Util.null2String(params.get("resourceId"));
            String kqDate = Util.null2String(params.get("kqDate"));
            String groupname = Util.null2String(params.get("groupname"));
            String kqtype = Util.null2String(params.get("kqtype"));

            String backFields = " a.id,a.id as id1,a.id as id2,a.groupname,a.subcompanyid,a.kqtype,a.excludeid,a.signintype,a.ipscope,locationcheck,locationcheckscope,wificheck,isdelete ";
            String sqlFrom = " kq_group a";
            String sqlWhere = " (isdelete is null or  isdelete <> '1') ";
            String orderby = " id ";
            String tableString = "";

            if (hrmdetachable) {
                int[] arrSubcompanyids = checkSubCompanyRight.getSubComByUserRightId(user.getUID(), "HrmKQGroup:Add");
                String subcompanyids = "";
                if (user.getUID() != 1) {
                    for (int i = 0; arrSubcompanyids != null && i < arrSubcompanyids.length; i++) {   
                        if (subcompanyids.length() > 0) subcompanyids += ",";
                        subcompanyids += arrSubcompanyids[i];
                    }
                    if (subcompanyids.length() > 0) {
                        sqlWhere += " and subcompanyid in(" + subcompanyids + ")";
                    } else {
                        sqlWhere += " and 1=2 ";
                    }
                }
            }

            if (subcompanyId.length() > 0) {
                sqlWhere += " and subcompanyId = " + subcompanyId;
            }

            if (groupname.length() > 0) {
                sqlWhere += " and groupname like '%" + groupname + "%' ";
            }
            if (kqtype.length() > 0) {
                sqlWhere += " and kqtype = " + kqtype;
            }

            if (resourceId.length() > 0) {
                String groupIds = new KQGroupMemberComInfo().getKQGroupIds(resourceId);
                sqlWhere += " and id in(" + groupIds + ")";
            }

            String pageUid = PageUidFactory.getHrmPageUid("KQGroupSearchList");


            String operateString = "<operates width=\"20%\">";
            if (hrmdetachable && (user.getUID() != 1)) {
                operateString += " <popedom transmethod=\"com.engine.kq.util.TransMethod.getOperateByGroup\" otherpara=\"column:kqtype+column:subcompanyid+" + user.getUID() + "\"></popedom> ";
            } else {
                operateString += " <popedom transmethod=\"com.engine.kq.util.TransMethod.getOperate\" otherpara=\"column:kqtype\" ></popedom> ";
            }

            operateString += "     <operate href=\"javascript:doEditBase();\" text=\"" + SystemEnv.getHtmlLabelName(388734, user.getLanguage()) + "\" index=\"0\"/>";
            operateString += "     <operate href=\"javascript:doEditKQType();\" text=\"" + SystemEnv.getHtmlLabelName(388735, user.getLanguage()) + "\" index=\"1\"/>";
            operateString += "     <operate href=\"javascript:doEditKQMember();\" text=\"" + SystemEnv.getHtmlLabelName(388736, user.getLanguage()) + "\" index=\"2\"/>";
            operateString += "     <operate href=\"javascript:doEditSchedule();\" text=\"" + SystemEnv.getHtmlLabelName(16695, user.getLanguage()) + "\" index=\"3\"/>";

            operateString += "     <operate href=\"javascript:doDelete();\" text=\"" + SystemEnv.getHtmlLabelName(91, user.getLanguage()) + "\" index=\"4\"/>";

            operateString += "     <operate href=\"javascript:showLog();\" text=\"" + SystemEnv.getHtmlLabelName(83, user.getLanguage()) + "\" index=\"5\"/>";
            operateString += "</operates>";
            String checkboxpopedomString = " <checkboxpopedom showmethod=\"com.engine.kq.util.TransMethod.getKQGroupCheckbox\"  id=\"checkbox\"  popedompara=\"column:id\" />";
            if (hrmdetachable && (user.getUID() != 1)) {
                checkboxpopedomString=  " <checkboxpopedom showmethod=\"com.engine.kq.util.TransMethod.getKQGroupCheckboxByGroup\"  id=\"checkbox\"  popedompara=\"column:id+column:subcompanyid+"+user.getUID() +"\" />";
            }
            tableString = "" +
                    "<table pageUid=\"" + pageUid + "\" pagesize=\"10\" tabletype=\"checkbox\">" +
                    "<sql backfields=\"" + backFields + "\" sqlform=\"" + sqlFrom + "\" sqlprimarykey=\"a.id\" sqlorderby=\"" + orderby + "\" sqlsortway=\"asc\" sqldistinct=\"true\" sqlwhere=\"" + Util.toHtmlForSplitPage(sqlWhere) + "\"/>" +
                    checkboxpopedomString + operateString +
                    "<head>" +
                    "				<col width=\"0%\"  hide=\"true\"  text=\"\" column=\"id\"/>" +
                    "				<col width=\"30%\" text=\"" + SystemEnv.getHtmlLabelName(388700, user.getLanguage()) + "\" column=\"groupname\" transmethod=\"com.engine.kq.util.TransMethod.getKQGroupName\" otherpara=\"column:id+" + resourceId + "+" + kqDate + "+" + user.getLanguage() + "\" orderkey=\"groupname\"/>" +
                    (hrmdetachable ? "<col width=\"20%\" text=\"" + SystemEnv.getHtmlLabelName(19799, user.getLanguage()) + "\" column=\"subcompanyid\" transmethod=\"weaver.hrm.company.SubCompanyComInfo.getSubCompanyname\"/>" : "") +
                    "				<col width=\"20%\" text=\"" + SystemEnv.getHtmlLabelName(1859, user.getLanguage()) + "\" column=\"id1\" transmethod=\"com.engine.kq.util.TransMethod.getGroupUserCount\"/>" +
                    "				<col width=\"20%\" text=\"" + SystemEnv.getHtmlLabelName(63, user.getLanguage()) + "\" column=\"kqtype\" transmethod=\"com.engine.kq.util.TransMethod.getKQTypeName\" otherpara=\"" + user.getLanguage() + "\" orderkey=\"kqtype\"/>" +
                    "				<col width=\"30%\" text=\"" + SystemEnv.getHtmlLabelName(27961, user.getLanguage()) + "\" column=\"id2\" transmethod=\"com.engine.kq.util.TransMethod.getKQGroupDetial\" otherpara=\"column:kqtype+" + user.getLanguage() + "\" />" +
                    "</head>" +
                    "</table>";

            //主要用于 显示定制列以及 表格 每页展示记录数选择
            String sessionkey = pageUid + "_" + Util.getEncrypt(Util.getRandom());
            Util_TableMap.setVal(sessionkey, tableString);
            retmap.put("sessionkey", sessionkey);
            retmap.put("status", "1");
            if(hrmdetachable){
                if(subcompanyId.length()>0 && !subcompanyId.equalsIgnoreCase("0")){
                    operatelevel=checkSubCompanyRight.ChkComRightByUserRightCompanyId(user.getUID(),"HrmKQGroup:Add",Util.getIntValue(subcompanyId,-1));
                }
            }
            if(operatelevel>0){
                retmap.put("canAdd", true);
            }else{
                retmap.put("canAdd", false);
            }
            if(operatelevel>1){
                retmap.put("canDel", true);
            }else{
                retmap.put("canDel", false);
            }
        } 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;
    }

}
