|
@@ -11,40 +11,37 @@ class BaseModel {
|
|
|
async findByDateRange(conditions = {}, dateField, startDate, endDate, limit = null, offset = 0, orderBy = null) {
|
|
|
this.queryBuilder.clear();
|
|
|
|
|
|
- // 构建基础条件
|
|
|
- const fullConditions = {
|
|
|
- ...conditions,
|
|
|
- [`${dateField} >=`]: startDate, // 注意这里的写法
|
|
|
- [`${dateField} <=`]: endDate // 注意这里的写法
|
|
|
- };
|
|
|
-
|
|
|
- // 或者这样写:
|
|
|
- /*
|
|
|
- const fullConditions = {
|
|
|
- ...conditions
|
|
|
- };
|
|
|
- fullConditions[`${dateField} >=`] = startDate;
|
|
|
- fullConditions[`${dateField} <=`] = endDate;
|
|
|
- */
|
|
|
-
|
|
|
- let builder = this.queryBuilder
|
|
|
- .select()
|
|
|
- .from(this.tableName)
|
|
|
- .where(fullConditions);
|
|
|
-
|
|
|
+ // 先构建基础条件
|
|
|
+ let sql = `SELECT * FROM ${this.tableName} WHERE 1=1`;
|
|
|
+ let params = [];
|
|
|
+
|
|
|
+ // 添加其他条件
|
|
|
+ for(const [key, value] of Object.entries(conditions)) {
|
|
|
+ sql += ` AND ${key} = ?`;
|
|
|
+ params.push(value);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加时间范围条件
|
|
|
+ sql += ` AND ${dateField} >= ? AND ${dateField} <= ?`;
|
|
|
+ params.push(startDate, endDate);
|
|
|
+
|
|
|
+ // 添加排序
|
|
|
if (orderBy) {
|
|
|
- builder.orderBy(orderBy);
|
|
|
+ sql += ` ORDER BY ${orderBy}`;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ // 添加分页
|
|
|
if (limit) {
|
|
|
- builder.limit(limit, offset);
|
|
|
+ sql += ` LIMIT ${limit}`;
|
|
|
+ if (offset) {
|
|
|
+ sql += ` OFFSET ${offset}`;
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- const { sql, params } = builder.getQuery();
|
|
|
- console.log("sql:",sql)
|
|
|
- console.log("params:",params)
|
|
|
- // 正确的SQL应该是:
|
|
|
- // SELECT * FROM origin_data WHERE status = 1 AND create_time >= '2024-12-10 00:00:00' AND create_time <= '2024-12-11 00:00:00' LIMIT 500
|
|
|
+
|
|
|
+ // 执行查询前打印SQL和参数用于调试
|
|
|
+ console.log('SQL:', sql);
|
|
|
+ console.log('Params:', params);
|
|
|
+
|
|
|
return await db.query(sql, params);
|
|
|
}
|
|
|
|