package com.engine.kq.cmd.attendanceEvent;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.biz.KQTimesArrayComInfo;
import com.engine.kq.enums.KqSplitFlowTypeEnum;
import com.engine.kq.log.KQLog;
import com.engine.kq.wfset.util.KQFlowProcessChangeUtil;
import com.google.common.collect.Maps;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

/**
 * 考勤变更流程提交校验事件
 */
public class CheckProcessChangeCmd extends AbstractCommonCommand<Map<String, Object>> {

	public CheckProcessChangeCmd(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{
		  KQTimesArrayComInfo kqTimesArrayComInfo = new KQTimesArrayComInfo();
      DateTimeFormatter datetimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
      String resMap = Util.null2String(params.get("resMap"));
      int workflowid = Util.getIntValue(Util.null2String(params.get("workflowid")));
      int nodeid = Util.getIntValue(Util.null2String(params.get("nodeid")));
      String resourceId = Util.null2String(params.get("resourceid"));
      String requestid = Util.null2String(params.get("requestid"));
      String changerequestid = Util.null2String(params.get("changerequestid"));
      String changetype = Util.null2String(params.get("changetype"));
      String attid = Util.null2String(params.get("attid"));
      String checkRuleData2json = Util.null2String(params.get("checkRuleData2json"));
      int currentnodetype = Util.getIntValue(Util.null2String(params.get("currentnodetype")));
      new KQLog().info("checkRuleData2json:"+checkRuleData2json);
      if(resourceId.length() == 0 || requestid.length() == 0 || changetype.length() == 0){
        retmap.put("status", "2");
        retmap.put("message",  SystemEnv.getHtmlLabelName(513781,user.getLanguage()));
        return retmap;
      }

      JSONObject checkRuleData2jsonObject = (JSONObject) JSON.parse(checkRuleData2json);
      if(!checkRuleData2jsonObject.isEmpty()){
        Set<Entry<String, Object>> checkRuleDataSet = checkRuleData2jsonObject.entrySet();

        for (Entry<String, Object> entry : checkRuleDataSet) {
          String duplicateValue = Util.null2String(entry.getValue());
          String[] duplicateValues = duplicateValue.split("_",-1);
          if(duplicateValues.length == 10){
            String fromDate = duplicateValues[1];
            String fromTime = duplicateValues[2];
            String toDate = duplicateValues[3];
            String toTime = duplicateValues[4];
            String attendancefromDate = duplicateValues[6];
            String attendancefromTime = duplicateValues[7];
            String attendancetoDate = duplicateValues[8];
            String attendancetoTime = duplicateValues[9];
            boolean canProcessChange = KQFlowProcessChangeUtil.canProcessChange(resourceId,changerequestid,attendancefromDate,attendancefromTime,attendancetoDate,attendancetoTime);
            if(!canProcessChange){
              retmap.put("status", "2");
              retmap.put("message",  ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005328,weaver.general.ThreadVarLanguage.getLang())+","+weaver.systeminfo.SystemEnv.getHtmlLabelName(383337,weaver.general.ThreadVarLanguage.getLang())+"");
              return retmap;
            }
          }
        }

        if("0".equalsIgnoreCase(changetype)){
          //撤销的话就不需要校验重复了
          CheckRuleUtil checkDuplicateUtil = new CheckRuleUtil();
          checkDuplicateUtil.checkRule(checkRuleDataSet,retmap,user,""+ KqSplitFlowTypeEnum.PROCESSCHANGE.getFlowtype(),
              Util.getIntValue(requestid),currentnodetype,attid);
        }
      }
      if(retmap.isEmpty()){
        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;
	}

}
