package com.engine.kq.biz;

import com.engine.kq.biz.chain.cominfo.ShiftInfoCominfoBean;
import com.engine.kq.enums.DurationTypeEnum;
import com.engine.kq.util.KQDurationCalculatorUtil;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import weaver.common.StringUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.resource.ResourceComInfo;
import weaver.hrm.schedule.domain.HrmSchedule;
import weaver.hrm.schedule.manager.HrmScheduleManager;
import weaver.systeminfo.SystemEnv;

/**
 * 新版考勤为原考勤类HrmScheduleManager的通用方法做单独的处理
 */
public class KQ4ScheduleManager extends BaseBean {

  private int scale = 2;

  private User user;

  private static final String B_TIME = "00:00";

  private static final String E_TIME = "23:59";

  private KQWorkTime kqWorkTime;

  public KQ4ScheduleManager(User user) {
    this.user = user;
    this.kqWorkTime = new KQWorkTime();
  }


  /**
   * HrmScheduleManager里的getTotalWorkData方法
   * @param fromDate
   * @param fromTime
   * @param toDate
   * @param toTime
   * @param $user
   * @return
   */
  public String[] getTotalWorkData(String fromDate, String fromTime, String toDate, String toTime, User $user) {

    String[] result = {"", ""};
    if($user == null){
      $user = user;
    }
    if(fromTime.length() == 0){
      fromTime = B_TIME;
    }
    if(toTime.length() == 0){
      toTime = E_TIME;
    }
    String ressourceid = $user.getUID()+"";
    KQDurationCalculatorUtil kqDurationCalculatorUtil =new KQDurationCalculatorUtil.DurationParamBuilder(ressourceid).
        fromDateParam(fromDate).toDateParam(toDate).fromTimeParam(fromTime).toTimeParam(toTime).durationRuleParam("1")
        .computingModeParam("1").durationTypeEnumParam(DurationTypeEnum.COMMON_CAL).build();

    Map<String,Object> durationMap = kqDurationCalculatorUtil.getWorkDuration();
    String duration4day = Util.null2s(Util.null2String(durationMap.get("duration")),"0");
    String duration4min = Util.null2s(Util.null2String(durationMap.get("min_duration")),"0");
    double duration4hour = Util.getDoubleValue(duration4min)/60.0;

    result[0] = String.valueOf(StringUtil.round(Util.getDoubleValue(duration4day), 5));
    result[1] = String.valueOf(StringUtil.round(duration4hour, 5));
    return result;
  }

  /**
   * HrmScheduleManager里的getTotalRestHours方法
   * @param fromDate
   * @param fromTime
   * @param toDate
   * @param toTime
   * @param $user
   * @return
   */
  public String getTotalRestHours(String fromDate, String fromTime, String toDate, String toTime, User $user) {
    if($user == null){
      $user = user;
    }
    String ressourceid = $user.getUID()+"";
    KQDurationCalculatorUtil kqDurationCalculatorUtil =new KQDurationCalculatorUtil.DurationParamBuilder(ressourceid).
        fromDateParam(fromDate).toDateParam(toDate).fromTimeParam(fromTime).toTimeParam(toTime).computingModeParam("1").
        durationRuleParam("3").durationTypeEnumParam(DurationTypeEnum.OVERTIME).build();
    Map<String,Object> durationMap = kqDurationCalculatorUtil.getNonWorkDuration();
    String duration = Util.null2String(durationMap.get("duration"));

    return Util.null2String(Util.getDoubleValue(duration,0.0));

  }

  /**
   * 判断是否为工作日
   * @param currentDate
   * @return
   */

  public boolean isWorkday(String currentDate){
   return isWorkday(currentDate, user);
  }

  /**
   * 判断是否为工作日
   * @param currentDate
   * @param user
   * @return
   */
  public boolean isWorkday(String currentDate, User user){
    boolean isWorkday = false;
    if(user!=null){
      isWorkday = kqWorkTime.isWorkDay(""+user.getUID(),currentDate);
    }
    return isWorkday;
  }

  public Map<String, String> getOnDutyAndOffDutyTimeMap(String currentDate) {
    Map<String, String> map = new HashMap<>();
    if(user!=null){
      ShiftInfoCominfoBean shiftInfoCominfoBean = kqWorkTime.getShiftInfoCominfoBean(""+user.getUID(), currentDate);
      if(shiftInfoCominfoBean != null){
        //只有一次签到签退的 且设置了休息时间的才能对应上这个方法
        List<String> allWorkTime = shiftInfoCominfoBean.getAllWorkTime();
        List<String> restTimes = shiftInfoCominfoBean.getAllRestTime();
        if(allWorkTime != null && !allWorkTime.isEmpty()){
          //一天一次
          if(allWorkTime.size() == 2){
            if(restTimes != null && !restTimes.isEmpty() && restTimes.size() == 2){
              map.put("onDutyTimeAM", allWorkTime.get(0));
              map.put("offDutyTimeAM", restTimes.get(0));
              map.put("onDutyTimePM", restTimes.get(1));
              map.put("offDutyTimePM",allWorkTime.get(1));
              map.put("signType", "1");
              map.put("signStartTime", "");
            }
          }
          //一天两次
          if(allWorkTime.size() == 4){
            map.put("onDutyTimeAM", allWorkTime.get(0));
            map.put("offDutyTimeAM", allWorkTime.get(1));
            map.put("onDutyTimePM", allWorkTime.get(2));
            map.put("offDutyTimePM",allWorkTime.get(3));
            map.put("signType", "1");
            map.put("signStartTime", "");
          }
        }

      }
    }
    return map;
  }

    public int getScale() {
    return scale;
  }

  public void setScale(int scale) {
    this.scale = scale;
  }

  public User getUser() {
    return user;
  }

  public void setUser(User user) {
    this.user = user;
  }

}