king
2020-06-16 6c16e43cd6521460c804391c042348dbb14086fc
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
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { notification, Switch} from 'antd'
import moment from 'moment'
 
import Api from '@/api'
import zhCN from '@/locales/zh-CN/main.js'
import enUS from '@/locales/en-US/main.js'
import Utils from '@/utils/utils.js'
import options from '@/store/options.js'
import { buttonConfig, tabConfig } from '../config'
 
import asyncSpinComponent from '@/utils/asyncSpinComponent'
import SubAction from '../actionList'
import SubSearch from '../topSearch'
 
import './index.scss'
 
const SubTable = asyncSpinComponent(() => import('@/tabviews/zshare/normalTable'))
 
class VerupSubTabViewTable extends Component {
  static propTpyes = {
    Tab: PropTypes.object,           // 标签信息
    BID: PropTypes.string,           // 上级数据ID
    BData: PropTypes.any,            // 上级数据
    MenuID: PropTypes.string,        // 菜单Id
    SupMenuID: PropTypes.string,     // 上级菜单Id
    ContainerId: PropTypes.any,      // 三级菜单Container(html) ID
    handleTableId: PropTypes.func,   // 控制表格数据切换时,更新在主表中的id
    handleMainTable: PropTypes.func, // 刷新主表
    refreshtabs:PropTypes.any
  }
 
  state = {
    dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
    config: null,         // 页面配置信息,包括按钮、搜索、显示列、标签等
    searchlist: null,     // 搜索条件
    actions: null,        // 按钮集
    columns: null,        // 显示列
    logcolumns: null,     // 日志中显示的列信息 (增加至全部列,除去合并列)
    arr_field: '',        // 使用 sPC_Get_TableData 时的查询字段集
    setting: null,        // 页面全局设置:数据源、按钮及显示列固定、主键等
    data: null,           // 列表数据集
    selectedData: [],     // 已选表格数据
    resetTable: false,    // 表格重置,值在true与false之间切换,切换时表格重置
    total: 0,             // 总数
    loading: false,       // 列表数据加载中
    pageIndex: 1,         // 页码
    pageSize: 10,         // 每页数据条数
    orderBy: '',          // 排序
    search: '',           // 搜索条件数组,使用时需分场景处理
    popAction: false,     // 弹框页面,按钮信息
    popData: false,       // 弹框页面,所选的表格数据
    visible: false,       // 弹框显示隐藏控制
    pickup: false,         // 子表数据隐藏显示切换
  }
 
  /**
   * @description 上级菜单id变化时,刷新数据
   */
  UNSAFE_componentWillReceiveProps(nextProps) {
    if (this.state.config && this.props.Tab.supMenu && !is(fromJS(this.props.BID), fromJS(nextProps.BID))) {
      this.setState({
        pageIndex: 1,
        selectedData: [],
        resetTable: !this.state.resetTable,
      }, () => {
        this.loadmaindata(nextProps.BID, 'refresh')
      })
    } else if (this.state.config && nextProps.refreshtabs && nextProps.refreshtabs.includes(this.props.Tab.uuid)) {
 
      this.reloadtable()
    }
  }
 
  /**
   * @description 获取页面配置信息
   */
  async loadconfig () {
    const { Tab, BID } = this.props
 
    let config = tabConfig[this.props.MenuID]
 
    let _arrField = []     // 字段集
    let _columns = []      // 显示列
    let _logcolumns = []   // 日志显示列
    let _hideCol = []      // 隐藏及合并列中字段的uuid集
    let colMap = new Map()
 
    // 1、筛选字段集,2、过滤隐藏列及合并列中的字段uuid
    config.columns.forEach(col => {
      if (col.field) {
        _arrField.push(col.field)
 
        _logcolumns.push(col)
      }
      if (col.type === 'colspan' && col.sublist) { // 筛选隐藏列
        _hideCol = _hideCol.concat(col.sublist)
      } else if (col.Hide === 'true') {
        _hideCol.push(col.uuid)
      }
      colMap.set(col.uuid, col)
    })
 
    // 生成显示列,处理合并列中的字段
    config.columns.forEach(col => {
      if (_hideCol.includes(col.uuid)) return
 
      if (col.type === 'colspan' && col.sublist) {
        let _col = JSON.parse(JSON.stringify(col))
        let subColumn = []
        _col.sublist.forEach(sub => {
          if (colMap.has(sub)) {
            subColumn.push(colMap.get(sub))
          }
        })
        _col.subColumn = subColumn
        _columns.push(_col)
      } else {
        _columns.push(col)
      }
    })
 
    this.setState({
      config: config,
      setting: config.setting,
      searchlist: config.search,
      actions: config.action.map(item => {
        if (buttonConfig[item.uuid]) {
          item = {...buttonConfig[item.uuid], ...item}
        }
        return item
      }),
      columns: _columns,
      logcolumns: _logcolumns,
      arr_field: _arrField.join(','),
      search: Utils.initMainSearch(config.search) // 搜索条件初始化(含有时间格式,需要转化)
    }, () => {
      if (config.setting.onload !== 'false' && (!Tab.supMenu || BID)) { // 初始化可加载
        this.setState({
          loading: true
        })
        this.loadmaindata()
      }
    })
  }
 
