const config = require("../etc/config.json");
const helper = require("../src/helper");
const HttpClient = require("../src/HttpClient");
const redis_help = require('../src/use_redis');
const mysql = require('mysql2/promise');
const tools = require("../tools");
const video_product_controllers = require('../src/data_manager/Controllers/video_product_controllers');
const dbConfig = config.isDebug?config.debug_mysql:config.release_mysql
const taskdbConfig = config.isDebug?config.debug_task_mysql:config.release_task_mysql
const CMD = {}
let page_index = 1;

async function processTask(){
    let right_status = true
    let connection  = null
    try{
        connection = await mysql.createConnection({
            ...dbConfig,
            multipleStatements: true
        });
        let postData =  {
            "cmd":"video_product",
            "fun":"search_book_data",
            "data":{
                "product_id":"",
                "product_name":"",
                "tg_platform_id":config.platform_qimao,
                "is_auto":"",
                "page_size":500,
                "page_number":page_index,
                "oce_material_id":"",
                "genre":"",
                "is_store":"",
                "alias_name":"",
                "stat_cost":"",
                "min_book_word":"",
                "max_book_word":""
            }
        };
    
        console.log("发送请求数据:", postData);

        let client = tools.getOneNewClinet()
        let response = await client.post('http://127.0.0.1:9100/tg/back/api', postData, {
            headers: {
                'Content-Type': 'application/json',
                'X-Request-Type': 'API', // 标记 API 请求
            }
        });

        response = response.data
        // 检查响应数据结构
        if (!response || !response.data || !Array.isArray(response.data)) {
            console.log("响应数据无效或格式错误");
            throw 0;
        }

        // 检查是否有数据
        if (response.data.length <= 0) {
            console.log("没有更多数据");
            throw 0;
        }

        console.log("response:",response)
        let total = response.total;
        let product_list = response.data;
        for (let index = 0; index < product_list.length; index++) {
            const element = product_list[index];
            const [rows] = await connection.execute(
                `SELECT * FROM qm_iap_lib WHERE book_id = ${element.product_id} AND is_have_iaa = 1  LIMIT 1`
            );
            if(rows.length<=0){

            }else{
                let qm_iap_book_info = rows[0]
                let iaa_book_info = await CMD.getQMMFVideoProduct(qm_iap_book_info.iaa_book_id)
                if(iaa_book_info==null){
                    await CMD.addQMMFVideoProduct(qm_iap_book_info.iaa_book_id,element.product_id)
                }
            }
        }
    }catch(e){
        if(e==0){
            right_status = false
        }
        console.error("processTask error:",e)
    } finally{
        if(right_status){
            page_index++;
            global.setTimeout(processTask, 1000);
        }

    }

}

//插入
CMD.addQMMFVideoProduct = async function(product_id,product_parent_id){
    let connection = await mysql.createConnection({
        ...taskdbConfig,
        multipleStatements: true
    });

    let qm_book_data = await require('../src/api/qm_mf/qm_mf_search_book').search_id(product_id)
    console.log("qm_book_data:",qm_book_data,product_id,product_parent_id)
    if(qm_book_data!=null){
        await connection.execute(
            `INSERT INTO video_product (product_name,product_id,book_platform,genre,words,product_parent_id)  VALUES ("${qm_book_data.book_name}",${product_id},${config.platform_qmmf},${qm_book_data.genre},"${qm_book_data.words}",${product_parent_id})`
        );
    }

    await connection.end();
}

CMD.getQMMFVideoProduct = async function(product_id){
    let connection = await mysql.createConnection({
        ...taskdbConfig,
        multipleStatements: true
    });

    const [rows] = await connection.execute(
        `SELECT * FROM video_product WHERE product_id = ${product_id} AND book_platform = ${config.platform_qmmf}  LIMIT 1`
    );
    await connection.end();
    
    if(rows.length<=0){
        return null
    }
    return rows[0]
}

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

}

CMD.init()