|
@@ -1,5 +1,6 @@
|
|
|
import { get, head, last, isArray } from 'lodash';
|
|
|
import { Opera } from './dbOpera';
|
|
|
+import { Between, Equal, ILike, In, IsNull, LessThan, LessThanOrEqual, Like, MoreThan, MoreThanOrEqual, Not } from 'typeorm';
|
|
|
/**
|
|
|
*
|
|
|
* @param builder model的createQueryBuilder,只有到最后要查数据的时候才是异步的
|
|
@@ -28,64 +29,83 @@ export const completeBuilderCondition = (builder, query = {}, operas = {}, model
|
|
|
const valueStr = `value${i}`;
|
|
|
let valueArr = [];
|
|
|
const strArr = [];
|
|
|
+ // 当前使用options方式查询,queryObject为条件整理后的对象,且json先咱不处理
|
|
|
+ const queryObject: any = {}
|
|
|
switch (opera) {
|
|
|
case Opera.Between:
|
|
|
+ queryObject[key] = Between(head(value), last(value));
|
|
|
str = `"${key}" Between :${valueStr}_1 AND :${valueStr}_2`;
|
|
|
params = { [`${valueStr}_1`]: head(value), [`${valueStr}_2`]: last(value) };
|
|
|
break;
|
|
|
case Opera.Not:
|
|
|
+ queryObject[key] = Not(value);
|
|
|
str = `"${key}" != :${valueStr}`;
|
|
|
params = { [`${valueStr}`]: value };
|
|
|
break;
|
|
|
case Opera.Like:
|
|
|
+ queryObject[key] = Like(`%${value}%`);
|
|
|
str = `"${key}" Like :${valueStr}`;
|
|
|
params = { [`${valueStr}`]: `%${value}%` };
|
|
|
break;
|
|
|
case Opera.LikeLeft:
|
|
|
+ queryObject[key] = Like(`%${value}`);
|
|
|
str = `"${key}" Like :${valueStr}`;
|
|
|
params = { [`${valueStr}`]: `%${value}` };
|
|
|
break;
|
|
|
case Opera.LikeRight:
|
|
|
+ queryObject[key] = Like(`${value}%`);
|
|
|
str = `"${key}" Like :${valueStr}`;
|
|
|
params = { [`${valueStr}`]: `${value}%` };
|
|
|
break;
|
|
|
case Opera.ILike:
|
|
|
+ queryObject[key] = ILike(`%${value}%`);
|
|
|
str = `"${key}" Not Like :${valueStr}`;
|
|
|
params = { [`${valueStr}`]: `%${value}%` };
|
|
|
break;
|
|
|
case Opera.ILikeLeft:
|
|
|
+ queryObject[key] = ILike(`%${value}`);
|
|
|
str = `"${key}" Not Like :${valueStr}`;
|
|
|
params = { [`${valueStr}`]: `%${value}` };
|
|
|
break;
|
|
|
case Opera.ILikeRight:
|
|
|
+ queryObject[key] = ILike(`${value}%`);
|
|
|
str = `"${key}" Not Like :${valueStr}`;
|
|
|
params = { [`${valueStr}`]: `${value}%` };
|
|
|
break;
|
|
|
case Opera.LessThan:
|
|
|
+ queryObject[key] = LessThan(value);
|
|
|
str = `"${key}" < :${valueStr}`;
|
|
|
params = { [`${valueStr}`]: value };
|
|
|
break;
|
|
|
case Opera.LessThanOrEqual:
|
|
|
+ queryObject[key] = LessThanOrEqual(value);
|
|
|
str = `"${key}" <= :${valueStr}`;
|
|
|
params = { [`${valueStr}`]: value };
|
|
|
break;
|
|
|
case Opera.MoreThan:
|
|
|
+ queryObject[key] = MoreThan(value);
|
|
|
str = `"${key}" > :${valueStr}`;
|
|
|
params = { [`${valueStr}`]: value };
|
|
|
break;
|
|
|
case Opera.MoreThanOrEqual:
|
|
|
+ queryObject[key] = MoreThanOrEqual(value);
|
|
|
str = `"${key}" >= :${valueStr}`;
|
|
|
params = { [`${valueStr}`]: value };
|
|
|
break;
|
|
|
case Opera.In:
|
|
|
+ if (!isArray(value)) queryObject[key] = In([value])
|
|
|
+ else queryObject[key] = In(value)
|
|
|
+
|
|
|
if (!isArray(value)) str = `"${key}" IN (:${valueStr})`;
|
|
|
else str = `"${key}" IN (:...${valueStr})`;
|
|
|
params = { [`${valueStr}`]: value };
|
|
|
break;
|
|
|
case Opera.IsNull:
|
|
|
+ queryObject[key] = IsNull()
|
|
|
str = `"${key}" IS NULL`;
|
|
|
break;
|
|
|
case Opera.IsNotNull:
|
|
|
+ queryObject[key] = Not(IsNull())
|
|
|
str = `"${key}" IS NOT NULL`;
|
|
|
params = { [`${valueStr}`]: value };
|
|
|
break;
|
|
@@ -148,6 +168,7 @@ export const completeBuilderCondition = (builder, query = {}, operas = {}, model
|
|
|
}
|
|
|
break;
|
|
|
case Opera.Equal:
|
|
|
+ queryObject[key] = Equal(value)
|
|
|
str = `"${key}" = :${valueStr}`;
|
|
|
params = { [`${valueStr}`]: value };
|
|
|
break;
|
|
@@ -156,16 +177,18 @@ export const completeBuilderCondition = (builder, query = {}, operas = {}, model
|
|
|
if (model) isString = columnIsString(key, model);
|
|
|
if (isString) {
|
|
|
// 字符串默认使用模糊查询
|
|
|
+ queryObject[key] = Like(`%${value}%`);
|
|
|
str = `"${key}" Like :${valueStr}`;
|
|
|
params = { [`${valueStr}`]: `%${value}%` };
|
|
|
} else {
|
|
|
+ queryObject[key] = Equal(value)
|
|
|
str = `"${key}" = :${valueStr}`;
|
|
|
params = { [`${valueStr}`]: value };
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
if (!str) continue;
|
|
|
- builder[method](str, params);
|
|
|
+ builder[method](queryObject);
|
|
|
}
|
|
|
};
|
|
|
|