// ml_oceanengine
const db = require('./Database');
const QueryBuilder = require('./QueryBuilder');

class BaseModel {
    constructor(tableName) {
        this.tableName = tableName;
        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 findOne(conditions) {
        this.queryBuilder.clear()
        const { sql, params } = this.queryBuilder
            .select()
            .from(this.tableName)
            .where(conditions)
            .limit(1)
            .getQuery();
        
        const results = await db.query(sql, params);
        return results[0] || null;
    }

    async findDataByLimit(conditions,limit=1,orderBy="") {
        this.queryBuilder.clear()
        let { sql, params } = this.queryBuilder
            .select()
            .from(this.tableName)
            .where(conditions)
            .orderBy('id',orderBy)
            .limit(limit)
            .getQuery();
        const results = await db.query(sql, params);
        return results || null;
    }

    async findAll(conditions = {}, limit = null, offset = 0, orderBy = null) {
        this.queryBuilder.clear()
        let builder = this.queryBuilder
            .select()
            .from(this.tableName)
            .where(conditions);

        if (orderBy) {
            builder.orderBy(orderBy);
        }

        if (limit) {
            builder.limit(limit, offset);
        }

        const { sql, params } = builder.getQuery();
        return await db.query(sql, params);
    }

    async create(data) {
        this.queryBuilder.clear()
        const { sql, params } = this.queryBuilder
            .insert(this.tableName, data)
            .getQuery();

        const result = await db.query(sql, params);
        return result.insertId;
    }

    async update(conditions, data) {
        this.queryBuilder.clear()
        const { sql, params } = this.queryBuilder
            .update(this.tableName, data)
            .where(conditions)
            .getQuery();
        const result = await db.query(sql, params);
        return result.affectedRows;
    }

    async delete(conditions) {
        this.queryBuilder.clear()
        const { sql, params } = this.queryBuilder
            .delete(this.tableName)
            .where(conditions)
            .getQuery();

        const result = await db.query(sql, params);
        return result.affectedRows;
    }

    async transaction(callback) {
        return await db.transaction(callback);
    }
}

module.exports = BaseModel;