package com.engine.kq.cmd.importlog;

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.User;
import weaver.systeminfo.SystemEnv;

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

/**
 * 获取历史导入记录
 */
public class GetImportHistoryCmd extends AbstractCommonCommand<Map<String, Object>> {

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

    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            String importType = Util.null2String(params.get("importType"));

            String sqlWhere = " where importtype = '" + importType + "' ";
            if (user.getUID() != 1) {
                sqlWhere += " and operator = " + user.getUID() + " ";
            }
            String tabletype = "none";
            String backfields = " a.*, (select count(1) from hrmimporthistorydetail where pid  = a.id) as allnum , (select count(1) from hrmimporthistorydetail where pid = a.id and status = '1') as succnum ";
            String sqlform = "  hrmimporthistory a ";
            String groupby = " ";
            String pageUid = "d8d953d3-bbd6-b3b9-f283-45b70b91ea7b";
            String tableString = "" +
                    "<table  pageUid=\"" + pageUid + "\"   pagesize=\"" + PageIdConst.getPageSize("KQ:ImportHistoryList", user.getUID(), PageIdConst.HRM) + "\" tabletype=\"" + tabletype + "\">" +
                    "<sql backfields=\"" + Util.toHtmlForSplitPage(backfields) + "\"    sqlform=\"" + sqlform + "\" sqlwhere=\"" + Util.toHtmlForSplitPage(sqlWhere) + "\" sqlorderby=\"a.id\" sqlsortway=\"desc\" sqlgroupby=\"" + groupby + "\" sqlprimarykey=\"a.id\"  />" +
                    "<head>" +
                    "		<col width=\"0%\"  hide=\"true\"  text=\"\" column=\"a.id\"/>" +
                    "		<col width=\"0%\"  hide=\"true\"  text=\"\" column=\"operator\"/>" +
                    "		<col width=\"17%\" text=\"" + SystemEnv.getHtmlLabelName(10000438,weaver.general.Util.getIntValue(user.getLanguage())) + "\" column=\"operator\" transmethod=\"weaver.hrm.resource.ResourceComInfo.getResourcename\" />" +
                    "		<col width=\"17%\" text=\"" + SystemEnv.getHtmlLabelName(20515, user.getLanguage()) + "\" column=\"operatetime\"   />" +
                    "		<col width=\"17%\" text=\"" + SystemEnv.getHtmlLabelName(82341, user.getLanguage()) + "\" column=\"allnum\"   transmethod=\"com.engine.hrm.util.HrmTransMethod.getImoprtResult\"   otherpara=\"" + user.getLanguage() + "\"   otherpara2=\"column:succnum\" />" +
                    "		<col width=\"17%\" text=\"" + SystemEnv.getHtmlLabelName(33586, user.getLanguage()) + "\" column=\"clientaddress\"  />" +
                    "</head>" +
                    "</table>";
            //主要用于 显示定制列以及 表格 每页展示记录数选择
            String sessionkey = pageUid + "_" + Util.getEncrypt(Util.getRandom());
            Util_TableMap.setVal(sessionkey, tableString);

            resultMap.put("status", "1");
            resultMap.put("sessionkey", sessionkey);
        } catch (Exception e) {
            resultMap.put("status", "-1");
            resultMap.put("message", SystemEnv.getHtmlLabelName(382661, user.getLanguage()));
            writeLog(e);
        }

        return resultMap;
    }

    @Override
    public BizLogContext getLogContext() {
        // TODO Auto-generated method stub
        return null;
    }

}
