king
2020-11-13 8190d6e5ac14616d85e3992169ecef6d99d03b76
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
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { fromJS } from 'immutable'
import { Form, Tabs, Row, Col, Button, Table, Popconfirm, Icon, notification, Modal, message, InputNumber, Radio } from 'antd'
import moment from 'moment'
 
import Api from '@/api'
import Utils from '@/utils/utils.js'
 
import ColumnForm from './columnform'
import CodeMirror from '@/templates/zshare/codemirror'
import './index.scss'
 
const { TabPane } = Tabs
const { confirm } = Modal
 
class VerifyCard extends Component {
  static propTpyes = {
    dict: PropTypes.object,    // 字典项
    config: PropTypes.object,
    card: PropTypes.object,
  }
 
  state = {
    verify: {},
    defaultscript: '', // 自定义脚本
    excelColumns: [
      {
        title: this.props.dict['model.form.field'],
        dataIndex: 'Column',
        width: '25%'
      },
      {
        title: this.props.dict['model.name'],
        dataIndex: 'Text',
        width: '25%'
      },
      {
        title: this.props.dict['model.form.columnWidth'],
        dataIndex: 'Width',
        width: '25%'
      },
      {
        title: '操作',
        align: 'center',
        dataIndex: 'operation',
        render: (text, record) =>
          (
            <div>
              <span className="operation-btn" title={this.props.dict['model.edit']} onClick={() => this.handleEdit(record, 'columns')} style={{color: '#1890ff'}}><Icon type="edit" /></span>
              <span className="operation-btn" title={this.props.dict['header.form.up']} onClick={() => this.handleUpDown(record, 'columns', 'up')} style={{color: '#1890ff'}}><Icon type="arrow-up" /></span>
              <span className="operation-btn" title={this.props.dict['header.form.down']} onClick={() => this.handleUpDown(record, 'columns', 'down')} style={{color: '#ff4d4f'}}><Icon type="arrow-down" /></span>
              <Popconfirm
                overlayClassName="popover-confirm"
                title={this.props.dict['model.query.delete']}
                onConfirm={() => this.handleDelete(record, 'columns')
              }>
                <span style={{color: '#ff4d4f', cursor: 'pointer'}}><Icon type="delete" /></span>
              </Popconfirm>
            </div>
          )
      }
    ]
  }
 
  UNSAFE_componentWillMount() {
    const { config, card } = this.props
    let _verify = {}
 
    if (card.verify) {
      _verify = fromJS(card.verify).toJS()
    }
 
    _verify.enable = _verify.enable || 'false'
    _verify.columns = _verify.columns || []
 
    // 同步显示列
    // if (!_verify.columns || _verify.columns.length === 0) {
    //   _verify.columns = []
    //   config.columns.forEach(item => {
    //     if (!item.field) return
  
    //     _verify.columns.push({
    //       Column: item.field,
    //       Text: item.label,
    //       Width: 20,
    //       uuid: Utils.getuuid()
    //     })
    //   })
    // }
 
    if (card.intertype !== 'system') {
      _verify.enable = 'false'
    }
 
    let defaultscript = ''
    if (!_verify.script && card.intertype === 'system') {
      let search = this.formatSearch(config.search)
      search = Utils.joinMainSearchkey(search)
      search = search ? 'where ' + search : ''
      
      defaultscript = `update ${config.setting.tableName || ''} set idefine5= idefine5+1 ,modifydate=getdate(),cdefine5='已导出',modifyuserid=@userid@ ${search}`
    }
 
 
    this.setState({
      verify: _verify,
      defaultscript: defaultscript
    })
  }
 
