package com.engine.kq.cmd.balanceofleave;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.util.ImportProcessUtil;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
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;

/**
 * 假期余额--保存Excel导入
 */
public class SaveImportCmd extends AbstractCommonCommand<Map<String, Object>> {

    private HttpServletRequest request;

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

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            boolean canEdit = HrmUserVarify.checkUserRight("KQLeaveRulesEdit:Edit", user);//是否具有新建权限
            if (!canEdit) {
                resultMap.put("status", "-1");
                resultMap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
                return resultMap;
            }
            List<Object> errorInfoList = new ArrayList<Object>();
            ImportProcessUtil importProcessUtil = new ImportProcessUtil();
            List errorInfo = importProcessUtil.importXls(params, user, request);
            if (errorInfo != null && !errorInfo.isEmpty()) {
                Map<String, Object> error = null;
                for (int i = 0; i < errorInfo.size(); i++) {
                    error = new HashMap<String, Object>();
                    error.put("message", Util.null2String(errorInfo.get(i)));
                    errorInfoList.add(error);
                }
            }
            resultMap.put("errorInfo", errorInfoList);
            resultMap.put("status", "1");
            resultMap.put("message", SystemEnv.getHtmlLabelName(24645, user.getLanguage()));
        } catch (Exception e) {
            writeLog(e);
            writeLog("导入员工假期余额数据失败：" + e.getMessage());
            resultMap.put("status", "-1");
            resultMap.put("message", e.getMessage());
        }
        resultMap.put("pId", request.getSession(true).getAttribute("KQBalanceOfLeavePId"));
        return resultMap;
    }
}
