package com.engine.kq.cmd.schedulecode;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;

import java.util.*;

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;

public class SaveScheduleCodeImportCmd extends AbstractCommonCommand<Map<String, Object>> {
  private HttpServletRequest request;

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

  @Override
  public Map<String, Object> execute(CommandContext commandContext) {
    Map<String, Object> resultMap = new HashMap<String, Object>();
    try {
      boolean canEdit = HrmUserVarify.checkUserRight("ScheduleCode: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);
      resultMap.put("status", "-1");
      resultMap.put("message", e.getMessage());
    }
    resultMap.put("pId", request.getSession(true).getAttribute("kqScheduleCodePId"));
    return resultMap;
  }

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