-
Notifications
You must be signed in to change notification settings - Fork 0
/
undo_log.go
141 lines (128 loc) · 3.09 KB
/
undo_log.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
package main
import (
"OrderManager-cli/config"
"OrderManager-cli/pb"
"context"
"strconv"
)
type logNode struct {
//add , del , update
next *logNode
prev *logNode
operation string
taskId string
oldData interface{}
newData interface{}
}
var maxSize, _ = strconv.Atoi(config.Cfg.UndoLogTaskSize)
type UndoLogChain struct {
index int
curSize int
dummyHead *logNode
dummyTail *logNode
client pb.ServiceClient
}
var logChain *UndoLogChain
func NewLogChain(cli pb.ServiceClient) *UndoLogChain {
dummyHead := new(logNode)
dummyTail := new(logNode)
dummyHead.next = dummyTail
dummyTail.prev = dummyHead
return &UndoLogChain{
index: 0,
curSize: 0,
dummyHead: dummyHead,
dummyTail: dummyTail,
client: cli,
}
}
func (ulc *UndoLogChain) append(op string, oldData interface{}, newData interface{}) {
curNode := ulc.dummyHead
for i := 0; i < ulc.index; i++ {
curNode = curNode.next
}
newNode := &logNode{
operation: op,
oldData: oldData,
newData: newData,
prev: ulc.dummyHead,
next: curNode.next,
}
ulc.dummyHead.next = newNode
curNode.next.prev = newNode
ulc.curSize = ulc.curSize - (ulc.index) + 1
ulc.index = 0
for ulc.curSize > maxSize {
ulc.dummyTail.prev.next = nil
ulc.dummyTail = ulc.dummyTail.prev
}
}
// 撤销
func (ulc *UndoLogChain) undo() error {
if ulc.index == ulc.curSize {
return nil
}
ulc.index++
curNode := ulc.dummyHead
for i := 0; i < ulc.index; i++ {
curNode = curNode.next
}
switch curNode.operation {
case "add": //撤销时为del
if _, err := ulc.client.DelTask(context.Background(), &pb.DelTaskRequest{
TaskNo: curNode.newData.(*pb.AddTaskRequest).T.TaskId,
User: config.Cfg.Login.UserName,
Principal: curNode.newData.(*pb.AddTaskRequest).T.Principal,
}); err != nil {
return err
}
case "del": //撤销时为add
if _, err := ulc.client.AddTask(context.Background(), &pb.AddTaskRequest{
T: curNode.oldData.(*pb.Task),
User: config.Cfg.Login.UserName,
}); err != nil {
return err
}
case "update":
if _, err := ulc.client.ModTask(context.Background(), &pb.ModTaskRequest{
T: curNode.oldData.(*pb.Task),
User: config.Cfg.Login.UserName,
}); err != nil {
return err
}
}
return nil
}
// 重做
func (ulc *UndoLogChain) redo() error {
if ulc.index == 0 {
return nil
}
curNode := ulc.dummyHead
for i := 0; i < ulc.index; i++ {
curNode = curNode.next
}
ulc.index--
switch curNode.operation {
case "add": //撤销时为del
if _, err := ulc.client.AddTask(context.Background(), curNode.newData.(*pb.AddTaskRequest)); err != nil {
return err
}
case "del": //撤销时为add
if _, err := ulc.client.DelTask(context.Background(), &pb.DelTaskRequest{
TaskNo: curNode.oldData.(*pb.Task).TaskId,
User: config.Cfg.Login.UserName,
Principal: curNode.oldData.(*pb.Task).Principal,
}); err != nil {
return err
}
case "update":
if _, err := ulc.client.ModTask(context.Background(), &pb.ModTaskRequest{
T: curNode.newData.(*pb.Task),
User: config.Cfg.Login.UserName,
}); err != nil {
return err
}
}
return nil
}