package com.engine.kq.cmd.attendanceEvent;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.enums.KqSplitFlowTypeEnum;
import com.google.common.collect.Lists;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

/**
 * 获取考勤变更流程类型，是出差还是请假还是其他
 */
public class GetProcessChangeTypeCmd extends AbstractCommonCommand<Map<String, Object>> {

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

	@Override
	public Map<String, Object> execute(CommandContext commandContext) {
		Map<String, Object> retmap = new HashMap<String, Object>();
		try{
      String resourceId = Util.null2String(params.get("resourceId"));
      String requestid = Util.null2String(params.get("requestid"));
      boolean isAll = "1".equalsIgnoreCase(Util.null2String(params.get("isAll")));
      //1表示不需要再去获取一遍半天全天的数据
      boolean nonHalfWhole = "1".equalsIgnoreCase(Util.null2String(params.get("nonHalfWhole")));

      RecordSet rs = new RecordSet();
      List<KqSplitFlowTypeEnum> tables = Lists.newArrayList();
      tables.add(KqSplitFlowTypeEnum.EVECTION);
      tables.add(KqSplitFlowTypeEnum.OUT);

      for(KqSplitFlowTypeEnum tablenameEnum : tables){
        String requestSql = "select * from "+ tablenameEnum.getTablename()+" where 1=1 "+(isAll?"":" and (status is null or status<>1)")+"  and requestid=? and resourceid=? order by belongdate,fromdate ";
        rs.executeQuery(requestSql,requestid,resourceId);
        if(rs.next()){
          retmap.put("process_type", tablenameEnum.getFlowtype());
          retmap.put("process_tablename", tablenameEnum.getTablename());
          if(!nonHalfWhole){
            if(KqSplitFlowTypeEnum.EVECTION.getFlowtype() == tablenameEnum.getFlowtype()){
              new GetProcessChangeInfoCmd(params,user).do4HalfWholeInfo(retmap,KqSplitFlowTypeEnum.EVECTION);
            }else if(KqSplitFlowTypeEnum.OUT.getFlowtype() == tablenameEnum.getFlowtype()){
              new GetProcessChangeInfoCmd(params,user).do4HalfWholeInfo(retmap,KqSplitFlowTypeEnum.OUT);
            }
          }
          break;
        }
      }
      retmap.put("status", "1");

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

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

}
