king
2020-12-04 d441fa1e1cc80f4ea462a750a42a2b25c1f2b202
src/utils/utils-datamanage.js
@@ -6,7 +6,6 @@
  /**
   * @description 数据源名称,用于统一查询
   * @param {Object}   setting      数据源设置
   * @param {String}   customScript 自定义脚本
   * @param {String}   arrFields    查询字段
   * @param {Array}    search       搜索条件
   * @param {String}   orderBy      排序方式
@@ -14,23 +13,22 @@
   * @param {Number}   pageSize     每页数量
   * @param {String}   BID          上级ID
   * @param {String}   menuType     菜单类型,普通菜单与HS
   * @param {Boolean}  dataManager  数据权限
   * @return {Object}  param
   */
  static getQueryDataParams (setting, customScript, arrFields, search = [], orderBy = '', pageIndex = 1, pageSize = 10, BID, menuType, dataManager) {
  static getQueryDataParams (setting, arrFields, search = [], orderBy = '', pageIndex = 1, pageSize = 10, BID, menuType, id) {
    let param = null
    if (setting.interType === 'system' || (setting.interType === 'inner' && !setting.innerFunc)) {
      param = this.getDefaultQueryParam(setting, customScript, arrFields, search, orderBy, pageIndex, pageSize, menuType)
      param = this.getDefaultQueryParam(setting, arrFields, search, orderBy, pageIndex, pageSize, menuType, id)
    } else {
      param = this.getCustomQueryParam(setting, search, orderBy, pageIndex, pageSize, menuType)
      param = this.getCustomQueryParam(setting, search, orderBy, pageIndex, pageSize, menuType, id)
    }
    if (BID) {
      param.BID = BID
    }
    // 数据管理权限
    if (dataManager) {
    if (sessionStorage.getItem('dataM') === 'true') {
      param.dataM = 'Y'
    }
@@ -40,14 +38,16 @@
  /**
   * @description 获取用户自定义存储过程传参
   */
  static getCustomQueryParam (setting, search, orderBy, pageIndex, pageSize, menuType) {
  static getCustomQueryParam (setting, search, orderBy, pageIndex, pageSize, menuType, id) {
    let param = Utils.formatCustomMainSearch(search)
    if (orderBy) {
      param.OrderCol = orderBy
    }
    if (setting.laypage) {
    if (id) {
      param.ID = id
    } else if (setting.laypage) {
      param.PageIndex = pageIndex
      param.PageSize = pageSize
    }
@@ -80,20 +80,33 @@
  /**
   * @description 获取系统存储过程 sPC_Get_TableData 的参数
   */
  static getDefaultQueryParam (setting, customScript, arrFields, search, orderBy, pageIndex, pageSize, menuType) {
  static getDefaultQueryParam (setting, arrFields, search, orderBy, pageIndex, pageSize, menuType, id) {
    let param = {
      func: 'sPC_Get_TableData',
      obj_name: 'data',
      arr_field: arrFields,
      default_sql: setting.execute ? 'true' : 'false'
    }
    let _dataresource = setting.dataresource
    let _customScript = customScript
    let _customScript = ''
    if (setting.customScript) {
      _customScript = `declare @ErrorCode nvarchar(50),@retmsg nvarchar(4000) select @ErrorCode='',@retmsg =''
        ${setting.customScript}
      `
    }
    let regoptions = null
    if (setting.queryType === 'statistics' || _customScript) {
      let allSearch = Utils.getAllSearchOptions(search)
      let userName = sessionStorage.getItem('User_Name') || ''
      let fullName = sessionStorage.getItem('Full_Name') || ''
      if (sessionStorage.getItem('isEditState') === 'true') {
        userName = sessionStorage.getItem('CloudUserName') || ''
        fullName = sessionStorage.getItem('CloudFullName') || ''
      }
      regoptions = allSearch.map(item => {
        return {
@@ -102,6 +115,12 @@
        }
      })
      regoptions.push({
        reg: new RegExp('@userName@', 'ig'),
        value: userName
      }, {
        reg: new RegExp('@fullName@', 'ig'),
        value: fullName
      }, {
        reg: new RegExp('@orderBy@', 'ig'),
        value: orderBy
      }, {
@@ -119,11 +138,14 @@
      regoptions.forEach(item => {
        _dataresource = _dataresource.replace(item.reg, item.value)
      })
    } else if (_dataresource) {
    } else if (_dataresource && !id) {
      _search = Utils.joinMainSearchkey(search)
      if (_search) {
        _search = 'where ' + _search
      }
    } else if (_dataresource && id) {
      _search = Utils.joinMainSearchkey(search)
      _search = `where ${_search ? _search + ' AND ' : ''} ${setting.primaryKey || 'ID'}='${id}'`
    }
    if (_customScript) {
@@ -135,7 +157,7 @@
    let LText = ''
    let DateCount = ''
    if (_dataresource && setting.laypage && orderBy) {
    if (_dataresource && setting.laypage && orderBy && !id) {
      LText = ` select top ${pageSize} ${arrFields} from (select ${arrFields} ,ROW_NUMBER() over(order by ${orderBy}) as rows from ${_dataresource} ${_search}) tmptable where rows > ${pageSize * (pageIndex - 1)} order by tmptable.rows `
      DateCount = `select count(1) as total from ${_dataresource} ${_search}`
    } else if (_dataresource && orderBy) {
@@ -162,18 +184,128 @@
    // 测试系统打印查询语句
    if ((options.sysType === 'local' && !window.GLOB.systemType) || window.debugger === true) {
      _customScript &&  console.log(`${LText ? '' : '/*不执行默认sql*/\n'}${_customScript}`)
      LText &&  console.log(LText)
      _customScript &&  console.info(`${LText ? '' : '/*不执行默认sql*/\n'}${_customScript}`)
      LText &&  console.info(LText)
    }
    
    param.custom_script = Utils.formatOptions(_customScript)
    param.LText = Utils.formatOptions(LText)
    param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss') + '.000'
    param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
    param.secretkey = Utils.encrypt(param.LText, param.timestamp)
    param.DateCount = Utils.formatOptions(DateCount)
    if (menuType === 'HS') { // 云端数据验证
      param.open_key = Utils.encrypt(param.secretkey, param.timestamp, true)
      param.open_key = Utils.encryptOpenKey(param.secretkey, param.timestamp)
    }
    return param
  }
  /**
   * @description 获取系统存储过程 sPC_Get_TableData 合计值的参数
   */
  static getStatQueryDataParams (setting, statFields, search, orderBy, BID, menuType) {
    let param = {
      func: 'sPC_Get_TableData',
      obj_name: 'data',
      arr_field: statFields.map(col => col.field).join(','),
      default_sql: setting.execute ? 'true' : 'false'
    }
    let _dataresource = setting.dataresource
    let _customScript = ''
    if (setting.customScript) {
      _customScript = `declare @ErrorCode nvarchar(50),@retmsg nvarchar(4000) select @ErrorCode='',@retmsg =''
        ${setting.customScript}
      `
    }
    let regoptions = null
    if (setting.queryType === 'statistics' || _customScript) {
      let allSearch = Utils.getAllSearchOptions(search)
      let userName = sessionStorage.getItem('User_Name') || ''
      let fullName = sessionStorage.getItem('Full_Name') || ''
      if (sessionStorage.getItem('isEditState') === 'true') {
        userName = sessionStorage.getItem('CloudUserName') || ''
        fullName = sessionStorage.getItem('CloudFullName') || ''
      }
      regoptions = allSearch.map(item => {
        return {
          reg: new RegExp('@' + item.key + '@', 'ig'),
          value: `'${item.value}'`
        }
      })
      regoptions.push({
        reg: new RegExp('@userName@', 'ig'),
        value: userName
      }, {
        reg: new RegExp('@fullName@', 'ig'),
        value: fullName
      }, {
        reg: new RegExp('@orderBy@', 'ig'),
        value: orderBy
      }, {
        reg: new RegExp('@pageSize@', 'ig'),
        value: 999999
      }, {
        reg: new RegExp('@pageIndex@', 'ig'),
        value: 1
      })
    }
    let _search = Utils.joinMainSearchkey(search)
    if (_search) {
      _search = 'where ' + _search
    }
    if (setting.queryType === 'statistics') { // 统计数据源,内容替换
      regoptions.forEach(item => {
        _dataresource = _dataresource.replace(item.reg, item.value)
      })
    }
    if (_customScript) {
      regoptions.forEach(item => {
        _customScript = _customScript.replace(item.reg, item.value)
      })
    }
    let LText = ` select ${statFields.map(col => `isnull(sum(${col.field}),0) as ${col.field}`).join(',')} from ${_dataresource} ${_search} `
    if (_customScript) {
      LText = `${LText}
        aaa:
        if @ErrorCode!=''
          insert into tmp_err_retmsg (ID, ErrorCode, retmsg, CreateUserID) select @time_id@,@ErrorCode, @retmsg,@UserID@
      `
    }
    // 测试系统打印查询语句
    if ((options.sysType === 'local' && !window.GLOB.systemType) || window.debugger === true) {
      _customScript &&  console.info(`${LText ? '' : '/*不执行默认sql*/\n'}${_customScript}`)
      LText &&  console.info(LText)
    }
    param.custom_script = Utils.formatOptions(_customScript)
    param.LText = Utils.formatOptions(LText)
    param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
    param.secretkey = Utils.encrypt(param.LText, param.timestamp)
    param.DateCount = ''
    if (menuType === 'HS') { // 云端数据验证
      param.open_key = Utils.encryptOpenKey(param.secretkey, param.timestamp)
    }
    if (BID) {
      param.BID = BID
    }
    // 数据管理权限
    if (sessionStorage.getItem('dataM') === 'true') {
      param.dataM = 'Y'
    }
    return param