king
2025-02-06 eed746580d059ab1bb01fec5a4c117a82c861ff2
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
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Button, Modal, Empty, notification } from 'antd'
 
import Api from '@/api'
import Utils from '@/utils/utils.js'
import EditCard from './editcard'
 
import MKEmitter from '@/utils/events.js'
import './index.scss'
 
class FieldsComponent extends Component {
  static propTpyes = {
    type: PropTypes.string,
    config: PropTypes.object,
    updatefield: PropTypes.func
  }
 
  state = {
    appType: sessionStorage.getItem('appType'),
    fields: [],             // 字段集
    visible: false,         // 模态框控制
    selectCards: []
  }
 
  checkField = () => {
    if (!window.GLOB.publicTables || window.GLOB.publicTables.length === 0) {
      notification.warning({
        top: 92,
        message: '请选择表名!',
        duration: 5
      })
      return
    }
 
    window.GLOB.tableFields = window.GLOB.tableFields || []
 
    let index = 0
 
    let deffers = window.GLOB.publicTables.map(item => {
      let tb = window.GLOB.tableFields.filter(tab => tab.tableName === item.TbName)[0]
 
      if (tb) {
        return Promise.resolve(fromJS(tb).toJS())
      }
 
      return new Promise(resolve => {
        setTimeout(() => {
          Api.getCloudConfig({func: 'sPC_Get_FieldName', TBName: item.TbName}).then(res => {
            let tabmsg = {
              status: res.status,
              message: res.message,
              tableName: item.TbName,
              columns: []
            }
 
            if (res.FDName && res.FDName.length > 0) {
              tabmsg.columns = res.FDName.map(item => {
                let _type = item.FieldType.toLowerCase()
                let _datatype = item.FieldType.toLowerCase()
                let _decimal = 0
                let _length = 50
                if (/^nvarchar/.test(_type)) {
                  try { // 存在max
                    _length = +_type.match(/\d+/)[0] || 50
                  } catch (e) {
                    _length = 4000
                  }
                  _type = 'text'
                } else if (/^int/.test(_type)) {
                  _type = 'number'
                } else if (/^decimal/.test(_type)) {
                  _decimal = _type.split(',')[1]
                  _decimal = parseInt(_decimal)
                  _type = 'number'
                } else if (/^datetime/.test(_type)) {
                  _type = 'datetime'
                } else if (/^date/.test(_type)) {
                  _type = 'date'
                } else {
                  _type = 'text'
                }
 
                if (/^nvarchar/.test(_datatype)) {
                  _datatype = _datatype.replace(/^nvarchar/, 'Nvarchar')
                } else if (/^decimal/.test(_datatype)) {
                  _datatype = _datatype.replace(/^decimal/, 'Decimal')
                } else if (/^int/.test(_datatype)) {
                  _datatype = _datatype.replace(/^int/, 'Int')
                }
    
                return {
                  field: item.FieldName || '',
                  label: item.FieldDec,
                  type: _type,
                  datatype: _type,
                  decimal: _decimal,
                  length: _length,
                  $datatype: _datatype
                }
              })
            }
 
            resolve(tabmsg)
          })
        }, index * 50)
 
        index++
      })
    })
    Promise.all(deffers).then(response => {
      let error = false
      let _columns = response
 
      response.forEach(item => {
        if (!item.status) {
          error = item.message || '字段查询失败!'
        }
      })
 
      if (error) {
        notification.warning({
          top: 92,
          message: error,
          duration: 5
        })
      } else {
        window.GLOB.tableFields = _columns
        this.queryField(_columns)
      }
    })
  }
 
