package com.engine.kq.cmd.leaverules;

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.hrm.moduledetach.ManageDetachComInfo;
import weaver.systeminfo.SystemEnv;
import weaver.systeminfo.systemright.CheckSubCompanyRight;

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

/**
 * 假期规则--查询结果列表
 */
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>();
        String ruleName = Util.null2String(params.get("ruleName"));//假期规则名称
        String typeId = Util.null2String(params.get("typeId"));//假期类型的ID

        boolean canEdit = HrmUserVarify.checkUserRight("KQLeaveRulesEdit:Edit", user);//是否具有编辑权限
        boolean canDelete = HrmUserVarify.checkUserRight("KQLeaveRulesDelete:Delete", user);//是否具有删除权限
        boolean canLog = HrmUserVarify.checkUserRight("KQLeaveRules:Log", user);//是否具有查看日志的权限

        /*如果没有权限*/
        if (!canEdit) {
            resultMap.put("status", "-1");
            resultMap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
            return resultMap;
        }

        String backFields = " * ";
        String sqlFrom = " from kq_LeaveRulesDetail ";
        String sqlWhere = " where (isDelete is null or isDelete!=1) ";
        String orderBy = " id ";
        if (!ruleName.equals("")) {
            sqlWhere += " and ruleName like '%" + ruleName + "%'";
        }
        if(!typeId.equals("")){
            sqlWhere += " and ruleId="+typeId;
        }
        String pageUid = "b0c005a2-447f-0977-b692-6d8de7ead576";
        String operateString = "";
        operateString = "<operates width=\"20%\">";
        operateString += "<popedom transmethod=\"weaver.hrm.common.SplitPageTagOperate.getBasicOperate\" 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:LeaveRulesDetailList\" pageUid=\"" + pageUid + "\" tabletype=\"checkbox\" pagesize=\"" + PageIdConst.getPageSize("Kq:LeaveRulesDetailList", user.getUID(), PageIdConst.HRM) + "\" >" +
                "<sql backfields=\"" + backFields + "\" sqlform=\"" + sqlFrom + "\" sqlwhere=\"" + Util.toHtmlForSplitPage(sqlWhere) + "\"  sqlorderby=\"" + orderBy + "\"  sqlprimarykey=\"id\" sqlsortway=\"asc\" sqlisdistinct=\"false\"/>"
                + operateString +
                "   <head>" +
                "       <col width=\"20%\" text=\"" + SystemEnv.getHtmlLabelName(19829, user.getLanguage()) + "\" column=\"ruleName\" otherpara=\"" + user.getLanguage() + "\" transmethod=\"weaver.general.Util.formatMultiLang\" />" +
                "       <col width=\"20%\" text=\"" + SystemEnv.getHtmlLabelName(129811, user.getLanguage()) + "\" column=\"ruleId\" otherpara=\"" + user.getLanguage() + "\"  transmethod=\"com.engine.kq.util.KQTransMethod.getLeaveTypesName\"/>" +
                "       <col width=\"20%\" text=\"" + SystemEnv.getHtmlLabelName(388946, user.getLanguage()) + "\" column=\"distributionMode\" otherpara=\"" + user.getLanguage() + "\"  transmethod=\"com.engine.kq.util.KQTransMethod.getDistributionModName\"/>" +
                "       <col width=\"20%\" text=\"" + SystemEnv.getHtmlLabelName(388952, user.getLanguage()) + "\" column=\"validityRule\" otherpara=\"" + user.getLanguage() + "\"  transmethod=\"com.engine.kq.util.KQTransMethod.getValidityRuleShow\"/>" +
                "       <col width=\"20%\" text=\"" + SystemEnv.getHtmlLabelName(389093, user.getLanguage()) + "\" column=\"releaseRule\" otherpara=\"" + user.getLanguage() + "\"  transmethod=\"com.engine.kq.util.KQTransMethod.getReleaseRuleName\"/>" +
                "       <col width=\"10%\" text=\"" + SystemEnv.getHtmlLabelName(19374, user.getLanguage()) + "\" column=\"scopeType\"  otherpara=\"" + user.getLanguage() + "\"  transmethod=\"com.engine.kq.util.KQTransMethod.getScopeTypeName\"/>" +
                "   </head>" +
                "</table>";
        String sessionKey = pageUid + "_" + Util.getEncrypt(Util.getRandom());
        Util_TableMap.setVal(sessionKey, tableString);
        resultMap.put("sessionkey", sessionKey);
        return resultMap;
    }
}
