P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
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
package foundation.dao.version;
 
import foundation.data.entity.Entity;
import foundation.data.entity.EntitySet;
import foundation.json.IJSONProvider;
import foundation.json.IJSONWriter;
import foundation.util.MapList;
 
public class PackageChange implements IJSONProvider {
 
    private MapList<String, IDataChange> items;
    
    public PackageChange() {
        items = new MapList<String, IDataChange>();
    }
 
    public void compareEntity(String name, Entity one, Entity another) throws Exception {
        EntityChange entityChanges = doCompareEntity(name, one, another);
        
        if (entityChanges == null) {
            return;
        }
        
        items.add(name, entityChanges);
    }
 
    public void compareEntitySet(String name, EntitySet one, EntitySet another) throws Exception {
        if (one == null && another == null) {
            return;
        }
        
        EntitySetChange entitySetChanges = new EntitySetChange(name);
        items.add(name, entitySetChanges);
        
        //1. 如果当前记录没有--已经删除
        if (one == null) {
            entitySetChanges.setChangeType(ChangeType.Delete);
            return;
        }
        
        //2. 如果对照记录没有--新增加
        if (another == null) {
            entitySetChanges.setChangeType(ChangeType.Add);
            return;
        }
        
        //3. 按Entity: one-->another 对比
        for (Entity oneEntity: one) {
            String id = oneEntity.getId();
            Entity anotherEntity = another.getEntity("id", id);
            
            EntityChange entityChange = doCompareEntity(id, oneEntity, anotherEntity);
            
            if (entityChange == null) {
                continue;
            }
            
            entitySetChanges.addOneEntityChange(id, entityChange);
        }
 
        //4. 按Entity: another-->one 对比
        for (Entity anotherEntity: another) {
            String id = anotherEntity.getId();
            
            if (!one.contains("id", id)) {
                EntityChange entityChange = new EntityChange(name);
                entityChange.setChangeType(ChangeType.Delete);
                
                entitySetChanges.addOneEntityChange(id, entityChange);
            }
        }
    }
 
    private EntityChange doCompareEntity(String name, Entity one, Entity another) throws Exception {
        EntityChange entityChange = new EntityChange(name);
        
        if (one == null && another == null) {
            return null;
        }
        
        //1. 如果当前记录没有--已经删除
        if (one == null) {
            entityChange.setChangeType(ChangeType.Delete);
            return entityChange;
        }
        
        //2. 如果对照记录没有--新增加
        if (another == null) {
            entityChange.setChangeType(ChangeType.Add);
            return entityChange;
        }
        
        //3. 按字段对比数据
        String[] fields = one.getFieldsMeta().getFieldNames();
        int max = fields.length;
        
        for (int i = 0; i < max; i++) {
            Object oneValue = one.getValue(i);
            Object anotherValue = another.getValue(i);
            
            if (valueEquals(oneValue, anotherValue)) {
                continue;
            }
            
            String oneJSON = one.getJSONString(i);
            String anotherJSON = another.getJSONString(i);
                    
            entityChange.addOneFieldChange(fields[i], ChangeType.Change, oneValue, anotherValue, oneJSON, anotherJSON);
        }
        
        if (entityChange.isEmpty()) {
            entityChange.setChangeType(ChangeType.NoChange);
            return null;
        }
        
        entityChange.setChangeType(ChangeType.Change);
        return entityChange;
    }
    
    private boolean valueEquals(Object oneValue, Object anotherValue) {
        if (oneValue == null) {
            if (anotherValue == null) {
                return true;
            }
            else {
                return false;
            }
        }
        else {
            if (anotherValue == null) {
                return false;
            }
            
            return oneValue.equals(anotherValue);
        }
    }
 
    @Override
    public void writeJSON(IJSONWriter writer) {
        writer.beginObject();
        
        //1. write list
        writer.beginArray("list");
        
        for (IDataChange dataChange: items) {
            writer.write(dataChange.getDataName());
        }
        
        writer.endArray();
        
        //2. write changes
        writer.beginArray("changes");
        
        for (IDataChange dataChange: items) {
            dataChange.writeJSON(writer);
        }
        
        writer.endArray();
    }
 
}