king
2023-09-19 9a1416e0b5cdb40f49b3c2061b04b35551d77e99
src/utils/utils-custom.js
@@ -932,6 +932,178 @@
}
/**
 * @description 格式化搜索条件
 */
export function formatSearch (searches) {
  if (!searches) return []
  let newsearches = []
  searches.forEach(item => {
    if (!item.field) return
    if (item.type === 'group') {
      newsearches.push({
        key: item.field,
        match: '',
        type: item.type,
        value: 'customized',
        forbid: true
      }, {
        key: item.datefield,
        match: 'between',
        type: 'daterange',
        value: '1949-10-01 00:00:00.000,1949-10-02 00:00:00.000',
        forbid: item.query === 'false'
      })
    } else {
      let value = item.initval
      let type = item.type
      if (item.type === 'date') {
        value = '1949-10-01 00:00:00.000'
      } else if (item.type === 'datemonth') {
        value = '1949-10-01 00:00:00.000,1949-10-02 00:00:00.000'
      } else if (item.type === 'dateweek') {
        value = '1949-10-01 00:00:00.000,1949-10-02 00:00:00.000'
      } else if (item.type === 'daterange') {
        value = '1949-10-01 00:00:00.000,1949-10-02 00:00:00.000'
      } else if (item.type === 'range') {
        value = `${item.minValue},${item.maxValue}`
      } else if (item.type === 'multiselect' || (item.type === 'checkcard' && item.multiple === 'true')) {
        type = 'multi'
        value = '0'
      } else {
        value = '0'
      }
      newsearches.push({
        key: item.field,
        match: item.match,
        type: type,
        value: value,
        precision: item.precision || 'day',
        forbid: item.query === 'false'
      })
    }
  })
  return newsearches
}
/**
 * @description 拼接where条件
 */
export function joinMainSearchkey (searches) {
  if (!searches || searches.length === 0) return ''
  let searchText = []
  searches.forEach(item => {
    if (item.forbid) return
    if (item.type === 'text' || item.type === 'select') { // 综合搜索,文本或下拉,所有字段拼接
      let str = item.match === 'like' || item.match === 'not like' ? '%' : ''
      let fields = item.key.split(',').map(field => {
        return field + ' ' + item.match + ' \'' + str + item.value + str + '\''
      })
      searchText.push('(' + fields.join(' OR ') + ')')
    } else if (item.type === 'checkcard') {
      let str = item.match === 'like' || item.match === 'not like' ? '%' : ''
      searchText.push('(' + item.key + ' ' + item.match + ' \'' + str + item.value + str + '\')')
    } else if (item.type === 'multi') {
      searchText.push(`('${item.value}' ${item.match} '%'+${item.key}+'%')`)
    } else if (item.type === 'date') {
      searchText.push('(' + item.key + ' ' + item.match + ' \'' + item.value + '\')')
    } else if (item.type === 'datemonth' || item.type === 'dateweek' || item.type === 'range') {
      let val = item.value.split(',')
      searchText.push('(' + item.key + ' >= \'' + val[0] + '\' AND ' + item.key + ' < \'' + val[1] + '\')')
    } else if (item.type === 'daterange') {
      let val = item.value.split(',')
      let _skey = item.key
      let _ekey = item.key
      if (/,/.test(item.key)) {
        _skey = item.key.split(',')[0]
        _ekey = item.key.split(',')[1]
      }
      searchText.push('(' + _skey + ' >= \'' + val[0] + '\' AND ' + _ekey + ' < \'' + val[1] + '\')')
    } else {
      searchText.push('(' + item.key + ' ' + item.match + ' \'' + item.value + '\')')
    }
  })
  return searchText.join(' AND ')
}
/**
 * @description 获取搜索正则替换
 */
export function getSearchRegs (searches) {
  if (!searches) return []
  let options = []
  let fieldmap = new Map()
  searches.forEach(item => {
    if (item.type === 'date') {
      if (fieldmap.has(item.key)) {
        options.push({
          reg: new RegExp('@' + item.key + '1@', 'ig'),
          value: `'${item.value}'`
        })
      } else {
        fieldmap.set(item.key, true)
        options.push({
          reg: new RegExp('@' + item.key + '@', 'ig'),
          value: `'${item.value}'`
        })
      }
    } else if (['dateweek', 'datemonth', 'range'].includes(item.type)) {
      let val = item.value.split(',')
      options.push({
        reg: new RegExp('@' + item.key + '@', 'ig'),
        value: `'${val[0]}'`
      }, {
        reg: new RegExp('@' + item.key + '1@', 'ig'),
        value: `'${val[1]}'`
      })
    } else if (item.type === 'daterange') {
      let val = item.value.split(',')
      let _skey = item.key
      let _ekey = item.key + '1'
      if (/,/.test(item.key)) {
        _skey = item.key.split(',')[0]
        _ekey = item.key.split(',')[1]
      }
      options.push({
        reg: new RegExp('@' + _skey + '@', 'ig'),
        value: `'${val[0]}'`
      }, {
        reg: new RegExp('@' + _ekey + '@', 'ig'),
        value: `'${val[1]}'`
      })
    } else if (item.type === 'text' || item.type === 'select') {
      item.key.split(',').forEach(field => {
        options.push({
          reg: new RegExp('@' + field + '@', 'ig'),
          value: `'${item.value}'`
        })
      })
    } else {
      options.push({
        reg: new RegExp('@' + item.key + '@', 'ig'),
        value: `'${item.value}'`
      })
    }
  })
  return options
}
/**
 * @description 重置移动端style
 * @return {Object}  style
 */