king
2023-06-16 e4666c46c685ec7eabff25af9890d54c0ff2952b
src/tabviews/custom/components/table/normal-table/index.jsx
@@ -1,14 +1,14 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import {connect} from 'react-redux'
import { is, fromJS } from 'immutable'
import { notification, Collapse } from 'antd'
import { notification, Collapse, Modal } from 'antd'
import Api from '@/api'
import Utils from '@/utils/utils.js'
import UtilsDM from '@/utils/utils-datamanage.js'
import asyncComponent from '@/utils/asyncComponent'
import MKEmitter from '@/utils/events.js'
import TimerTask from '@/utils/timer-task.js'
import './index.scss'
// 通用组件
@@ -21,21 +21,18 @@
class NormalTable extends Component {
  static propTpyes = {
    BID: PropTypes.any,              // 父级Id
    data: PropTypes.array,           // 统一查询数据
    config: PropTypes.object,        // 组件配置信息
    mainSearch: PropTypes.any,       // 外层搜索条件
    menuType: PropTypes.any,         // 菜单类型
  }
  state = {
    BID: '',              // 上级ID
    BData: '',            // 上级组件行数据
    config: {},           // 页面配置信息,包括按钮、搜索、显示列、标签等
    searchlist: null,     // 搜索条件
    actions: null,        // 按钮集
    columns: null,        // 显示列
    arr_field: '',        // 使用 sPC_Get_TableData 时的查询字段集
    arr_field: '',        // 查询字段集
    setting: null,        // 页面全局设置:数据源、按钮及显示列固定、主键等
    data: [],             // 列表数据集
    selectedData: [],     // 已选表格数据
@@ -48,16 +45,29 @@
    statFValue: []        // 合计值
  }
  loaded = false
  /**
   * @description 初始化处理
   * 1、 initdata 为打印时使用的数据集
   */
  UNSAFE_componentWillMount () {
    const { data, initdata, BID, BData } = this.props
    const { data, initdata } = this.props
    let _config = fromJS(this.props.config).toJS()
    let _cols = new Map()
    let _data = null
    let _sync = _config.setting.sync === 'true'
    let BID = ''
    let BData = ''
    if (_config.setting.supModule) {
      BData = window.GLOB.CacheData.get(_config.setting.supModule)
    } else {
      BData = window.GLOB.CacheData.get(_config.$pageId)
    }
    if (BData) {
      BID = BData.$BID || ''
    }
    if (_config.wrap.controlField) {
      if (_config.wrap.controlVal) {
@@ -67,23 +77,24 @@
      }
    }
    let setting = {..._config.setting, ..._config.wrap, style: {}}
    let setting = {..._config.setting, ..._config.wrap}
    if (setting.selected !== 'always' && setting.selected !== 'init') {
    if (setting.selected !== 'always' && setting.selected !== 'init' && setting.selected !== 'sign') {
      setting.selected = 'false'
    } else {
      setting.orisel = true
    }
    if (_config.setting.sync === 'true' && data) {
    if (_sync && data) {
      _data = data[_config.dataName] || []
      _sync = false
    } else if (_config.setting.sync === 'true' && initdata) {
    } else if (_sync && initdata) {
      _data = initdata || []
      _sync = false
    }
    if (_data) {
      this.loaded = true
      _data = _data.map((item, index) => {
        item.key = index
        item.$$uuid = item[_config.setting.primaryKey] || ''
@@ -92,8 +103,16 @@
        item.$$BData = BData || ''
        item.$Index = index + 1 + ''
        if (_config.absFields) {
          _config.absFields.forEach(f => {
            if (!isNaN(item[f])) {
              item[f] = Math.abs(item[f])
            }
          })
        }
        if (setting.controlField) {
          if (setting.controlVal.includes(item[setting.controlField])) {
          if (setting.controlVal.includes(item[setting.controlField] + '')) {
            item.$disabled = true
          }
        }
@@ -103,37 +122,12 @@
      if (setting.selected !== 'false' && _data && _data.length > 0) {
        setTimeout(() => {
          MKEmitter.emit('mkCheckTopLine', _config.uuid)
          MKEmitter.emit('mkCheckTopLine', _config.uuid, '', setting.selected)
        }, 200)
        if (setting.selected === 'init') {
          setting.selected = 'false'
        }
      }
    }
    _config.columns.forEach(item => {
      if (item.type !== 'number') return
      _cols.set(item.field, item)
    })
    _config.cols.forEach(column => {
      if (column.type === 'custom') {
        column.elements = column.elements.map(item => {
          if (item.eleType === 'number' && item.field && _cols.has(item.field) && typeof(item.decimal) !== 'number') {
            item.decimal = _cols.get(item.field).decimal || 0
          }
          return item
        })
      } else if (column.type === 'action') {
        column.operations = column.elements
      }
    })
    if (setting.color) {
      setting.style.color = setting.color
    }
    if (setting.fontSize) {
      setting.style.fontSize = setting.fontSize
    }
    if (_config.wrap.collapse === 'true') {
@@ -151,7 +145,6 @@
      data: _data,
      config: _config,
      setting: setting,
      searchlist: _config.search,
      actions: _config.action,
      columns: _config.cols,
      arr_field: _config.columns.map(col => col.field).join(','),
@@ -173,7 +166,7 @@
   * @param { Boolean } reset  表格是否重置
   * @param { String }  repage 表格是否重置页码
   */
  async loadmaindata (reset, repage, id) {
  async loadmaindata (reset, repage, id, type) {
    const { mainSearch } = this.props
    const { setting, config, arr_field, search, orderBy, BID, pageIndex, pageSize, BData } = this.state
@@ -185,10 +178,12 @@
      })
      
      MKEmitter.emit('resetSelectLine', config.uuid, '', '') // 广播数据切换
      reset && MKEmitter.emit('resetTable', config.uuid, repage) // 列表重置
      reset && MKEmitter.emit('resetTable', config.uuid, 'true') // 列表重置
      if (setting.$hasSyncModule) {
        MKEmitter.emit('syncBalconyData', config.uuid, [], false)
      }
      this.loaded = true
      return
    }
@@ -207,18 +202,42 @@
      return
    }
    this.setState({
      loading: true
    })
    if (type !== 'timer') {
      this.setState({
        loading: true
      })
    }
    let _orderBy = orderBy || setting.order
    let param = UtilsDM.getQueryDataParams(setting, arr_field, searches, _orderBy, pageIndex, pageSize, BID, this.props.menuType)
    let param = UtilsDM.getQueryDataParams(setting, arr_field, searches, _orderBy, pageIndex, pageSize, BID)
    let result = await Api.genericInterface(param)
    if (result.status) {
      this.loaded = true
      if (config.$cache && pageIndex === 1 && config.setting.onload !== 'false') {
        Api.writeCacheConfig(config.uuid, result.data || '')
      }
      if (repage === 'false' && result.data && result.data.length === 0 && result.total > 0 && pageIndex > 1) {
        let _pageIndex = Math.ceil(result.total / pageSize)
        if (_pageIndex < pageIndex) {
          MKEmitter.emit('resetTable', config.uuid, 'repage', _pageIndex)
          this.setState({
            pageIndex: _pageIndex,
            data: [],
            selectedData: [],
            total: result.total
          }, () => {
            this.loadmaindata()
          })
          return
        }
      }
      if ((setting.selected !== 'false' || (setting.orisel && id)) && result.data && result.data.length > 0) {
        setTimeout(() => {
          MKEmitter.emit('mkCheckTopLine', config.uuid, id)
          MKEmitter.emit('mkCheckTopLine', config.uuid, id, setting.selected)
        }, 200)
        if (setting.selected === 'init') {
          this.setState({setting: {...setting, selected: 'false'}})
@@ -237,36 +256,61 @@
        start = pageSize * (pageIndex - 1) + 1
      }
      this.setState({
        data: result.data.map((item, index) => {
          item.key = index
          item.$$uuid = item[setting.primaryKey] || ''
          item.$$key = '' + item.key + item.$$uuid
          item.$$BID = BID || ''
          item.$$BData = BData || ''
          item.$Index = start + index + ''
      let data = result.data.map((item, index) => {
        item.key = index
        item.$$uuid = item[setting.primaryKey] || ''
        item.$$key = '' + item.key + item.$$uuid
        item.$$BID = BID || ''
        item.$$BData = BData || ''
        item.$Index = start + index + ''
          if (setting.controlField) {
            if (setting.controlVal.includes(item[setting.controlField])) {
              item.$disabled = true
        if (config.absFields) {
          config.absFields.forEach(f => {
            if (!isNaN(item[f])) {
              item[f] = Math.abs(item[f])
            }
          })
        }
        if (setting.controlField) {
          if (setting.controlVal.includes(item[setting.controlField] + '')) {
            item.$disabled = true
          }
          return item
        }),
        }
        return item
      })
      this.setState({
        data: data,
        selectedData: [],
        total: result.total,
        loading: false
      })
      if (config.timer && config.clearField && result.data && result.data[0]) {
        let vals = (config.clearValue || '').split(',')
        if (vals.includes(result.data[0][config.clearField])) {
          this.timer && this.timer.stop()
        }
      }
    } else {
      this.setState({
        loading: false
      })
      notification.error({
        top: 92,
        message: result.message,
        duration: 10
      })
      this.timer && this.timer.stop()
      if (result.ErrCode === 'N') {
        Modal.error({
          title: result.message,
        })
      } else {
        notification.error({
          top: 92,
          message: result.message,
          duration: 10
        })
      }
    }
  }
@@ -292,40 +336,56 @@
    })
    let _orderBy = orderBy || setting.order
    let param = UtilsDM.getQueryDataParams(setting, arr_field, searches, _orderBy, pageIndex, pageSize, BID, this.props.menuType, id)
    let param = UtilsDM.getQueryDataParams(setting, arr_field, searches, _orderBy, pageIndex, pageSize, BID, id)
    let result = await Api.genericInterface(param)
    if (result.status) {
      if (!result.data || !result.data[0]) {
        this.setState({
          loading: false
        })
        return
      }
      let data = fromJS(this.state.data).toJS()
      let selectedData = fromJS(this.state.selectedData).toJS()
      if (result.data && result.data[0]) {
        let _data = result.data[0] || {}
        _data.$$uuid = _data[setting.primaryKey] || ''
        _data.$$BID = BID || ''
        _data.$$BData = BData || ''
        try {
          data = data.map(item => {
            if (item.$$uuid === _data.$$uuid) {
              _data.key = item.key
              _data.$$key = '' + item.key + item.$$uuid
              _data.$Index = item.$Index
              return _data
            } else {
              return item
            }
          })
          selectedData = selectedData.map(item => {
            if (_data.$$uuid === item.$$uuid) {
              return _data
            }
            return item
          })
        } catch (e) {
          console.warn('数据查询错误')
        }
        MKEmitter.emit('resetSelectLine', config.uuid, _data.$$uuid || '', _data)
      let _data = result.data[0]
      _data.$$uuid = _data[setting.primaryKey] || ''
      _data.$$BID = BID || ''
      _data.$$BData = BData || ''
      if (config.absFields) {
        config.absFields.forEach(f => {
          if (!isNaN(_data[f])) {
            _data[f] = Math.abs(_data[f])
          }
        })
      }
      try {
        data = data.map(item => {
          if (item.$$uuid === _data.$$uuid) {
            _data.key = item.key
            _data.$$key = '' + item.key + item.$$uuid
            _data.$Index = item.$Index
            return _data
          } else {
            return item
          }
        })
        selectedData = selectedData.map(item => {
          if (_data.$$uuid === item.$$uuid) {
            return _data
          }
          return item
        })
      } catch (e) {
        console.warn('数据查询错误')
      }
      MKEmitter.emit('resetSelectLine', config.uuid, _data.$$uuid || '', _data)
      this.setState({
        data,
@@ -375,7 +435,7 @@
    }
    let _orderBy = orderBy || setting.order
    let param = UtilsDM.getStatQueryDataParams(setting, config.statFields, searches, _orderBy, BID, this.props.menuType)
    let param = UtilsDM.getStatQueryDataParams(setting, config.statFields, searches, _orderBy, BID)
    Api.genericInterface(param).then(res => {
      if (res.status) {
@@ -415,26 +475,13 @@
   * 含有初始不加载的页面,修改设置
   */
  refreshbysearch = (searches) => {
    const { setting } = this.state
    if (setting.onload === 'false') {
      this.setState({
        pageIndex: 1,
        search: searches,
        setting: {...setting, onload: 'true'}
      }, () => {
        this.loadmaindata()
        this.getStatFieldsValue()
      })
    } else {
      this.setState({
        pageIndex: 1,
        search: searches
      }, () => {
        this.loadmaindata(true, 'true')
        this.getStatFieldsValue()
      })
    }
    this.setState({
      pageIndex: 1,
      search: searches
    }, () => {
      this.loadmaindata(true, 'true')
      this.getStatFieldsValue()
    })
  }
  /**
@@ -478,7 +525,7 @@
  /**
   * @description 导出Excel时,获取页面搜索排序等参数
   */
  queryModuleParam = (menuId, btnId) => {
  queryModuleParam = (menuId, callback) => {
    const { mainSearch } = this.props
    const { arr_field, config, orderBy, search, setting} = this.state
@@ -494,7 +541,7 @@
      })
    }
    MKEmitter.emit('returnModuleParam', config.uuid, btnId, {
    callback({
      arr_field: arr_field,
      orderBy: orderBy || setting.order,
      search: searches,
@@ -518,6 +565,7 @@
    const { setting } = this.state
    if (!setting.supModule || setting.supModule !== MenuID) return
    if (id !== this.state.BID || id !== '') {
      this.setState({
        pageIndex: 1,
@@ -541,31 +589,43 @@
    if (config.uuid !== menuId) return
    let supModule = config.setting.supModule
    btn.syncComponentId && MKEmitter.emit('reloadData', btn.syncComponentId)
    if (!btn.syncComponentId || btn.syncComponentId !== supModule) {
      if (position === 'line') {
        if (lines && lines.length === 1) {
          this.loadmainLinedata(lines[0].$$uuid)
        } else {
          this.reloadtable(btn, id)
        }
      } else if (position === 'mainline' || position === 'popclose') { // 刷新源组件时,附带刷新上级行与当前组件
        if (supModule && BID) {
          MKEmitter.emit('reloadData', supModule, BID)
        } else {
          this.reloadtable(btn, id)
        }
    if (position === 'line') {
      if (lines && lines.length === 1) {
        this.loadmainLinedata(lines[0].$$uuid)
      } else {
        this.reloadtable(btn, id)
      }
    } else if ((position === 'mainline' || position === 'popclose') && config.setting.supModule && BID) { // 刷新源组件时,附带刷新上级行与当前组件
      MKEmitter.emit('reloadData', config.setting.supModule, BID)
    } else {
      this.reloadtable(btn, id)
    }
    if (position === 'popclose') { // 执行启动弹窗的按钮所选择的刷新项
      btn.$tabId && MKEmitter.emit('refreshPopButton', btn.$tabId)
    }
  }
  refreshLineData = (menuId, btn, uuid, count) => {
    const { config } = this.state
    if (config.uuid !== menuId) return
    let _data = fromJS(this.state.data).toJS().forEach(item => {
      if (item.$$uuid === uuid) {
        item[btn.field] = count
      }
    })
    let _selectedData = fromJS(this.state.selectedData).toJS().forEach(item => {
      if (item.$$uuid === uuid) {
        item[btn.field] = count
      }
    })
    this.setState({
      data: _data,
      selectedData: _selectedData
    })
  }
  UNSAFE_componentWillReceiveProps(nextProps) {
@@ -583,8 +643,16 @@
          item.$$BData = BData || ''
          item.$Index = index + 1 + ''
          if (config.absFields) {
            config.absFields.forEach(f => {
              if (!isNaN(item[f])) {
                item[f] = Math.abs(item[f])
              }
            })
          }
          if (setting.controlField) {
            if (setting.controlVal.includes(item[setting.controlField])) {
            if (setting.controlVal.includes(item[setting.controlField] + '')) {
              item.$disabled = true
            }
          }
@@ -594,7 +662,7 @@
        if (setting.selected !== 'false' && _data && _data.length > 0) {
          setTimeout(() => {
            MKEmitter.emit('mkCheckTopLine', config.uuid)
            MKEmitter.emit('mkCheckTopLine', config.uuid, '', setting.selected)
          }, 200)
          if (setting.selected === 'init') {
            this.setState({setting: {...setting, selected: 'false'}})
@@ -602,8 +670,10 @@
        }
      }
      this.loaded = true
      this.setState({sync: false, data: _data})
    } else if (config.setting.syncRefresh && nextProps.mainSearch && !is(fromJS(this.props.mainSearch), fromJS(nextProps.mainSearch))) {
    } else if (config.setting.useMSearch && nextProps.mainSearch && !is(fromJS(this.props.mainSearch), fromJS(nextProps.mainSearch))) {
      this.setState({pageIndex: 1}, () => {
        this.reloadtable()
      })
@@ -615,10 +685,54 @@
  }
  componentDidMount () {
    const { config, setting } = this.state
    MKEmitter.addListener('reloadData', this.reloadData)
    MKEmitter.addListener('resetSelectLine', this.resetParentParam)
    MKEmitter.addListener('queryModuleParam', this.queryModuleParam)
    MKEmitter.addListener('refreshLineData', this.refreshLineData)
    MKEmitter.addListener('refreshByButtonResult', this.refreshByButtonResult)
    if (config.timer) {
      this.timer = new TimerTask()
      this.timer.init(config.uuid, config.timer, config.timerRepeats, () => {
        this.setState({
          pageIndex: 1
        }, () => {
          this.loadmaindata(true, 'true', '', 'timer')
          this.getStatFieldsValue()
        })
      })
    }
    if (config.$cache && !this.loaded) {
      Api.getLCacheConfig(config.uuid).then(res => {
        if (!res || this.loaded) return
        this.setState({data: res.map((item, index) => {
          item.key = index
          item.$$uuid = item[config.setting.primaryKey] || ''
          item.$$key = '' + item.key + item.$$uuid
          item.$Index = index + 1 + ''
          if (config.absFields) {
            config.absFields.forEach(f => {
              if (!isNaN(item[f])) {
                item[f] = Math.abs(item[f])
              }
            })
          }
          if (setting.controlField) {
            if (setting.controlVal.includes(item[setting.controlField] + '')) {
              item.$disabled = true
            }
          }
          return item
        })})
      })
    }
  }
  /**
@@ -631,11 +745,14 @@
    MKEmitter.removeListener('reloadData', this.reloadData)
    MKEmitter.removeListener('resetSelectLine', this.resetParentParam)
    MKEmitter.removeListener('queryModuleParam', this.queryModuleParam)
    MKEmitter.removeListener('refreshLineData', this.refreshLineData)
    MKEmitter.removeListener('refreshByButtonResult', this.refreshByButtonResult)
    this.timer && this.timer.stop()
  }
  render() {
    const { BID, setting, searchlist, actions, config, columns, selectedData, BData, data } = this.state
    const { BID, setting, actions, config, columns, selectedData, BData, data } = this.state
    let style = {...config.style}
    if (config.wrap.empty === 'hidden' && (!data || data.length === 0)) {
@@ -643,21 +760,21 @@
    }
    return (
      <div className="custom-normal-table" style={style}>
      <div className="custom-normal-table" id={'anchor' + config.uuid} style={style}>
        {config.wrap.collapse === 'true' ? <Collapse bordered={false} defaultActiveKey="1" expandIconPosition="right">
          <Panel forceRender={true} header={<NormalHeader config={config}/>} key="1">
            {searchlist && searchlist.length ?
              <MainSearch BID={BID} setting={config.wrap} searchlist={searchlist} menuType={this.props.menuType} refreshdata={this.refreshbysearch}/> : null
            {config.search && config.search.length ?
              <MainSearch BID={BID} config={config} refreshdata={this.refreshbysearch}/> : null
            }
            <MainAction
            {actions.length > 0 ? <MainAction
              BID={BID}
              setting={setting}
              actions={actions}
              BData={BData}
              columns={config.columns}
              selectedData={selectedData}
            />
            <div className={'main-table-box ' + (!actions || actions.length === 0 ? 'no-action' : '')}>
            /> : <div className="mk-action-space" style={{height: '25px'}}></div>}
            <div className="main-table-box">
              <MainTable
                setting={setting}
                columns={columns}
@@ -675,17 +792,17 @@
          </Panel>
        </Collapse> : <>
          <NormalHeader config={config}/>
          {searchlist && searchlist.length ?
            <MainSearch BID={BID} setting={config.wrap} searchlist={searchlist} menuType={this.props.menuType} refreshdata={this.refreshbysearch}/> : null
          {config.search && config.search.length ?
            <MainSearch BID={BID} config={config} refreshdata={this.refreshbysearch}/> : null
          }
          <MainAction
          {actions.length > 0 ? <MainAction
            BID={BID}
            setting={setting}
            actions={actions}
            BData={BData}
            columns={config.columns}
            selectedData={selectedData}
          />
          /> : <div className="mk-action-space" style={{height: '25px'}}></div>}
          <div className={'main-table-box ' + (!actions || actions.length === 0 ? 'no-action' : '')}>
            <MainTable
              setting={setting}
@@ -707,14 +824,4 @@
  }
}
const mapStateToProps = (state) => {
  return {
    menuType: state.editLevel
  }
}
const mapDispatchToProps = () => {
  return {}
}
export default connect(mapStateToProps, mapDispatchToProps)(NormalTable)
export default NormalTable