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

import com.alibaba.fastjson.JSON;
import com.engine.kq.biz.KQOvertimeRulesBiz;
import com.engine.kq.biz.KQShiftRuleInfoBiz;
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 com.google.common.collect.Lists;
import java.util.List;
import java.util.Map;

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

  public OvertimeWorkday2WorkdayChain(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 == 2){
      doOverTimeSplit2_2(splitBean);
    }else{
      this.success.handleOvertimeChain(splitBean);
    }
  }

  public void doOverTimeSplit2_2(SplitBean splitBean) throws Exception {
    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("OvertimeWorkday2WorkdayChain doOverTimeSplit2_2:resourceid："+resourceid+":preComputingMode:"+preComputingMode);
    if(preComputingMode == 3){
//          无需审批，根据打卡时间计算加班时长
      splitBean.setD_Mins(0.0);
      splitBean.setComputingMode(preComputingMode+"");
      return ;
    }else{
      ShiftInfoBean shiftInfoBean = KQDurationCalculatorUtil.getWorkTime(resourceid, splitDate,true);
      kqLog.info("OvertimeWorkday2WorkdayChain doOverTimeSplit2_2:resourceid："+resourceid+":shiftInfoBean:"+(shiftInfoBean != null ? JSON
          .toJSONString(shiftInfoBean) : null));

      if(shiftInfoBean != null){
        List<int[]> workLongTimeIndex = shiftInfoBean.getWorkLongTimeIndex();
        List<int[]> real_workLongTimeIndex = Lists.newArrayList();

        //list带数组，这里要深拷贝
        for(int[] tmp : workLongTimeIndex){
          int[] real_tmp = new int[tmp.length];
          System.arraycopy(tmp, 0, real_tmp, 0, tmp.length);
          real_workLongTimeIndex.add(real_tmp);
        }
        if(real_workLongTimeIndex.size() == 1){
          //个性化设置只支持一次打卡的
          KQShiftRuleInfoBiz kqShiftRuleInfoBiz = new KQShiftRuleInfoBiz();
          kqShiftRuleInfoBiz.rest_workLongTimeIndex(shiftInfoBean,splitBean,real_workLongTimeIndex,arrayComInfo,null);
        }
        int curFirstTimeIndex = handlePreWork(splitBean, arrayComInfo, shiftInfoBean,real_workLongTimeIndex);
        kqLog.info("OvertimeWorkday2WorkdayChain doOverTimeSplit2_2:resourceid："+resourceid+":curFirstTimeIndex:"+curFirstTimeIndex);
        if(curFirstTimeIndex >= 0){
          handleCurWork(splitBean, arrayComInfo, shiftInfoBean, real_workLongTimeIndex,
              curFirstTimeIndex);
        }else{
          kqLog.info("doOverTimeSplit2_2，工作日加班shiftInfoBean为null:resourceid:"+resourceid+":splitDate:"+splitDate);
        }
      }else{
        //前一天工作日为null 直接视为普通的工作日加班
        doOverTimeSplit2(splitBean);
      }
    }


  }
}
