计算特定时间段内的时长

lvv
• 发表于:2023年04月07日 17:44 • 更新于:2023年04月21日 17:37
491

场景描述:

    为了体现工作效率,员工在处理工单任务的时候一般都需要计算处理耗时,但是实际需求中,员工可能会跨天处理完成一项工单任务,因此只用完成工单的日期时间减去开始处理工单的日期时间来计算耗时是不完全准确的。

    为了更准确体现出员工的工作效率,我们可以规定好在一个时间段内计算耗时,比如早上9点到下午6点才累计处理工单耗时,以下将演示如何实现该场景需求。

流程描述:

(1)  建单人创建任务工单。

(2)  领单人选择一个工单进行接收,此时即为开始任务,记录开始时间。

(3)  领单人完成工单内容后,选择工单进行完成的操作,此时记录完成时间,即可记录该工单在工作时段中的耗时。

前期准备:

创建工单列表,处理总耗时选为数字,开始受理日期时间以及解决日期及时间选为字符即可,处理人字段关联系统开始的用户表即可。

https://pan.bnocode.com/project/642283bde81aad5e5587ce87/attachment/20230407/1680837631643_26.png

实现步骤:

(1)创建工单任务功能

注意:问题描述设为长文本,工单状态为待接单。

https://pan.bnocode.com/project/642283bde81aad5e5587ce87/attachment/20230407/1680837682151_27.pnghttps://pan.bnocode.com/project/642283bde81aad5e5587ce87/attachment/20230407/1680837695211_28.png

https://pan.bnocode.com/project/642283bde81aad5e5587ce87/attachment/20230407/1680837705046_29.png

https://pan.bnocode.com/project/642283bde81aad5e5587ce87/attachment/20230407/1680837718624_30.png

(2)创建接收工单任务功能

该功能为领单人选择待接单的工单进行领单,此时将记录工单的开始处理时间。

https://pan.bnocode.com/project/642283bde81aad5e5587ce87/attachment/20230407/1680837763369_31.png

https://pan.bnocode.com/project/642283bde81aad5e5587ce87/attachment/20230407/1680837774615_32.png

注意:若工单状态不为待接单,则提示被领走,并不可提交

https://pan.bnocode.com/project/642283bde81aad5e5587ce87/attachment/20230407/1680837801888_33.png

https://pan.bnocode.com/project/642283bde81aad5e5587ce87/attachment/20230407/1680837822506_34.png

注意:开始受理日期时间格式为当前时间,YYYY-MM-DD HH:mm:ss

https://pan.bnocode.com/project/642283bde81aad5e5587ce87/attachment/20230407/1680837842870_35.png

https://pan.bnocode.com/project/642283bde81aad5e5587ce87/attachment/20230407/1680837854342_36.png

https://pan.bnocode.com/project/642283bde81aad5e5587ce87/attachment/20230407/1680837868513_37.png

(3)创建计算工单耗时的API

注意:开始时间和结束时间的格式都是YYYY-MM-DD HH:mm:ss

https://pan.bnocode.com/project/642283bde81aad5e5587ce87/attachment/20230407/1680837988422_38.png

代码如下:         复制代码

