king
2024-02-03 06a670976e2145a10ea05207041d3cf3164cd380
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Form, Row, Col, Input, Select, InputNumber, Radio, Tooltip, Modal, notification, Popover } from 'antd'
import { QuestionCircleOutlined } from '@ant-design/icons'
 
import Api from '@/api'
import Utils from '@/utils/utils.js'
import { checkSQL } from '@/utils/utils-custom.js'
import { getColumnForm } from './formconfig'
import asyncComponent from '@/utils/asyncComponent'
import { formRule } from '@/utils/option.js'
import CodeMirror from '@/templates/zshare/codemirror'
import EditTable from '@/templates/zshare/modalform/modaleditable'
import './index.scss'
 
const FieldsTable = asyncComponent(() => import('@/templates/zshare/editTable'))
 
const { TextArea } = Input
const columnTypeOptions = {
  text: ['label', 'field', 'type', 'Align', 'Hide', 'IsSort', 'Width', 'prefix', 'postfix', 'textFormat', 'editable', 'initval', 'blacklist'],
  number: ['label', 'field', 'type', 'Align', 'Hide', 'IsSort', 'Width', 'decimal', 'format', 'prefix', 'postfix', 'editable', 'initval', 'sum', 'blacklist'],
  textarea: ['label', 'field', 'type', 'Align', 'Hide', 'Width', 'prefix', 'initval', 'postfix', 'blacklist'],
  custom: ['label', 'type', 'Align', 'Width', 'blacklist', 'IsSort'],
  colspan: ['label', 'type', 'Align', 'Hide', 'blacklist'],
  action: ['label', 'type', 'Align', 'Width'],
  formula: ['label', 'type', 'Align', 'Hide', 'Width', 'prefix', 'postfix', 'eval', 'formula', 'blacklist'],
  index: ['label', 'type', 'Align', 'Width']
}
 
class EdiTableColumn extends Component {
  static propTpyes = {
    column: PropTypes.object,
    wrap: PropTypes.object,
    columns: PropTypes.array,
    fields: PropTypes.array,
    submitCol: PropTypes.func,  // 提交事件
    cancelCol: PropTypes.func   // 取消时删除事件
  }
 
  state = {
    visible: false,
    loading: false,
    formlist: null,
    transfield: {}
  }
 
  record = null
 
  UNSAFE_componentWillMount() {
    let transfield = {}
    this.props.columns.forEach(item => {
      transfield[item.field] = item.label
    })
 
    this.setState({transfield})
  }
 
  UNSAFE_componentWillReceiveProps (nextProps) {
    if (nextProps.column && !is(fromJS(this.props.column), fromJS(nextProps.column))) {
      this.editColumn(nextProps.column)
    }
  }
 
  getOptions = () => {
    let _options = fromJS(columnTypeOptions[this.record.type]).toJS()
 
    if (['number', 'text'].includes(this.record.type) && this.record.editable === 'true') {
      _options.push('ctrlField')
      if (this.record.ctrlField) {
        _options.push('ctrlValue')
      }
      if (this.record.type === 'text') {
        _options.push('editType')
 
        if (this.record.editType === 'switch') {
          _options.push('enter', 'openVal', 'closeVal', 'openText', 'closeText')
        } else if (this.record.editType === 'date') {
          _options.push('required', 'precision', 'enter', 'declareType')
        } else if (this.record.editType === 'popSelect') {
          _options.push('required', 'enter', 'linkSubField', 'columns', 'dataSource', 'primaryKey', 'order', 'controlField', 'searchKey', 'popWidth', 'laypage', 'cache', 'onload')
        } else if (this.record.editType === 'select') {
          _options.push('required', 'enter', 'resourceType', 'linkSubField', 'dropdown')
 
          if (this.record.resourceType === '0') {
            _options.push('options')
          } else {
            _options.push('dataSource', 'valueField', 'valueText', 'orderBy', 'orderType', 'disableField', 'database')
          }
        } else {
          _options.push('required', 'enter')
        }
      } else if (this.record.type === 'number') {
        _options.push('max', 'min', 'enter', 'clearField')
      }
    }
    if (this.record.type === 'formula' && this.record.eval === 'true') {
      _options.push('decimal')
    } else if (this.record.type === 'custom' && this.record.IsSort === 'true') {
      _options.push('sortField')
    }
    if (['number', 'formula'].includes(this.record.type) && this.record.Hide !== 'true') {
      _options.push('noValue')
    }
 
    return _options
  }
 
