king
2024-05-20 8f9f9322f32e0553cb58068b39ae57e3d997ec65
src/menu/components/table/edit-table/columns/editColumn/index.jsx
@@ -1,22 +1,29 @@
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 } from 'antd'
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', 'Hide', 'Width', 'blacklist'],
  custom: ['label', 'type', 'Align', 'Width', 'blacklist', 'IsSort'],
  colspan: ['label', 'type', 'Align', 'Hide', 'blacklist'],
  extend: ['label', 'field', 'type', 'Align', 'Width', 'colUnit', 'shift', 'quota', 'supField'],
  action: ['label', 'type', 'Align', 'Width'],
  formula: ['label', 'type', 'Align', 'Hide', 'Width', 'prefix', 'postfix', 'eval', 'formula', 'blacklist'],
  index: ['label', 'type', 'Align', 'Width']
@@ -24,8 +31,8 @@
class EdiTableColumn extends Component {
  static propTpyes = {
    visible: PropTypes.bool,
    column: PropTypes.object,
    wrap: PropTypes.object,
    columns: PropTypes.array,
    fields: PropTypes.array,
    submitCol: PropTypes.func,  // 提交事件
@@ -34,11 +41,12 @@
  state = {
    visible: false,
    loading: false,
    formlist: null,
    transfield: {}
  }
  column = null
  record = null
  UNSAFE_componentWillMount() {
    let transfield = {}
@@ -56,18 +64,28 @@
  }
  getOptions = () => {
    let _options = fromJS(columnTypeOptions[this.column.type]).toJS()
    let _options = fromJS(columnTypeOptions[this.record.type]).toJS()
    if (this.column.editable === 'true') {
      if (this.column.type === 'text') {
    let reLabel = {}
    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.column.editType === 'switch') {
          _options.push('enter', 'openVal', 'closeVal', 'openText', 'closeText', 'editField')
        } else if (this.column.editType === 'select') {
          _options.push('required', 'enter', 'resourceType', 'linkSubField', 'editField', 'dropdown')
        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', 'showField', 'controlField', 'searchKey', 'popWidth', 'laypage', 'cache', 'onload')
        } else if (this.record.editType === 'select') {
          _options.push('required', 'enter', 'resourceType', 'linkSubField', 'dropdown')
          if (this.column.resourceType === '0') {
          if (this.record.resourceType === '0') {
            _options.push('options')
          } else {
            _options.push('dataSource', 'valueField', 'valueText', 'orderBy', 'orderType', 'disableField', 'database')
@@ -75,12 +93,35 @@
        } else {
          _options.push('required', 'enter')
        }
      } else if (this.column.type === 'number') {
        _options.push('max', 'min', 'enter')
        reLabel.required = '必填'
      } else if (this.record.type === 'number') {
        _options.push('max', 'min', 'required', 'enter', 'clearField')
        reLabel.required = '不等于0'
      }
    } else if (this.record.type === 'extend') {
      if (this.record.colUnit === 'day') {
        _options.push('dayFormat')
      } else {
        _options.push('hourFormat')
      }
    }
    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 (this.record.Hide !== 'true') {
      if (['number', 'formula'].includes(this.record.type)) {
        _options.push('noValue')
      } else if (this.record.type === 'text' && ['YYYY-MM-DD', 'YYYY-MM-DD HH:mm:ss'].includes(this.record.textFormat)) {
        _options.push('noValue')
      }
    }
    return _options
    return {options: _options, reLabel}
  }
  editColumn = (column) => {
@@ -89,18 +130,28 @@
      return item
    })
    let formlist = getColumnForm(column, fields, this.props.columns)
    let formlist = getColumnForm(column, fields, this.props.columns, this.props.wrap)
    this.record = {}
    this.column = fromJS(column).toJS()
    this.column.editType = this.column.editType || 'text'
    this.column.resourceType = this.column.resourceType || '0'
    formlist.forEach(item => {
      this.record[item.key] = item.initVal
    })
    
    let _options = this.getOptions()
    let { options, reLabel } = this.getOptions()
    this.setState({
      visible: true,
      formlist: formlist.map(item => {
        item.hidden = !_options.includes(item.key)
        item.hidden = !options.includes(item.key)
        if (reLabel[item.key]) {
          item.label = reLabel[item.key]
        }
        if (item.key === 'formula') {
          item.fields = this.props.fields.map(col => col.field)
          item.fields = item.fields.join(', ')
        }
        return item
      })
@@ -118,21 +169,43 @@
  }
  typeChange = (key, value, option) => {
    this.column[key] = value
    this.record[key] = value
    if (key === 'type') {
      let _options = this.getOptions()
      if (['textarea', 'custom'].includes(value)) {
        this.record.IsSort = 'false'
      }
      let { options, reLabel } = this.getOptions()
      let _field = ''
      if (value === 'formula') {
        _field = this.props.form.getFieldValue('field') || ''
      }
      this.setState({
        formlist: this.state.formlist.map(item => {
          item.initVal = this.column[item.key] || item.initVal
          item.hidden = !_options.includes(item.key)
          if (item.key === 'decimal' && value === 'formula') {
            this.record.decimal = ''
          }
          item.initVal = this.record[item.key]
          item.hidden = !options.includes(item.key)
          if (reLabel[item.key]) {
            item.label = reLabel[item.key]
          }
          return item
        })
      }, () => {
        if (value === 'action') {
        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') {
@@ -148,16 +221,19 @@
        values.type = 'text'
      }
      let _type = this.column.type
      this.column.type = values.type
      let _type = this.record.type
      this.record.type = values.type
      if (values.type !== _type) {
        let _options = this.getOptions()
        let { options, reLabel } = this.getOptions()
        this.setState({
          formlist: this.state.formlist.map(item => {
            item.initVal = this.column[item.key] || item.initVal
            item.hidden = !_options.includes(item.key)
            item.initVal = this.record[item.key]
            item.hidden = !options.includes(item.key)
            if (reLabel[item.key]) {
              item.label = reLabel[item.key]
            }
            return item
          })
@@ -169,13 +245,34 @@
      }
    } else if (key === 'format' && value === 'percent') {
      this.props.form.setFieldsValue({postfix: '%'})
    } else if (key === 'editable' || key === 'editType' || key === 'resourceType') {
      let _options = this.getOptions()
    } else if (key === 'editType') {
      let { options, reLabel } = this.getOptions()
      this.setState({
        formlist: this.state.formlist.map(item => {
          item.initVal = this.column[item.key] || item.initVal
          item.hidden = !_options.includes(item.key)
          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)
          if (reLabel[item.key]) {
            item.label = reLabel[item.key]
          }
          return item
        })
      })
    } else if (['editable', 'editType', 'resourceType', 'ctrlField', 'eval', 'Hide', 'IsSort', 'textFormat'].includes(key)) {
      let { options, reLabel } = this.getOptions()
      this.setState({
        formlist: this.state.formlist.map(item => {
          item.initVal = this.record[item.key]
          item.hidden = !options.includes(item.key)
          if (reLabel[item.key]) {
            item.label = reLabel[item.key]
          }
          return item
        })
@@ -186,7 +283,7 @@
  multiselectChange = (key, value) => {
    if (key !== 'linkSubField') return
    this.column[key] = value
    this.record[key] = value
  }
  handleEmpty = () => {
@@ -223,8 +320,8 @@
    this.props.form.setFieldsValue({dataSource: resource})
  }
  changeOptions = (data) => {
    this.column.options = data || []
  changeOptions = (data, key) => {
    this.record[key] = data || []
  }
  getFields() {
@@ -243,6 +340,14 @@
      let content = null
      let extra = null
      let initVal = item.initVal || ''
      let label = item.label
      if (item.tooltip) {
        if (item.toolWidth) {
          label = <Tooltip placement="topLeft" overlayStyle={{maxWidth: item.toolWidth}} title={<div onClick={(e) => e.stopPropagation()}>{item.tooltip}</div>}><QuestionCircleOutlined className="mk-form-tip" />{item.label}</Tooltip>
        } else {
          label = <Tooltip placement="topLeft" title={<div onClick={(e) => e.stopPropagation()}>{item.tooltip}</div>}><QuestionCircleOutlined className="mk-form-tip" />{item.label}</Tooltip>
        }
      }
      if (item.type === 'text') {
        rules = [
@@ -269,6 +374,12 @@
        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}
@@ -276,8 +387,8 @@
          onChange={(value, option) => {this.typeChange(item.key, value, option)}}
          getPopupContainer={() => document.getElementById('edit-table-column-winter')}
        >
          {item.options.map((option, i) =>
            <Select.Option key={i} datatype={option.datatype || ''} label={option.label || ''} value={(option.value || option.field || option.MenuID)}>
          {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>
          )}
@@ -304,10 +415,26 @@
        </Select>
      } else if (item.type === 'textarea') {
        span = 24
        className = 'text-area'
        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={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') {
@@ -315,28 +442,44 @@
          { required: item.required, message: '请输入' + item.label + '!' }
        ]
        span = 24
        className = 'text-area'
        extra = <span className="add-resource-empty" onClick={this.handleEmpty}>空</span>
        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
        className = 'text-area'
        let linkSubFields = this.column.linkSubField || []
        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 type={'select'} module="form" transfield={transfield} linkSubFields={linkSubFields} onChange={this.changeOptions}/>
        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
          }>
          <Form.Item className={className} extra={extra} label={label}>
            {getFieldDecorator(item.key, {
              initialValue: initVal,
              rules: rules
@@ -348,38 +491,190 @@
    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 { columns, column } = this.props
    const { fields } = this.props
    // 表单提交时检查输入值是否正确
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        values.uuid = column.uuid
        values.marks = column.marks || []
        if (values.field && columns.filter(col => col.field && col.uuid !== values.uuid && col.field === values.field).length > 0) {
          notification.warning({
            top: 92,
            message: '字段已添加!',
            duration: 5
        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}@)`})
            }
          })
          return
        }
        this.setState({visible: false, formlist: null})
        this.props.submitCol(values)
        this.column = null
          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, '')
          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, `''`)
          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, formlist: null})
    this.setState({visible: false, loading: false, formlist: null})
    this.props.cancelCol()
  }
  render() {
    const { visible } = this.state
    const { visible, loading } = this.state
    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
@@ -395,11 +690,13 @@
      <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">