318 lines
8.5 KiB
JavaScript
318 lines
8.5 KiB
JavaScript
'use strict';
|
|
|
|
function isPlainObject(value) {
|
|
return value && typeof value === 'object' && !Array.isArray(value);
|
|
}
|
|
|
|
function isEmptyValue(value) {
|
|
if (value === undefined || value === null) {
|
|
return true;
|
|
}
|
|
|
|
if (typeof value === 'string' && value.trim() === '') {
|
|
return true;
|
|
}
|
|
|
|
if (Array.isArray(value) && value.length === 0) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
function formatLocalDateTime(date, isEnd = false) {
|
|
const year = date.getFullYear();
|
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
const day = String(date.getDate()).padStart(2, '0');
|
|
return `${year}-${month}-${day} ${isEnd ? '23:59:59' : '00:00:00'}`;
|
|
}
|
|
|
|
function normalizeDate(value, options = {}) {
|
|
const { isEnd = false } = options;
|
|
|
|
if (value === undefined || value === null) {
|
|
return value;
|
|
}
|
|
|
|
if (value instanceof Date) {
|
|
if (isNaN(value.getTime())) return value;
|
|
return formatLocalDateTime(value, isEnd);
|
|
}
|
|
|
|
if (typeof value === 'number') {
|
|
const date = new Date(value);
|
|
if (isNaN(date.getTime())) return value;
|
|
return formatLocalDateTime(date, isEnd);
|
|
}
|
|
|
|
if (typeof value === 'string') {
|
|
const trimmed = value.trim();
|
|
if (!trimmed) {
|
|
return trimmed;
|
|
}
|
|
|
|
if (/^\d+$/.test(trimmed)) {
|
|
const date = new Date(Number(trimmed));
|
|
if (!isNaN(date.getTime())) {
|
|
return formatLocalDateTime(date, isEnd);
|
|
}
|
|
return trimmed;
|
|
}
|
|
|
|
const parsed = new Date(trimmed);
|
|
if (!isNaN(parsed.getTime())) {
|
|
return formatLocalDateTime(parsed, isEnd);
|
|
}
|
|
|
|
return trimmed;
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
function processConditionValue(value, type) {
|
|
if (value === undefined || value === null) {
|
|
return value;
|
|
}
|
|
|
|
switch (type) {
|
|
case 'boolean':
|
|
if (value === true || value === 'true' || value === 'Y' || value === 'y') {
|
|
return 'Y';
|
|
}
|
|
if (value === false || value === 'false' || value === 'N' || value === 'n') {
|
|
return 'N';
|
|
}
|
|
return value;
|
|
|
|
case 'date':
|
|
return normalizeDate(value, { isEnd: false });
|
|
|
|
case 'daterange':
|
|
if (Array.isArray(value)) {
|
|
return value.map((v, index) => normalizeDate(v, { isEnd: index === value.length - 1 }));
|
|
}
|
|
return normalizeDate(value, { isEnd: false });
|
|
|
|
case 'number': {
|
|
const num = parseFloat(value);
|
|
return isNaN(num) ? value : num;
|
|
}
|
|
|
|
case 'text':
|
|
if (Array.isArray(value)) {
|
|
return value.map(item => String(item).trim()).filter(Boolean);
|
|
}
|
|
return String(value).split(',').map(item => item.trim()).filter(Boolean);
|
|
|
|
default:
|
|
return value;
|
|
}
|
|
}
|
|
|
|
function buildSequelizeCondition(fieldKey, condition, app) {
|
|
if (!isPlainObject(condition)) {
|
|
return { ok: false, reason: 'condition 必须是对象' };
|
|
}
|
|
|
|
const { Sequelize } = app;
|
|
const {
|
|
operator = 'eq', value, type,
|
|
} = condition;
|
|
|
|
const fieldName = condition.field || fieldKey;
|
|
const processedValue = processConditionValue(value, type);
|
|
const op = String(operator || 'eq').toLowerCase();
|
|
|
|
switch (op) {
|
|
case 'eq':
|
|
return { ok: true, condition: { [fieldName]: processedValue } };
|
|
|
|
case 'ne':
|
|
return { ok: true, condition: { [fieldName]: { [Sequelize.Op.ne]: processedValue } } };
|
|
|
|
case 'contains':
|
|
case 'like':
|
|
if (Array.isArray(processedValue) && processedValue.length > 0) {
|
|
return {
|
|
ok: true,
|
|
condition: {
|
|
[Sequelize.Op.or]: processedValue.map(val => ({
|
|
[fieldName]: {
|
|
[Sequelize.Op.like]: `%${val}%`,
|
|
},
|
|
})),
|
|
},
|
|
};
|
|
}
|
|
return { ok: true, condition: { [fieldName]: { [Sequelize.Op.like]: `%${processedValue}%` } } };
|
|
|
|
case 'startswith':
|
|
if (Array.isArray(processedValue) && processedValue.length > 0) {
|
|
return {
|
|
ok: true,
|
|
condition: {
|
|
[Sequelize.Op.or]: processedValue.map(val => ({
|
|
[fieldName]: {
|
|
[Sequelize.Op.like]: `${val}%`,
|
|
},
|
|
})),
|
|
},
|
|
};
|
|
}
|
|
return { ok: true, condition: { [fieldName]: { [Sequelize.Op.like]: `${processedValue}%` } } };
|
|
|
|
case 'endswith':
|
|
if (Array.isArray(processedValue) && processedValue.length > 0) {
|
|
return {
|
|
ok: true,
|
|
condition: {
|
|
[Sequelize.Op.or]: processedValue.map(val => ({
|
|
[fieldName]: {
|
|
[Sequelize.Op.like]: `%${val}`,
|
|
},
|
|
})),
|
|
},
|
|
};
|
|
}
|
|
return { ok: true, condition: { [fieldName]: { [Sequelize.Op.like]: `%${processedValue}` } } };
|
|
|
|
case 'gt':
|
|
return { ok: true, condition: { [fieldName]: { [Sequelize.Op.gt]: processedValue } } };
|
|
|
|
case 'lt':
|
|
return { ok: true, condition: { [fieldName]: { [Sequelize.Op.lt]: processedValue } } };
|
|
|
|
case 'ge':
|
|
return { ok: true, condition: { [fieldName]: { [Sequelize.Op.gte]: processedValue } } };
|
|
|
|
case 'le':
|
|
return { ok: true, condition: { [fieldName]: { [Sequelize.Op.lte]: processedValue } } };
|
|
|
|
case 'in':
|
|
if (!Array.isArray(processedValue)) {
|
|
return { ok: false, reason: `${fieldName} 的 in 操作符要求 value 为数组` };
|
|
}
|
|
return { ok: true, condition: { [fieldName]: { [Sequelize.Op.in]: processedValue } } };
|
|
|
|
case 'notin':
|
|
if (!Array.isArray(processedValue)) {
|
|
return { ok: false, reason: `${fieldName} 的 notin 操作符要求 value 为数组` };
|
|
}
|
|
return { ok: true, condition: { [fieldName]: { [Sequelize.Op.notIn]: processedValue } } };
|
|
|
|
case 'between':
|
|
if (!Array.isArray(processedValue) || processedValue.length !== 2) {
|
|
return { ok: false, reason: `${fieldName} 的 between 操作符要求 value 为长度为2的数组` };
|
|
}
|
|
return { ok: true, condition: { [fieldName]: { [Sequelize.Op.between]: processedValue } } };
|
|
|
|
case 'notbetween':
|
|
if (!Array.isArray(processedValue) || processedValue.length !== 2) {
|
|
return { ok: false, reason: `${fieldName} 的 notbetween 操作符要求 value 为长度为2的数组` };
|
|
}
|
|
return { ok: true, condition: { [fieldName]: { [Sequelize.Op.notBetween]: processedValue } } };
|
|
|
|
case 'isnull':
|
|
return { ok: true, condition: { [fieldName]: { [Sequelize.Op.is]: null } } };
|
|
|
|
case 'isnotnull':
|
|
return { ok: true, condition: { [fieldName]: { [Sequelize.Op.not]: null } } };
|
|
|
|
case 'regexp':
|
|
return { ok: true, condition: { [fieldName]: { [Sequelize.Op.regexp]: processedValue } } };
|
|
|
|
case 'notregexp':
|
|
return { ok: true, condition: { [fieldName]: { [Sequelize.Op.notRegexp]: processedValue } } };
|
|
|
|
default:
|
|
return { ok: true, condition: { [fieldName]: processedValue } };
|
|
}
|
|
}
|
|
|
|
function parseWhereConditions(options = {}) {
|
|
const {
|
|
where,
|
|
app,
|
|
specialFieldMap = {},
|
|
} = options;
|
|
|
|
if (where === undefined || where === null) {
|
|
return {
|
|
ok: true,
|
|
result: {
|
|
dynamicConditions: [],
|
|
bucketConditions: {},
|
|
bucketFlags: {},
|
|
rawConditionsByField: {},
|
|
},
|
|
};
|
|
}
|
|
|
|
if (!isPlainObject(where)) {
|
|
return { ok: false, reason: 'where 必须是对象' };
|
|
}
|
|
|
|
const dynamicConditions = [];
|
|
const bucketConditions = {};
|
|
const bucketFlags = {};
|
|
const rawConditionsByField = {};
|
|
|
|
for (const [ fieldKey, condition ] of Object.entries(where)) {
|
|
if (!isPlainObject(condition)) {
|
|
return { ok: false, reason: `字段 ${fieldKey} 条件必须是对象` };
|
|
}
|
|
|
|
if (isEmptyValue(condition.value)) {
|
|
continue;
|
|
}
|
|
|
|
const mapped = specialFieldMap[fieldKey] || {};
|
|
const normalizedCondition = {
|
|
...condition,
|
|
};
|
|
|
|
if (mapped.field) {
|
|
normalizedCondition.field = mapped.field;
|
|
}
|
|
|
|
const built = buildSequelizeCondition(fieldKey, normalizedCondition, app);
|
|
if (!built.ok) {
|
|
return {
|
|
ok: false,
|
|
reason: `字段 ${fieldKey} 解析失败: ${built.reason}`,
|
|
};
|
|
}
|
|
|
|
if (mapped.bucket) {
|
|
if (!bucketConditions[mapped.bucket]) {
|
|
bucketConditions[mapped.bucket] = [];
|
|
}
|
|
bucketConditions[mapped.bucket].push(built.condition);
|
|
bucketFlags[mapped.bucket] = true;
|
|
rawConditionsByField[fieldKey] = normalizedCondition;
|
|
continue;
|
|
}
|
|
|
|
dynamicConditions.push(built.condition);
|
|
}
|
|
|
|
return {
|
|
ok: true,
|
|
result: {
|
|
dynamicConditions,
|
|
bucketConditions,
|
|
bucketFlags,
|
|
rawConditionsByField,
|
|
},
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
isEmptyValue,
|
|
normalizeDate,
|
|
processConditionValue,
|
|
buildSequelizeCondition,
|
|
parseWhereConditions,
|
|
};
|