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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
import fetch from 'dva/fetch';
import React from 'react';
import ReactDOM from 'react-dom';
import {
Select,
Button,
Modal,
Input,
Transfer,
Row,
Col,
Form,
message,
notification,
} from 'antd';
import { connect } from 'dva';
import { getToken } from '@/utils/authority';
import config from '@/config/config';
import QueryItem from './QueryItem';
import OrderItem from './OrderItem';
import ButtonDiy from '@/baseComponent/ButtonDiy';
import FormdataWrapper from '@/utils/object-to-formdata-custom';
import { getServicesNomal } from '@/webPublic/one_stop_public/Services/services';
import { getExportInfoApi } from '@/webPublic/one_stop_public/Services/apiConfig';
const Option = Select.Option;
let keyX = 1;
function swapArray(arr, index1, index2) {
arr[index1] = arr.splice(index2, 1, arr[index1])[0];
return arr;
}
const FormItem = Form.Item;
@connect(({ DataObj, loading }) => ({
DataObj,
loading: loading.models.DataObj,
}))
@Form.create()
export default class Index extends React.Component {
constructor(props) {
super(props);
this.state = {
visiable: false,
queryVisiable: false,
groupVisiable: false,
sortVisiable: false,
currentQueryKey: null,
currentGroupKey: null,
orderVisiable: false,
currentOrderKey: null,
querys: [],
orders: [],
groups: [],
gs: [],
os: [],
qs: [],
mockData: [],
targetKeys: [],
sourceSelectedKeys: [],
targetSelectedKeys: [],
infos: {},
confirmLoading: false,
};
}
onSelectChange = (sourceSelectedKeys, targetSelectedKeys) => {
this.setState({ sourceSelectedKeys, targetSelectedKeys });
};
deleteQuery = (i) => {
const querys = this.state.querys;
querys.splice(i, 1);
this.setState({ querys });
};
deleteOrder = (i) => {
const orders = this.state.orders;
orders.splice(i, 1);
this.setState({ orders });
};
deleteGroup = (i) => {
const groups = this.state.groups;
groups.splice(i, 1);
this.setState({ groups });
};
open = () => {
getServicesNomal(getExportInfoApi, {
objId: this.props.objId,
}).then(infos => {
const mockData = [];
const qs = [];
const gs = [];
const os = [];
for (var key in infos) {
const x = infos[key];
mockData.push({
key: x.field,
title: x.name,
chosen: false,
});
if (x.canQuery) {
qs.push(x);
}
if (x.canGroup) {
gs.push(x);
}
if (x.canOrder) {
os.push(x);
}
}
this.setState({
mockData,
infos,
visiable: true,
qs,
gs,
os,
});
});
};
onCancle = () => {
this.setState({ visiable: false });
};
handleChange = (targetKeys, direction, moveKeys) => {
this.setState({ targetKeys });
};
up = () => {
keyX = keyX + 1;
var { targetKeys, targetSelectedKeys } = this.state;
for (var i = 0; i < targetKeys.length; i++) {
if (targetKeys[i] === targetSelectedKeys[0]) {
if (i === 0) return;
targetKeys = swapArray(targetKeys, i, i - 1);
break;
}
}
this.setState({ targetKeys });
};
down = () => {
keyX = keyX + 1;
var { targetKeys, targetSelectedKeys } = this.state;
for (var i = 0; i < targetKeys.length; i++) {
if (targetKeys[i] === targetSelectedKeys[0]) {
if (i === targetKeys.length - 1) return;
targetKeys = swapArray(targetKeys, i, i + 1);
break;
}
}
this.setState({ targetKeys });
};
bottom = () => {
keyX = keyX + 1;
const { targetKeys, targetSelectedKeys } = this.state;
var x = [];
for (var i = 0; i < targetKeys.length; i++) {
if (!targetSelectedKeys.includes(targetKeys[i])) {
x.push(targetKeys[i]);
}
}
x = [...x, ...targetSelectedKeys];
this.setState({ targetKeys: x });
};
top = () => {
keyX = keyX + 1;
const { targetKeys, targetSelectedKeys } = this.state;
const x = [...targetSelectedKeys];
for (var i = 0; i < targetKeys.length; i++) {
if (!targetSelectedKeys.includes(targetKeys[i])) {
x.push(targetKeys[i]);
}
}
this.setState({ targetKeys: x });
};
renderItem = (item) => {
if (this.state.targetKeys.includes(item.key)) {
const customLabel = (
<span className="custom-item" key={item.key}>
{item.title}
</span>
);
return {
label: customLabel, // for displayed item
value: item.title, // for title and filter matching
};
} else {
const customLabel = (
<span className="custom-item" key={item.key}>
{item.title}
</span>
);
return {
label: customLabel, // for displayed item
value: item.title, // for title and filter matching
};
}
};
addOrder = () => {
this.setState({ orderVisiable: true });
};
okOrder = () => {
const orders = this.state.orders;
orders.push(this.state.infos[this.state.currentOrderKey]);
this.setState({ orders, orderVisiable: false, currentOrderKey: null });
};
okGroup = () => {
const groups = this.state.groups;
groups.push(this.state.infos[this.state.currentGroupKey]);
this.setState({ groups, groupVisiable: false, currentGroupKey: null });
};
addGroup = () => {
this.setState({ groupVisiable: true });
};
addQuery = () => {
this.setState({ queryVisiable: true });
};
okQuery = () => {
const querys = this.state.querys;
querys.push(this.state.infos[this.state.currentQueryKey]);
this.setState({ querys, queryVisiable: false, currentQueryKey: null });
};
export = () => {
this.props.form.validateFields((err, fieldsValue) => {
if (err) return;
//form.resetFields();
var qqs;
if (this.props.mustQuerys != null) {
qqs = [...this.props.mustQuerys];
} else {
qqs = [];
}
const oos = [];
const ggs = [];
for (let key in fieldsValue) {
var x = key.indexOf('__');
let kk = key.substr(x + 2);
if (key.indexOf('q__') > -1) {
qqs.push({
notes: this.state.infos[kk].notes,
hql: this.state.infos[kk].hql,
c: this.state.infos[kk].type,
x: fieldsValue[key].stringX,
v: fieldsValue[key].string,
});
} else if (key.indexOf('o__') > -1) {
oos.push({ hql: this.state.infos[kk].hql, x: fieldsValue[key].stringX });
} else if (key.indexOf('g__') > -1) {
ggs.push({ hql: this.state.infos[kk].hql });
}
}
if (this.props.voClass && ggs.length === 0) {
message.error('请至少选择一个聚合条件');
return;
}
if (this.state.targetKeys.length === 0) {
message.error('请至少选择一个导出字段');
return;
}
const names = [];
for (let i = 0; i < this.state.targetKeys.length; i++) {
names.push(this.state.infos[this.state.targetKeys[i]].name);
}
let divElement = document.getElementById('downloadDiv');
let downloadUrl = config.sqlFormsServer + '/DataObjApi/exportFormData?';
const token = getToken() != null && getToken() != 'null' ? getToken() : '0000';
downloadUrl = `${downloadUrl}token=${token}`;
let param = {
fields: this.state.targetKeys,
names: names,
beanName: this.props.beanName,
querys: JSON.stringify(qqs),
orders: JSON.stringify(oos),
groups: JSON.stringify(ggs),
voClass: this.props.voClass,
objId: this.props.objId,
modelClass: this.props.modelClass,
preHandleClass: this.props.preHandleClass,
hqlHandleClass: this.props.hqlHandleClass,
};
param = { ...param, ...(this.props.params || {}) };
this.downloadFile(downloadUrl, param);
});
};
downloadFile(url, params) {
this.setState({ confirmLoading: true });
fetch(url, {
method: 'POST',
body: FormdataWrapper(params),
})
.then((res) => {
if (res.status != '200') {
return res.json();
} else {
return res.blob();
}
})
.then((data) => {
if (data instanceof Blob) {
let a = document.createElement('a');
let url = window.URL.createObjectURL(data);
let filename = (this.props.fileName ? this.props.fileName : '导出文件') + '.xlsx';
a.href = url;
a.download = filename;
a.click();
window.URL.revokeObjectURL(url);
a = null;
} else {
notification.error({
message: `文件导出错误`,
description: data.errMsg,
});
}
})
.catch((err) => {
notification.error({
message: `网络请求超时`,
});
})
.finally(() => {
this.setState({ confirmLoading: false });
});
}
cancelGroup = () => {
this.setState({ currentGroupKey: null, groupVisiable: false });
};
cancelQuery = () => {
this.setState({ currentQueryKey: null, queryVisiable: false });
};
cancelOrder = () => {
this.setState({ currentOrderKey: null, orderVisiable: false });
};
selectOrder = (e) => {
this.setState({ currentOrderKey: e });
};
selectQuery = (e) => {
this.setState({ currentQueryKey: e });
};
selectGroup = (e) => {
this.setState({ currentGroupKey: e });
};
render() {
const { form } = this.props;
const {
visiable,
targetKeys,
sourceSelectedKeys,
queryVisiable,
querys,
orders,
groups,
confirmLoading,
targetSelectedKeys,
mockData,
qs,
os,
gs,
currentQueryKey,
currentOrderKey,
orderVisiable,
currentGroupKey,
groupVisiable,
} = this.state;
return (
<span>
<ButtonDiy handleClick={this.open} name={'自定义导出'} />
<div id="downloadDiv" style={{ display: 'none' }} />
<Modal
width={670}
maskClosable={false}
destroyOnClose
title="自定义查询、排序、导出"
style={{ textAlign: 'center' }}
visible={visiable}
onOk={this.export}
onCancel={this.onCancle}
confirmLoading={confirmLoading}>
<div style={{ overflowY: 'auto', maxHeight: '500px', height: '500px' }}>
<div style={{ textAlign: 'left' }}>
<Button type="primary">新增查询条件</Button>
<Select
style={{ width: 200, paddingRight: '10px' }}
value={currentQueryKey}
onChange={this.selectQuery}>
{qs.map((r) => {
return (
<Option key={r.field} value={r.field}>
{r.name}
</Option>
);
})}
</Select>
{currentQueryKey ? (
<span style={{ paddingLeft: '0' }}>
<Button type="primary" onClick={this.okQuery}>
确定
</Button>
</span>
) : (
''
)}
<div style={{ height: '12px' }} />
{querys.map((r, i) => (
<FormItem
key={r.name + i}
labelCol={{ span: 4 }}
wrapperCol={{ span: 20 }}
colon={false}
label={r.name}>
{form.getFieldDecorator(i + 'q__' + r.field, {
initialValue: { stringX: '=' },
rules: [
{
validator: (rule, value, callback) => {
var errors = [];
if (value == null) {
errors.push(new Error('请输入查询值!', rule.field));
} else {
if (
value.stringX != 'IS NOT NULL' &&
value.stringX != 'IS NULL' &&
value.string == null
) {
errors.push(new Error('请输入查询值!', rule.field));
}
}
callback(errors);
},
},
],
})(
<QueryItem
obj={r}
key={r.name + i}
deleteQuery={this.deleteQuery.bind(this, i)}
/>,
)}
</FormItem>
))}
</div>
<div style={{ textAlign: 'left', paddingTop: '15px' }}>
<Button type="primary">新增排序条件</Button>
<Select
style={{ width: 200, paddingRight: '10px' }}
value={currentOrderKey}
onChange={this.selectOrder}>
{os.map((r) => {
for (let j = 0; j < orders.length; j++) {
if (orders[j].field === r.field) return;
}
return (
<Option key={r.field} value={r.field}>
{r.name}
</Option>
);
})}
</Select>
{currentOrderKey ? (
<span style={{ paddingLeft: '0' }}>
<Button type="primary" onClick={this.okOrder}>
确定
</Button>
</span>
) : (
''
)}
<div style={{ height: '12px' }} />
{orders.map((r, i) => (
<FormItem
key={r.name + i}
labelCol={{ span: 4 }}
wrapperCol={{ span: 20 }}
colon={false}
label={'按' + r.name}>
{form.getFieldDecorator(i + 'o__' + r.field, {
initialValue: { stringX: 'desc' },
})(<OrderItem deleteOrder={this.deleteOrder.bind(this, i)} />)}
</FormItem>
))}
</div>
{this.props.voClass ? (
<div style={{ textAlign: 'left', paddingTop: '15px' }}>
<span style={{ paddingRight: '10px' }}>聚合条件:</span>
{!groupVisiable ? (
<Button type="primary" onClick={this.addGroup}>
新增
</Button>
) : (
<span>
<Select
style={{ width: 100 }}
value={currentGroupKey}
onChange={this.selectGroup}>
{gs.map((r) => {
for (var j = 0; j < groups.length; j++) {
if (groups[j].field == r.field) return;
}
return (
<Option key={r.field} value={r.field}>
{r.name}
</Option>
);
})}
</Select>
{currentGroupKey ? (
<Button type="primary" onClick={this.okGroup}>
确定
</Button>
) : (
''
)}
<Button type="primary" onClick={this.cancelGroup}>
取消
</Button>
</span>
)}
{groups.map((r, i) => (
<FormItem
key={i + r.name}
labelCol={{ span: 5 }}
wrapperCol={{ span: 15 }}
label={'按' + r.name + '聚合'}>
{form.getFieldDecorator(i + 'g__' + r.field)(
<Input style={{ display: 'none' }} value="" />,
)}
<Button type="danger" onClick={this.deleteGroup.bind(this, i)}>
删除
</Button>
</FormItem>
))}
</div>
) : (
''
)}
<Row>
<Col span={24} style={{ textAlign: 'left', paddingTop: '15px' }}>
<span style={{ paddingRight: '10px' }}>导出项:</span>
</Col>
<Col span={18}>
<Transfer
key={keyX}
style={{ textAlign: 'left' }}
dataSource={mockData}
listStyle={{
width: 200,
height: 300,
}}
onSelectChange={this.onSelectChange}
selectedKeys={[...sourceSelectedKeys, ...targetSelectedKeys]}
rowKey={(record) => record.key}
targetKeys={targetKeys}
onChange={this.handleChange}
render={this.renderItem}
/>
</Col>
<Col span={2}>
<div style={{ marginTop: 100 }}>
<Button
size="small"
disabled={targetSelectedKeys.length === 0}
onClick={this.top}>
批量置顶
</Button>
<Button
size="small"
style={{ marginTop: 10 }}
disabled={targetSelectedKeys.length !== 1}
onClick={this.up}>
单项上移
</Button>
<Button
size="small"
style={{ marginTop: 10 }}
disabled={targetSelectedKeys.length !== 1}
onClick={this.down}>
单项下移
</Button>
<Button
size="small"
style={{ marginTop: 10 }}
disabled={targetSelectedKeys.length === 0}
onClick={this.bottom}>
批量置底
</Button>
</div>
</Col>
</Row>
</div>
</Modal>
</span>
);
}
}