const removeEmptyProperties = (obj) => {
|
for (const key in obj) {
|
if (obj.hasOwnProperty(key) && (obj[key] === "" || obj[key] === undefined)) {
|
delete obj[key];
|
}
|
}
|
return obj;
|
};
|
|
export const dataFilter = (data, filters) => {
|
const andLikeParams = {};
|
const andEqParams = {};
|
Object.keys(data).forEach(key => {
|
if (data[key]) {
|
filters.forEach(field => {
|
if (field.prop === key) {
|
if (field.type === 'el-select') {
|
andEqParams[key] = data[key];
|
}
|
if (field.type === 'el-input') {
|
andLikeParams[key] = data[key];
|
}
|
}
|
});
|
}
|
})
|
|
return convertFilter({andLikeParams, andEqParams})
|
}
|
|
export const convertFilter = ({
|
orLikeParams = {},
|
andLikeParams = {},
|
orEqParams = {},
|
andEqParams = {},
|
orInParams = {},
|
andInParams = {}
|
}) => {
|
let params = "1=1 ";
|
|
/**
|
* 1=1%20and%20province_code%20=%20'500000'%20and%20%201=1%20%20and%20customer_eas_code%20like%20'%252312411000%25'%20and%20customer_name%20like%20'%252312411000%25'
|
*/
|
const orLikeKeys = Object.keys(removeEmptyProperties(orLikeParams));
|
|
if (orLikeKeys.length > 0) {
|
params += 'and ( '
|
}
|
|
orLikeKeys.forEach((key, index) => {
|
params += `${key} like ` + "'" + `%${orLikeParams[key]}%` + "'" +
|
`${index < orLikeKeys.length - 1 ? ' or ' : ''}`;
|
});
|
|
if (orLikeKeys.length > 0) {
|
params += ') '
|
}
|
|
const orEqKeys = Object.keys(removeEmptyProperties(orEqParams));
|
|
if (orEqKeys.length > 0) {
|
params += 'and ( '
|
}
|
|
orEqKeys.forEach((key, index) => {
|
params += `${key} = ` + "'" + `${orEqParams[key]}` + "'" +
|
`${index < orEqKeys.length - 1 ? ' or ' : ''}`;
|
});
|
|
if (orEqKeys.length > 0) {
|
params += ') '
|
}
|
|
const andLikeKeys = Object.keys(removeEmptyProperties(andLikeParams));
|
|
if (andLikeKeys.length > 0) {
|
params += ''
|
}
|
|
andLikeKeys.forEach(key => {
|
params += ` and ${key} like ` + "'" + `%${andLikeParams[key]}%` + "' ";
|
});
|
|
if (andLikeKeys.length > 0) {
|
params += ''
|
}
|
|
|
const andEqKeys = Object.keys(removeEmptyProperties(andEqParams));
|
|
if (andEqKeys.length > 0) {
|
params += ''
|
}
|
|
andEqKeys.forEach(key => {
|
params += ` and ${key} = ` + "'" + `${andEqParams[key]}` + "' ";
|
});
|
|
if (andEqKeys.length > 0) {
|
params += ''
|
}
|
|
const orInKeys = Object.keys(removeEmptyProperties(orInParams));
|
if (orInKeys.length > 0) {
|
params += " and ( "
|
}
|
for (var key in orInParams) {
|
if (typeof (orInParams[key]) == "object") {
|
const orInKeysChildren = Object.keys(removeEmptyProperties(orInParams[key]));
|
if (orInKeysChildren.length > 0) {
|
params += " or "
|
params += key + ' in ('
|
orInKeysChildren.forEach((key1, index) => {
|
params += "'" + `${orInParams[key][key1]}` + "'" + ' , ';
|
})
|
//去除逗号
|
params = params.substring(0, params.length - 2)
|
params += ") "
|
|
|
}
|
|
}
|
}
|
if (orInKeys.length > 0) {
|
params += " ) "
|
}
|
|
const andInKeys = Object.keys(removeEmptyProperties(andInParams));
|
if (andInKeys.length > 0) {
|
params += ""
|
}
|
|
for (const key in andInParams) {
|
if (typeof (andInParams[key]) == "object") {
|
const andInKeysChildren = Object.keys(removeEmptyProperties(andInParams[key]));
|
if (andInKeysChildren.length > 0) {
|
params += " and "
|
params += key + ' in ('
|
andInKeysChildren.forEach((key1, index) => {
|
params += "'" + `${andInParams[key][key1]}` + "'" + ' , ';
|
})
|
//去除逗号
|
params = params.substring(0, params.length - 2)
|
params += ") "
|
|
}
|
|
}
|
}
|
|
if (andInKeys.length > 0) {
|
params += ""
|
}
|
|
return params;
|
};
|