searchQueryBuilder.js 10.7 KB
Newer Older
wangdanlei's avatar
wangdanlei committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

/**
 * 构建过滤条件
 */
import { getOpenPropsByLists, setOpenPropsSearch } from './util'
class SearchItems {
  /**
   *
   * @param {String} operator
   * @param {SearchItems} children
   */
  constructor(operator = 'AND') {
    this.data = {
      operator,
      items: []
    }
    Object.preventExtensions(this)
  }

  // 合并两个条件
  static combine(searchItemsA, searchItemsB, operator = 'AND') {
    const searchItems = new SearchItems(operator)
    searchItems.addChildren(searchItemsA)
    searchItems.addChildren(searchItemsB)
    return searchItems
  }

  /**
   * 添加一个搜索条件
   * @param {String} fieldName
   * @param {*} value
   * @param {String} operator
   */
  addItem(fieldName, value, operator = 'EQ') {
    this.data.items.push({
      fieldName,
      operator,
      value
    })
    return this
  }

  /**
   * 添加一个搜索条件,条件值为空时忽略
   * @param {String} fieldName
   * @param {*} value
   * @param {String} operator
   */
  $addItem(fieldName, value, operator = 'EQ', value1) {
    if (fieldName && value) {
      if (operator === 'BTWN') {
        this.data.items.push({
          fieldName,
          operator,
          value,
          value1
        })
wangdanlei's avatar
wangdanlei committed
58 59 60 61 62 63
      } else if (operator === 'ISNULL') {
        this.data.items.push({
          fieldName,
          operator: 'EQ',
          value: null
        })
wangdanlei's avatar
wangdanlei committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
      } else {
        this.data.items.push({
          fieldName,
          operator,
          value
        })
      }
    } else {
      console.info(`已忽略查询条件 属性:${fieldName || '未定义'},值:${value || '未定义'}`)
    }
    return this
  }

  /**
   * IN
   * @param {String} fieldName
   * @param {Array} values
   */
  in(fieldName, values = []) {
    this.addItem(fieldName, values, 'IN')
    return this
  }

  /**
   * IN
   * @param {String} fieldName
   * @param {Array} values
   */
  $in(fieldName, values) {
    this.$addItem(fieldName, values, 'IN')
    return this
  }

  /**
   * 等于
   * @param {String} fieldName
   * @param {*} value
   */
  eq(fieldName, value) {
    this.addItem(fieldName, value)
    return this
  }

  /**
   * 等于,条件值为空时忽略
   * @param {String} fieldName
   * @param {*} value
   */
  $eq(fieldName, value) {
    this.$addItem(fieldName, value)
    return this
  }

  /**
   * 不等于
   * @param {String} fieldName
   * @param {*} value
   */
  ne(fieldName, value) {
    this.addItem(fieldName, value, 'NEQ')
    return this
  }

  /**
   * 不等于,条件值为空时忽略
   * @param {String} fieldName
   * @param {*} value
   */
  $ne(fieldName, value) {
    this.$addItem(fieldName, value, 'NEQ')
    return this
  }

  /**
   * 大于
   * @param {String} fieldName
   * @param {*} value
   */
  gt(fieldName, value) {
    this.addItem(fieldName, value, 'GT')
    return this
  }

  /**
   * 大于,条件值为空时忽略
   * @param {String} fieldName
   * @param {*} value
   */
  $gt(fieldName, value) {
    this.$addItem(fieldName, value, 'GT')
    return this
  }

  /**
   * 小于
   * @param {String} fieldName
   * @param {*} value
   */
  lt(fieldName, value) {
    this.addItem(fieldName, value, 'LT')
    return this
  }

  /**
   * 小于,条件值为空时忽略
   * @param {String} fieldName
   * @param {*} value
   */
  $lt(fieldName, value) {
    this.$addItem(fieldName, value, 'LT')
    return this
  }

  /**
   * 模糊
   * @param {String} fieldName
   * @param {*} value
   */
  like(fieldName, value) {
    this.addItem(fieldName, value, 'LIKE')
    return this
  }

  /**
   * 模糊,条件值为空时忽略
   * @param {String} fieldName
   * @param {*} value
   */
  $like(fieldName, value) {
    this.$addItem(fieldName, value, 'LIKE')
    return this
  }

