|
@@ -8,6 +8,60 @@ class TaskDbBaseModel {
|
|
|
this.queryBuilder = new QueryBuilder();
|
|
|
}
|
|
|
|
|
|
+ async query(sql, params) {
|
|
|
+ return await db.query(sql, params);
|
|
|
+ }
|
|
|
+
|
|
|
+ async findByDateRange(conditions = {}, dateField, startDate, endDate, limit = null, offset = 0, orderBy = null) {
|
|
|
+ this.queryBuilder.clear();
|
|
|
+
|
|
|
+ // 先构建基础条件
|
|
|
+ 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) {
|
|
|
+ sql += ` ORDER BY ${orderBy}`;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加分页
|
|
|
+ if (limit) {
|
|
|
+ sql += ` LIMIT ${limit}`;
|
|
|
+ if (offset) {
|
|
|
+ sql += ` OFFSET ${offset}`;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 执行查询前打印SQL和参数用于调试
|
|
|
+ console.log('SQL:', sql);
|
|
|
+ console.log('Params:', params);
|
|
|
+
|
|
|
+ return await db.query(sql, params);
|
|
|
+ }
|
|
|
+
|
|
|
+ async findDataByLimit(conditions,limit=1) {
|
|
|
+ this.queryBuilder.clear()
|
|
|
+ const { sql, params } = this.queryBuilder
|
|
|
+ .select()
|
|
|
+ .from(this.tableName)
|
|
|
+ .where(conditions)
|
|
|
+ .limit(limit)
|
|
|
+ .getQuery();
|
|
|
+ const results = await db.query(sql, params);
|
|
|
+ return results || null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
async findOne(conditions) {
|
|
|
this.queryBuilder.clear()
|
|
|
const { sql, params } = this.queryBuilder
|