king
2021-07-23 c7414c3cc93649479119d51b230c4b8e36884ca7
src/utils/utils-datamanage.js
@@ -1,3 +1,4 @@
import md5 from 'md5'
import moment from 'moment'
import options from '@/store/options.js'
import Utils from './utils.js'
@@ -575,4 +576,132 @@
    return [...lineMap.values()]
  }
}
/**
 * @description 生成单个组件sPC_Get_structured_data请求参数
 */
export function getStructDefaultParam (component, searchlist) {
  const { columns, setting, dataName, format } = component
  let arr_field = columns.map(col => col.field)
  let _dataresource = setting.dataresource
  let _customScript = setting.customScript
  if (setting.queryType === 'statistics' || _customScript) {
    let allSearch = Utils.getAllSearchOptions(searchlist)
    let regoptions = allSearch.map(item => {
      return {
        reg: new RegExp('@' + item.key + '@', 'ig'),
        value: `'${item.value}'`
      }
    })
    regoptions.forEach(item => {
      if (_dataresource && setting.queryType === 'statistics') {
        _dataresource = _dataresource.replace(item.reg, item.value)
      }
      _customScript = _customScript.replace(item.reg, item.value)
    })
  }
  _dataresource = _dataresource.replace(/@select\$|\$select@/ig, '')
  _customScript = _customScript.replace(/@select\$|\$select@/ig, '')
  _dataresource = _dataresource.replace(/\$sum@/ig, '/*')
  _dataresource = _dataresource.replace(/@sum\$/ig, '*/')
  _customScript = _customScript.replace(/\$sum@/ig, '/*')
  _customScript = _customScript.replace(/@sum\$/ig, '*/')
  let _search = ''
  if (setting.queryType !== 'statistics' && _dataresource) {
    _search = Utils.joinMainSearchkey(searchlist)
    _search = _search ? 'where ' + _search : ''
  }
  if (setting.order && _dataresource) {
    _dataresource = `select top 1000 ${arr_field.join(',')} from (select ${arr_field.join(',')} ,ROW_NUMBER() over(order by ${setting.order}) as rows from ${_dataresource} ${_search}) tmptable order by tmptable.rows `
  } else if (_dataresource) {
    _dataresource = `select top 1000 ${arr_field.join(',')} from ${_dataresource} ${_search} `
  }
  // 测试系统打印查询语句
  if ((options.sysType === 'local' && !window.GLOB.systemType) || window.debugger === true) {
    _customScript &&  console.info(`${setting.$name ? `/*组件-${setting.$name} 自定义脚本*/\n` : ''}${_dataresource ? '' : '/*不执行默认sql*/\n'}${_customScript}`)
    _dataresource &&  console.info(`${setting.$name ? `/*组件-${setting.$name} 数据源*/\n` : ''}` + _dataresource)
  }
  return {
    name: dataName,
    columns: columns,
    par_tablename: '',
    type: format === 'array' ? format : '',
    primaryKey: setting.primaryKey || '',
    foreign_key: '',
    sql: _dataresource,
    script: _customScript
  }
}
/**
 * @description 生成sPC_Get_structured_data请求参数
 * 1、把大接口sPC_Get_structured_data的ltext拆成三份,第一段:@LText1,第二段@LText,第三段@LText2
 */
export function getStructuredParams (params, config, BID = '') {
  let LText_field = []
  let diffUser = false
  let userName = sessionStorage.getItem('User_Name') || ''
  let fullName = sessionStorage.getItem('Full_Name') || ''
  let city = sessionStorage.getItem('city') || ''
  if (sessionStorage.getItem('isEditState') === 'true') {
    userName = sessionStorage.getItem('CloudUserName') || ''
    fullName = sessionStorage.getItem('CloudFullName') || ''
  }
  let _LText = params.map((item, index) => {
    let _script = item.script
    if (index === 0) {
      _script = `declare @ErrorCode nvarchar(50),@retmsg nvarchar(4000),@UserName nvarchar(50),@FullName nvarchar(50),@login_city nvarchar(50)
        select @ErrorCode='',@retmsg ='',@UserName='${userName}', @FullName='${fullName}', @login_city='${city}'
        ${_script}
      `
    }
    if (!diffUser && (/@userid@/ig.test(item.sql) || /@userid@/ig.test(_script))) {
      diffUser = true
    }
    item.columns.forEach(cell => {
      LText_field.push(`Select '${item.name}' as tablename,'${cell.field}' as fieldname,'${cell.datatype}' as field_type`)
    })
    return `Select '${item.name}' as tablename,'${window.btoa(window.encodeURIComponent(item.sql))}' as LText,'${window.btoa(window.encodeURIComponent(_script))}' as Lcustomize,'${item.type}' as table_type,'${item.primaryKey}' as primary_key,'${item.par_tablename}' as par_tablename,'${item.foreign_key}' as foreign_key,'${index}' as Sort`
  })
  let param = {
    func: 'sPC_Get_structured_data',
    LText: _LText.join(' union all '),
    LText_field: LText_field.join(' union all '),
    BID: BID
  }
  let { LText, LText1, LText2 } = Utils.sPCInUpDeFormatOptions(param.LText)
  param.LText1 = LText1
  param.LText = LText
  param.LText2 = LText2
  param.LText_field = Utils.formatOptions(param.LText_field)
  if (config.cacheUseful === 'true') {
    param.time_type = config.timeUnit
    param.time_limit = config.cacheTime
    if (diffUser) {
      param.userid = sessionStorage.getItem('UserID')
    }
    param.data_md5 = md5(JSON.stringify(param))
  }
  param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
  param.secretkey = Utils.encrypt(param.LText, param.timestamp)
  return param
}