  /**
   * @description 获取全部搜索条件
   * @param {Array} searches 搜索条件数组
   */
  formatSearch (searches) {
    if (!searches || searches.length === 0) return []
 
    let newsearches = []
    searches.forEach(search => {
      let item = {
        key: search.field,
        match: search.match,
        type: search.type,
        label: search.label,
        value: `@${search.field}@`,
        required: search.required === 'true'
      }
      if (item.type === 'group') {
        let copy = fromJS(item).toJS()
        copy.key = search.datefield
 
        item.value = `@${search.field}@`
        item.match = '='
        
        copy.type = 'daterange'
        copy.match = 'between'
        copy.value = [`@${search.datefield}@`, `@${search.datefield}1@`]
 
        if (search.transfer === 'true') {
          newsearches.push(item)
        }
        newsearches.push(copy)
        return
      } else if (item.type === 'dateweek') {
        item.value = [`@${search.field}@`, `@${search.field}1@`]
      } else if (item.type === 'daterange') {
        item.value = [`@${search.field}@`, `@${search.field}1@`]
      } else if (item.type === 'multiselect') {
        item.value = [`@${search.field}@`]
      }
      newsearches.push(item)
    })
    
    return newsearches
  }
 
  columnChange = (values) => {
    let verify = JSON.parse(JSON.stringify(this.state.verify))
 
    if (values.uuid) {
      verify.columns = verify.columns.map(item => {
        if (item.uuid === values.uuid) {
          return values
        } else {
          return item
        }
      })
    } else {
      let fields = verify.columns.map(item => item.Column)
      if (fields.includes(values.Column)) {
        notification.warning({
          top: 92,
          message: values.Column + '字段已存在!',
          duration: 5
        })
        return
      }
      values.uuid = Utils.getuuid()
      verify.columns.push(values)
    }
 
    this.setState({
      verify: verify
    })
  }
 
  handleDelete = (record, type) => {
    const { verify } = this.state
 
    verify.columns = verify.columns.filter(item => item.uuid !== record.uuid)
 
    this.setState({ verify: verify })
  }
 
  handleEdit = (record, type) => {
    this.columnForm.edit(record)
 
    let node = document.getElementById('verify-excelout-box-tab').parentNode
 
    if (node && node.scrollTop) {
      let inter = Math.ceil(node.scrollTop / 10)
 
      let timer = setInterval(() => {
        if (node.scrollTop - inter > 0) {
          node.scrollTop = node.scrollTop - inter
        } else {
          node.scrollTop = 0
          clearInterval(timer)
        }
      }, 10)
    }
  }
 
  handleUpDown = (record, type, direction) => {
    let verify = JSON.parse(JSON.stringify(this.state.verify))
    let index = 0
 
    verify.columns = verify.columns.filter((item, i) => {
      if (item.uuid === record.uuid) {
        index = i
      }
 
      return item.uuid !== record.uuid
    })
    if ((index === 0 && direction === 'up') || (index === verify.columns.length && direction === 'down')) {
      return
    }
 
    if (direction === 'up') {
      verify.columns.splice(index - 1, 0, record)
    } else {
      verify.columns.splice(index + 1, 0, record)
    }
 
    this.setState({
      verify: verify
    })
  }
 
  showError = (errorType) => {
    if (errorType === 'S') {
      notification.success({
        top: 92,
        message: '执行成功!',
        duration: 2
      })
    } else if (errorType === 'Y') {
      Modal.success({
        title: '执行成功!'
      })
    } else if (errorType === 'F') {
      notification.error({
        className: 'notification-custom-error',
        top: 92,
        message: '执行失败!',
        duration: 10
      })
    } else if (errorType === 'N') {
      notification.error({
        top: 92,
        message: '执行失败!',
        duration: 10
      })
    } else if (errorType === 'E') {
      Modal.error({
        title: '执行失败!'
      })
    } else if (errorType === 'NM') {
      message.error('执行失败!')
    }
  }
 
  timeChange = (val, type) => {
    const { verify } = this.state
 
    this.setState({
      verify: {...verify, [type]: val}
    })
  }
 
