-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspdag.go
422 lines (357 loc) · 10.5 KB
/
spdag.go
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package spdag
import (
"errors"
"fmt"
"sync"
"time"
)
// SubbranchPlan 顶点数据结构。
// 依照安心筑业务扩展顶点数据结构,尽量靠近子分部计划。
type SubbranchPlan struct {
// SubbranchPlanId 顶点id,唯一标识。在整个有向无环图中必须唯一,通常传入的是数据主键id。
SubbranchPlanId int `pg:"subbranch_plan_id,pk" json:"subbranch_plan_id"`
// PreSeq 入度,指向该顶点的顶点集合,可以理解为传入数据的父节点id数组。
PreSeq []int `pg:",array" json:"pre_seq"`
// 出度。
SubSeq []int `pg:",array" json:"sub_seq"`
// 与入度的最小边长。
InDegreeMinSideLen time.Duration
// 与入度的最大时间量。
InDegreeMaxTime time.Time
ParentVertexs []*SubbranchPlan
ChildrenVertexs []*SubbranchPlan
/* 以下是子分部计划数据结构SubbranchPlan。*/
// 子分部计划名称
SubbranchPlanName string `json:"subbranch_plan_name"`
// 所属子分部ID
SubbranchEngId int `json:"subbranch_eng_id"`
// 所属的单元工程ID
UnitEngId int `json:"unit_eng_id"`
// 子分部计划的状态,未开工 =0,施工中=1,已完成=2
SubbranchPlanStatus int `pg:",use_zero",json:"subbranch_plan_status"`
// 计划预置开始时间
PlanStartDate time.Time `json:"plan_start_date"`
// 计划预置结束时间
PlanEndDate time.Time `json:"plan_end_date"`
// 计划实际开始时间
RealStartDate time.Time `json:"real_start_date"`
// 计划实际结束时间
RealEndDate time.Time `json:"real_end_date"`
// 子分部计划的描述
SubbranchPlanDesc string `json:"subbranch_plan_desc"`
// 分部计划的创建时间
CreatedAt time.Time `json:"created_at"`
// 分部计划的更新时间
UpdatedAt time.Time `json:"updated_at"`
// CompleteTaskCount 已完成班组任务单数量。
CompleteTaskCount int
}
// 有向无环图数据结构。
type spdag struct {
// 顶点的数量。
vertexCount int
// 有向边的数量。
edgeCount int
// 所有顶点。
vertexsMap map[int]*SubbranchPlan
rwmutex *sync.RWMutex
SubbranchEngId int `pg:"subbranch_eng_id,pk" json:"subbranch_eng_id"`
// 结束时间
EndDate time.Time `json:"end_date"`
}
// 描绘父节点。
func (d *spdag) drawParent(vertex, parentVertex *SubbranchPlan) {
has := false
var tmp time.Time
if len(vertex.ParentVertexs) == 0 {
tmp = parentVertex.PlanEndDate
}
for _, v := range vertex.ParentVertexs {
if v.SubbranchPlanId == parentVertex.SubbranchPlanId {
has = true
}
if tmp.IsZero() {
tmp = v.PlanEndDate
continue
}
if tmp.Before(v.PlanEndDate) {
tmp = v.PlanEndDate
}
}
// 存储离自己最近的时间。
vertex.InDegreeMaxTime = tmp
// 计算边长。
sub := vertex.PlanStartDate.Sub(tmp)
vertex.InDegreeMinSideLen = sub
if !has {
vertex.ParentVertexs = append(vertex.ParentVertexs, parentVertex)
}
hasChildrenID := false
for _, v := range parentVertex.SubSeq {
if v == vertex.SubbranchPlanId {
hasChildrenID = true
}
}
if !hasChildrenID {
parentVertex.SubSeq = append(parentVertex.SubSeq, vertex.SubbranchPlanId)
}
}
// 描绘子节点。
func (d *spdag) drawChildren(vertex, childrenVertex *SubbranchPlan) {
has := false
for _, v := range childrenVertex.ChildrenVertexs {
if v.SubbranchPlanId == childrenVertex.SubbranchPlanId {
has = true
}
}
if !has {
vertex.ChildrenVertexs = append(vertex.ChildrenVertexs, childrenVertex)
}
hasChildrenID := false
for _, v := range vertex.SubSeq {
if v == childrenVertex.SubbranchPlanId {
hasChildrenID = true
}
}
if !hasChildrenID {
vertex.SubSeq = append(vertex.SubSeq, childrenVertex.SubbranchPlanId)
}
}
func (d *spdag) updateTime(vertex *SubbranchPlan, sub time.Duration) {
for _, v := range vertex.ChildrenVertexs {
v.PlanStartDate = v.PlanStartDate.Add(sub)
d.updateTime(v, sub)
}
}
func (d *spdag) allChildrenPlanDate(vertex *SubbranchPlan) {
for _, v := range vertex.ChildrenVertexs {
if vertex.PlanEndDate.After(v.InDegreeMaxTime) && !v.InDegreeMaxTime.IsZero() {
tmp := v.PlanEndDate.Sub(v.PlanStartDate)
v.PlanStartDate = vertex.PlanEndDate.Add(v.InDegreeMinSideLen)
v.PlanEndDate = v.PlanStartDate.Add(tmp).Add(v.InDegreeMinSideLen)
v.InDegreeMaxTime = vertex.PlanEndDate
d.allChildrenPlanDate(v)
}
}
}
func (d *spdag) removeIDSlice(ids []int, index int) []int {
return append(ids[:index], ids[index+1:]...)
}
// AddRealEndDate 添加实际结束时间。
func (d *spdag) AddRealEndDate(vertexID int, realEndDate time.Time) {
vertex := d.vertexsMap[vertexID]
for _, v := range vertex.ChildrenVertexs {
if realEndDate.After(v.InDegreeMaxTime) && v.InDegreeMaxTime.IsZero() {
v.PlanStartDate = v.PlanStartDate.Add(v.InDegreeMinSideLen)
v.PlanEndDate = v.PlanEndDate.Add(v.InDegreeMinSideLen)
v.InDegreeMaxTime = v.PlanEndDate
d.allChildrenPlanDate(v)
}
}
}
// 画边。
func (d *spdag) drawSide(vertex *SubbranchPlan) {
for _, ind := range vertex.PreSeq {
v, ok := d.vertexsMap[ind]
if ok {
d.drawParent(vertex, v)
}
}
for _, ind := range vertex.SubSeq {
if v, ok := d.vertexsMap[ind]; ok {
vertex.ChildrenVertexs = append(vertex.ChildrenVertexs, v)
}
}
}
// 删除vertex切片中的某个数据。
func (d *spdag) removeVertexSlice(vertexs []*SubbranchPlan, index int) []*SubbranchPlan {
return append(vertexs[:index], vertexs[index+1:]...)
}
// 修改。
func (d *spdag) Update(vertex, oldVertex *SubbranchPlan) (*spdag, error) {
has := false
var hasInd int
for _, ind := range vertex.PreSeq {
for _, outid := range oldVertex.SubSeq {
if ind == outid {
hasInd = ind
has = true
}
}
}
if has {
hasV := d.vertexsMap[hasInd]
return nil, fmt.Errorf("绘图失败,此操作会导致计划图产生回环。前置计划(%s)已经是(%s)后置计划。", hasV.SubbranchPlanName, vertex.SubbranchPlanName)
}
// 判断新传的的预计时间是否会影响到子节点。
for _, ov := range oldVertex.ChildrenVertexs {
if vertex.PlanEndDate.After(ov.InDegreeMaxTime) && !ov.InDegreeMaxTime.IsZero() {
tmp := ov.PlanEndDate.Sub(ov.PlanStartDate)
ov.PlanStartDate = vertex.PlanEndDate.Add(ov.InDegreeMinSideLen)
ov.PlanEndDate = ov.PlanStartDate.Add(tmp)
ov.InDegreeMaxTime = vertex.PlanEndDate
d.allChildrenPlanDate(ov)
}
}
for _, pv := range oldVertex.ParentVertexs {
// 删除父节点对应的子节点自己。
for i, cv := range pv.ChildrenVertexs {
if cv.SubbranchPlanId == oldVertex.SubbranchPlanId {
if len(pv.ChildrenVertexs) == 1 {
pv.ChildrenVertexs = make([]*SubbranchPlan, 0)
} else {
pv.ChildrenVertexs = d.removeVertexSlice(pv.ChildrenVertexs, i)
}
break
}
}
// 删除父节点对应的子节点自己id。
for i, cid := range pv.SubSeq {
if oldVertex.SubbranchPlanId == cid {
if len(pv.SubSeq) == 1 {
pv.SubSeq = make([]int, 0)
} else {
pv.SubSeq = d.removeIDSlice(pv.SubSeq, i)
fmt.Println(pv.SubSeq)
}
break
}
}
}
// 重新赋值。
oldVertex.PreSeq = vertex.PreSeq
oldVertex.ParentVertexs = make([]*SubbranchPlan, 0)
oldVertex.PlanStartDate = vertex.PlanStartDate
oldVertex.PlanEndDate = vertex.PlanEndDate
oldVertex.RealStartDate = vertex.RealStartDate
oldVertex.RealEndDate = vertex.RealEndDate
oldVertex.SubbranchPlanDesc = vertex.SubbranchPlanDesc
oldVertex.SubbranchPlanName = vertex.SubbranchPlanName
oldVertex.SubbranchPlanStatus = vertex.SubbranchPlanStatus
oldVertex.UnitEngId = vertex.UnitEngId
// 重新绘图。
d.drawSide(oldVertex)
for _, ps := range oldVertex.PreSeq {
pv := d.vertexsMap[ps]
d.drawChildren(pv, oldVertex)
}
return d, nil
}
// 画图。
func (d *spdag) draw(vertex *SubbranchPlan) {
d.vertexsMap[vertex.SubbranchPlanId] = vertex
d.drawSide(vertex)
}
// 构建有向无环图。
func (d *spdag) Build(vertex *SubbranchPlan) {
d.draw(vertex)
}
func (d *spdag) Remove(vertexID int) (map[int]*SubbranchPlan, error) {
oldVertex, ok := d.vertexsMap[vertexID]
if !ok {
return nil, errors.New("未找到计划。")
}
for _, ind := range oldVertex.PreSeq {
if v, ok := d.vertexsMap[ind]; ok {
// 先删除入度顶点对应的出度数组。
for i, outID := range v.SubSeq {
if outID == oldVertex.SubbranchPlanId {
if len(v.SubSeq) == 1 {
v.SubSeq = make([]int, 0)
} else {
v.SubSeq = d.removeIDSlice(v.SubSeq, i)
}
break
}
}
// 再删除子节点vertex数组。
for i, cv := range v.ChildrenVertexs {
if cv.SubbranchPlanId == oldVertex.SubbranchPlanId {
if len(v.ChildrenVertexs) == 1 {
v.ChildrenVertexs = make([]*SubbranchPlan, 0)
} else {
v.ChildrenVertexs = d.removeVertexSlice(v.ChildrenVertexs, i)
}
break
}
}
}
}
for _, outid := range oldVertex.SubSeq {
if v, ok := d.vertexsMap[outid]; ok {
// 先删除出度顶点对应的出度数组。
for i, outID := range v.SubSeq {
if outID == oldVertex.SubbranchPlanId {
if len(v.SubSeq) == 1 {
v.SubSeq = make([]int, 0)
} else {
v.SubSeq = d.removeIDSlice(v.SubSeq, i)
}
break
}
}
// 再删除子节点vertex数组。
for i, pv := range v.ParentVertexs {
if pv.SubbranchPlanId == oldVertex.SubbranchPlanId {
if len(pv.ParentVertexs) == 1 {
pv.ParentVertexs = make([]*SubbranchPlan, 0)
} else {
pv.ParentVertexs = d.removeVertexSlice(pv.ParentVertexs, i)
}
break
}
}
}
}
delete(d.vertexsMap, oldVertex.SubbranchPlanId)
return d.vertexsMap, nil
}
func (d *spdag) Map() map[int]*SubbranchPlan {
return d.vertexsMap
}
func (d *spdag) List() []*SubbranchPlan {
if d == nil {
return make([]*SubbranchPlan, 0)
}
vertexs := make([]*SubbranchPlan, 0)
for _, v := range d.vertexsMap {
vertexs = append(vertexs, v)
}
return vertexs
}
func (d *spdag) Get(vertexID int) *SubbranchPlan {
return d.vertexsMap[vertexID]
}
func (d *spdag) delChildrenRepeated(sp []*SubbranchPlan) []*SubbranchPlan {
newsp := make([]*SubbranchPlan, 0)
for i := 0; i < len(sp); i++ {
repeat := false
for j := i + 1; j < len(sp); j++ {
if sp[i].SubbranchPlanId == sp[j].SubbranchPlanId {
repeat = true
break
}
}
if !repeat {
newsp = append(newsp, sp[i])
}
}
return newsp
}
func (d *spdag) listChildren(vertex *SubbranchPlan) *SubbranchPlan {
for _, v := range vertex.ChildrenVertexs {
return d.listChildren(v)
}
return nil
}
// RecursionChildrens 递归当前计划下的所有子节点。
func (d *spdag) RecursionChildrens(vertexID int) []*SubbranchPlan {
v, ok := d.vertexsMap[vertexID]
if !ok {
return nil
}
vertexs := make([]*SubbranchPlan, 0)
cv := d.listChildren(v)
vertexs = append(vertexs, cv)
return vertexs
}