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

import com.engine.kq.biz.KQOverTimeFlowBiz;
import com.engine.kq.biz.KQOvertimeRulesBiz;
import com.engine.kq.biz.chain.overtime.OvertimeChain;
import com.engine.kq.biz.chain.overtime.OvertimeHoliday2HolidayChain;
import com.engine.kq.biz.chain.overtime.OvertimeHoliday2RestdayChain;
import com.engine.kq.biz.chain.overtime.OvertimeHoliday2WorkdayChain;
import com.engine.kq.biz.chain.overtime.OvertimeRestday2HolidayChain;
import com.engine.kq.biz.chain.overtime.OvertimeRestday2RestdayChain;
import com.engine.kq.biz.chain.overtime.OvertimeRestday2WorkdayChain;
import com.engine.kq.biz.chain.overtime.OvertimeWorkday2HolidayChain;
import com.engine.kq.biz.chain.overtime.OvertimeWorkday2RestdayChain;
import com.engine.kq.biz.chain.overtime.OvertimeWorkday2WorkdayChain;
import com.engine.kq.log.KQLog;
import com.engine.kq.wfset.bean.SplitBean;
import com.engine.kq.wfset.util.KQFlowUtil;
import com.engine.kq.wfset.util.SplitActionUtil;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import org.springframework.beans.BeanUtils;
import weaver.general.Util;

/**
 * 非工作时长 计算
 */
public abstract class NonWorkDurationChain {

  protected NonWorkDurationChain success;	//定义下一个处理对象
  protected List<SplitBean> splitBeans;
  protected KQLog kqLog = new KQLog();

  public NonWorkDurationChain(List<SplitBean> splitBeans) {
    this.splitBeans = splitBeans;
  }
  //创建链
  public void setDurationChain(NonWorkDurationChain durationIndex) {
    this.success=durationIndex;
  }

  public abstract void handleDuration(SplitBean splitBean)  throws Exception;

  /**
   * 这个是流程上使用的，当流程拆分了之后，每一天的日期和时间传过来
   * 需要根据这些条件和请假，出差的半全天的规则给开始时间和结束时间重新赋值
   * 在这里获取到bean从splitBean里拿到需要的开始时间和结束时间
   * @return
   */
  public void getSplitDurationBean(SplitBean splitBean) throws Exception{
    try{
      if(true){
        KQOverTimeFlowBiz kqNonWorkDurationBiz = new KQOverTimeFlowBiz();
        kqNonWorkDurationBiz.getSplitDurationBean_new(splitBean,splitBeans);
      }else{
        getSplitDurationBean_old(splitBean);
      }
    }catch (Exception e){
      KQLog kqLog = new KQLog();
      kqLog.info("WorkDurationChain:getSplitDurationBean:");
      StringWriter errorsWriter = new StringWriter();
      e.printStackTrace(new PrintWriter(errorsWriter));
      kqLog.info(errorsWriter.toString());
      e.printStackTrace();
      throw new Exception("NonWorkDurationChain  报错异常:"+e.getMessage());
    }
  }

  public void getSplitDurationBean_old(SplitBean splitBean) throws Exception{

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

    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    double oneDayHour = KQFlowUtil.getOneDayHour(splitBean.getDurationTypeEnum(),"");
    int workmins = (int)(oneDayHour * 60);

    //获取对应人员和对应日期下的工作时段
    List<Map<String,String>> splitLists = SplitActionUtil.getSplitList(fromDate,toDate,fromTime,toTime);

    if(!splitLists.isEmpty()) {
      int splitCount = splitLists.size();
      for (int i = 0; i < splitCount; i++) {
        Map<String, String> splitMap = splitLists.get(i);
        String splitDate = Util.null2String(splitMap.get("splitDate"));
        int changeType = KQOvertimeRulesBiz.getChangeType(resourceid, splitDate);
        String preSplitDate = LocalDate.parse(splitDate).minusDays(1).format(dateFormatter);
        int preChangeType = KQOvertimeRulesBiz.getChangeType(resourceid, preSplitDate);
        String splitFromTime = Util.null2String(splitMap.get("fromTime"));
        String splitToTime = Util.null2String(splitMap.get("toTime"));

        splitMap.put("preSplitDate", preSplitDate);

        splitBean.setOneDayHour(oneDayHour);
        splitBean.setWorkmins(workmins);
        kqLog.info("getSplitDurationBean:resourceid："+resourceid+":preChangeType:"+preChangeType+":changeType:"+changeType);

        //如果是跨天的，而且班次也跨天，流程存在跨天的情况，那么除了第一天，其他的每一天都针对前一天的班次需要多加1分钟
        if(splitCount > 1){
          if(!splitDate.equalsIgnoreCase(toDate)){
            splitMap.put("shouldAcross", "1");
          }
        }
        splitBean.setFromDate(splitDate);
        splitBean.setFromTime(splitFromTime);
        splitBean.setToDate(splitDate);
        splitBean.setToTime(splitToTime);
        splitBean.setBelongDate(splitDate);
        splitBean.setChangeType(changeType);
        splitBean.setPreChangeType(preChangeType);

        SplitBean overSplitBean = new SplitBean();
        //然后把bean重新赋值下，根据拆分后的时间
        BeanUtils.copyProperties(splitBean, overSplitBean);

        OvertimeChain holiday2HolidayChain = new OvertimeHoliday2HolidayChain(splitMap,splitBeans);
        OvertimeChain holiday2WorkdayChain = new OvertimeHoliday2WorkdayChain(splitMap,splitBeans);
        OvertimeChain holiday2RestdayChain = new OvertimeHoliday2RestdayChain(splitMap,splitBeans);

        OvertimeChain workday2HolidayChain = new OvertimeWorkday2HolidayChain(splitMap,splitBeans);
        OvertimeChain workday2WorkdayChain = new OvertimeWorkday2WorkdayChain(splitMap,splitBeans);
        OvertimeChain workday2RestdayChain = new OvertimeWorkday2RestdayChain(splitMap,splitBeans);

        OvertimeChain restday2HolidayChain = new OvertimeRestday2HolidayChain(splitMap,splitBeans);
        OvertimeChain restday2WorkdayChain = new OvertimeRestday2WorkdayChain(splitMap,splitBeans);
        OvertimeChain restday2RestdayChain = new OvertimeRestday2RestdayChain(splitMap,splitBeans);

        holiday2HolidayChain.setOvertimeChain(holiday2WorkdayChain);
        holiday2WorkdayChain.setOvertimeChain(holiday2RestdayChain);
        holiday2RestdayChain.setOvertimeChain(workday2HolidayChain);
        workday2HolidayChain.setOvertimeChain(workday2WorkdayChain);
        workday2WorkdayChain.setOvertimeChain(workday2RestdayChain);
        workday2RestdayChain.setOvertimeChain(restday2HolidayChain);
        restday2HolidayChain.setOvertimeChain(restday2WorkdayChain);
        restday2WorkdayChain.setOvertimeChain(restday2RestdayChain);

        holiday2HolidayChain.handleOvertimeChain(overSplitBean);

      }
    }
  }

}
