package com.engine.kq.cmd.hrmAttProcSet;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.biz.SimpleBizLogger;
import com.engine.common.constant.BizLogSmallType4Hrm;
import com.engine.common.constant.BizLogType;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.biz.KQAttFlowSetComInfo;
import com.engine.kq.wfset.attendance.domain.HrmAttProcSet;
import com.engine.kq.wfset.attendance.manager.HrmAttProcRelationManager;
import com.engine.kq.wfset.attendance.manager.HrmAttProcSetManager;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import weaver.common.DateUtil;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

/**
 * 保存考勤流程设置的 字段对应信息
 * @author pzy
 *
 */
public class SaveStateProcSetFlowWfFieldsCmd extends AbstractCommonCommand<Map<String, Object>>{
	
  private HttpServletRequest request;
  private SimpleBizLogger logger;

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

	public SaveStateProcSetFlowWfFieldsCmd(Map<String, Object> params, HttpServletRequest request,User user) {
		this.user = user;
		this.params = params;
		this.request = request;
    HrmAttProcSetLogUtil hrmAttProcSetLogUtil = new HrmAttProcSetLogUtil(user,BizLogType.HRM_ENGINE, BizLogSmallType4Hrm.HRM_ENGINE_HRM_ATT_PROCSET, BizLogSmallType4Hrm.HRM_ENGINE_HRM_ATT_PROCSET);
    String realLogSql = "select * from kq_ATT_PROC_RELATION";
    this.logger = hrmAttProcSetLogUtil.logStateSetLogSql(true, realLogSql, params,"wffield");
  }

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

  @Override
  public List<BizLogContext> getLogContexts() {
    return logger.getBizLogContexts();
  }

	@Override
	public Map<String, Object> execute(CommandContext commandContext) {
		Map<String,Object> retmap = new HashMap<String,Object>();
		 
		try {
			if(!HrmUserVarify.checkUserRight("HrmAttendanceProcess:setting", user)){
				retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
				return retmap;
			}
      KQAttFlowSetComInfo kqAttFlowSetComInfo = new KQAttFlowSetComInfo();
			HrmAttProcSetManager attProcSetManager = new HrmAttProcSetManager();
			HrmAttProcRelationManager attProcRelationManager = new HrmAttProcRelationManager();
			HrmAttProcSet bean = attProcSetManager.get(Util.getIntValue(Util.null2String(request.getParameter("field001")), 0));
			if(bean != null) {
        attProcRelationManager.save(request,bean,retmap,user);
        if(!retmap.isEmpty()){
          return retmap;
        }
        bean.setField009(DateUtil.getCurrentDate());
        attProcSetManager.update(bean);
			}else{
        retmap.put("status", "-1");
        retmap.put("message",  SystemEnv.getHtmlLabelName(382661,user.getLanguage()));
        writeLog("字段对应保存的时候获取HrmAttProcSet报错");
        return retmap;
      }
      kqAttFlowSetComInfo.removeKQAttFlowSetComInfoCache();
      retmap.put("status", "1");

		} catch (Exception e) {
			retmap.put("status", "-1");
			retmap.put("message",  SystemEnv.getHtmlLabelName(382661,user.getLanguage()));
			writeLog(e);
		}
			
		return retmap;
	}
}
