UNPKG

yinghe-lowcode-zln

Version:

基于vue、ant-design-vue,datagrid的低代码平台

226 lines (224 loc) 7.27 kB
/* * @Description:datagrid 查询参数转化中间层 * @version: 1.0 * @Author: 九哥 * @Date: 2020-08-22 10:04:08 * @LastEditors: 九哥 * @LastEditTime: 2020-08-31 05:57:42 */ /** * * [ [ "field", "=", 10 ], "and", [ [ "otherField", "<", 3 ], "or", [ "otherField", ">", 11 ] ] ] [["code","=","A"],"and",["name","=","1"],"and",["enable","=","3"]] * @description: 转化参数 */ import { toLowerLine, isTimestamp } from '@/utils/util' import moment from 'moment' // const searchOperation = ['=', '<>', '>', '>=', '<', '<=', 'startswith', 'endswith', 'contains', 'notcontains', 'isblank', 'isnotblank'] export const filterParse = function (filter, queryParamWrapList = []) { const key = filter if (!Array.isArray(filter[0]) && filter.length === 3 && filter[2] === null) { const queryParamWrap = {} queryParamWrap.column = toLowerLine(filter[0]) if (filter[0].indexOf('#') >= 0) { queryParamWrap.column = filter[0].replace('#', '.') } queryParamWrap.condition = 'andIsNull' queryParamWrap.v1 = '1' return queryParamWrapList.push(queryParamWrap) } if (!Array.isArray(filter[0]) && filter.length === 3 && filter[2] !== null) { const condition = parseCondition(filter[1]) let value = filter[2] const queryParamWrap = {} queryParamWrap.column = toLowerLine(filter[0]) if (filter[0].indexOf('#') >= 0) { queryParamWrap.column = filter[0].replace('#', '.') } queryParamWrap.condition = condition if (typeof (value) === 'string') { value = value.toUpperCase() } if (isTimestamp(value)) { value = moment(value).format('YYYY-MM-DD HH:MM:SS') } queryParamWrap.v1 = value return queryParamWrapList.push(queryParamWrap) } let orFlag = false if (Array.isArray(key)) { for (const item of filter) { if (item === 'and' || item === 'or') { if (item === 'or') { orFlag = true } continue } // (1)["code","=","A"] (2) [ "otherField", "<", 3 ], "or", [ "otherField", ">", 11 ] if (Array.isArray(item[0])) { // [ [ "otherField", "<", 3 ], "or", [ "otherField", ">", 11 ]] let condition = parseCondition(item[1]) const column = toLowerLine(item[0][0]) if (column === 'id') { continue } const subcondition1 = parseCondition(item[0][1]) let v1 = item[0][2] const subWheres = [] if (isTimestamp(v1)) { v1 = moment(v1).format('YYYY-MM-DD HH:MM:SS') } subWheres.push({ column: column, condition: subcondition1, v1: v1 }) if (item[2]) { const subcondition2 = parseCondition(item[2][1]) let v2 = item[2][2] if (isTimestamp(v2)) { v2 = moment(v2).format('YYYY-MM-DD HH:MM:SS') } subWheres.push({ column: column, condition: subcondition2, v1: v2 }) } let subWhere = {} if (condition !== 'and' && condition !== 'or') { condition = 'and' } subWhere = { 'condition': condition, 'andSubWheres': subWheres } queryParamWrapList.push(subWhere) } else { if (item[2] != null) { const condition = parseCondition(item[1]) let value = item[2] const queryParamWrap = {} queryParamWrap.column = toLowerLine(item[0]) queryParamWrap.condition = orFlag ? 'orEq' : condition if (typeof (value) === 'string') { value = value.toUpperCase() } if (isTimestamp(value)) { value = moment(value).format('YYYY-MM-DD HH:MM:SS') } queryParamWrap.v1 = value queryParamWrapList.push(queryParamWrap) } else { const queryParamWrap = {} queryParamWrap.column = toLowerLine(item[0]) queryParamWrap.condition = 'andIsNull' queryParamWrap.v1 = '1' queryParamWrapList.push(queryParamWrap) } } } } else { // 只有一层情况 const condition = parseCondition(filter[1]) let value = filter[2] const queryParamWrap = {} // 子表字段 if (key.indexOf('#') >= 0) { queryParamWrap.column = key.replace('#', '.') } else { // 主表字段 queryParamWrap.column = toLowerLine(key) } queryParamWrap.condition = condition if (typeof (value) === 'string') { value = value.toUpperCase() } if (isTimestamp(value)) { value = moment(value).format('YYYY-MM-DD HH:MM:SS') } queryParamWrap.v1 = value queryParamWrapList.push(queryParamWrap) } } // 转换万能过滤器查询信息 export function paramsRequest (queryParam, queryParamWrapList) { for (var item in queryParam) { if (queryParam[item] && item) { if (item.indexOf('_') >= 0) { const queryParamWrap = {} if (item.indexOf('#') >= 0) { queryParamWrap.column = toLowerLine(item.split('_')[0].replace('#', '.')) queryParamWrap.condition = item.split('_')[1] } else { queryParamWrap.column = toLowerLine(item.split('_')[0]) queryParamWrap.condition = item.split('_')[1] } queryParamWrap.v1 = queryParam[item] if (Array.isArray(queryParamWrap.v1)) { if (queryParamWrap.v1.length > 0) { if (queryParamWrap.condition === 'andBetween') { const v1 = queryParamWrap.v1[0] const v2 = queryParamWrap.v1[1] queryParamWrap.v1 = v1 queryParamWrap.v2 = v2 } if (queryParamWrap.condition === 'andIsNull' || queryParamWrap.condition === 'andIsNotNull') { delete queryParamWrap.v1 } queryParamWrapList.push(queryParamWrap) } } else { queryParamWrapList.push(queryParamWrap) } } else { if (item === 'staDate' || item === 'closeboundTime' || item === 'staDateActual' || item === 'status' ) { continue } else { const queryParamWrap = {} if (item.indexOf('#') >= 0) { queryParamWrap.column = item.replace('#', '.') } else { queryParamWrap.column = toLowerLine(item) } // 未指定下默认是like查询 queryParamWrap.condition = 'andLike' queryParamWrap.v1 = queryParam[item] if (Array.isArray(queryParamWrap.v1)) { if (queryParamWrap.v1.length > 0) { queryParamWrapList.push(queryParamWrap) } } else { queryParamWrapList.push(queryParamWrap) } } } } } } export function parseCondition (condition) { switch (condition) { case '=': return 'andEq' case '<': return 'andLess' case '<=': return 'andLessEq' case '>=': return 'andGreatEq' case '>': return 'andGreat' case 'contains': return 'andLike' case 'notcontains': return 'andNotLike' case 'startswith': return 'andRightLike' case 'endswith': return 'andLeftLike' case '<>': return 'andNotEq' case 'and': return 'andBetween' default: return 'andLike' } }