king
2023-10-01 437c6d72f76072e5ab1b09e78101370805113c4b
src/menu/datasource/verifycard/index.jsx
@@ -8,7 +8,7 @@
import Api from '@/api'
import Utils from '@/utils/utils.js'
import { formatSearch, joinMainSearchkey } from '@/utils/utils-custom.js'
import asyncComponent from '@/utils/asyncComponent'
import ColForm from './columnform'
import CustomScriptsForm from './customscript'
@@ -40,11 +40,11 @@
    subColumns: [],
    activeKey: 'setting',
    loading: false,
    colLoading: false,
    searchKey: '',
    initsql: '',          // sql验证时变量声明及赋值
    usefulfields: '',
    defaultsql: '',       // 默认Sql
    defaultSearch: '',
    systemScripts: [],
    median: {},
    visible: false,
@@ -210,16 +210,32 @@
      })
    }
    let _search = this.formatSearch(search)
    _search = Utils.joinMainSearchkey(_search)
    _search = _search.replace(/@\$@/ig, '')
    _search = _search ? 'where ' + _search : ''
    let columns = config.columns ? fromJS(config.columns).toJS() : []
    let subColumns = config.subColumns ? fromJS(config.subColumns).toJS() : []
    columns.reverse()
    subColumns.reverse()
    columns.forEach(col => {
      if (!col.datatype) return
      if (/^nvarchar/.test(col.datatype)) {
        col.datatype = col.datatype.replace(/^nvarchar/, 'Nvarchar')
      } else if (/^decimal/.test(col.datatype)) {
        col.datatype = col.datatype.replace(/^decimal/, 'Decimal')
      } else if (/^int/.test(col.datatype)) {
        col.datatype = col.datatype.replace(/^int/, 'Int')
      }
    })
    subColumns.forEach(col => {
      if (!col.datatype) return
      if (/^nvarchar/.test(col.datatype)) {
        col.datatype = col.datatype.replace(/^nvarchar/, 'Nvarchar')
      } else if (/^decimal/.test(col.datatype)) {
        col.datatype = col.datatype.replace(/^decimal/, 'Decimal')
      } else if (/^int/.test(col.datatype)) {
        col.datatype = col.datatype.replace(/^int/, 'Int')
      }
    })
    this.setState({
      scripts,
@@ -227,8 +243,7 @@
      subColumns: subColumns,
      setting: _setting,
      median: _setting,
      searches: search,
      defaultSearch: _search,
      searches: formatSearch(search),
      searchKey: '',
      debugId: _setting.debugId || ''
    })