  handleConfirm = () => {
    let verify = fromJS(this.state.verify).toJS()
    
    // 表单提交时检查输入值是否正确
    return new Promise((resolve, reject) => {
      let _cols = verify.columns.map(col => col.Column)
      let _vcols = Array.from(new Set(_cols))
 
      if (_cols.length > _vcols.length) {
        notification.warning({
          top: 92,
          message: 'Excel列字段名,不可重复!',
          duration: 5
        })
        
        return
      }
 
      if (verify.enable === 'true') {
        this.props.form.validateFieldsAndScroll((err, values) => {
          if (!err) {
            values.sql = values.sql || ''
 
            let _quot = values.sql.match(/'{1}/g)
            let _lparen = values.sql.match(/\({1}/g)
            let _rparen = values.sql.match(/\){1}/g)
    
            _quot = _quot ? _quot.length : 0
            _lparen = _lparen ? _lparen.length : 0
            _rparen = _rparen ? _rparen.length : 0
    
            if (_quot % 2 !== 0) {
              notification.warning({
                top: 92,
                message: 'sql中\'必须成对出现',
                duration: 5
              })
              return
            } else if (_lparen !== _rparen) {
              notification.warning({
                top: 92,
                message: 'sql中()必须成对出现',
                duration: 5
              })
              return
            } else if (/--/ig.test(values.sql)) {
              notification.warning({
                top: 92,
                message: '自定义sql语句中,不可出现字符 -- ,注释请用 /*内容*/',
                duration: 5
              })
              return
            }
    
            let error = Utils.verifySql(values.sql, 'customscript')
    
            if (error) {
              notification.warning({
                top: 92,
                message: 'sql中不可使用' + error,
                duration: 5
              })
              return
            }
    
            let param = {
              func: 's_debug_sql',
              LText: values.sql
            }
    
            param.LText = param.LText.replace(/@\$|\$@/ig, '')
    
            param.LText = Utils.formatOptions(param.LText)
            param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
            param.secretkey = Utils.encrypt(param.LText, param.timestamp)
            
            Api.getLocalConfig(param).then(res => {
              if (res.status) {
                resolve({...verify, script: values.sql})
              } else {
                Modal.error({
                  title: res.message
                })
              }
            })
          } else {
            notification.warning({
              top: 92,
              message: '自定义脚本不可为空!',
              duration: 5
            })
          }
        })
      } else {
        resolve(verify)
      }
    })
  }
 
  changeEnable = (e) => {
    const { verify } = this.state
 
    this.setState({
      verify: {...verify, enable: e.target.value}
    })
  }
 
  columnFieldInput = () => {
    const { config } = this.props
    const { verify } = this.state
 
    let columns = fromJS(verify.columns).toJS()
    let fields = columns.map(item => item.Column)
 
    config.columns.forEach(item => {
      if (fields.includes(item.field) || !item.field) return
      fields.push(item.field)
 
      columns.push({
        Column: item.field,
        Text: item.label,
        Width: 20,
        uuid: Utils.getuuid()
      })
    })
 
    this.setState({
      verify: {...verify, columns: columns}
    })
  }
 
  clearField = () => {
    const { verify } = this.state
    const _this = this
 
    confirm({
      content: `确定清空Excel列吗?`,
      onOk() {
        _this.setState({
          verify: {
            ...verify,
            columns: []
          }
        })
      },
      onCancel() {}
    })
  }
 
  render() {
    const { card } = this.props
    const { verify, excelColumns, defaultscript } = this.state
    const { getFieldDecorator } = this.props.form
    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      }
    }
 
