李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.chinaztt.mes.aps.core.domain;
 
import com.chinaztt.mes.aps.core.helper.Constants;
import com.chinaztt.mes.aps.core.solver.StartTimeUpdatingVariableListener;
import com.chinaztt.mes.aps.core.solver.TaskDifficultyComparator;
import com.chinaztt.mes.aps.entity.ResourceDuration;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.optaplanner.core.api.domain.entity.PlanningEntity;
import org.optaplanner.core.api.domain.variable.*;
 
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Optional;
import java.util.Set;
 
/**
 * @Author: zhangxy
 * @Date: 2020-10-19 19:42
 */
@PlanningEntity(difficultyComparatorClass = TaskDifficultyComparator.class)
@Data
public class Task extends AbstractTaskOrResource {
 
    /**
     * id 可能为空
     */
    private Long id;
 
    private Long orderId;
 
    private Long routingId;
 
    private Long routingOperationId;
 
    private int orderPriority;
 
    private int operationPriority;
 
    private String opNo;
 
    private String name;
 
    private BigDecimal quantity;
 
    private Long partId;
 
    private Set<Long> capabilityIds;
 
    @JsonIgnore
    private Task successorOfRouting;
 
    @JsonIgnore
    private Task predecessorOfRouting;
 
 
    @CustomShadowVariable(variableListenerClass = StartTimeUpdatingVariableListener.class,
            sources = {@PlanningVariableReference(variableName = "previousTaskOrResource"),})
    private LocalDateTime planStartTime;
 
    @JsonIgnore
    @PlanningVariable(valueRangeProviderRefs = {"resourceRange", "taskRange"},
            graphType = PlanningVariableGraphType.CHAINED)
    private AbstractTaskOrResource previousTaskOrResource;
 
 
    @AnchorShadowVariable(sourceVariableName = "previousTaskOrResource")
    private ResourceBo resourceBo;
 
 
 
    @Override
    public LocalDateTime getPlanEndTime() {
        if (this.planStartTime == null || this.getResourceBo() == null || this.getResourceBo().getDurations() == null) {
            return LocalDateTime.now();
        }
        Optional<ResourceDuration> opt = this.getResourceBo().getDurations().stream().filter(d -> d.getPartId().equals(this.getPartId())).findFirst();
        if (opt.isPresent()) {
            return this.planStartTime.plusMinutes(calculateCostTime(quantity, opt.get()));
        }
        return this.planStartTime;
    }
 
 
    /**
     * 计算task耗用时间,单位分钟
     * @param quantity
     * @param rd
     * @return
     */
    private long calculateCostTime(BigDecimal quantity, ResourceDuration rd) {
        long cost = 0L;
        if (Constants.UNIT_PER_MIN.equals(rd.getProductionRateUnit())) {
            //单位数/分钟
            cost += rd.getSetupTime().longValue();
            cost += quantity.divide(rd.getProductionRate(), 0, BigDecimal.ROUND_UP).longValue();
            cost += rd.getPostProcessing().longValue();
        } else if (Constants.MIN_PER_UNIT.equals(rd.getProductionRateUnit())) {
            //分钟数/单位
            cost += rd.getSetupTime().longValue();
            cost += quantity.multiply(rd.getProductionRate()).intValue();
            cost += rd.getPostProcessing().longValue();
        }
        return cost;
    }
 
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
 
        Task task = (Task) o;
 
        if (orderPriority != task.orderPriority) {
            return false;
        }
        if (operationPriority != task.operationPriority) {
            return false;
        }
        if (!orderId.equals(task.orderId)) {
            return false;
        }
        if (!routingOperationId.equals(task.routingOperationId)) {
            return false;
        }
        if (!quantity.equals(task.quantity)) {
            return false;
        }
        return partId.equals(task.partId);
    }
 
    @Override
    public int hashCode() {
        int result = orderId.hashCode();
        result = 31 * result + routingOperationId.hashCode();
        result = 31 * result + orderPriority;
        result = 31 * result + operationPriority;
        result = 31 * result + quantity.hashCode();
        result = 31 * result + partId.hashCode();
        return result;
    }
 
    @Override
    public String toString() {
        return "Task{" +
                "operationPriority=" + operationPriority +
                ", name='" + name + '\'' +
                ", quantity=" + quantity +
                ", capabilityIds=" + capabilityIds +
                ", planStartTime=" + planStartTime +
                '}';
    }
}