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.TimeUtil;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

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

/**
 * 获取导入日志
 */
public class GetImportColResultLogCmd extends AbstractCommonCommand<Map<String, Object>> {


    protected User user;

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

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

    public Map<String, Object> getImportSyslog(Map<String, Object> params, User user) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            String importType = Util.null2String(params.get("importType"));
            String pId = Util.null2String(params.get("pId"));
            String fromdate = Util.null2String(params.get("fromdate"));
            String enddate = Util.null2String(params.get("enddate"));
            String operatorid = Util.null2String(params.get("operatorid"));
            String departmentid = Util.null2String(params.get("departmentid"));
            String subcompanyid = Util.null2String(params.get("subcompanyid"));
            String dateSelect = Util.null2String(params.get("dateSelect"));
            if (!dateSelect.equals("") && !dateSelect.equals("0") && !dateSelect.equals("6")) {
                fromdate = TimeUtil.getDateByOption(dateSelect, "0");
                enddate = TimeUtil.getDateByOption(dateSelect, "1");
            }
            if (!enddate.equals("")) {
                enddate = TimeUtil.dateAdd(enddate, 1);
            }
            String status = Util.null2String(params.get("status"));

            String tabletype = "none";
            String backfields = " a.id,b.operator,b.operatetime,b.operatetype,b.importtype,b.clientaddress,a.relatedname,a.status,a.operatedetail ";
            String sqlform = " hrmimporthistorydetail a, hrmimporthistory b, hrmresourceallview c ";
            String sqlwhere = " a.pid=b.id and b.operator = c.id ";

            if (pId.length() > 0) {
                sqlwhere += " and a.pid=" + pId;
            } else {
                sqlwhere += " and b.importtype = '" + importType + "'";
            }

            if (!fromdate.equals("")) {
                sqlwhere += " and b.operatetime>='" + fromdate + "' and b.operatetime<='" + enddate + "' ";
            }
            if (!operatorid.equals("")) {
                sqlwhere += " and b.operator in (" + operatorid + ") ";
            }
            if (user.getUID() != 1) {
                sqlwhere += " and b.operator in (" + user.getUID() + ") ";
            }
            if (!departmentid.equals("")) {
                sqlwhere += " and c.departmentid in (" + departmentid + ") ";
            }
            if (!subcompanyid.equals("")) {
                sqlwhere += " and c.subcompanyid1 in (" + subcompanyid + ")  ";
            }
            if (!status.equals("")) {
                sqlwhere += " and a.status = " + status;
            }

            String pageUid = "3e2f875a-65bf-422a-7d78-cf188a805afd";
            String tableString = "" +
                    "<table  pageUid=\"" + pageUid + "\" tabletype=\"" + tabletype + "\">" +
                    "<sql backfields=\"" + Util.toHtmlForSplitPage(backfields) + "\"    sqlform=\"" + sqlform + "\" sqlwhere=\"" + Util.toHtmlForSplitPage(sqlwhere) + "\" sqlorderby=\"a.id\" sqlsortway=\"asc\" sqlprimarykey=\"a.id\"  />" +
                    "<head>" +
                    "		<col width=\"15%\" text=\"" + SystemEnv.getHtmlLabelName(97, user.getLanguage()) + "\" column=\"operatetime\" />" +
                    "		<col width=\"10%\" text=\"" + SystemEnv.getHtmlLabelName(99, user.getLanguage()) + "\" column=\"operator\" transmethod=\"weaver.hrm.resource.ResourceComInfo.getResourcename\"  />" +
                    "		<col width=\"10%\" text=\"" + SystemEnv.getHtmlLabelName(63, user.getLanguage()) + "\" column=\"operatetype\" transmethod=\"com.engine.hrm.util.HrmTransMethod.getImportOperateTypeName\" otherpara=\"" + user.getLanguage() + "\"   />" +
                    "		<col width=\"10%\" text=\"" + SystemEnv.getHtmlLabelName(101, user.getLanguage()) + "\" column=\"importtype\" transmethod=\"com.engine.hrm.util.HrmTransMethod.getImportTypeName\" otherpara=\"" + user.getLanguage() + "\"  />" +
                    "		<col width=\"10%\" text=\"" + SystemEnv.getHtmlLabelName(106, user.getLanguage()) + "\" column=\"relatedname\"  />" +
                    "		<col width=\"10%\" text=\"" + SystemEnv.getHtmlLabelName(602, user.getLanguage()) + "\" column=\"status\"/>" +
                    "		<col width=\"15%\" text=\"" + SystemEnv.getHtmlLabelName(17463, user.getLanguage()) + "\" column=\"operatedetail\"  />" +
                    "		<col width=\"10%\" text=\"" + SystemEnv.getHtmlLabelName(17484, 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;
    }

}
