-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
222 lines (205 loc) · 5.02 KB
/
test.html
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>测试</title>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
function random(){
return Math.floor(Math.random() * 1000);
}
function gljs(){
var n = Math.floor(Math.random() * 10) + 1;
var types = ["人工", "材料", "机械"];
var gljs = "";
for(var i = 0; i < n; i++){
gljs += "<工料机><类型>"+types[i%3]+"</类型><编码>"+random()+"</编码><单价>"+random()+"</单价><定额系数>"+Math.random()+"</定额系数></工料机>"
}
return gljs;
}
function arrayRandom(arr){
return arr[Math.floor(Math.random() * arr.length)];
}
function arrPush(arr, e, n){
if(n){
for(var i = 0; i < n; i++){
arr.push(e);
}
}else{
arr.push(e);
}
}
function shuffle(o){ //v1.0
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
function arrRemove(arr, element){
var index = arr.indexOf(element);
if(index > -1){
arr.splice(index, 1);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
function Tester(){
this.file = ""; //cost file id
this.ztgc = ""; //整体工程id
this.dxgc = []; //单项工程ids
this.dwgc = []; //单位工程ids
this.fbfx = []; //分部分项ids
this.qd = []; //清单ids
this.de = []; //定额ids
}
//新建整体工程
Tester.prototype.newFile = function(callback){
var me = this;
$.ajax({
type:"POST",
url:"/cost",
async:true,
data:{type:'整体工程'},
success:function(data){
me.file = data.file;
me.ztgc = data.id;
callback(null, data);
}
});
}
//新建单项工程
Tester.prototype.newDxgc = function(callback){
var me = this;
$.ajax({
type:"POST",
url:"/cost",
async:true,
data:{type:'单项工程', file:me.file, parentid: me.ztgc},
success:function(data){
me.dxgc.push(data.id);
callback(null, data);
}
});
}
//新建单位工程
Tester.prototype.newDwgc = function(callback){
var me = this;
$.ajax({
type:"POST",
url:"/cost",
async:true,
data:{type:'单位工程', file:me.file, parentid: arrayRandom(me.dxgc)},
success:function(data){
me.dwgc.push(data.id);
callback(null, data);
}
});
}
//新建分部分项
Tester.prototype.newFbfx = function(callback){
var me = this;
$.ajax({
type:"POST",
url:"/cost",
async:true,
data:{type:'分部分项', file:me.file, parentid: arrayRandom(me.dwgc)},
success:function(data){
me.fbfx.push(data.id);
callback(null, data);
}
});
}
//新建清单
Tester.prototype.newQd = function(callback){
var me = this;
var qd = "<清单><编码>"+random()+"</编码><工程量>"+random()+"</工程量></清单>";
$.ajax({
type:"POST",
url:"/cost",
async:true,
data:{type:'清单', node:qd, file:me.file, parentid: arrayRandom(me.fbfx)},
success:function(data){
me.qd.push(data.id);
callback(null, data);
}
});
}
//新建定额
Tester.prototype.newDe = function(callback){
var me = this;
var de = "<定额><编码>"+random()+"</编码><工程量>"+random()+"</工程量>"+gljs()+"</定额>";
$.ajax({
type:"POST",
url:"/cost",
async:true,
data:{type:'定额', node:de, file:me.file, parentid: arrayRandom(me.qd)},
success:function(data){
me.de.push(data.id);
callback(null, data);
}
});
}
//修改工程量
Tester.prototype.modGcl = function(callback){
var me = this;
var modQd = Math.random() < 0.5;
var nid = modQd ? arrayRandom(me.qd) : arrayRandom(me.de);
$.ajax({
type:"PUT",
url:"/cost",
async:true,
data: {file:me.file, id:nid, name:"工程量", value:random()},
success:function(data){
callback(null, data);
}
});
}
//删除清单或定额
Tester.prototype.delNode = function(callback){
var me = this;
var delQd = Math.random() < 0.5;
var nid = delQd ? arrayRandom(me.qd) : arrayRandom(me.de);
$.ajax({
type:"DELETE",
url:"/cost",
async:true,
data:{file:me.file, id:nid},
success:function(data){
delQd ? arrRemove(me.qd, nid) : arrRemove(me.de, nid);
callback(null, data);
}
});
}
///////////////////////////////////////////////////////////////////////////////////
function step(actor, actions, i){
if(i == actions.length){
alert('done');
}else{
var act = actions[i];
actor[act](function(err, res){
console.log("step " + i + ": " + act);
step(actor, actions, i+1);
});
}
}
function run(){
var inits = ['newFile'];
arrPush(inits, "newDxgc", 2);
arrPush(inits, "newDwgc", 4);
arrPush(inits, "newFbfx", 8);
arrPush(inits, "newQd", 10);
arrPush(inits, "newDe", 20);
var ops = [];
arrPush(ops, "newQd", 100);
arrPush(ops, "newDe", 300);
arrPush(ops, "modGcl", 50);
arrPush(ops, "delNode", 20);
var actions = inits.concat(shuffle(ops));
var actor = new Tester();
step(actor, actions, 0);
}
$(document).ready(function(){
run();
})
</script>
</head>
<body>
</body>
</html>