package com.engine.kq.cmd.importlog;

import com.api.hrm.util.PageUidFactory;
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.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

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

/**
 * 获取导入结果
 */
public class GetImportResultCmd extends AbstractCommonCommand<Map<String, Object>> {

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

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> resultMap = new HashMap<String, Object>();

        try {
            String pId = Util.null2String(params.get("pId"));
            Map<String, Object> dataMap = new HashMap<String, Object>();
            int failnum = 0;//失败的数量
            int succnum = 0;//成功的数量
            RecordSet rs = new RecordSet();
            rs.executeSql("select status,count(status) from hrmimporthistorydetail where pid =" + pId + " group by status ");
            while (rs.next()) {
                int status = rs.getInt("status");
                int rowCount = rs.getInt(2);
                if (status == 1) {
                    succnum = rowCount;
                } else {
                    failnum = rowCount;
                }
            }

            dataMap.put("succnum", succnum);
            dataMap.put("failnum", failnum);
            if (failnum > 0) {
                String sqlwhere = " where pid =" + pId + " and status = '0' ";
                String tabletype = "none";
                String backfields = " * ";
                String sqlform = "hrmimporthistorydetail";
                String groupby = "  ";
                String pageUid = "34d997a0-55bf-4221-a303-2cca5917436e";
                String tableString = "" +
                        "<table  pageUid=\"" + pageUid + "\" tabletype=\"" + tabletype + "\">" +
                        "<sql backfields=\"" + Util.toHtmlForSplitPage(backfields) + "\"    sqlform=\"" + sqlform + "\" sqlwhere=\"" + Util.toHtmlForSplitPage(sqlwhere) + "\" sqlorderby=\"id\" sqlsortway=\"asc\" sqlgroupby=\"" + groupby + "\" sqlprimarykey=\"id\"  />" +
                        "<head>" +
                        "		<col width=\"15%\" text=\"" + SystemEnv.getHtmlLabelName(18620, user.getLanguage()) + "\" column=\"rownums\"/>" +
                        "		<col width=\"85%\" text=\"" + SystemEnv.getHtmlLabelName(24960, user.getLanguage()) + "\" column=\"operatedetail\"  />" +
                        "</head>" +
                        "</table>";
                //主要用于 显示定制列以及 表格 每页展示记录数选择
                String sessionkey = pageUid + "_" + Util.getEncrypt(Util.getRandom());
                Util_TableMap.setVal(sessionkey, tableString);
                dataMap.put("sessionkey", sessionkey);
            }

            resultMap.put("status", "1");
            resultMap.put("datas", dataMap);
        } 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;
    }

}
