package com.engine.kq.cmd.attendanceEvent;

import com.engine.kq.biz.KQLeaveRulesComInfo;
import com.engine.kq.biz.KQUnitBiz;
import com.engine.kq.util.KQDurationCalculatorUtil;
import java.math.BigDecimal;
import weaver.conn.RecordSet;
import weaver.general.Util;

public class AttendanceUtil {

  /**
   * 获取冻结数据
   * @param newLeaveType
   * @param resourceId
   */
  public static double getFreezeDuration(String newLeaveType, String resourceId) {
    double d_duration = 0.0;
    //以防止出现精度问题
    BigDecimal big_d_duration = new BigDecimal(Double.toString(d_duration));
    RecordSet rs = new RecordSet();
    KQLeaveRulesComInfo kqLeaveRulesComInfo = new KQLeaveRulesComInfo();
    String minimumUnit = kqLeaveRulesComInfo.getMinimumUnit(newLeaveType);
    double proportion = Util.getDoubleValue(kqLeaveRulesComInfo.getProportion(newLeaveType),0.0);

    String getFreezeSql = "select a.* from KQ_ATT_VACATION a left join workflow_requestbase b on a.requestid=b.requestid where a.status=1 and b.requestid > 0 ";
    if(resourceId.length() > 0 && newLeaveType.length() > 0){
      getFreezeSql += " and a.resourceId="+resourceId+" and a.newLeaveType="+newLeaveType;
      rs.execute(getFreezeSql);
      while (rs.next()){
        String duration = rs.getString("duration");

        double value = Util.getDoubleValue(duration,0.0);
        String durationrule = rs.getString("durationrule");
        if(KQUnitBiz.isLeaveHour(minimumUnit)){//按小时
          if(!KQUnitBiz.isLeaveHour(minimumUnit)){
            if(proportion>0) value = value*proportion;
          }
        }else{//按天
          if(KQUnitBiz.isLeaveHour(durationrule)){
            if(proportion>0) value = value/proportion;
          }
        }
        BigDecimal big_value = new BigDecimal(KQDurationCalculatorUtil.getDurationRound5(value+""));
        big_d_duration = big_d_duration.add(big_value);
      }
    }
    return Util.getDoubleValue(KQDurationCalculatorUtil.getDurationRound(big_d_duration.doubleValue()+""));
  }
}
