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

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 NonWholeUnitSplitChain extends NonWorkDurationChain {

  /**
   * 全天规则 下拉框对应的开始时间
   */
  public static final String daylong_start = "08:00";

  /**
   * 全天规则 下拉框对应的结束时间
   */
  public static final String daylong_end = "18:00";

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

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

  @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.daylong_start))){
      return;
    }
    if(!(toTime.equalsIgnoreCase(SplitSelectSet.daylong_end))){
      return;
    }

    //获取对应人员和对应日期下的工作时段
    List<Map<String,String>> splitLists = SplitActionUtil.getSplitDayList(fromDate,toDate);
    double hoursToDay = KQOvertimeRulesBiz.getHoursToDay();
    int workmins = (int)(hoursToDay * 60);

    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 overtimeEnable = KQOvertimeRulesBiz.getOvertimeEnable(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 duration = "1.0";

        splitBean.setFromTime(daylong_start);
        splitBean.setToTime(daylong_end);
        splitBean.setDuration(duration);
        splitBean.setOneDayHour(hoursToDay);
        splitBean.setWorkmins(workmins);
        splitBean.setBelongDate(splitDate);
        splitBean.setFromDate(splitDate);
        splitBean.setToDate(splitDate);
        splitBean.setComputingMode(computingMode+"");
        double d_mins = Util.getDoubleValue(splitBean.getDuration())*splitBean.getWorkmins();
        splitBean.setD_Mins(d_mins);

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

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

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

}
