|
|
@@ -0,0 +1,74 @@ |
|
|
|
package com.ningdatech.pmapi.sys.manage; |
|
|
|
|
|
|
|
import cn.hutool.core.util.NumberUtil; |
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
|
|
|
import com.ningdatech.basic.exception.BizException; |
|
|
|
import com.ningdatech.basic.util.CollUtils; |
|
|
|
import com.ningdatech.pmapi.projectlib.enumeration.InstTypeEnum; |
|
|
|
import com.ningdatech.pmapi.projectlib.model.entity.ProjectInst; |
|
|
|
import com.ningdatech.pmapi.projectlib.service.IProjectInstService; |
|
|
|
import com.ningdatech.pmapi.sys.model.vo.ProcessDetailStatVO; |
|
|
|
import lombok.AllArgsConstructor; |
|
|
|
import org.flowable.engine.HistoryService; |
|
|
|
import org.flowable.engine.history.HistoricProcessInstance; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
import java.util.HashSet; |
|
|
|
import java.util.List; |
|
|
|
import java.util.LongSummaryStatistics; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
/** |
|
|
|
* <p> |
|
|
|
* ProcessStatisticsManage |
|
|
|
* </p> |
|
|
|
* |
|
|
|
* @author WendyYang |
|
|
|
* @since 2023/7/31 |
|
|
|
**/ |
|
|
|
@Component |
|
|
|
@AllArgsConstructor |
|
|
|
public class ProcessStatisticsManage { |
|
|
|
|
|
|
|
private final HistoryService historyService; |
|
|
|
private final IProjectInstService projectInstService; |
|
|
|
|
|
|
|
public ProcessDetailStatVO processDetailStat(TimeUnit unit, InstTypeEnum instType) { |
|
|
|
if (!TimeUnit.DAYS.equals(unit) && !TimeUnit.HOURS.equals(unit)) { |
|
|
|
throw BizException.wrap("仅支持以天和小时为单位的统计"); |
|
|
|
} |
|
|
|
ProcessDetailStatVO detailStat = ProcessDetailStatVO.init(); |
|
|
|
LambdaQueryWrapper<ProjectInst> modelQuery = Wrappers.lambdaQuery(ProjectInst.class) |
|
|
|
.select(ProjectInst::getInstCode) |
|
|
|
.eq(ProjectInst::getInstType, instType.getCode()); |
|
|
|
List<ProjectInst> projectInsts = projectInstService.list(modelQuery); |
|
|
|
if (projectInsts.isEmpty()) { |
|
|
|
return detailStat; |
|
|
|
} |
|
|
|
List<String> instCodeList = CollUtils.fieldList(projectInsts, ProjectInst::getInstCode); |
|
|
|
List<HistoricProcessInstance> instances = historyService.createHistoricProcessInstanceQuery() |
|
|
|
.processInstanceIds(new HashSet<>(instCodeList)) |
|
|
|
.list(); |
|
|
|
detailStat.setTotalInst(instances.size()); |
|
|
|
LongSummaryStatistics statistics = instances.stream().filter(w -> { |
|
|
|
boolean finished = w.getEndTime() != null; |
|
|
|
if (finished) { |
|
|
|
detailStat.incrFinished(); |
|
|
|
} else { |
|
|
|
detailStat.incrPending(); |
|
|
|
} |
|
|
|
return finished; |
|
|
|
}).map(HistoricProcessInstance::getDurationInMillis) |
|
|
|
.collect(Collectors.summarizingLong(Long::longValue)); |
|
|
|
if (detailStat.getFinishedInst() != 0) { |
|
|
|
long unitMillis = unit.toMillis(1); |
|
|
|
detailStat.setAvgTime(NumberUtil.div(statistics.getAverage(), unitMillis, 1)); |
|
|
|
detailStat.setMaxTime(NumberUtil.div(statistics.getMax(), unitMillis, 1)); |
|
|
|
detailStat.setMinTime(NumberUtil.div(statistics.getMin(), unitMillis, 1)); |
|
|
|
} |
|
|
|
return detailStat; |
|
|
|
} |
|
|
|
|
|
|
|
} |