@@ -426,15 +441,8 @@
          search = [...search, ...mainSearch]
        }
        let _search = this.formatSearch(search)
        _search = Utils.joinMainSearchkey(_search)
        _search = _search.replace(/@\$@/ig, '')
        _search = _search ? 'where ' + _search : ''
        this.setState({
          searches: search,
          defaultSearch: _search,
          searches: formatSearch(search),
          setting: res
        }, () => {
          this.sqlverify(() => { // 验证成功
@@ -513,9 +521,11 @@
  }
  getdefaultSql = () => {
    const { columns, setting, defaultSearch } = this.state
    const { columns, setting, searches } = this.state
    let defaultsql = ''
    let arr_field = columns.map(col => col.field).join(',')
    let _search = joinMainSearchkey(searches)
    if (setting.dataresource) {
      let _dataresource = setting.dataresource
@@ -524,59 +534,10 @@
        _dataresource = '(' + _dataresource + ') tb'
      }
      defaultsql = `select top @pageSize@ ${arr_field} from (select ${arr_field} ,ROW_NUMBER() over(order by @orderBy@) as rows from ${_dataresource} ${defaultSearch}) tmptable where rows > (@pageSize@ * (@pageIndex@ - 1)) order by tmptable.rows`
      defaultsql = `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`
    }
    this.setState({defaultsql})
  }
  /**
   * @description 获取全部搜索条件
   * @param {Array} searches 搜索条件数组
   */
  formatSearch (searches) {
    if (!searches) return []
    let newsearches = []
    searches.forEach(search => {
      if (!search.field) return
      let item = {
        key: search.field,
        match: search.match,
        type: search.type,
        label: search.label,
        value: search.initval,
        required: search.required === 'true'
      }
      if (item.type === 'group') {
        item.key = search.datefield
        item.type = 'daterange'
        item.match = 'between'
        item.value = [moment().format('YYYY-MM-DD'), moment().format('YYYY-MM-DD')].join(',')
        newsearches.push(item)
        return
      } else if (item.type === 'date') {
        item.value = moment().format('YYYY-MM-DD')
      } else if (item.type === 'datemonth') {
        item.value = moment().format('YYYY-MM')
      } else if (item.type === 'dateweek') {
        item.value = moment().format('YYYY-MM-DD')
      } else if (item.type === 'daterange') {
        item.value = [moment().format('YYYY-MM-DD'), moment().format('YYYY-MM-DD')].join(',')
      } else if (item.type === 'range') {
      } else if (item.type === 'multiselect' || (item.type === 'checkcard' && search.multiple === 'true')) {
        item.type = 'multi'
        item.value = '@$@'
      } else {
        item.value = '@$@'
      }
      newsearches.push(item)
    })
    return newsearches
  }
  submitDataSource = () => {
@@ -611,15 +572,8 @@
            search = [...search, ...mainSearch]
          }
          let _search = this.formatSearch(search)
          _search = Utils.joinMainSearchkey(_search)
          _search = _search.replace(/@\$@/ig, '')
          _search = _search ? 'where ' + _search : ''
          this.setState({
            searches: search,
            defaultSearch: _search,
            searches: formatSearch(search),
            setting: res
          }, () => {
            this.sqlverify(() => { resolve({setting: res, columns, subColumns, scripts, cols }) }, reject, 'submit')
@@ -674,7 +628,7 @@
  sqlverify = (resolve, reject, type, testScripts) => {
    const { config } = this.props
    const { columns, setting, scripts, searches, defaultSearch, debugId } = this.state
    const { columns, setting, scripts, searches, debugId, subColumns } = this.state
    let _scripts = scripts.filter(item => item.status !== 'false')
@@ -697,7 +651,11 @@
    }
    if ((setting.interType === 'system' && setting.execute !== 'false') || _scripts.length > 0) {
      let r = SettingUtils.getDebugSql(setting, _scripts, columns, searches, defaultSearch, config.type)
      let _columns = columns
      if (config.subtype === 'dualdatacard') {
        _columns = [...columns, ...subColumns]
      }
      let r = SettingUtils.getDebugSql(setting, _scripts, _columns, searches, config.type)
      let _debugId = md5(r.sql)
@@ -907,11 +865,25 @@
    const { columns } = this.state
    let m = []
    let n = []
    let s = []
    columns.forEach(col => {
      m.push(`${col.field} ${col.datatype}`)
      n.push(col.field)
      m.unshift(`${col.field} ${col.datatype}`)
      n.unshift(col.field)
      if (/decimal|int/ig.test(col.datatype)) {
        s.unshift(`${Math.round(Math.random() * 10)} as ${col.field}`)
      } else if (/datetime/.test(col.datatype)) {
        s.unshift(`'${moment().format('YYYY-MM-DD HH:mm:ss')}' as ${col.field}`)
      } else if (/date/.test(col.datatype)) {
        s.unshift(`'${moment().format('YYYY-MM-DD')}' as ${col.field}`)
      } else {
        s.unshift(`'${col.label}' as ${col.field}`)
      }
    })
    if (window.debugger) {
      console.info('select ' + s.join(', '))
    }
    let oInput = document.createElement('input')
    oInput.value = `/*${m.join(',')}*/
@@ -988,6 +960,49 @@
    })
  }
  updateColumns = (tableName) => {
    const { config } = this.props
    const { colLoading, columns } = this.state
    if (colLoading || columns.length > 0) return
    if (config.subtype === 'basetable') return
    let param = {func: 'sPC_Get_FieldName', TBName: tableName}
    if (window.GLOB.cloudServiceApi) { // 且存在云端地址
      param.rduri = window.GLOB.cloudServiceApi
      param.userid = sessionStorage.getItem('CloudUserID') || ''
      param.LoginUID = sessionStorage.getItem('CloudLoginUID') || ''
    }
    this.setState({colLoading: true})
    Api.getSystemCacheConfig(param).then(result => {
      this.setState({colLoading: false})
      if (!result.status) return
      let fields = []
      result.FDName.forEach(item => {
        if (item.f_type !== 0) return
        if (/nvarchar|int|decimal/ig.test(item.FieldType) || item.FieldType === 'date' || item.FieldType === 'datetime') {
          fields.unshift({
            uuid: Utils.getuuid(),
            label: item.FieldDec,
            field: item.FieldName,
            datatype: item.FieldType.toLowerCase()
          })
        }
      })
      if (fields.length === 0) return
      this.setState({columns: fields})
    }, () => {
      this.setState({colLoading: false})
    })
  }
  /**
   * @description 组件销毁,清除state更新
   */
@@ -1018,6 +1033,7 @@
              setting={setting}
              scripts={scripts}
              updateStatus={this.updateStatus}
              updateColumns={this.updateColumns}
              addProcess={this.addProcess}
              wrappedComponentRef={(inst) => this.settingForm = inst}
            /> : null}
@@ -1035,7 +1051,15 @@
              updatefield={this.updatefields}
            />
            <EditTable actions={['edit', 'move', 'copy', 'del', 'clear']} searchKey={searchKey} type="datasourcefield" wrappedComponentRef={(inst) => this.datasource = inst} data={columns} columns={colColumns} onChange={(columns) => this.setState({columns})}/>
          </TabPane> : null}
          </TabPane> : <TabPane tab={
            <span>
              字段集
              {columns.length ? <span className="count-tip">{columns.length}</span> : null}
            </span>
          } key="columns">
            <div className="base-table-columns"></div>
            <EditTable actions={[]} searchKey={searchKey} type="datasourcefield" data={columns} columns={colColumns}/>
          </TabPane>}
          {config.subtype === 'dualdatacard' ? <TabPane tab={
            <span>
              子表字段集