package com.engine.kq.cmd.holidaySet;

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.company.CompanyComInfo;
import weaver.hrm.company.SubCompanyComInfo;
import weaver.hrm.location.LocationComInfo;
import weaver.hrm.moduledetach.ManageDetachComInfo;
import weaver.systeminfo.SystemEnv;
import weaver.systeminfo.systemright.CheckSubCompanyRight;

import java.util.Calendar;
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 groupId = Util.null2String(params.get("groupId"));//考勤组ID
        String currentYear = Util.null2String(params.get("currentYear"));//年份
        String holidayDesc = Util.null2String(params.get("holidayDesc"));//说明
        String isSync = Util.null2String(params.get("isSync"));//同步还是批量删除：true--同步、false--批量删除

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

        /*如果没有传入年份，默认为今年的年份*/
        Calendar today = Calendar.getInstance();
        if (currentYear.equals("")) {
            currentYear = "" + today.get(Calendar.YEAR);
        }

        String backFields = " * ";
        String sqlFrom = " from kq_HolidaySet ";
        String sqlWhere = " where 1=1 ";
        String orderBy = " groupId,holidayDate ";

        if (!groupId.equals("")) {
            sqlWhere += " and groupId in (" + groupId + ")";
        }
        if (!currentYear.equals("")) {
            String dateFrom = currentYear + "-01-01";
            String dateTo = currentYear + "-12-31";
            sqlWhere += " and holidayDate>='" + dateFrom + "' and holidayDate<='" + dateTo + "'";
        }
        if (!holidayDesc.equals("")) {
            sqlWhere += " and holidayDesc like '%" + holidayDesc + "%'";
        }
        String pageUid = "70efc34c-7a61-da9b-87a7-69a4a4d72859";
        String operateString = "";
        operateString = "<operates width=\"20%\">";
        operateString += "<popedom transmethod=\"weaver.hrm.common.SplitPageTagOperate.getBasicOperate\" otherpara=\"" + canEdit + ":" + canDelete + ":" + canLog + ":" + canEdit + "\"></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 += "  <operate href=\"javascript:syns()\"   text=\"" + SystemEnv.getHtmlLabelName(19015, user.getLanguage()) + "\"  index=\"3\"/>";
        operateString += "</operates>";
        String tableString = "" +
                "<table pageId=\"Hrm:HolidaySet\" pageUid=\"" + pageUid + "\" tabletype=\"checkbox\" pagesize=\"" + PageIdConst.getPageSize("Hrm:HolidaySet", 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(97, user.getLanguage()) + "\" column=\"holidayDate\" orderkey=\"holidayDate\"/>" +
                "       <col width=\"30%\" text=\"" + SystemEnv.getHtmlLabelName(85, user.getLanguage()) + "\" column=\"holidayDesc\" orderkey=\"holidayDesc\"/>" +
                "       <col width=\"20%\" text=\"" + SystemEnv.getHtmlLabelName(132033, user.getLanguage()) + "\" column=\"changeType\" transmethod=\"com.engine.kq.util.KQTransMethod.getChangeTypeName\" otherpara=\"" + user.getLanguage() + "\" orderkey=\"changeType\"/>" +
                "       <col width=\"30%\" text=\"" + SystemEnv.getHtmlLabelName(390221, user.getLanguage()) + "\" column=\"groupId\" transmethod=\"com.engine.kq.biz.KQGroupComInfo.getGroupname\" orderkey=\"groupId\"/>" +
                "   </head>" +
                "</table>";
        String sessionKey = pageUid + "_" + Util.getEncrypt(Util.getRandom());
        Util_TableMap.setVal(sessionKey, tableString);
        resultMap.put("sessionkey", sessionKey);
        return resultMap;
    }
}