  /**
   * 添加子查询
   */
  addChildren(searchItems) {
    if (!this.data.children) {
      this.data.children = []
    }
    if (searchItems.data) {
      this.data.children.push(searchItems.data)
    }
    return this
  }

  /**
   * 设置为 and 查询
   */
  and() {
    this.data.operator = 'AND'
    return this
  }

  /**
   * 设置为 or 查询
   */
  or() {
    this.data.operator = 'OR'
    return this
  }
}

const SEARCH_MODE = {
  SEARCH: 'search',
  RECURSION: 'recursion'
}

/**
 * 构建搜索请求
 */
class SearchQueryBuilder {
  /**
   * 构建搜索请求
   * @param {DEABaseService} service
   * @param {SearchItems} searchItems
   */
  constructor(service, fixSearchItems = null) {
    this.service = service

    this.queryData = {
      indices: this.service ? [this.service.$modelName] : [],
      pageFrom: 1, // 页码
      pageSize: 9999, // 每页行数
      openProps: [] // 展开属性
    }

    /**
     * 搜索模式,search or recursion
     */
    this.searchMode = SEARCH_MODE.SEARCH

    /**
     * 搜索条件
     */
    this.searchItems = null

    /**
     * 固定的搜索条件, 不管再传入什么条件都会使用AND追加
     */
    this.baseSearchItems = fixSearchItems
    if (!this.baseSearchItems) {
      // 提交查询时,没有查询条件,服务端貌似会返回未知异常
      this.baseSearchItems = new SearchItems().ne('id', 0)
    }
    Object.preventExtensions(this)
  }

  /**
   * 关键字查询
   * @param {String,Number} value
   */
  keyword(value) {
    this.queryData.keyWord = value
    return this
  }
wangdanlei's avatar
wangdanlei committed
280 281 282 283 284 285 286 287
  /**
   * 是否为用户行为
   * @param {String,Number} value
   */
  isUserAction(flag) {
    this.queryData.userAction = flag
    return this
  }
wangdanlei's avatar
wangdanlei committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
  /**
   * 权限点
   * @param {Array} keys
   */
  toValidateKeys(keys) {
    this.queryData.toValidateKeys = keys
    return this
  }
  /**
   * 索引
   * @param {Array} subTypeNames
   */
  indices(subTypeNames = []) {
    this.queryData.indices = subTypeNames
  }

  /**
   * 降序
   * @param {String} value
   */
  orderByDesc(name, sortOrder) {
    this.queryData.sortItem = this.queryData.sortItem || []
wangdanlei's avatar
wangdanlei committed
310 311
    const findData = this.queryData.sortItem.find((el) => {
      return el.fieldName === name
wangdanlei's avatar
wangdanlei committed
312
    })
wangdanlei's avatar
wangdanlei committed
313 314 315 316 317 318 319 320 321 322
    if (findData) {
      findData.fieldName = name
      findData.sortOrder = sortOrder || 'desc'
    } else {
      this.queryData.sortItem.push({
        fieldName: name,
        sortOrder: sortOrder || 'desc'
      })
    }

wangdanlei's avatar
wangdanlei committed
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
    return this
  }

  /**
   * 升序
   * @param {String} value
   */
  orderByAsc(name) {
    this.queryData.sortItem = this.queryData.sortItem || []
    this.queryData.sortItem.push({
      fieldName: name,
      sortOrder: 'asc'
    })
    return this
  }

  /**
   * 分页
   * @param {Number} pageFrom
   * @param {Number} pageSize
   */
  page(pageFrom = 1, pageSize = 9999) {
    this.queryData.pageFrom = pageFrom
    this.queryData.pageSize = pageSize
    return this
  }