    return (
      <div id="verify-excelout-box-tab">
        <Tabs defaultActiveKey="1" className="verify-card-box" onChange={this.tabchange}>
          <TabPane tab={
            <span>
              Excel导出列
              {verify.columns.length ? <span className="count-tip">{verify.columns.length}</span> : null}
            </span>
          } key="1">
            <ColumnForm
              dict={this.props.dict}
              columnChange={this.columnChange}
              wrappedComponentRef={(inst) => this.columnForm = inst}
            />
            <Button className="excel-col-add mk-green" title="添加显示列字段" onClick={this.columnFieldInput}>
              同步显示列
            </Button>
            <Button className="excel-col-add mk-red" title="清空Excel列" onClick={this.clearField}>
              清空Excel列
            </Button>
            <Table
              bordered
              rowKey="uuid"
              className="custom-table"
              dataSource={verify.columns}
              columns={excelColumns}
              pagination={false}
            />
          </TabPane>
          {card.intertype === 'system' ? <TabPane tab={
            <span>
              自定义脚本
              {verify.enable === 'true' ? <span className="count-tip">1</span> : null}
            </span>
          } key="6">
            <Form {...formItemLayout} className="verify-form">
              <Row gutter={24}>
                <Col span={8}>
                  <Form.Item style={{marginBottom: 10}} label={'启用'}>
                    <Radio.Group defaultValue={verify.enable || 'false'} onChange={this.changeEnable}>
                      <Radio value="true">是</Radio>
                      <Radio value="false">否</Radio>
                    </Radio.Group>
                  </Form.Item>
                </Col>
                <Col span={24} className="sql">
                  <Form.Item label={'sql'}>
                    {getFieldDecorator('sql', {
                      initialValue: verify.script || defaultscript,
                      rules: [
                        {
                          required: true,
                          message: this.props.dict['form.required.input'] + 'sql!'
                        }
                      ]
                    })(<CodeMirror />)}
                  </Form.Item>
                </Col>
              </Row>
            </Form>
          </TabPane> : null}
          <TabPane tab="信息提示" key="7">
            <Form {...formItemLayout}>
              <Row gutter={24}>
                <Col offset={6} span={6}>
                  <Form.Item label={'提示编码'}>
                    <span className="errorval"> S </span>
                    <Button onClick={() => {this.showError('S')}} type="primary" size="small">
                      查看
                    </Button>
                  </Form.Item>
                </Col>
                <Col span={8}>
                  <Form.Item label={'停留时间'}>
                    <InputNumber defaultValue={verify.stime || 2} min={1} max={10000} precision={0} onChange={(val) => {this.timeChange(val, 'stime')}} />
                  </Form.Item>
                </Col>
              </Row>
              <Row gutter={24}>
                <Col offset={6} span={6}>
                  <Form.Item label={'提示编码'}>
                    <span className="errorval"> Y </span>
                    <Button onClick={() => {this.showError('Y')}} type="primary" size="small">
                      查看
                    </Button>
                  </Form.Item>
                </Col>
              </Row>
              <Row gutter={24}>
                <Col offset={6} span={6}>
                  <Form.Item label={'提示编码'}>
                    <span className="errorval"> N </span>
                    <Button onClick={() => {this.showError('N')}} type="primary" size="small">
                      查看
                    </Button>
                  </Form.Item>
                </Col>
                <Col span={8}>
                  <Form.Item label={'停留时间'}>
                    <InputNumber defaultValue={verify.ntime || 10} min={1} max={10000} precision={0} onChange={(val) => {this.timeChange(val, 'ntime')}} />
                  </Form.Item>
                </Col>
              </Row>
              <Row gutter={24}>
                <Col offset={6} span={6}>
                  <Form.Item label={'提示编码'}>
                    <span className="errorval"> F </span>
                    <Button onClick={() => {this.showError('F')}} type="primary" size="small">
                      查看
                    </Button>
                  </Form.Item>
                </Col>
                <Col span={8}>
                  <Form.Item label={'停留时间'}>
                    <InputNumber defaultValue={verify.ftime || 10} min={1} max={10000} precision={0} onChange={(val) => {this.timeChange(val, 'ftime')}} />
                  </Form.Item>
                </Col>
              </Row>
              <Row gutter={24}>
                <Col offset={6} span={6}>
                  <Form.Item label={'提示编码'}>
                    <span className="errorval"> E </span>
                    <Button onClick={() => {this.showError('E')}} type="primary" size="small">
                      查看
                    </Button>
                  </Form.Item>
                </Col>
              </Row>
              <Row gutter={24}>
                <Col offset={6} span={6}>
                  <Form.Item label={'提示编码'}>
                    <span className="errorval"> NM </span>
                    <Button onClick={() => {this.showError('NM')}} type="primary" size="small">
                      查看
                    </Button>
                  </Form.Item>
                </Col>
              </Row>
              <Row gutter={24}>
                <Col offset={6} span={6}>
                  <Form.Item label={'提示编码'}>
                    <span className="errorval"> -1 </span>
                    不提示
                  </Form.Item>
                </Col>
              </Row>
            </Form>
          </TabPane>
        </Tabs>
      </div>
    )
  }
}
 
export default Form.create()(VerifyCard)