import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { notification, Tabs, Icon, 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 asyncLoadComponent from '@/utils/asyncLoadComponent'
|
import { verupMainTable, buttonConfig } from './config.js'
|
|
import MainTable from '@/tabviews/zshare/normalTable'
|
import TopSearch from './topSearch'
|
import MainAction from './actionList'
|
import './index.scss'
|
|
const SubTable = asyncLoadComponent(() => import('./subtabtable'))
|
|
const { TabPane } = Tabs
|
|
class VerupTable extends Component {
|
static propTpyes = {
|
MenuNo: PropTypes.string, // 菜单参数
|
MenuName: PropTypes.string, // 菜单参数
|
MenuID: PropTypes.string // 菜单Id
|
}
|
|
state = {
|
dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
|
ContainerId: Utils.getuuid(), // 菜单外层html Id
|
config: {}, // 页面配置信息,包括按钮、搜索、显示列、标签等
|
searchlist: [], // 搜索条件
|
actions: [], // 按钮集
|
columns: [], // 显示列
|
arr_field: '', // 使用 sPC_Get_TableData 时的查询字段集
|
logcolumns: null, // 日志中显示的列信息 (增加至全部列,除去合并列)
|
setting: {}, // 页面全局设置:数据源、按钮及显示列固定、主键等
|
data: [], // 列表数据集
|
total: 0, // 总数
|
loading: false, // 列表数据加载中
|
pageIndex: 1, // 页码
|
pageSize: 10, // 每页数据条数
|
orderBy: '', // 排序
|
search: '', // 搜索条件数组,使用时需分场景处理
|
BIDs: {}, // 上级表id
|
pickup: false, // 主表数据隐藏显示切换
|
visible: false, // 弹框显示隐藏控制
|
refreshtabs: null // 需要刷新的标签集
|
}
|
|
/**
|
* @description 获取页面配置信息
|
*/
|
async loadconfig () {
|
let config = verupMainTable
|
|
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)
|
}, () => {
|
this.setState({
|
loading: true
|
})
|
this.loadmaindata()
|
})
|
}
|
|
/**
|
* @description 主表数据加载
|
*/
|
async loadmaindata () {
|
const { setting, BIDs } = this.state
|
let param = ''
|
|
if (setting.interType !== 'inner' || (setting.interType === 'inner' && setting.innerFunc)) {
|
param = this.getCustomParam()
|
} else {
|
param = this.getDefaultParam()
|
}
|
|
this.setState({
|
pickup: false
|
})
|
|
this.handleTableId('mainTable', '', '')
|
|
if (!param) { // 未获取参数时,不发请求
|
return
|
}
|
|
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,
|
loading: false,
|
BIDs: {
|
...BIDs,
|
mainTable: ''
|
}
|
})
|
} else {
|
this.setState({
|
loading: false
|
})
|
notification.error({
|
top: 92,
|
message: result.message,
|
duration: 15
|
})
|
}
|
}
|
|
/**
|
* @description 获取用户自定义存储过程传参
|
*/
|
getCustomParam = () => {
|
const { pageIndex, pageSize, orderBy, search, setting } = this.state
|
|
let _search = Utils.formatCustomMainSearch(search)
|
|
let param = {
|
PageIndex: pageIndex,
|
PageSize: pageSize,
|
OrderCol: orderBy || setting.order,
|
..._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
|
}
|
|
param.appkey = window.GLOB.appkey || '' // 调用外部接口增加appkey
|
|
if (setting.outerFunc) {
|
param.func = setting.outerFunc
|
}
|
}
|
|
return param
|
}
|
|
/**
|
* @description 获取系统存储过程 sPC_Get_TableData 的参数
|
*/
|
getDefaultParam = () => {
|
const { arr_field, pageIndex, pageSize, orderBy, search, setting } = this.state
|
|
if (!arr_field) {
|
notification.warning({
|
top: 92,
|
message: '未设置显示列!',
|
duration: 10
|
})
|
return null
|
}
|
|
let _search = Utils.joinMainSearchkey(search)
|
|
_search = _search ? 'where ' + _search : ''
|
|
let param = {
|
func: 'sPC_Get_TableData',
|
obj_name: 'data',
|
arr_field: arr_field,
|
appkey: window.GLOB.appkey || ''
|
}
|
|
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)
|
|
return param
|
}
|
|
/**
|
* @description 搜索条件改变时,重置表格数据
|
* 含有初始不加载的页面,修改设置
|
*/
|
refreshbysearch = (searches) => {
|
this.refs.mainTable.resetTable()
|
|
this.setState({
|
loading: true,
|
pageIndex: 1,
|
search: searches
|
}, () => {
|
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,
|
pageIndex: pagination.current,
|
pageSize: pagination.pageSize,
|
orderBy: (sorter.field && sorter.order) ? `${sorter.field} ${sorter.order}` : ''
|
}, () => {
|
this.loadmaindata()
|
})
|
}
|
|
/**
|
* @description 表格刷新
|
*/
|
reloadtable = () => {
|
this.refs.mainTable.resetTable()
|
this.setState({
|
loading: true,
|
pageIndex: 1
|
}, () => {
|
this.loadmaindata()
|
})
|
}
|
|
/**
|
* @description 页面刷新,重新获取配置
|
*/
|
reloadview = () => {
|
this.setState({
|
config: {},
|
searchlist: [],
|
actions: [],
|
columns: [],
|
arr_field: '',
|
setting: {},
|
data: [],
|
total: 0,
|
loading: false,
|
pageIndex: 1,
|
pageSize: 10,
|
orderBy: '',
|
search: '',
|
BIDs: {},
|
pickup: false
|
}, () => {
|
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()
|
}
|
}
|
|
/**
|
* @description 子表操作完成后刷新主表
|
*/
|
handleMainTable = (type, tab) => {
|
if (type === 'maingrid' && tab.supMenu === 'mainTable') {
|
this.reloadtable()
|
} else if (type === 'maingrid' && tab.supMenu) {
|
this.setState({
|
refreshtabs: [tab.supMenu]
|
}, () => {
|
this.setState({
|
refreshtabs: null
|
})
|
})
|
} else if (type === 'equaltab' && tab.equalTab && tab.equalTab.length > 0) {
|
this.setState({
|
refreshtabs: tab.equalTab
|
}, () => {
|
this.setState({
|
refreshtabs: null
|
})
|
})
|
}
|
}
|
|
/**
|
* @description 获取表格选择项
|
*/
|
gettableselected = () => {
|
let data = []
|
this.refs.mainTable.state.selectedRowKeys.forEach(item => {
|
data.push(this.refs.mainTable.props.data[item])
|
})
|
return data
|
}
|
|
/**
|
* @description 表格Id变化
|
*/
|
handleTableId = (type, id, data) => {
|
const { BIDs } = this.state
|
|
this.setState({
|
BIDs: {
|
...BIDs,
|
[type]: id,
|
[type + 'data']: data
|
}
|
})
|
}
|
|
/**
|
* @description 数据展开合并切换
|
*/
|
pickupChange = () => {
|
const { pickup } = this.state
|
this.setState({
|
pickup: !pickup
|
})
|
}
|
|
UNSAFE_componentWillMount () {
|
// 组件加载时,获取菜单数据
|
this.loadconfig()
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
/**
|
* @description 组件销毁,清除state更新
|
*/
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
}
|
|
render() {
|
const { dict, searchlist, setting, actions, columns, pickup, config } = this.state
|
|
return (
|
<div className="veruptable pick-control" id={this.state.ContainerId}>
|
<TopSearch
|
dict={dict}
|
searchlist={searchlist}
|
refreshdata={this.refreshbysearch}
|
/>
|
<MainAction
|
BID=""
|
type="main"
|
setting={setting}
|
actions={actions}
|
dict={this.state.dict}
|
MenuID={this.props.MenuID}
|
logcolumns={this.state.logcolumns}
|
ContainerId={this.state.ContainerId}
|
refreshdata={this.refreshbyaction}
|
gettableselected={this.gettableselected}
|
/>
|
<div className="main-table-box">
|
{this.state.data && this.state.data.length > 0 ?
|
<div className="pickchange">
|
<Switch title="收起" checkedChildren="开" unCheckedChildren="关" defaultChecked={pickup} onChange={this.pickupChange} />
|
</div> : null
|
}
|
<MainTable
|
ref="mainTable"
|
tableId="mainTable"
|
pickup={pickup}
|
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}
|
/>
|
</div>
|
<Tabs defaultActiveKey="0">
|
{config.tabs && config.tabs.map((_tab, index) => {
|
return (
|
<TabPane tab={
|
<span>
|
{_tab.icon ? <Icon type={_tab.icon} /> : null}
|
{_tab.label}
|
</span>
|
} key={`${index}`}>
|
<SubTable
|
menuType="HS"
|
Tab={_tab}
|
MenuID={_tab.linkTab}
|
SupMenuID={this.props.MenuID}
|
refreshtabs={this.state.refreshtabs}
|
ContainerId={this.state.ContainerId}
|
BID={this.state.BIDs[_tab.supMenu] || ''}
|
BData={this.state.BIDs[_tab.supMenu + 'data'] || ''}
|
handleTableId={this.handleTableId}
|
handleMainTable={(type) => this.handleMainTable(type, _tab)}
|
/>
|
</TabPane>
|
)
|
})}
|
</Tabs>
|
</div>
|
)
|
}
|
}
|
|
export default VerupTable
|