  queryField = (tableFields) => {
    const { type, config } = this.props
 
    // 表字段集转为map数据
    let columns = new Map()
    tableFields.forEach(table => {
      table.columns.forEach(column => {
        columns.set(column.field.toLowerCase(), column)
      })
    })
 
    if (type === 'search') {
      // 添加搜索条件,字段集中存在搜索条件字段,使用搜索条件对象替换字段集,设置数据类型
      config.search.forEach(item => {
        if (item.field) {
          if (/,/.test(item.field)) {
            item.field.split(',').forEach(n => {
              if (columns.has(n.toLowerCase())) {
                let _datatype = columns.get(n.toLowerCase()).datatype
                columns.set(n.toLowerCase(), {...item, field: n, origin: true, datatype: _datatype})
              }
            })
          } else {
            if (columns.has(item.field.toLowerCase())) {
              let _datatype = columns.get(item.field.toLowerCase()).datatype
              columns.set(item.field.toLowerCase(), {...item, origin: true, datatype: _datatype})
            }
          }
        }
      })
    } else if (type === 'columns') {
      // 添加显示列,字段集中存在显示列字段,使用显示列对象替换字段集,设置数据类型
      config.columns.forEach(item => {
        if (item.field && columns.has(item.field.toLowerCase())) {
          let _datatype = columns.get(item.field.toLowerCase()).datatype
          columns.set(item.field.toLowerCase(), {...item, origin: true, datatype: _datatype})
        }
      })
    } else if (type === 'fields') {
      config.columns.forEach(item => {
        if (columns.has(item.field.toLowerCase())) {
          let _datatype = columns.get(item.field.toLowerCase()).datatype
          columns.set(item.field.toLowerCase(), {...item, origin: true, datatype: _datatype})
        }
      })
    } else if (type === 'form') {
      config.fields.forEach(item => {
        if (item.field && columns.has(item.field.toLowerCase())) {
          let _datatype = columns.get(item.field.toLowerCase()).datatype
          columns.set(item.field.toLowerCase(), {...item, origin: true, datatype: _datatype})
        }
      })
    }
 
    // 显示字段集弹窗
    this.setState({
      visible: true,
      selectCards: [],
      fields: [...columns.values()]
    })
  }
 