async function run($input, $output, $modules = modules) {
    /**
     * 计算两个时间之间相差的分钟数,且只计算每天的09:00~18:00
     */
    const moment = $modules.moment;

    let start = moment($input.start).valueOf();
    let end = moment($input.end).valueOf();

    let res = diffTime(start, end, ["09:00", "18:00"]);
    $output.times = res.times;
    function diffTime(start, end, dateTimeFrame) {
        let startObj = moment(start);
        let endObj = moment(end);

        let times = 0;

        if (start >= end) {
            return times;
        }

        let startDate = startObj.format("YYYY-MM-DD");
        let endDate = endObj.format("YYYY-MM-DD");

        let start_dateTimeStart = moment(startDate + " " + dateTimeFrame[0]);
        let start_dateTimeEnd = moment(startDate + " " + dateTimeFrame[1]);
        let end_dateTimeStart = moment(endDate + " " + dateTimeFrame[0]);
        let end_dateTimeEnd = moment(endDate + " " + dateTimeFrame[1]);

        //相差天数
        // let days = moment.duration(endObj.diff(startObj)).days();
        let days = moment(endDate).diff(moment(startDate), 'days');


        //超过一天的部分
        let fullDays = Math.max(0, days - 1);
        let fullDaysTimes = fullDays * (start_dateTimeEnd - start_dateTimeStart) / 1000 / 60;

        times += fullDaysTimes;

        let firstDayTimes = 0;//第一天分钟数
        let lastDayTimes = 0;//最后一天分钟数
        //开始到结束时间,忽略中间天数
        //同一天
        if (days == 0) {
            times +=
                ((start_dateTimeEnd.valueOf() < endobj.valueof() ? start_datetimeend.valueof() : endobj.valueof())
                    -
                    (start_dateTimeStart.valueOf() < startobj.valueof() ? startobj.valueof() : start_datetimestart.valueof())
                ) / 1000 / 60;

        } else {
            //超过一天
            if (startObj.valueOf() > start_dateTimeEnd.valueOf()) {
                firstDayTimes = 0;
            } else {
                firstDayTimes = (start_dateTimeEnd.valueOf() - (startObj.valueOf() < start_datetimestart.valueof() ? start_datetimestart.valueof() : startobj.valueof())) >            }

            if (endObj.valueOf() < end_datetimestart.valueof()) {
                lastDayTimes = 0;
            } else {
                lastDayTimes = ((endObj.valueOf() < end_datetimeend.valueof() ? endobj.valueof() : end_datetimeend.valueof()) - end_datetimestart) >            }

            times += firstDayTimes
            times += lastDayTimes
            // times = times / 60;
        }
        return {
            days,//跨越天数
            fullDaysTimes,
            firstDayTimes,
            lastDayTimes,
            times:times/60,//小时数
        };
    }
}


(4)创建完成工单任务的功能

选择自己接收的工单,该功能提交后将记录工单总耗时

https://pan.bnocode.com/project/642283bde81aad5e5587ce87/attachment/20230407/1680838113520_39.png

https://pan.bnocode.com/project/642283bde81aad5e5587ce87/attachment/20230407/1680838125598_40.png

注意:获取该单的受理人,与当前用户进行对比,判断是否为该单的处理人,若不是则不能完成

https://pan.bnocode.com/project/642283bde81aad5e5587ce87/attachment/20230407/1680838143222_41.png

https://pan.bnocode.com/project/642283bde81aad5e5587ce87/attachment/20230407/1680838155371_42.png

https://pan.bnocode.com/project/642283bde81aad5e5587ce87/attachment/20230407/1680838167242_43.png

https://pan.bnocode.com/project/642283bde81aad5e5587ce87/attachment/20230407/1680838178629_44.png

https://pan.bnocode.com/project/642283bde81aad5e5587ce87/attachment/20230407/1680838191185_45.png

https://pan.bnocode.com/project/642283bde81aad5e5587ce87/attachment/20230407/1680838202982_46.png

https://pan.bnocode.com/project/642283bde81aad5e5587ce87/attachment/20230407/1680838214134_47.png

https://pan.bnocode.com/project/642283bde81aad5e5587ce87/attachment/20230407/1680838224881_48.png

https://pan.bnocode.com/project/642283bde81aad5e5587ce87/attachment/20230407/1680838238726_49.png

场景演示:

https://pan.bnocode.com/project/62dfc125b702df15e7988dba/attachment/20230421/1682069842021_17.gif


本文是否对您有帮助?
有帮助
没帮助
您是否遇到了以下问题?
内容过期或不准确
缺少场景、事例
链接有误
太简单,步骤待完善
其他
提交反馈
如需获取即时帮助,请联系
小助理
微信扫码添加小助理
让你的想法快速变成软件吧~
计算特定时间段内的时长
联系我们
售前咨询电话
020-88520693
意见箱 · 建议反馈
您的宝贵建议,使白码更完美!
微信扫码添加白码小助理
返回顶部