//将筛选已存在的书进行赋值
const config = require("../etc/config.json");
const mysql = require('mysql2/promise');
const dbConfig = config.isDebug?config.debug_mysql:config.release_mysql
const taskdbConfig = config.isDebug?config.debug_task_mysql:config.release_task_mysql
const redis_help = require('../src/use_redis');
const filter_data_controllers = require('../src/data_manager/Controllers/filter_data_controllers');
const helper = require("../src/helper");
const get_fq_mf_book_tab_controllers = require('../src/data_manager/Controllers/get_fq_mf_book_tab_controllers');
const CMD = {}
async function processTask(){
    let right_status = true
    let connection  = null
    let tg_platform_id = config.platform_fanqiemf
    try{
        connection = await mysql.createConnection({
            ...dbConfig,
            multipleStatements: true
        });
        let sql = `SELECT * FROM filter_data WHERE tg_platform_id = ${tg_platform_id} AND status = 0 AND weight = 0 LIMIT 1`
        
        const [row] = await connection.execute(sql)

        if(row.length<=0){
            throw 0
        }

        let filter_info = row[0]

        if(filter_info.book_id.length<18){
            let fq_book_result = await get_fq_mf_book_tab_controllers.getData({ book_id: filter_info.book_id });
            if(fq_book_result.success){


                const [info_list] = await connection.execute(
                `SELECT id FROM filter_data 
                    WHERE tg_platform_id = ? AND book_id = ? 
                    LIMIT 100`,
                [tg_platform_id, filter_info.book_id]
                );
                
                // 2. 提取 id 数组
                const ids = info_list.map(row => row.id);

                const placeholders = ids.join(',');

                // 3. 批量更新（如果 ids 不为空）
                if (ids.length > 0) {
                    await connection.execute(
                        `UPDATE filter_data 
                        SET status = ? ,
                        book_name = ? ,
                        genre = ? ,
                        words = ? ,
                        book_id = ? 
                        WHERE id IN (${placeholders})`,  // MySQL 支持 IN (?) 语法
                        [
                            1,
                            fq_book_result.data.info.book_name,
                            fq_book_result.data.info.genre,
                            fq_book_result.data.info.words==undefined?0:fq_book_result.data.info.words,
                            fq_book_result.data.book_id
                        ]
                    );
                }
                // console.log("fq_book_result:",fq_book_result,filter_info.id)
                // sql = `UPDATE filter_data SET status = 1 , book_name = "${fq_book_result.data.info.book_name}" , genre = ${fq_book_result.data.info.genre} ,words = "${fq_book_result.data.info.words}" , book_id = ${fq_book_result.data.book_id} WHERE id = "${filter_info.id}" `
                // await connection.execute(
                //     sql
                // );
            }else{
                sql = `UPDATE filter_data SET weight = 1  WHERE id = "${filter_info.id}" `
                await connection.execute(
                    sql
                );
            }
        }

    }catch(e){
        if(e==0){
            right_status = false
        }
        console.error("processTask error:",e)
    } finally{

        if(connection!=null){
            connection.end()
        }

        if(right_status){
            global.setTimeout(processTask, 1000);
        }

    }

}

CMD.init = async function(){
    redis_help.connect((results)=>{
        if(results){
            processTask();
        }
    })

}

CMD.init()