业务中根据节假日双休日计算工期的实现

78 阅读1分钟

需求背景

在项目管理中,任务的时间安排和工期计算是非常重要的,本次需求具体要求如下:

  1. 用户可以通过表格界面录入分组任务和子任务的信息,包括任务名称、开始时间、结束时间等。
  2. 用户可以设置是否去掉节假日,以及是否为单休或双休。如果选择单休,用户可以进一步选择是周六休息还是周日休息。
  3. 根据用户录入的任务信息和设置的假期、工作日类型,系统将自动计算分组任务的开始时间和结束时间、工期等。

实现效果

1685672373337.png

具体实现

1、动态获取节假日数组,并缓存。

store.commit("holidays/SET_holidays",["20091001","20091002","20091003",...]);

2、动态获取因节假日而调休的周六周日

 store.commit("holidays/SET_workdays",["20180211","20180224",...]);

3、编写根据开始时间和结束时间计算工期方法

 const handleUseDay=(startDate, endDate)=>{
      if(!startDate||!endDate){
        return 0
      }
      const start = new Date(startDate);
      const end = new Date(endDate);
      let count = 0;
      start.setDate(start.getDate() - 1);
      // 循环遍历日期范围
      while (start < end) {
        // 递增日期
        start.setDate(start.getDate() + 1);
        const formattedDate = moment(start).format("YYYYMMDD");
       
        // 如果开启去掉节假日,并且当前时间在节假日内
        if(props.formRef.holidays&&state.holidays.includes(formattedDate)){
          continue
        }
        // 如果开启双休,并且当前不调休
        if(props.formRef.weekend&&(start.getDay() == 0 || start.getDay() == 6)&&!state.workdays.includes(formattedDate)){
          continue
        }
        // 开启单休
        if(props.formRef.singleRest&&!state.workdays.includes(formattedDate)){
          //周六单休
          if(props.formRef.offDay=='saturday'&&start.getDay() == 6){
            continue
          }
          //周日单休
          if(props.formRef.offDay=='sunday'&&start.getDay() == 0){
            continue
          }
        }
       
        count++;
      }
    
      return count;
    }

4、绑定到表格的日期change事件上

5、添加切换'去掉法定节假日'、'双休日'等功能

// 更改节假日双休日时,重新计算工期
    const calcDays=()=>{
      state.records.map(item=>{
        item['useDay']= handleUseDay(item['startDate'],item['endDate'])
        if(item['children']&&item['children'].length>0){
          item['children'].map(cItem=>{
            cItem['useDay']= handleUseDay(cItem['startDate'],cItem['endDate'])
          })
        }
      })
    }

扫码_搜索联合传播样式-标准色版.png