package com.engine.kq.cmd.importlog;

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 javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class GetImportProcessLogCmd extends AbstractCommonCommand<Map<String, Object>> {

    protected HttpServletRequest request;

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

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

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        return getImportMessage(params, request, user);
    }

    @SuppressWarnings("unchecked")
    public Map<String, Object> getImportMessage(Map<String, Object> params, HttpServletRequest request, User user) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Map<String, Object> dataMap = new HashMap<String, Object>();
        List<Object> dataList = new ArrayList<Object>();
        int logId = Util.getIntValue((String)params.get("index"), 0);
        String importType = Util.null2String(params.get("importType"));
        String pIdName = "hrm" + importType + "PId";
        String pId = Util.null2String(this.request.getSession(true).getAttribute(pIdName)).trim();
        try {
            String importStatus = "";
            String sql = "";
            RecordSet rs = new RecordSet();

            int index = logId;
            if (pId.length() > 0) {
                sql = "select * from hrmimporthistory where id= " + pId;
                rs.executeSql(sql);
                if (rs.next()) {
                    importStatus = rs.getString("status");
                }

                if ("importing".equals(importStatus) || "over".equals(importStatus)) {
                    sql = "select * from hrmimporthistorydetail where pid = " + pId;
                    if (logId > 0) {
                        sql += " and id> " + logId;
                    }
                    rs.executeSql(sql);
                    while (rs.next()) {
                        index = rs.getInt("id");
                        dataMap = new HashMap<String, Object>();
                        dataMap.put("rownum", rs.getInt("rownums"));
                        dataMap.put("status", rs.getString("status"));
                        dataMap.put("message", rs.getString("operateDetail"));
                        dataList.add(dataMap);
                    }
                    if ("over".equals(importStatus)) {
                        this.request.getSession(true).removeAttribute(pIdName);
                    }
                }
            }

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

}
