package com.engine.kq.biz.chain.overtime;

import com.alibaba.fastjson.JSON;
import com.engine.kq.biz.KQOvertimeRulesBiz;
import com.engine.kq.biz.KQTimesArrayComInfo;
import com.engine.kq.biz.chain.shiftinfo.ShiftInfoBean;
import com.engine.kq.util.KQDurationCalculatorUtil;
import com.engine.kq.wfset.bean.SplitBean;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import weaver.general.Util;

/**
 * 如果前一天是工作日，当天是休息日的情况
 */
public class OvertimeWorkday2RestdayChain extends OvertimeChain{

  public OvertimeWorkday2RestdayChain(Map<String, String> splitMap,List<SplitBean> splitBeans) {
    super(splitMap,splitBeans);
  }

  @Override
  public void handleOvertimeChain(SplitBean splitBean) throws Exception {
    int changeType = splitBean.getChangeType();
    int preChangeType = splitBean.getPreChangeType();
    if(preChangeType == 2 && changeType == 3){
      doOverTimeSplit2_3(splitBean);
    }else{
      this.success.handleOvertimeChain(splitBean);
    }
  }

  public void doOverTimeSplit2_3(SplitBean splitBean) {

    KQTimesArrayComInfo arrayComInfo = new KQTimesArrayComInfo();

    String resourceid = splitBean.getResourceId();
    String splitDate = splitBean.getBelongDate();
    String preSplitDate = splitMap.get("preSplitDate");
    int preComputingMode = KQOvertimeRulesBiz.getComputingMode(resourceid, preSplitDate);

    kqLog.info("OvertimeWorkday2RestdayChain doOverTimeSplit2_3:resourceid："+resourceid+":preComputingMode:"+preComputingMode);
    if(preComputingMode == 3){
//          前一天是无需审批，根据打卡时间计算加班时长,那么只需要按照普通的休息日加班处理就可以
      doOverTimeSplit3(splitBean);
    }else{
      ShiftInfoBean shiftInfoBean = KQDurationCalculatorUtil.getWorkTime(resourceid, splitDate,true);
      kqLog.info("OvertimeWorkday2RestdayChain doOverTimeSplit2_3:resourceid："+resourceid+":shiftInfoBean:"+(shiftInfoBean != null ? JSON
          .toJSONString(shiftInfoBean) : null));

      if(shiftInfoBean != null) {
        int firstWorkIndex = handlePreWorkday(splitBean, arrayComInfo, shiftInfoBean);
        if(firstWorkIndex >= 0){
          handleCurRestday(splitBean, arrayComInfo, firstWorkIndex);
        }
      }else{
        //前一天工作日为null 直接视为普通的休息日加班
        doOverTimeSplit3(splitBean);
      }

    }
  }
}
