king
2024-09-10 4d6b9f8a2d7b316633e43b489eae9ada949c07b5
src/utils/utils-datamanage.js
@@ -5,6 +5,133 @@
import MKEmitter from '@/utils/events.js'
import Utils from './utils.js'
const getBackendQueryParam = (setting, search, orderBy, pageIndex, pageSize, id, BID, year, dataName) => {
  let item = window.GLOB.CacheData.get('sql_' + setting.uuid)
  let searchKeys = null
  if (setting.dataresource) {
    searchKeys = []
    if (search.length) {
      searchKeys = Utils.getSearchkeys(search)
    }
    if (id) {
      if (/^excel:/.test(id)) {
        id = id.replace(/^excel:/, '')
        searchKeys.push({
          key: setting.primaryKey || 'ID',
          match: '',
          type: 'text_in',
          value: id
        })
      } else {
        searchKeys.push({
          key: setting.primaryKey || 'ID',
          match: '03',
          type: 'text',
          value: id
        })
      }
    }
  }
  let values = {
    time_id: Utils.getguid(),
    roleid: sessionStorage.getItem('role_id') || '',
    mk_departmentcode: sessionStorage.getItem('departmentcode') || '',
    mk_organization: sessionStorage.getItem('organization') || '',
    mk_user_type: sessionStorage.getItem('mk_user_type') || '',
    mk_nation: sessionStorage.getItem('nation') || '',
    mk_province: sessionStorage.getItem('province') || '',
    mk_city: sessionStorage.getItem('city') || '',
    mk_district: sessionStorage.getItem('district') || '',
    mk_address: sessionStorage.getItem('address') || '',
    orderby: orderBy || '',
    pagesize: setting.laypage ? pageSize : '9999',
    pageindex: pageIndex,
    id: id || '',
    bid: BID || '',
    typename: 'admin',
    datam: sessionStorage.getItem('dataM') === 'true' ? 'Y' : '',
    datam_begin: sessionStorage.getItem('dataM') === 'true' ? 'Y' : '',
    datam_end: sessionStorage.getItem('dataM') === 'true' ? 'Y' : '',
    mk_obj_name: dataName || 'data'
  }
  if (setting.hasExtend) {
    values.mk_time = setting.extendTime
  }
  if (setting.$re_year) {
    values.mk_year = year
  }
  if (window.GLOB.externalDatabase !== null) {
    values.db = window.GLOB.externalDatabase
  }
  if (item.works_flow_code) {
    values.works_flow_code = item.works_flow_code
  }
  item.urlkeys && item.urlkeys.forEach(key => {
    values[key] = item.urlparam[key]
  })
  let allSearch = Utils.getAllSearchOptions(search)
  allSearch.forEach(cell => {
    values[cell.key.toLowerCase()] = cell.value
  })
  let exps = []
  if (searchKeys) {
    exps.push({
      key: 'mk_search',
      value: searchKeys
    })
  }
  item.reps.forEach(n => {
    let key = n.toLowerCase()
    if (values.hasOwnProperty(key)) {
      exps.push({
        key: n,
        value: values[key]
      })
    }
  })
  let md5_id = ''
  if (window.GLOB.probation) {
    md5_id = md5(item.id + JSON.stringify(exps) + Math.floor(new Date().getTime() / 600000))
    md5_id = moment().format('YYYYMMDDHHmmss') + md5_id.slice(-18)
  }
  let cell = {
    id: item.id,
    exps: exps,
    menuname: setting.$name || '',
    md5_id: md5_id
  }
  if (dataName) {
    cell.dataName = dataName
    cell.luser = item.luser
    return cell
  }
  let param = {
    $backend: true,
    data: [cell]
  }
  if (setting.database === 'sso' && window.GLOB.mainSystemApi) {
    param.rduri = window.GLOB.mainSystemApi
  }
  return param
}
export default class DataUtils {
  /**
   * @description 数据源统一查询
@@ -13,22 +140,23 @@
    let param = null
    if (setting.interType === 'system') {
      if (window.backend && window.GLOB.CacheData.has('sql_' + setting.uuid) && !window.GLOB.mkHS) {
        param = this.getDefaultQueryParam(setting, search, orderBy, pageIndex, pageSize, id, BID, year)
        this.getBackendQueryParam(setting, search, orderBy, pageIndex, pageSize, id, BID, year)
      if (window.backend && window.GLOB.CacheData.has('sql_' + setting.uuid)) {
        param = getBackendQueryParam(setting, search, orderBy, pageIndex, pageSize, id, BID, year)
      } else {
        param = this.getDefaultQueryParam(setting, search, orderBy, pageIndex, pageSize, id, BID, year)
        param.BID = BID || ''
        param.dataM = sessionStorage.getItem('dataM') === 'true' ? 'Y' : ''
      }
    } else {
      param = this.getCustomQueryParam(setting, search, orderBy, pageIndex, pageSize, id, year)
    }
    if (BID) {
      param.BID = BID
    }
    // 数据管理权限
    if (sessionStorage.getItem('dataM') === 'true') {
      param.dataM = 'Y'
      if (BID) {
        param.BID = BID
      }
      // 数据管理权限
      if (sessionStorage.getItem('dataM') === 'true') {
        param.dataM = 'Y'
      }
    }
    return param
@@ -336,235 +464,6 @@
  }
  /**
   * @description 获取系统存储过程的参数
   */
  static getBackendQueryParam (setting, search, orderBy, pageIndex, pageSize, id, BID, year) {
    let item = window.GLOB.CacheData.get('sql_' + setting.uuid)
    let param = {}
    let searchKeys = null
    if (setting.dataresource) {
      searchKeys = []
      if (search.length) {
        searchKeys = Utils.getSearchkeys(search)
      }
      if (id) {
        if (/^excel:/.test(id)) {
          id = id.replace(/^excel:/, '')
          searchKeys.push({
            key: setting.primaryKey || 'ID',
            match: 'in',
            type: 'text_in',
            value: id
          })
        } else {
          searchKeys.push({
            key: setting.primaryKey || 'ID',
            match: '=',
            type: 'text',
            value: id
          })
        }
      }
    }
    let values = {
      userid: sessionStorage.getItem('UserID') || '',
      time_id: Utils.getguid(),
      username: sessionStorage.getItem('User_Name') || '',
      fullname: sessionStorage.getItem('Full_Name') || '',
      roleid: sessionStorage.getItem('role_id') || '',
      mk_departmentcode: sessionStorage.getItem('departmentcode') || '',
      mk_organization: sessionStorage.getItem('organization') || '',
      mk_user_type: sessionStorage.getItem('mk_user_type') || '',
      mk_nation: sessionStorage.getItem('nation') || '',
      mk_province: sessionStorage.getItem('province') || '',
      mk_city: sessionStorage.getItem('city') || '',
      mk_district: sessionStorage.getItem('district') || '',
      mk_address: sessionStorage.getItem('address') || '',
      orderby: orderBy,
      pagesize: setting.laypage ? pageSize : '9999',
      pageindex: pageIndex,
      id: id || '',
      bid: BID || '',
      loginuid: sessionStorage.getItem('LoginUID') || '',
      sessionuid: localStorage.getItem('SessionUid') || '',
      appkey: window.GLOB.appkey || '',
      lang: sessionStorage.getItem('lang'),
      typename: 'admin',
      datam: sessionStorage.getItem('dataM') === 'true' ? 'Y' : '',
      datam_begin: sessionStorage.getItem('dataM') === 'true' ? 'Y' : '',
      datam_end: sessionStorage.getItem('dataM') === 'true' ? 'Y' : '',
      // datam_begin: sessionStorage.getItem('dataM') === 'true' ? '/*' : '',
      // datam_end: sessionStorage.getItem('dataM') === 'true' ? '*/' : '',
      mk_obj_name: 'data'
    }
    if (setting.hasExtend) {
      values.mk_time = setting.extendTime
    }
    if (setting.$re_year) {
      values.mk_year = year
    }
    if (window.GLOB.externalDatabase !== null) {
      values.db = window.GLOB.externalDatabase
    }
    if (item.works_flow_code) {
      values.works_flow_code = item.works_flow_code
    }
    item.urlkeys.forEach(key => {
      values[key] = item.urlparam[key]
    })
    let allSearch = Utils.getAllSearchOptions(search)
    allSearch.forEach(cell => {
      values[cell.key.toLowerCase()] = cell.value
    })
    let list = []
    if (searchKeys) {
      list.push({
        key: 'mk_search',
        value: searchKeys
      })
    }
    let sql = item.sql
    item.reps.forEach(n => {
      let key = n.toLowerCase()
      if (values.hasOwnProperty(key)) {
        list.push({
          key: n,
          value: values[key]
        })
      }
    })
    // let list = [
    //   {
    //     key: "mk_search",
    //     value: [
    //       { key: "nvarchar1,nvarchar3", match: "like", type: "text_or", value: "www" },
    //       { key: "int2", match: ">=", type: "number", value: "0" },
    //       { key: "int2", match: "<=", type: "number", value: "50" }
    //     ]
    //   },
    //   { key: "mk_obj_name", value: "data" },
    //   { key: "pageSize", value: 10 },
    //   { key: "orderBy", value: "ID desc" },
    //   { key: "pageIndex", value: 1 },
    //   {
    //     key: 'mk_excel_data',
    //     value: [[`'1'`, `'2'`, `'3'`, `'4'`, `'5'`], [`'6'`, `'7'`, `'8'`, `'9'`, `'10'`]]
    //   },
    //   {
    //     key: 'mk_outer_params',
    //     value: [
    //       {
    //         tb: '#tb1',
    //         keys: ['[nvarchar1]', '[nvarchar2]', '[nvarchar3]', '[mk_level]','[mk_id]','[mk_bid]'],
    //         values: [[`'1'`, `'2'`, `'3'`, `'4'`, `'5'`, `'6'`], [`'7'`, `'8'`, `'9'`, `'10'`, `'11'`, `'12'`]]
    //       },
    //       {
    //         tb: '@tb2',
    //         keys: ['[field1]', '[field2]', '[field3]', '[mk_level]','[mk_id]','[mk_bid]'],
    //         values: [[`'1'`, `'2'`, `'3'`, `'4'`, `'5'`, `'6'`], [`'7'`, `'8'`, `'9'`, `'10'`, `'11'`, `'12'`]]
    //       }
    //     ]
    //   },
    // ]
    list.forEach(cell => {
      if (cell.key === 'mk_outer_params') {
        let mk_outer_params = []
        cell.value.forEach(n => {
          mk_outer_params.push(` Insert into ${n.tb} (${n.keys.join(',')}) `)
          let vals = []
          n.values.forEach(m => {
            vals.push(` Select ${m.join(',')} `)
          })
          mk_outer_params.push(vals.join(' Union all '))
        })
        mk_outer_params = mk_outer_params.join('')
        sql = sql.replace(/@mk_outer_params@/ig, mk_outer_params)
      } else if (cell.key === 'mk_excel_data') {
        let mk_excel_data = []
        cell.value.forEach(n => {
          mk_excel_data.push(`Select ${n.join(',')}`)
        })
        mk_excel_data = mk_excel_data.join(' Union all ')
        sql = sql.replace(/@mk_excel_data@/ig, mk_excel_data)
      } else if (cell.key === 'datam_begin' || cell.key === 'mk_check_begin') {
        if (cell.value === 'Y') {
          sql = sql.replace(new RegExp('@' + cell.key + '@', 'ig'), '/*')
        } else {
          sql = sql.replace(new RegExp('@' + cell.key + '@', 'ig'), '')
        }
      } else if (cell.key === 'datam_end' || cell.key === 'mk_check_end') {
        if (cell.value === 'Y') {
          sql = sql.replace(new RegExp('@' + cell.key + '@', 'ig'), '*/')
        } else {
          sql = sql.replace(new RegExp('@' + cell.key + '@', 'ig'), '')
        }
      } else if (cell.key === 'mk_search') {
        let mk_search = []
        cell.value.forEach(n => {
          let str = n.match === 'like' || n.match === 'not like' ? '%' : ''
          if (n.type === 'text_or') {
            let vals = n.key.split(',').map(key => {
              return `${key} ${n.match} '${str}${n.value}${str}'`
            })
            mk_search.push(`(${vals.join(' OR ')})`)
          } else if (n.type === 'text_multi') {
            mk_search.push(`'${n.value}' ${n.match} '%'+${n.key}+'%'`)
          } else if (n.type === 'number') {
            mk_search.push(`${n.key} ${n.match} ${n.value}`)
          } else if (n.type === 'text_in') {
            mk_search.push(`${n.key} in (select ID from dbo.SplitComma('${n.value}'))`)
          } else {
            mk_search.push(`${n.key} ${n.match} '${str}${n.value}${str}'`)
          }
        })
        if (mk_search.length === 0) {
          mk_search = ''
        } else {
          mk_search = 'where ' + mk_search.join(' AND ')
        }
        sql = sql.replace(/@mk_search@/ig, mk_search)
      } else {
        sql = sql.replace(new RegExp('@' + cell.key + '@', 'ig'), cell.value)
      }
    })
    param.list = [
      {
        id: item.v_id,
        regExps: list,
        menuname: setting.$name || '',
        s_debug_type: window.GLOB.probation ? 'Y' : '',
        // md5_id: window.GLOB.probation ? md5(JSON.stringify(list)) : '',
      }
    ]
    if (setting.database === 'sso' && window.GLOB.mainSystemApi) {
      param.rduri = window.GLOB.mainSystemApi
    }
    return param
  }
  /**
   * @description 获取系统存储过程合计值的参数
   */
  static getStatQueryDataParams (setting, statFields, search, orderBy, BID) {
@@ -731,8 +630,12 @@
/**
 * @description 生成单个组件sPC_Get_structured_data请求参数
 */
export function getStructDefaultParam (component, searchlist, first) {
export function getStructDefaultParam (component, searchlist, first, BID) {
  const { columns, setting, dataName, format, uuid } = component
  if (window.backend && window.GLOB.CacheData.has('sql_' + uuid)) {
    return getBackendQueryParam(setting, searchlist, setting.order, 1, 1000, '', BID, '', dataName)
  }
  let _dataresource = setting.dataresource
  let _customScript = setting.customScript
@@ -789,7 +692,7 @@
  return {
    uuid: uuid,
    name: dataName,
    dataName: dataName,
    $name: setting.$name,
    columns: columns,
    par_tablename: '',
@@ -806,6 +709,38 @@
 * @description 生成sPC_Get_structured_data请求参数
 */
export function getStructuredParams (params, config, BID) {
  if (window.backend && params[0].exps) {
    let param = {
      $backend: true,
      data: params.map(item => {
        let cell = {...item}
        delete cell.dataName
        delete cell.luser
        return cell
      })
    }
    if (config.cacheUseful === 'true') {
      param.time_limit = config.cacheTime
      if (config.timeUnit === 'day') {
        param.time_limit = param.time_limit * 1440
      } else if (config.timeUnit === 'hour') {
        param.time_limit = param.time_limit * 60
      }
      if (params.findIndex(item => item.luser) > -1) {
        param.data_md5 = md5(window.GLOB.appkey + params[0].id + sessionStorage.getItem('UserID'))
      } else {
        param.data_md5 = md5(window.GLOB.appkey + params[0].id)
      }
    }
    return param
  }
  let LText_field = []
  let diffUser = false
@@ -845,9 +780,9 @@
    }
    item.columns.forEach(cell => {
      LText_field.push(`Select '${item.name}' as tablename,'${cell.field}' as fieldname,'${cell.datatype}' as field_type`)
      LText_field.push(`Select '${item.dataName}' as tablename,'${cell.field}' as fieldname,'${cell.datatype}' as field_type`)
    })
    return `Select '${item.name}' as tablename,'${window.btoa(window.encodeURIComponent(_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`
    return `Select '${item.dataName}' as tablename,'${window.btoa(window.encodeURIComponent(_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 = {