package com.engine.kq.cmd.leavetypes;

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 weaver.general.PageIdConst;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

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

/**
 * @Auther: Administrator
 * @Date: 2019-03-07 18:37
 * @Description:
 */
public class GetSearchListCmd extends AbstractCommonCommand<Map<String, Object>> {

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

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

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            boolean canEdit = HrmUserVarify.checkUserRight("KQLeaveRulesEdit:Edit", user);//是否具有编辑权限
            boolean canDelete = HrmUserVarify.checkUserRight("KQLeaveRulesDelete:Delete", user);//是否具有删除权限
            boolean canLog = HrmUserVarify.checkUserRight("KQLeaveRules:Log", user);//是否具有查看日志的权限

            String leaveName = Util.null2String(params.get("leaveName"));//假期类型的名称
            String minimumUnit = Util.null2String(params.get("minimumUnit"));//最小请假单位
            String computingMode = Util.null2String(params.get("computingMode"));//计算请假时长方式
            String balanceEnable = Util.null2String(params.get("balanceEnable"));//是否启用假期余额

            String backFields = " * ";
            String sqlFrom = " from kq_LeaveRules ";
            String sqlWhere = " where (isDelete is null or isDelete!=1) ";
            if (!leaveName.equals("")) {
                sqlWhere += " and leaveName like '%" + leaveName + "%' ";
            }
            if (!minimumUnit.equals("0") && !minimumUnit.equals("")) {
                sqlWhere += " and minimumUnit=" + minimumUnit;
            }
            if (!computingMode.equals("0") && !computingMode.equals("")) {
                sqlWhere += " and computingMode=" + computingMode;
            }
            if (!balanceEnable.equals("-1") && !balanceEnable.equals("")) {
                sqlWhere += " and balanceEnable=" + balanceEnable;
            }
            String orderBy = " showOrder,id ";
            String pageUid = "ff6a16be-57bc-002d-6223-600b9031e252";
            String operateString = "";
            operateString = "<operates width=\"20%\">";
            operateString += "<popedom transmethod=\"com.engine.kq.util.KQTransMethod.getLeaveRulesOperate\" otherpara=\"" + canEdit + ":" + canDelete + ":" + canLog + "\"></popedom> ";
            operateString += "  <operate href=\"javascript:openDialog();\" text=\"" + SystemEnv.getHtmlLabelName(93, user.getLanguage()) + "\" index=\"0\"/>";
            operateString += "  <operate href=\"javascript:doDel()\"   text=\"" + SystemEnv.getHtmlLabelName(91, user.getLanguage()) + "\"  index=\"1\"/>";
            operateString += "  <operate href=\"javascript:onLog()\"   text=\"" + SystemEnv.getHtmlLabelName(83, user.getLanguage()) + "\"  index=\"2\"/>";
            operateString += "</operates>";
            String tableString = "" +
                    "<table pageId=\"KQ:LeaveRulesList\" pageUid=\"" + pageUid + "\" tabletype=\"checkbox\" pagesize=\"" + PageIdConst.getPageSize("Kq:LeaveRulesList", user.getUID(), PageIdConst.HRM) + "\" >" +
                    "<checkboxpopedom showmethod=\"com.engine.kq.util.KQTransMethod.getLeaveRulesCheckbox\"  id=\"checkbox\"  popedompara=\"column:id\" />" +
                    "<sql backfields=\"" + backFields + "\" sqlform=\"" + sqlFrom + "\" sqlwhere=\"" + Util.toHtmlForSplitPage(sqlWhere) + "\"  sqlorderby=\"" + orderBy + "\"  sqlprimarykey=\"id\" sqlsortway=\"asc\" sqlisdistinct=\"false\"/>"
                    + operateString +
                    "   <head>" +
                    "       <col width=\"10%\" text=\"" + SystemEnv.getHtmlLabelName(388883, user.getLanguage()) + "\" column=\"leaveName\" otherpara=\"" + user.getLanguage() + "\" transmethod=\"weaver.general.Util.formatMultiLang\" />" +
                    "       <col width=\"10%\" text=\"" + SystemEnv.getHtmlLabelName(390881, user.getLanguage()) + "\" column=\"scopeType\"  otherpara=\"" + user.getLanguage() + "\"  transmethod=\"com.engine.kq.util.KQTransMethod.getScopeTypeName\"/>" +
                    "       <col width=\"10%\" text=\"" + SystemEnv.getHtmlLabelName(388884, user.getLanguage()) + "\" column=\"minimumUnit\"  otherpara=\"" + user.getLanguage() + "\"  transmethod=\"com.engine.kq.util.KQTransMethod.getMinimumUnitName\"/>" +
                    "       <col width=\"10%\" text=\"" + SystemEnv.getHtmlLabelName(388888, user.getLanguage()) + "\" column=\"computingMode\"  otherpara=\"" + user.getLanguage() + "\"  transmethod=\"com.engine.kq.util.KQTransMethod.getComputingModeName\"/>" +
                    "       <col width=\"10%\" text=\"" + SystemEnv.getHtmlLabelName(388945, user.getLanguage()) + "\" column=\"balanceEnable\"  otherpara=\"" + user.getLanguage() + "\"  transmethod=\"com.engine.kq.util.KQTransMethod.getBalanceEnableShow\"/>" +
                    "       <col width=\"10%\" text=\"" + SystemEnv.getHtmlLabelName(18624, user.getLanguage()) + "\" column=\"isEnable\" />" +
                    "       <col width=\"10%\" text=\"" + SystemEnv.getHtmlLabelName(15513, user.getLanguage()) + "\" column=\"showOrder\" />" +
                    "   </head>" +
                    "</table>";
            String sessionKey = pageUid + "_" + Util.getEncrypt(Util.getRandom());
            Util_TableMap.setVal(sessionKey, tableString);
            resultMap.put("sessionkey", sessionKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }
}
