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.biz.KQExitRulesBiz;
import com.engine.kq.biz.KQReportBiz;
import com.engine.kq.biz.KQTimeSelectionComInfo;
import com.engine.kq.biz.KQTravelRulesBiz;
import com.engine.kq.enums.KqSplitFlowTypeEnum;
import com.engine.kq.wfset.util.SplitSelectSet;
import java.util.ArrayList;
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 GetProcessChangeInfoCmd extends AbstractCommonCommand<Map<String, Object>> {

	public GetProcessChangeInfoCmd(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"));
      String changetype = Util.null2String(params.get("changetype"));
      if(resourceId.length() == 0 || requestid.length() == 0 || changetype.length() == 0){
        retmap.put("status", "2");
        retmap.put("message",  SystemEnv.getHtmlLabelName(513781,user.getLanguage()));
        return retmap;
      }

      List<Object> backList = new ArrayList<>();
      params.put("nonHalfWhole", "1");
      GetProcessChangeTypeCmd processChangeTypeCmd = new GetProcessChangeTypeCmd(params, user);
      Map<String, Object> retmaps = processChangeTypeCmd.execute(commandContext);
      if(!retmaps.isEmpty()){
        if(retmaps.containsKey("process_type") && retmaps.containsKey("process_tablename")){
          int process_type = Util.getIntValue(Util.null2String(retmaps.get("process_type")));
          String process_tablename = Util.null2String(retmaps.get("process_tablename"));
          getRequestInfoByTable(requestid, resourceId,changetype,process_tablename,backList);
          if(backList.isEmpty()){
            new KQReportBiz().reflow(requestid);
            getRequestInfoByTable(requestid, resourceId,changetype,process_tablename,backList);
          }
          if(KqSplitFlowTypeEnum.EVECTION.getFlowtype() == process_type){
            retmap.put("kqtype", "evection");
            do4HalfWholeInfo(retmap,KqSplitFlowTypeEnum.EVECTION);
          }else if(KqSplitFlowTypeEnum.OUT.getFlowtype() == process_type){
            retmap.put("kqtype", "out");
            do4HalfWholeInfo(retmap,KqSplitFlowTypeEnum.OUT);
          }
        }
      }

      if(backList.isEmpty()){
        retmap.put("status", "2");
        retmap.put("message",  SystemEnv.getHtmlLabelName(514170,user.getLanguage()));
      }else{
        retmap.put("backList", backList);
        retmap.put("status", "1");
      }

		}catch (Exception e) {
			retmap.put("status", "-1");
			retmap.put("message",  SystemEnv.getHtmlLabelName(382661,user.getLanguage()));
			writeLog(e);
		}
		return retmap;
	}
  /**
   * 半天和整天信息回显
   * @param retmap
   * @param splitFlowTypeEnum
   */
  public void do4HalfWholeInfo(Map<String, Object> retmap,
      KqSplitFlowTypeEnum splitFlowTypeEnum) {
    boolean isHalf = false;
    boolean isWhole = false;
    String halfFromSel = "[]";
    String halfToSel = "[]";
    String wholeFromSel = "[]";
    String wholeToSel = "[]";

    String forenoon_start = SplitSelectSet.forenoon_start;
    String forenoon_end = SplitSelectSet.forenoon_end;
    String afternoon_start = SplitSelectSet.afternoon_start;
    String afternoon_end = SplitSelectSet.afternoon_end;
    String daylong_start = SplitSelectSet.daylong_start;
    String daylong_end = SplitSelectSet.daylong_end;

    String minimumUnit = KQTravelRulesBiz.getMinimumUnit();
    String timeselection = KQTravelRulesBiz.getTimeselection();
    if(splitFlowTypeEnum == KqSplitFlowTypeEnum.OUT){
      minimumUnit = KQExitRulesBiz.getMinimumUnit();
      timeselection = KQExitRulesBiz.getTimeselection();
    }
    if(!"2".equalsIgnoreCase(timeselection)){
      KQTimeSelectionComInfo kqTimeSelectionComInfo = new KQTimeSelectionComInfo();
      String selectiontype = splitFlowTypeEnum.getFlowtype()+"";
      Map<String,String> map = kqTimeSelectionComInfo.getTimeselections(selectiontype,"0",minimumUnit);

      String am  = SystemEnv.getHtmlLabelName(16689, user.getLanguage());
      String pm  = SystemEnv.getHtmlLabelName(16690, user.getLanguage());
      if(!map.isEmpty()){
        am = Util.null2String(map.get("half_on"));
        pm = Util.null2String(map.get("half_off"));
        if(am.length() == 0){
          am = SystemEnv.getHtmlLabelName(16689, user.getLanguage());
        }
        if(pm.length() == 0){
          pm = SystemEnv.getHtmlLabelName(16690, user.getLanguage());
        }
      }

      halfFromSel = "[{key:'"+forenoon_start+"',showname:'"+am+"'},{key:'"+forenoon_end+"',showname:'"+pm+"'}]";
      halfToSel = "[{key:'"+afternoon_start+"',showname:'"+am+"'},{key:'"+afternoon_end+"',showname:'"+pm+"'}]";

      if("2".equalsIgnoreCase(minimumUnit)){
        isHalf = true;
      }
    }
    wholeFromSel = "[{key:'"+daylong_start+"',showname:'"+SystemEnv.getHtmlLabelName(390728, user.getLanguage())+"'}]";
    wholeToSel = "[{key:'"+daylong_end+"',showname:'"+SystemEnv.getHtmlLabelName(390728, user.getLanguage())+"'}]";
    if("4".equalsIgnoreCase(minimumUnit)){
      isWhole = true;
    }
    retmap.put("isHalf", isHalf?"1":"0");
    retmap.put("isWhole", isWhole?"1":"0");
    retmap.put("halfFromSel", halfFromSel);
    retmap.put("halfToSel", halfToSel);
    retmap.put("wholeFromSel", wholeFromSel);
    retmap.put("wholeToSel", wholeToSel);
  }

  public void getRequestInfoByTable(String requestid, String resourceId, String changetype,String tablename,List<Object> backList) {
    Map<String,Object> backMap = new HashMap<>();
    Map<String,Object> splitMap = new HashMap<>();

    RecordSet rs = new RecordSet();
   // String requestSql = "select * from "+ tablename+" where 1=1 and status = 0 and requestid=? and resourceid=? order by belongdate,fromdate ";
	String requestSql = "select usedetail,fromdatedb,fromtimedb,todatedb,totimedb,durationdb,durationrule,detailid,dataid "
        + " from "+ tablename +" where 1=1 and status = 0 and requestid=? and resourceid=? "
        + " group by usedetail,fromdatedb,fromtimedb,todatedb,totimedb,durationdb,durationrule,detailid,dataid ";
    rs.executeQuery(requestSql, requestid,resourceId);
    while(rs.next()){
      String usedetail = rs.getString("usedetail");
      String fromdatedb = rs.getString("fromdatedb");
      String fromtimedb = rs.getString("fromtimedb");
      String todatedb = rs.getString("todatedb");
      String totimedb = rs.getString("totimedb");
      String durationdb = rs.getString("durationdb");
      String durationrule = rs.getString("durationrule");
      splitMap = new HashMap<>();
      splitMap.put("fromdatedb", fromdatedb);
      splitMap.put("fromtimedb", fromtimedb);
      splitMap.put("todatedb", todatedb);
      splitMap.put("totimedb", totimedb);
      splitMap.put("durationdb", durationdb);
      splitMap.put("re_duration",durationdb);

      Map<String, Object> params = new HashMap<>();
      params.put("resourceId",resourceId);
      params.put("fromDate",fromdatedb);
      params.put("toDate",todatedb);
      params.put("fromTime",fromtimedb);
      params.put("toTime",totimedb);

      if(tablename.equalsIgnoreCase(KqSplitFlowTypeEnum.EVECTION.getTablename())){
        GetEvectionWorkDurationCmd getEvectionWorkDurationCmd = new GetEvectionWorkDurationCmd(params, user);
        Map<String, Object> evection_retmap = getEvectionWorkDurationCmd.execute(null);
        if(evection_retmap != null){
          String duration = Util.null2String(evection_retmap.get("duration"));
          //重新计算得到按照当前班次计算得到的时长
          splitMap.put("re_duration",duration);
        }

      }else if(tablename.equalsIgnoreCase(KqSplitFlowTypeEnum.OUT.getTablename())){
        GetOutWorkDurationCmd getOutWorkDurationCmd = new GetOutWorkDurationCmd(params, user);
        Map<String, Object> out_retmap = getOutWorkDurationCmd.execute(null);
        if(out_retmap != null){
          String duration = Util.null2String(out_retmap.get("duration"));
          //重新计算得到按照当前班次计算得到的时长
          splitMap.put("re_duration",duration);
        }
      }

      String key = "";
      if("1".equalsIgnoreCase(usedetail)){
        key = "detail_"+rs.getString("detailid");
      }else{
        key = "main_"+rs.getString("dataid");
      }
      if(!backMap.containsKey(key)){
        backList.add(splitMap);
        backMap.put(key, backList);
      }
    }

  }

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

}
