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

import com.engine.kq.biz.KQGroupMemberComInfo;
import com.engine.kq.biz.KQOvertimeRulesBiz;
import com.engine.kq.wfset.bean.SplitBean;
import com.engine.kq.wfset.util.SplitActionUtil;
import com.engine.kq.wfset.util.SplitSelectSet;
import java.util.List;
import java.util.Map;
import org.springframework.beans.BeanUtils;
import weaver.general.BaseBean;
import weaver.general.Util;

/**
 * 非工作时长 按半天计算
 */
public class NonHalfUnitSplitChain extends NonWorkDurationChain {

  /**
   * 半天规则下 上午-上午
   */
  public static final int fore_fore_index = 0;

  /**
   * 半天规则下 上午-下午
   */
  public static final int fore_after_index = 1;

  /**
   * 半天规则下 下午-上午
   */
  public static final int after_fore_index = 2;

  /**
   * 半天规则下 下午-下午
   */
  public static final int after_after_index = 3;


  public NonHalfUnitSplitChain(List<SplitBean> splitBeans) {
    super(splitBeans);
  }

  @Override
  public void handleDuration(SplitBean splitBean)  throws Exception{
    String durationrule = splitBean.getDurationrule();
    if("2".equalsIgnoreCase(durationrule)){
      getSplitDurationBean(splitBean);
      if(!splitBeans.isEmpty()){
        SplitActionUtil.turnOvertimeBeansWithRule(splitBeans);
      }
    }else{
      this.success.handleDuration(splitBean);
    }
  }

  @Override
  public void getSplitDurationBean(SplitBean splitBean){

    String resourceid = splitBean.getResourceId();
    String fromDate = splitBean.getFromdatedb();
    String toDate = splitBean.getTodatedb();
    String fromTime = splitBean.getFromtimedb();
    String toTime = splitBean.getTotimedb();

    if(!(fromTime.equalsIgnoreCase(SplitSelectSet.forenoon_start) || fromTime.equalsIgnoreCase(SplitSelectSet.forenoon_end))){
      return;
    }
    if(!(toTime.equalsIgnoreCase(SplitSelectSet.afternoon_start) || toTime.equalsIgnoreCase(SplitSelectSet.afternoon_end))){
      return;
    }
    //获取对应人员和对应日期下的工作时段
    List<Map<String,String>> splitLists = SplitActionUtil.getSplitHalfDayList(fromDate,toDate,fromTime,toTime);
    double hoursToDay = KQOvertimeRulesBiz.getHoursToDay();
    int workmins = (int)(hoursToDay * 60);
    KQGroupMemberComInfo kqGroupMemberComInfo = new KQGroupMemberComInfo();

    if(!splitLists.isEmpty()) {
      for (int i = 0; i < splitLists.size(); i++) {
        Map<String, String> splitMap = splitLists.get(i);
        String splitDate = Util.null2String(splitMap.get("splitDate"));
        int foreOrAfter = Util.getIntValue(Util.null2String(splitMap.get("foreOrAfter")));

        int overtimeEnable = KQOvertimeRulesBiz.getOvertimeEnable(resourceid, splitDate);
        String groupId = kqGroupMemberComInfo.getKQGroupId(resourceid, splitDate);

        //如果人再当前指定日期下未开启加班，直接跳过
        if(overtimeEnable != 1){
          kqLog.info("getOvertimeEnable:resourceid："+resourceid+":splitDate:"+splitDate+"不允许加班");
          continue;
        }

        int changeType = KQOvertimeRulesBiz.getChangeType(resourceid, splitDate);
        int computingMode = KQOvertimeRulesBiz.getComputingMode(resourceid, splitDate);
        if(computingMode == 3){
//          无需审批，根据打卡时间计算加班时长
          continue;
        }

        String splitFromTime = "";
        String splitToTime = "";
        String duration = "";
        boolean isRightTime = true;
        switch (foreOrAfter){
          case NonHalfUnitSplitChain.fore_fore_index:
            splitFromTime = SplitSelectSet.forenoon_start;
            splitToTime = SplitSelectSet.forenoon_end;
            duration = "0.5";
            break;
          case NonHalfUnitSplitChain.fore_after_index:
            splitFromTime = SplitSelectSet.forenoon_start;
            splitToTime = SplitSelectSet.afternoon_end;
            duration = "1.0";
            break;
          case NonHalfUnitSplitChain.after_fore_index:
            break;
          case NonHalfUnitSplitChain.after_after_index:
            splitFromTime = SplitSelectSet.forenoon_end;
            splitToTime = SplitSelectSet.afternoon_end;
            duration = "0.5";
            break;
          default:
            isRightTime = false;
            break;
        }
        if(!isRightTime){
          continue;
        }
        splitBean.setFromTime(splitFromTime);
        splitBean.setToTime(splitToTime);
        splitBean.setDuration(duration);
        splitBean.setOneDayHour(hoursToDay);
        splitBean.setWorkmins(workmins);
        splitBean.setBelongDate(splitDate);
        splitBean.setFromDate(splitDate);
        splitBean.setToDate(splitDate);
        splitBean.setComputingMode(computingMode+"");
        splitBean.setGroupid(groupId);
        double d_mins = Util.getDoubleValue(splitBean.getDuration())*splitBean.getWorkmins();
        splitBean.setD_Mins(d_mins);

//        1-节假日、2-工作日、3-休息日
        if(changeType == 1){
          splitBean.setChangeType(1);
          splitBean.setPub_duration(duration);
        }

        if(changeType == 2){
          splitBean.setChangeType(2);
          splitBean.setWork_duration(duration);
        }

        if(changeType == 3){
          splitBean.setChangeType(3);
          splitBean.setRest_duration(duration);
        }
        //然后把bean重新赋值下，根据拆分后的时间
        SplitBean newsplitBean = new SplitBean();
        BeanUtils.copyProperties(splitBean, newsplitBean);
        splitBeans.add(newsplitBean);
      }
    }
  }

}