  addFieldSubmit = () => {
    const { type } = this.props
    const { selectCards, fields } = this.state
    // 字段集为空,关闭弹窗
    if (!fields || fields.length === 0) {
      this.setState({ visible: false })
    }
 
    let config = fromJS(this.props.config).toJS()
 
    let items = []
    let keys = []
    if (type === 'search') {
      selectCards.forEach(item => {
        let _match = ''
        let initval = ''
        let _type = item.type
        if (item.type === 'date') {
          _type = 'daterange'
        } else if (item.type === 'select') {
          _match = '='
        } else {
          _type = 'text'
          _match = 'like'
        }
 
        let newcard = {
          uuid: Utils.getuuid(),
          label: item.label,
          field: item.field,
          initval: initval,
          type: _type,
          resourceType: '0',
          options: [],
          orderType: 'asc',
          match: _match,
        }
 
        items.push(newcard)
        keys.push(item.field.toLowerCase())
      })
      MKEmitter.emit('plusSearch', config.uuid, items, 'multil')
    } else if (type === 'columns') {
      selectCards.forEach(item => {
        let newcard = {
          uuid: Utils.getuuid(),
          Align: 'left',
          label: item.label,
          field: item.field,
          Hide: 'false',
          IsSort: 'false',
          type: item.type === 'number' ? 'number' : 'text',
          Width: item.type === 'number' ? 80 : 120
        }
 
        if (item.type === 'number') {
          newcard.decimal = item.decimal
        } else {
          newcard.fieldlength = item.length || 50
        }
 
        if (item.type === 'date') {
          newcard.textFormat = 'YYYY-MM-DD'
        } else if (item.type === 'datetime') {
          newcard.textFormat = 'YYYY-MM-DD HH:mm:ss'
        }
 
        items.push(newcard)
        keys.push(item.field.toLowerCase())
      })
      MKEmitter.emit('plusColumns', config.uuid, items)
    } else if (type === 'fields') {
      items = [...config.columns]
 
      selectCards.forEach(item => {
        let _t = item.$datatype || (item.type === 'number' ? 'Decimal(18,0)' : 'Nvarchar(50)')
        let newcard = {
          uuid: Utils.getuuid(),
          label: item.label,
          field: item.field,
          datatype: _t
        }
 
        items.unshift(newcard)
        keys.push(item.field.toLowerCase())
      })
 
      this.props.updatefield(items)
    } else if (type === 'form') {
      let firstItem = config.fields[0]
      let span = this.state.appType === 'mob' ? 24 : 12
      let labelwidth = 33.3
      if (firstItem && firstItem.span) {
        span = firstItem.span
        labelwidth = firstItem.labelwidth || 33.3
      }
      selectCards.forEach(item => { // 循环添加新增字段
        let newcard = {
          uuid: Utils.getuuid(),
          label: item.label,
          field: item.field,
          initval: item.type === 'number' ? 0 : '',
          type: item.type,
          resourceType: '0',
          span: span,
          labelwidth: labelwidth,
          options: [],
          dataSource: '',
          decimal: item.decimal,
          orderType: 'asc',
          readonly: 'false',
          required: 'true',
          readin: 'top'
        }
 
        if (/^icon|images?$/ig.test(item.field)) {
          newcard.type = 'fileupload'
          newcard.fileType = 'picture-card'
          newcard.fieldlength = item.length || 512
          newcard.maxSize = 1
          newcard.maxfile = 1
        } else if (item.type === 'text' && item.length >= 256) {
          newcard.type = 'textarea'
          newcard.required = 'false'
          newcard.fieldlength = item.length
          if (firstItem) {
            if (firstItem.type === newcard.type) {
              newcard.span = firstItem.span
              newcard.labelwidth = firstItem.labelwidth
            } else {
              newcard.span = 24
              if (firstItem.span === 12) {
                newcard.labelwidth = 16.2
              } else if (firstItem.span === 8) {
                newcard.labelwidth = 10.5
              } else if (firstItem.span === 6) {
                newcard.labelwidth = 7.7
              }
            }
          } else {
            newcard.span = 24
            newcard.labelwidth = 16.2
          }
        } else if (item.type === 'text') {
          newcard.fieldlength = item.length || 50
        }
 
        items.push(newcard)
        keys.push(item.field.toLowerCase())
      })
      
      this.props.plusFields(items)
    }
 
    this.setState({
      selectCards: [],
      fields: fromJS(fields).toJS().map(cell => {
        if (keys.includes(cell.field.toLowerCase())) {
          cell.origin = true
        }
        return cell
      })
    })
 
    notification.success({
      top: 92,
      message: '操作成功',
      duration: 2
    })
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.state), fromJS(nextState))
  }
 
  /**
   * @description 组件销毁,清除state更新
   */
  componentWillUnmount () {
    this.setState = () => {
      return
    }
  }
 
  onChange = (selectCards) => {
    this.setState({selectCards})
  }
 
  render() {
    const { type } = this.props
    const { fields } = this.state
 
    let label = '批量添加'
    if (type === 'search') {
      label = '添加搜索'
    } else if (type === 'columns') {
      label = '添加显示列'
    }
 
    return (
      <div className="quickly-add">
        <Button type="primary" block onClick={this.checkField}>{label}</Button>
        {/* 根据字段名添加显示列及搜索条件 */}
        <Modal
          wrapClassName="model-table-fieldmanage-modal"
          title="编辑"
          visible={this.state.visible}
          width={'65vw'}
          maskClosable={false}
          cancelText="关闭"
          onOk={this.addFieldSubmit}
          onCancel={() => this.setState({ visible: false })}
          destroyOnClose
        >
          {fields.length > 0 ? <EditCard data={fields} onChange={this.onChange} type={type} /> : <Empty />}
        </Modal>
      </div>
    )
  }
}
 
export default FieldsComponent