  /**
   * @description 子表数据加载
   */
  async loadmaindata (bid, type) {
    const { setting } = this.state
    let param = ''
    let _BID = this.props.BID
    
    if (type === 'refresh') {
      _BID = bid
      if (!bid) { // 主表ID不存在时,不查询子表
        this.setState({
          data: [],
          total: 0,
          loading: false
        })
 
        return
      }
    }
 
    if (setting.interType !== 'inner' || (setting.interType === 'inner' && setting.innerFunc)) {
      param = this.getCustomParam(_BID)
    } else {
      param = this.getDefaultParam(_BID)
    }
 
    this.handleTableId()
 
    let result = await Api.genericInterface(param)
    if (result.status) {
      this.setState({
        data: result.data.map((item, index) => {
          item.key = index
          return item
        }),
        total: result.total,
        pickup: false,
        loading: false
      })
    } else {
      this.setState({
        loading: false
      })
      notification.error({
        top: 92,
        message: result.message,
        duration: 10
      })
    }
  }
 
  /**
   * @description 获取用户自定义存储过程传参
   */
  getCustomParam = (BID) => {
    const { pageIndex, pageSize, orderBy, search, setting } = this.state
 
    let _search = Utils.formatCustomMainSearch(search)
 
    let param = {
      PageIndex: pageIndex,
      PageSize: pageSize,
      OrderCol: orderBy || setting.order,
      BID: BID,
      ..._search
    }
 
    if (setting.interType === 'inner') {
      param.func = setting.innerFunc
    } else {
      if (setting.sysInterface === 'true' && options.cloudServiceApi) {
        param.rduri = options.cloudServiceApi
      } else if (setting.sysInterface !== 'true') {
        param.rduri = setting.interface
      }
 
      if (setting.outerFunc) {
        param.func = setting.outerFunc
      }
    }
 
    return param
  }
 
  /**
   * @description 获取系统存储过程 sPC_Get_TableData 的参数
   */
  getDefaultParam = (BID) => {
    const { arr_field, pageIndex, pageSize, orderBy, search, setting } = this.state
 
    let _search = Utils.joinMainSearchkey(search)
    _search = _search ? 'where ' + _search : ''
 
    let param = {
      func: 'sPC_Get_TableData',
      obj_name: 'data',
      arr_field: arr_field,
      BID: BID
    }
 
    let _orderBy = orderBy || setting.order
    let _dataresource = setting.dataresource
 
    if (/\s/.test(_dataresource)) {
      _dataresource = '(' + _dataresource + ') tb'
    }
 
    if (setting.queryType === 'statistics') { // 统计数据源,内容替换
      let fieldmap = new Map()
      let options = search.map(item => {
        let _field = item.key
 
        if (fieldmap.has(_field)) {
          _field = _field + '1'
        }
 
        fieldmap.set(item.key, true)
 
        return {
          reg: new RegExp('@' + _field, 'ig'),
          value: item.value
        }
      })
 
      options.reverse()
 
      options.forEach(item => {
        _dataresource = _dataresource.replace(item.reg, `'${item.value}'`)
      })
 
      _search = ''
    }
 
    let LText = `select top ${pageSize} ${arr_field} from (select ${arr_field} ,ROW_NUMBER() over(order by ${_orderBy}) as rows from ${_dataresource} ${_search}) tmptable where rows > ${pageSize * (pageIndex - 1)} order by tmptable.rows`
    let DateCount = `select count(1) as total from ${_dataresource} ${_search}`
 
    param.LText = Utils.formatOptions(LText)
    param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss') + '.000'
    param.secretkey = Utils.encrypt(param.LText, param.timestamp)
    param.DateCount = Utils.formatOptions(DateCount)
 
    param.open_key = Utils.encrypt(param.secretkey, param.timestamp, true) // 云端数据验证
 
    return param
  }
 
  /**
   * @description 搜索条件改变时,重置表格数据
   * 含有初始不加载的页面,修改设置
   */
  refreshbysearch = (searches) => {
    this.setState({
      loading: true,
      pageIndex: 1,
      selectedData: [],
      search: searches,
      resetTable: !this.state.resetTable
    }, () => {
      this.loadmaindata()
    })
  }
 