  /**
   * 设置过滤条件,也可以在 search 方法传入
   * @param {SearchItems} searchItems
   */
  setSearchItems(searchItems) {
    this.searchItems = searchItems
    return this
  }
  /**
   * 查询参数 非searchItems中
   * @param {params} params
   */
  setQueryParams(params) {
    for (const attr in params) {
      this.queryData[attr] = params[attr]
    }
    return this
  }
  /**
   * 展开属性, 名称,子查询, 支持级联和数组
   * @param {String, Array} props
   */
  openProp(props) {
    this.queryData.openProps = getOpenPropsByLists(props, this.queryData.openProps)
    // this.queryData.openProps.push(subQuery.queryData)
    return this
  }
  /**
   * 展开属性中添加查询
   * @param {String, Array} props
   */
  openPropsSearch(props) {
    this.queryData.openProps = setOpenPropsSearch(props, this.queryData.openProps)
    return this
  }
  /**
   * 固定搜索条件, 可以在构造方法传入
   * @param {SearchItems} searchItems
   */
  setBaseSearchItems(searchItems) {
    this.baseSearchItems = searchItems
    if (!this.baseSearchItems) {
      this.baseSearchItems = new SearchItems()
      // 默认查询最后一个非检出版本
      this.baseSearchItems.ne('id', 0)
      if (this.service.versionControl) { // 受版本控制内置查询条件不查已检出
        this.baseSearchItems.ne('checkOuted', true)
      }
    }
    return this
  }

  /**
   * 获取请求的JSON
   */
  getQueryData(onlySearchItem = false) {
    const queryData = JSON.parse(JSON.stringify(this.queryData))
    if (onlySearchItem) {
      queryData.searchItems = this.searchItems.data
      return queryData
    }
    /* 如果固定条件和业务条件都有时合并条件*/
    if (this.searchItems && this.baseSearchItems) {
      queryData.searchItems = SearchItems.combine(this.baseSearchItems, this.searchItems).data
    } else if (this.searchItems) { /* 如果只有业务条件 */
      queryData.searchItems = this.searchItems.data
    } else if (this.baseSearchItems) /* 如果只有固定条件 */ {
      queryData.searchItems = this.baseSearchItems.data
    }
    return queryData
  }

  changeSearchMode(mode = SEARCH_MODE.RECURSION) {
    if (!mode) {
      this.searchMode = mode === SEARCH_MODE.SEARCH ? SEARCH_MODE.RECURSION : SEARCH_MODE.SEARCH
    } else {
      if (mode !== SEARCH_MODE.SEARCH && mode !== SEARCH_MODE.RECURSION) {
        console.warn(`SearchQuery 不支持搜索模式 "${mode}",情使用"${SEARCH_MODE.SEARCH}" 或 "${SEARCH_MODE.RECURSION}"`)
      } else {
        this.searchMode = mode
      }
    }
    return this
  }

  /**
   * 提交搜索、 search模式,根据后台是否同步ES,自动判断是否查询ES数据
   * @param {SearchItems} searchItems
   */
  search(searchItems = null) {
    if (searchItems) {
      this.setSearchItems(searchItems)
    }
    const sendQueryData = this.getQueryData()
    if (this.searchMode === SEARCH_MODE.SEARCH) {
      return this.service.search(sendQueryData)
    } else {
      return this.service.findReursion(sendQueryData)
    }
  }

  /**
   * 提交搜索、 recursion模式,数据库查询
   * @param {SearchItems} searchItems
   */
  recursion(searchItems = null) {
    if (searchItems) {
      this.setSearchItems(searchItems)
    }

    return this.service.findReursion(this.getQueryData())
  }

  findBySpec(queryParams = null) {
    if (queryParams) {
      this.setSearchItems(queryParams)
    }

    return this.service.findBySpec(this.getQueryData())
  }
wangdanlei's avatar
wangdanlei committed
470 471 472 473 474 475 476 477

  findBySpecOverride(queryParams = null) {
    if (queryParams) {
      this.setSearchItems(queryParams)
    }

    return this.service.findBySpecOverride(this.getQueryData())
  }
wangdanlei's avatar
wangdanlei committed
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
  /**
   * 提交查询 该模型类型 数据访问情况
   *
  */
  findLastVisitor(searchItems = null) {
    if (searchItems) {
      this.setSearchItems(searchItems)
    }

    return this.service.findLastVisitor(this.getQueryData(true))
  }
  /** *
   *
  */
  getListByNodeId(searchItems = null) {
    if (searchItems) {
      this.setSearchItems(searchItems)
    }

    return this.service.getListByNodeId(this.getQueryData(true))
  }
}
export { SearchQueryBuilder, SearchItems }