  editColumn = (column) => {
    let fields = fromJS(this.props.fields).toJS().map(item => {
      item.text = `${item.field}(${item.label})`
      return item
    })
 
    let formlist = getColumnForm(column, fields, this.props.columns, this.props.wrap)
    this.record = {}
 
    formlist.forEach(item => {
      this.record[item.key] = item.initVal
    })
    
    let _options = this.getOptions()
 
    this.setState({
      visible: true,
      formlist: formlist.map(item => {
        item.hidden = !_options.includes(item.key)
 
        if (item.key === 'formula') {
          item.fields = this.props.fields.map(col => col.field)
          item.fields = item.fields.join(', ')
        }
 
        return item
      })
    })
    if (column.focus) {
      setTimeout(() => {
        try {
          let _form = document.getElementById('label')
          _form && _form.select()
        } catch (e) {
          console.warn('表单focus失败!')
        }
      }, 200)
    }
  }
 
  typeChange = (key, value, option) => {
    this.record[key] = value
 
    if (key === 'type') {
      if (['textarea', 'custom'].includes(value)) {
        this.record.IsSort = 'false'
      }
 
      let _options = this.getOptions()
 
      let _field = ''
      if (value === 'formula') {
        _field = this.props.form.getFieldValue('field') || ''
      }
 
      this.setState({
        formlist: this.state.formlist.map(item => {
          if (item.key === 'decimal' && value === 'formula') {
            this.record.decimal = ''
          }
 
          item.initVal = this.record[item.key]
          item.hidden = !_options.includes(item.key)
 
          return item
        })
      }, () => {
        if (['textarea', 'custom'].includes(value)) {
          this.props.form.setFieldsValue({IsSort: 'false'})
        } else if (value === 'colspan') {
          this.props.form.setFieldsValue({Align: 'center'})
        } else if (value === 'formula' && _field) {
          this.props.form.setFieldsValue({formula: '@' + _field + '@'})
        } else if (value === 'index') {
          this.props.form.setFieldsValue({label: '序号'})
        }
      })
    } else if (key === 'field') {
      let values = {label: option.props.label || option.props.children}
      if (/Decimal|int/ig.test(option.props.datatype)) {
        let decimal = 0
        if (/Decimal/ig.test(option.props.datatype)) {
          decimal = +option.props.datatype.replace(/Decimal\(18,/ig, '').replace(')', '')
        }
        values.type = 'number'
        values.decimal = decimal
      } else {
        values.type = 'text'
      }
 
      let _type = this.record.type
      this.record.type = values.type
 
      if (values.type !== _type) {
        let _options = this.getOptions()
 
        this.setState({
          formlist: this.state.formlist.map(item => {
            item.initVal = this.record[item.key]
            item.hidden = !_options.includes(item.key)
 
            return item
          })
        }, () => {
          this.props.form.setFieldsValue(values)
        })
      } else {
        this.props.form.setFieldsValue(values)
      }
    } else if (key === 'format' && value === 'percent') {
      this.props.form.setFieldsValue({postfix: '%'})
    } else if (key === 'editType') {
      let _options = this.getOptions()
 
      this.setState({
        formlist: this.state.formlist.map(item => {
          if (item.key === 'enter' && item.options && item.options[item.options.length - 1].field === '$noActX') {
            item.options[item.options.length - 1].disabled = value !== 'select'
          }
 
          item.initVal = this.record[item.key]
          item.hidden = !_options.includes(item.key)
 
          return item
        })
      })
    } else if (['editable', 'editType', 'resourceType', 'ctrlField', 'eval', 'Hide', 'IsSort'].includes(key)) {
      let _options = this.getOptions()
 
      this.setState({
        formlist: this.state.formlist.map(item => {
          item.initVal = this.record[item.key]
          item.hidden = !_options.includes(item.key)
 
          return item
        })
      })
    }
  }
 
  multiselectChange = (key, value) => {
    if (key !== 'linkSubField') return
 
    this.record[key] = value
  }
 
  handleEmpty = () => {
    let field = this.props.form.getFieldValue('valueField')
 
    if (!field) {
      notification.warning({
        top: 92,
        message: '请填写值·字段。',
        duration: 5
      })
      return
    }
 
    let text = this.props.form.getFieldValue('valueText')
 
    if (!text) {
      notification.warning({
        top: 92,
        message: '请填写文本·字段。',
        duration: 5
      })
      return
    }
 
    let resource = this.props.form.getFieldValue('dataSource') || ''
 
    if (field === text) {
      resource = `select '' as ${field} union all \n${resource}`
    } else {
      resource = `select '' as ${field},'全部' as ${text} union all \n${resource}`
    }
 
    this.props.form.setFieldsValue({dataSource: resource})
  }
 
  changeOptions = (data, key) => {
    this.record[key] = data || []
  }
 
  getFields() {
    const { getFieldDecorator } = this.props.form
    const { formlist, transfield } = this.state
    const fields = []
 
    if (!formlist) return null
 
    formlist.forEach((item, index) => {
      if (item.hidden || item.forbidden) return
 
      let span = 12
      let rules = []
      let className = ''
      let content = null
      let extra = null
      let initVal = item.initVal || ''
 
      if (item.type === 'text') {
        rules = [
          { required: item.required, message: '请输入' + item.label + '!' },
          {
            max: formRule.input.max,
            message: formRule.input.message
          }
        ]
 
        content = <Input placeholder="" autoComplete="off" onPressEnter={this.handleSubmit} />
      } else if (item.type === 'number') {
        rules = [
          { required: item.required, message: '请输入' + item.label + '!' }
        ]
        initVal = item.initVal
 
        if (!item.unlimit) {
          content = <InputNumber min={item.min} max={item.max} precision={item.precision} onPressEnter={this.handleSubmit}/>
        } else {
          content = <InputNumber onPressEnter={this.handleSubmit}/>
        }
      } else if (item.type === 'select') {
        rules = [
          { required: item.required, message: '请选择' + item.label + '!' }
        ]
 
        let options = item.options
        if (typeof(item.options) === 'string') {
          options = this.record[item.options] || []
        }
 
        content = <Select
          showSearch
          allowClear={item.allowClear === true}
          filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
          onChange={(value, option) => {this.typeChange(item.key, value, option)}}
          getPopupContainer={() => document.getElementById('edit-table-column-winter')}
        >
          {options.map((option, i) =>
            <Select.Option key={i} disabled={option.disabled === true} datatype={option.datatype || ''} label={option.label || ''} value={(option.value || option.field || option.MenuID)}>
              {(option.text || option.label || option.MenuName)}
            </Select.Option>
          )}
        </Select>
      } else if (item.type === 'radio') {
        rules = [
          { required: item.required, message: '请选择' + item.label + '!' }
        ]
        initVal = item.initVal
 
        content = <Radio.Group onChange={(e) => {this.typeChange(item.key, e.target.value)}}>
          {item.options.map(option => <Radio key={option.value} value={option.value}>{option.text}</Radio>)}
        </Radio.Group>
      } else if (item.type === 'multiselect') { // 多选
        content = <Select
          showSearch
          mode="multiple"
          filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
          onChange={(value) => this.multiselectChange(item.key, value)}
        >
          {item.options.map((option, i) =>
            <Select.Option key={i} value={option.value || option.field}>{option.text || option.label}</Select.Option>
          )}
        </Select>
      } else if (item.type === 'textarea') {
        span = 24
        rules = [
          { required: item.required, message: '请输入' + item.label + '!' }
        ]
 
        if (item.key === 'formula') {
          fields.push(
            <Col span={span} key={index}>
              <Form.Item className={className} extra={extra} label={item.tooltip ?
                <Tooltip placement="topLeft" title={item.tooltip}>
                  <QuestionCircleOutlined className="mk-form-tip" />
                  {item.label}
                </Tooltip> : item.label
              }>
                {getFieldDecorator(item.key, {
                  initialValue: initVal,
                  rules: rules
                })(<TextArea rows={item.rows || 2}/>)}
              </Form.Item>
              <Popover overlayClassName="formula-fields" placement="topLeft" title="" content={<div>{item.fields}</div>} trigger="click">
                <span className="formula-icon">字段集</span>
              </Popover>
            </Col>
          )
          return
        }
 
        content = <TextArea rows={item.rows || 2}/>
      } else if (item.type === 'codemirror') {
        rules = [
          { required: item.required, message: '请输入' + item.label + '!' }
        ]
        span = 24
 
        if (this.record.editType !== 'popSelect') {
          extra = <span className="add-resource-empty" onClick={this.handleEmpty}>空</span>
        }
        if (item.placeholder) {
          extra = <><span className="resource-public-var">{item.placeholder}</span>{extra}</>
        }
 
        content = <CodeMirror />
      } else if (item.type === 'options') {
        span = 24
 
        let linkSubFields = this.record.linkSubField || []
 
        let columns = []
 
        columns.push({ title: 'Value', key: 'Value', strict: true })
        columns.push({ title: 'Text', key: 'Text' })
 
        linkSubFields.forEach(field => {
          if (field === 'Value' || field === 'Text') return
 
          columns.push({ title: transfield[field] || field, key: field })
        })
      
        content = <EditTable columns={columns} module="form" onChange={(data) => this.changeOptions(data, item.key)}/>
      } else if (item.type === 'fields') {
        span = 24
        rules = [
          { required: item.required, message: '请添加' + item.label + '!' }
        ]
 
        content = <FieldsTable indexShow={false} actions={['edit', 'move', 'del', 'add']} columns={item.columns} data={this.record[item.key] || []} onChange={(data) => this.changeOptions(data, item.key)}/>
      }
 
      fields.push(
        <Col span={span} key={index}>
          <Form.Item className={className} extra={extra} label={item.tooltip ?
            <Tooltip placement="topLeft" title={item.tooltip}>
              <QuestionCircleOutlined className="mk-form-tip" />
              {item.label}
            </Tooltip> : item.label
          }>
            {getFieldDecorator(item.key, {
              initialValue: initVal,
              rules: rules
            })(content)}
          </Form.Item>
        </Col>
      )
    })
    return fields
  }
 
  transfer = (options) => {
    if (options.length === 0) return options
 
    let isNumber = true
    options.forEach(item => {
      if (!item.Value || isNaN(item.Value)) {
        isNumber = false
      }
    })
 
    if (isNumber) {
      return options.map(item => {
        item.Value = +item.Value
        return item
      })
    } else {
      return options.map(item => {
        item.Value = item.Value + ''
        return item
      })
    }
  }
 
  handleSubmit = () => {
    const { fields } = this.props
    // 表单提交时检查输入值是否正确
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        if (values.type === 'number' && values.editable === 'true') {
          if (typeof(values.max) === 'number' && typeof(values.min) === 'number' && values.max < values.min) {
            notification.warning({
              top: 92,
              message: '最大值不可小于最小值!',
              duration: 5
            })
            return
          }
          // eslint-disable-next-line
        } else if (values.type === 'formula' && values.eval === 'true' && /^[\u4E00-\u9FA50-9a-zA-Z_\s@\+\-\*\/]*$/ig.test(values.formula) && /[\+\-\*\/]/ig.test(values.formula)) {
          let cols = []
          fields.forEach(col => {
            if (/^(Int|Decimal)/ig.test(col.datatype)) {
              cols.push({reg: new RegExp('@' + col.field + '@', 'ig'), value: `(@${col.field}@)`})
            }
          })
 
          cols.forEach(col => {
            values.formula = values.formula.replace(col.reg, col.value)
          })
        } else if (values.type === 'text' && values.editable === 'true' && values.editType === 'select') {
          if (values.resourceType === '0') {
            values.options = values.options || []
            
            values.options = this.transfer(values.options)
 
            if (values.options.filter(op => op.Text === '').length > 0) {
              notification.warning({
                top: 92,
                message: '提示文本(Text)不可为空!',
                duration: 5
              })
              return
            } else {
              let arr = values.options.map(m => m.Value)
              let _arr = Array.from(new Set(arr))
              if (arr.length > _arr.length) {
                notification.warning({
                  top: 92,
                  message: 'Value值不可重复!',
                  duration: 5
                })
                return
              }
            }
          }
        }
 
        if (values.type === 'text' && values.editable === 'true') {
          if (values.editType !== 'select' && values.enter === '$noActX') {
            values.enter = '$noAct'
          }
        }
 
        if (values.dataSource) {
          let pass = checkSQL(values.dataSource)
 
          if (!pass) return
        }
 
        if (values.editType === 'select' && values.resourceType === '1' && values.dataSource) {
          let _option = Utils.getSelectQueryOptions(values)
 
          let sql = `declare @mk_departmentcode nvarchar(512),@mk_organization nvarchar(512),@mk_user_type nvarchar(20)
          ${_option.sql}`
  
          // LoginUID|SessionUid|UserID|Appkey 已替换
          sql = sql.replace(/@\$|\$@/ig, '').replace(/@(BID|ID|time_id)@/ig, `'1949-10-01 15:00:00'`)
  
          let rduri = ''
          if (window.GLOB.mainSystemApi && values.database === 'sso') {
            rduri = window.GLOB.mainSystemApi
          }
          
          Api.sDebug(sql, rduri).then(result => {
            if (result.status || result.ErrCode === '-2') {
              this.setState({visible: false, loading: false, formlist: null})
              this.props.submitCol(values)
              this.record = null
            } else {
              this.setState({loading: false})
              Modal.error({
                title: result.message
              })
            }
          })
        } else if (values.editType === 'popSelect' && values.dataSource) {
          let arrfield = values.columns.map(f => f.field)
 
          if (values.linkSubField && values.linkSubField.length > 0) {
            values.linkSubField.forEach(n => {
              if (!arrfield.includes(n)) {
                arrfield.push(n)
              }
            })
          }
 
          let _datasource = values.dataSource
          let sql = ''
 
          if (/\s/.test(_datasource)) { // 拼接别名
            _datasource = '(' + _datasource + ') tb'
          }
 
          arrfield = arrfield.join(',')
 
          let _search = ''
 
          if (values.searchKey) {
            let fields = values.searchKey.split(',').map(field => field + ' like \'%mk%\'')
            _search = 'where ' + fields.join(' OR ')
          }
 
          if (values.laypage === 'true') {
            sql = `/*system_query*/select top 10 ${arrfield} from (select ${arrfield} ,ROW_NUMBER() over(order by ${values.order}) as rows from ${_datasource} ${_search}) tmptable where rows > 0 order by tmptable.rows `
          } else if (values.order) {
            sql = `/*system_query*/select ${arrfield} from (select ${arrfield} ,ROW_NUMBER() over(order by ${values.order}) as rows from ${_datasource} ${_search}) tmptable order by tmptable.rows `
          } else {
            sql = `/*system_query*/select ${arrfield} from ${_datasource} ${_search}  `
          }
 
          sql = `declare @mk_departmentcode nvarchar(512),@mk_organization nvarchar(512),@mk_user_type nvarchar(20)
            ${sql}`
 
          sql = sql.replace(/@\$|\$@/ig, '').replace(/@datam@/ig, '\'\'')
          sql = sql.replace(/@LoginUID@/ig, `'${sessionStorage.getItem('LoginUID') || ''}'`)
          sql = sql.replace(/@SessionUid@/ig, `'${localStorage.getItem('SessionUid') || ''}'`)
          sql = sql.replace(/@UserID@/ig, `'${sessionStorage.getItem('UserID') || ''}'`)
          sql = sql.replace(/@Appkey@/ig, `'${window.GLOB.appkey || ''}'`)
 
          Api.sDebug(sql).then(result => {
            if (result.status || result.ErrCode === '-2') {
              this.setState({visible: false, loading: false, formlist: null})
              this.props.submitCol(values)
              this.record = null
            } else {
              this.setState({loading: false})
              Modal.error({
                title: result.message
              })
            }
          })
        } else {
          this.setState({visible: false, formlist: null})
          this.props.submitCol(values)
          this.record = null
        }
      }
    })
  }
 
  editModalCancel = () => {
    this.setState({visible: false, loading: false, formlist: null})
 
    this.props.cancelCol()
  }
 
  render() {
    const { visible, loading } = this.state
    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 18 }
      }
    }
 
    return (
      <div style={{display: 'inline-block'}}>
        <Modal
          title="显示列编辑"
          wrapClassName="mk-scroll-modal"
          visible={visible}
          width={900}
          maskClosable={false}
          onOk={this.handleSubmit}
          onCancel={this.editModalCancel}
          confirmLoading={loading}
          destroyOnClose
        >
          <Form {...formItemLayout} className="commontable-column-form" id="edit-table-column-winter">
            <Row gutter={24}>{this.getFields()}</Row>
          </Form>
        </Modal>
      </div>
    )
  }
}
 
export default Form.create()(EdiTableColumn)