Commit cf1de4fb by 刘鑫

refactor(容量): 单小时时间片容量计算

增加时间片空闲时间片存储, 移除时间片订单数据统计
1 parent 91f4a25b
...@@ -101,23 +101,29 @@ public class CapacityUtils { ...@@ -101,23 +101,29 @@ public class CapacityUtils {
} }
public static Long getMaxRemainBlock(LocalDateTime startTime, LocalDateTime endTime, List<OccupyInfo> occupyInfos) { public static List<OccupyInfoDetail> getMaxRemainBlock(LocalDateTime startTime, LocalDateTime endTime, List<OccupyInfo> occupyInfos) {
if (org.springframework.util.CollectionUtils.isEmpty(occupyInfos)) { //存储空闲时间段
return Duration.between(startTime, endTime).toMinutes(); List<OccupyInfoDetail> leisureTime = new ArrayList<>();
if (CollectionUtils.isEmpty(occupyInfos)) {
long minutes = Duration.between(startTime, endTime).abs().toMinutes();
OccupyInfoDetail occupyInfo = new OccupyInfoDetail(startTime, endTime, minutes);
leisureTime.add(occupyInfo);
return leisureTime;
} }
//空闲时间 //空闲时间
List<Long> idlePeriods = new ArrayList<>();
LocalDateTime preLast = startTime; LocalDateTime preLast = startTime;
for (OccupyInfo o : occupyInfos) { for (OccupyInfo o : occupyInfos) {
if (o.getBeginTime().isAfter(preLast)) { if (o.getBeginTime().isAfter(preLast)) {
idlePeriods.add(Duration.between(startTime, o.getBeginTime()).abs().toMinutes()); long duraionMiu = Duration.between(startTime, o.getBeginTime()).abs().toMinutes();
leisureTime.add(new OccupyInfoDetail(startTime, o.getBeginTime(), duraionMiu));
} }
preLast = o.getEndTime(); preLast = o.getEndTime();
} }
if (preLast.isBefore(endTime)) { if (preLast.isBefore(endTime)) {
idlePeriods.add(Duration.between(preLast, endTime).abs().toMinutes()); long duraionMiu = Duration.between(preLast, endTime).abs().toMinutes();
leisureTime.add(new OccupyInfoDetail(preLast,endTime, duraionMiu));
} }
return org.springframework.util.CollectionUtils.isEmpty(idlePeriods) ? Duration.between(startTime, endTime).abs().toMinutes() : Collections.max(idlePeriods); return leisureTime;
} }
......
...@@ -12,6 +12,6 @@ import java.time.LocalDateTime; ...@@ -12,6 +12,6 @@ import java.time.LocalDateTime;
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
public class OccupyInfo { public class OccupyInfo {
private LocalDateTime beginTime; protected LocalDateTime beginTime;
private LocalDateTime endTime; protected LocalDateTime endTime;
} }
package com.dituhui.pea.order.common;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.time.LocalDateTime;
@EqualsAndHashCode(callSuper = true)
@Data
@Accessors(chain = true)
@NoArgsConstructor
public class OccupyInfoDetail extends OccupyInfo {
/**
* 时间片时长(分钟)
*/
private long duration;
public OccupyInfoDetail(LocalDateTime beginTime, LocalDateTime endTime, long duration) {
super(beginTime, endTime);
this.duration = duration;
}
}
package com.dituhui.pea.order.entity; package com.dituhui.pea.order.entity;
import com.dituhui.pea.order.common.OccupyInfoDetail;
import com.vladmihalcea.hibernate.type.json.JsonStringType;
import lombok.Getter; import lombok.Getter;
import org.hibernate.annotations.GenericGenerator; import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import javax.persistence.Basic; import javax.persistence.Basic;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
...@@ -9,17 +13,18 @@ import javax.persistence.Column; ...@@ -9,17 +13,18 @@ import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType; import javax.persistence.FetchType;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.OneToOne; import javax.persistence.OneToOne;
import javax.persistence.Table; import javax.persistence.Table;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects; import java.util.Objects;
@Getter @Getter
@Entity @Entity
@Table(name = "capacity_engineer_slice_used") @Table(name = "capacity_engineer_slice_used")
@TypeDef(name = "json", typeClass = JsonStringType.class)
public class CapacityEngineerSliceUsedEntity { public class CapacityEngineerSliceUsedEntity {
@Id @Id
@Column(name = "id") @Column(name = "id")
...@@ -51,11 +56,13 @@ public class CapacityEngineerSliceUsedEntity { ...@@ -51,11 +56,13 @@ public class CapacityEngineerSliceUsedEntity {
@Column(name = "cap_left") @Column(name = "cap_left")
private Long capLeft; private Long capLeft;
@Basic @Basic
@Column(name = "order_count")
private Long orderCount;
@Basic
@Column(name = "max_duration") @Column(name = "max_duration")
private Long maxDuration; private Long maxDuration;
@Type(type = "json")
@Column(name = "duration_time", columnDefinition = "json")
private List<OccupyInfoDetail> durationTime;
@Basic @Basic
@Column(name = "max_duration_type") @Column(name = "max_duration_type")
private String maxDurationType; private String maxDurationType;
...@@ -101,14 +108,15 @@ public class CapacityEngineerSliceUsedEntity { ...@@ -101,14 +108,15 @@ public class CapacityEngineerSliceUsedEntity {
this.capLeft = capLeft; this.capLeft = capLeft;
} }
public void setOrderCount(Long orderCount) {
this.orderCount = orderCount;
}
public void setMaxDuration(Long maxDuration) { public void setMaxDuration(Long maxDuration) {
this.maxDuration = maxDuration; this.maxDuration = maxDuration;
} }
public void setDurationTime(List<OccupyInfoDetail> durationTime) {
this.durationTime = durationTime;
}
public void setMaxDurationType(String maxDurationType) { public void setMaxDurationType(String maxDurationType) {
this.maxDurationType = maxDurationType; this.maxDurationType = maxDurationType;
} }
...@@ -130,11 +138,11 @@ public class CapacityEngineerSliceUsedEntity { ...@@ -130,11 +138,11 @@ public class CapacityEngineerSliceUsedEntity {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
CapacityEngineerSliceUsedEntity that = (CapacityEngineerSliceUsedEntity) o; CapacityEngineerSliceUsedEntity that = (CapacityEngineerSliceUsedEntity) o;
return id == that.id && timmeSlice == that.timmeSlice && Objects.equals(workday, that.workday) && Objects.equals(engineerCode, that.engineerCode) && Objects.equals(capTotal, that.capTotal) && Objects.equals(capUsed, that.capUsed) && Objects.equals(capUsedTravel, that.capUsedTravel) && Objects.equals(capLeft, that.capLeft) && Objects.equals(orderCount, that.orderCount) && Objects.equals(maxDuration, that.maxDuration) && Objects.equals(maxDurationType, that.maxDurationType) && Objects.equals(memo, that.memo) && Objects.equals(createTime, that.createTime) && Objects.equals(updateTime, that.updateTime); return Objects.equals(id, that.id) && Objects.equals(timmeSlice, that.timmeSlice) && Objects.equals(workday, that.workday) && Objects.equals(engineerCode, that.engineerCode) && Objects.equals(capTotal, that.capTotal) && Objects.equals(capUsed, that.capUsed) && Objects.equals(capUsedTravel, that.capUsedTravel) && Objects.equals(capLeft, that.capLeft) && Objects.equals(maxDuration, that.maxDuration) && Objects.equals(durationTime, that.durationTime) && Objects.equals(maxDurationType, that.maxDurationType) && Objects.equals(memo, that.memo) && Objects.equals(createTime, that.createTime) && Objects.equals(updateTime, that.updateTime);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(id, timmeSlice, workday, engineerCode, capTotal, capUsed, capUsedTravel, capLeft, orderCount, maxDuration, maxDurationType, memo, createTime, updateTime); return Objects.hash(id, timmeSlice, workday, engineerCode, capTotal, capUsed, capUsedTravel, capLeft, maxDuration, durationTime, maxDurationType, memo, createTime, updateTime);
} }
} }
...@@ -3,6 +3,7 @@ package com.dituhui.pea.order.scheduler; ...@@ -3,6 +3,7 @@ package com.dituhui.pea.order.scheduler;
import com.dituhui.pea.order.common.CapacityUtils; import com.dituhui.pea.order.common.CapacityUtils;
import com.dituhui.pea.order.common.DateUtils; import com.dituhui.pea.order.common.DateUtils;
import com.dituhui.pea.order.common.OccupyInfo; import com.dituhui.pea.order.common.OccupyInfo;
import com.dituhui.pea.order.common.OccupyInfoDetail;
import com.dituhui.pea.order.common.jackson.DateTimeUtil; import com.dituhui.pea.order.common.jackson.DateTimeUtil;
import com.dituhui.pea.order.common.jackson.DateUtil; import com.dituhui.pea.order.common.jackson.DateUtil;
import com.dituhui.pea.order.dao.CapacityEngineerCalendarDao; import com.dituhui.pea.order.dao.CapacityEngineerCalendarDao;
...@@ -140,11 +141,14 @@ public class CalcEngineerCapacityScheduler { ...@@ -140,11 +141,14 @@ public class CalcEngineerCapacityScheduler {
//已用容量 //已用容量
long totalUseTime = occupyInfo.stream().mapToLong(t -> Duration.between(t.getEndTime(), t.getBeginTime()).abs().toMinutes()).sum(); long totalUseTime = occupyInfo.stream().mapToLong(t -> Duration.between(t.getEndTime(), t.getBeginTime()).abs().toMinutes()).sum();
//最大连续时长 //最大连续时长
Long maxRemainBlock = CapacityUtils.getMaxRemainBlock(startTime, endTime, occupyInfo); List<OccupyInfoDetail> durationTime = CapacityUtils.getMaxRemainBlock(startTime, endTime, occupyInfo);
long maxRemainBlock = durationTime.stream().mapToLong(OccupyInfoDetail::getDuration).max().orElse(Duration.between(startTime, endTime).abs().toMinutes());
//剩余连续时间段
sliceCap.setDurationTime(durationTime);
sliceCap.setCapLeft(sliceCap.getCapTotal() - totalUseTime); sliceCap.setCapLeft(sliceCap.getCapTotal() - totalUseTime);
sliceCap.setCapUsed(totalUseTime); sliceCap.setCapUsed(totalUseTime);
sliceCap.setOrderCount((long) orders.size()); //计算时 存储时间差级(剩余时间段)
sliceCap.setMaxDuration(maxRemainBlock); sliceCap.setMaxDuration(maxRemainBlock);
sliceCap.setUpdateTime(LocalDateTime.now(ZoneId.of("+8"))); sliceCap.setUpdateTime(LocalDateTime.now(ZoneId.of("+8")));
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!