  /**
   * @description 表格条件改变时重置数据(分页或排序)
   */
  refreshbytable = (pagination, filters, sorter) => {
    if (sorter.order) {
      let _chg = {
        ascend: 'asc',
        descend: 'desc'
      }
      sorter.order = _chg[sorter.order]
    }
 
    this.setState({
      loading: true,
      selectedData: [],
      pageIndex: pagination.current,
      pageSize: pagination.pageSize,
      orderBy: (sorter.field && sorter.order) ? `${sorter.field} ${sorter.order}` : ''
    }, () => {
      this.loadmaindata()
    })
  }
 
  /**
   * @description 表格刷新
   */
  reloadtable = () => {
    this.setState({
      loading: true,
      pageIndex: 1,
      selectedData: [],
      resetTable: !this.state.resetTable
    }, () => {
      this.loadmaindata()
    })
  }
 
  /**
   * @description 表格选择项切换
   */
  changeSelectedData = (selectedData) => {
    this.setState({selectedData})
  }
 
  /**
   * @description 页面刷新,重新获取配置
   */
  reloadview = () => {
    this.setState({
      config: null,
      searchlist: null,
      actions: null,
      columns: null,
      arr_field: '',
      setting: null,
      data: null,
      total: 0,
      loading: false,
      pageIndex: 1,
      pageSize: 10,
      orderBy: '',
      search: ''
    }, () => {
      this.loadconfig()
    })
  }
 
  /**
   * @description 按钮操作完成后(成功或失败),页面刷新,重置页码及选择项
   */
  refreshbyaction = (btn, type) => {
    if (btn.execSuccess === 'grid' && type === 'success') {
      this.reloadtable()
    } else if (btn.execError === 'grid' && type === 'error') {
      this.reloadtable()
    } else if (btn.execSuccess === 'view' && type === 'success') {
      this.reloadview()
    } else if (btn.execError === 'view' && type === 'error') {
      this.reloadview()
    } else if (btn.popClose === 'grid' && type === 'pop') {
      this.reloadtable()
    } else if (btn.popClose === 'view' && type === 'pop') {
      this.reloadview()
    } else if (btn.popClose === 'maingrid' && type === 'pop') {
      this.props.handleMainTable('maingrid')
    }
  }
 
  /**
   * @description 表格Id变化
   */
  handleTableId = (type = this.props.Tab.uuid, id = '', data = '') => {
    this.props.handleTableId(type, id, data)
  }
 
  /**
   * @description 数据展开合并切换
   */
  pickupChange = () => {
    const { pickup } = this.state
 
    this.setState({
      pickup: !pickup
    })
  }
 
  UNSAFE_componentWillMount() {
    // 组件加载时,获取菜单数据
    this.loadconfig()
  }
 
  shouldComponentUpdate (nextProps, nextState) { // handleMainTable 函数判断时不相等
    return !is(fromJS({...this.props, handleMainTable: '', handleTableId: ''}), fromJS({...nextProps, handleMainTable: '', handleTableId: ''})) || !is(fromJS(this.state), fromJS(nextState))
  }
 
  /**
   * @description 组件销毁,清除state更新
   */
  componentWillUnmount () {
    this.setState = () => {
      return
    }
  }
 
  render() {
    const { config, setting, searchlist, actions, columns, pickup, selectedData } = this.state
 
    return (
      <div className="subtable" id={'subtable' + this.props.MenuID}>
        {searchlist && searchlist.length > 0 ?
          <SubSearch
            dict={this.state.dict}
            searchlist={searchlist}
            refreshdata={this.refreshbysearch}
          /> : null
        }
        {actions ?
          <div className="sub-action">
            <SubAction
              type="sub"
              setting={setting}
              actions={actions}
              Tab={this.props.Tab}
              BID={this.props.BID}
              BData={this.props.BData}
              dict={this.state.dict}
              selectedData={selectedData}
              MenuID={this.props.SupMenuID}
              logcolumns={this.state.logcolumns}
              refreshdata={this.refreshbyaction}
              ContainerId={this.props.ContainerId}
              getexceloutparam={this.getexceloutparam}
            />
          </div> : null
        }
        {columns ?
          <div className="subtable-box">
            {this.state.data && this.state.data.length > 0 ?
              <Switch title="收起" className="subtable-pickup" checkedChildren="开" unCheckedChildren="关" defaultChecked={pickup} onChange={this.pickupChange} /> : null
            }
            <SubTable
              tableId={this.props.Tab.uuid}
              pickup={pickup}
              config={config}
              setting={setting}
              columns={columns}
              dict={this.state.dict}
              data={this.state.data}
              total={this.state.total}
              MenuID={this.props.MenuID}
              loading={this.state.loading}
              refreshdata={this.refreshbytable}
              buttonTrigger={() => {}}
              handleTableId={this.handleTableId}
              resetTable={this.state.resetTable}
              chgSelectData={this.changeSelectedData}
            />
          </div> : null
        }
      </div>
    )
  }
}
 
export default VerupSubTabViewTable